Skip to content

Commit 3a44311

Browse files
author
Taylor Holberton
committed
Heavy Refactor Part 1
1 parent a71df4f commit 3a44311

30 files changed

+2275
-246
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
build
2+
*-build
23
doxygen

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
1212
set(px_cxxflags -Wall -Wextra -Werror -Wfatal-errors -std=c++14)
1313
endif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
1414

15+
if(EMSCRIPTEN)
16+
set(CMAKE_EXECUTABLE_SUFFIX .html)
17+
endif(EMSCRIPTEN)
18+
1519
add_library(px libpx.hpp libpx.cpp)
1620

1721
target_include_directories(px PUBLIC "${CMKAE_CURRENT_SOURCE_DIR}")

editor/App.cpp

+206-6
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,158 @@
11
#include "App.hpp"
22

3+
#include "AppState.hpp"
4+
#include "DocumentProperties.hpp"
5+
#include "DrawPanel.hpp"
6+
#include "History.hpp"
7+
#include "Input.hpp"
8+
#include "Log.hpp"
9+
#include "MenuBar.hpp"
310
#include "Platform.hpp"
11+
#include "Renderer.hpp"
12+
13+
#include <libpx.hpp>
414

515
#include <imgui.h>
616

17+
#include <glm/glm.hpp>
18+
19+
#include <memory>
20+
#include <vector>
21+
22+
#include <cstdio>
23+
724
namespace px {
825

926
namespace {
1027

28+
/// Represents the application when it is being used
29+
/// for drawing the artwork.
30+
class DrawState final : public AppState,
31+
public DrawPanel::Observer
32+
{
33+
DrawPanel drawPanel;
34+
public:
35+
DrawState(App* app) : AppState(app) {}
36+
/// Renders the draw state windows.
37+
void frame() override
38+
{
39+
renderDocument();
40+
41+
const auto* menuBar = getMenuBar();
42+
43+
if (menuBar->drawPanelVisible()) {
44+
drawPanel.frame(this);
45+
}
46+
}
47+
protected:
48+
/// Renders the document onto the window.
49+
void renderDocument()
50+
{
51+
auto* image = getApp()->getImage();
52+
53+
auto* color = getColorBuffer(image);
54+
auto w = getImageWidth(image);
55+
auto h = getImageHeight(image);
56+
57+
getPlatform()->getRenderer()->blit(color, w, h);
58+
}
59+
/// Observes an event from the draw panel.
60+
void observe(DrawPanel::Event event) override
61+
{
62+
switch (event) {
63+
case DrawPanel::Event::ChangedBlendMode:
64+
break;
65+
case DrawPanel::Event::ChangedPixelSize:
66+
break;
67+
case DrawPanel::Event::ChangedPrimaryColor:
68+
break;
69+
case DrawPanel::Event::ChangedTool:
70+
break;
71+
}
72+
}
73+
};
74+
1175
/// Implements the interface to the application.
12-
class AppImpl final : public App
76+
class AppImpl final : public App,
77+
public MenuBar::Observer,
78+
public DocumentProperties::Observer
1379
{
14-
/// A pointer to the platform that is hosting the app.
15-
Platform* platform = nullptr;
80+
/// The document history stack.
81+
History history;
82+
/// A pointer to the image that the
83+
/// document is rendered to.
84+
Image* image = nullptr;
85+
/// The stack of app states.
86+
/// The last element is the top of the stack.
87+
std::vector<std::unique_ptr<AppState>> stateStack;
88+
/// The menu bar attached to the window.
89+
MenuBar menuBar;
90+
/// The properties of the currently opened document.
91+
DocumentProperties docProperties;
92+
/// The log for events and errors.
93+
Log log;
1694
public:
1795
/// Constructs a new app instance.
18-
AppImpl(Platform* p) : platform(p) {}
96+
AppImpl(Platform* p) : App(p), image(createImage(64, 64))
97+
{
98+
pushAppState(new DrawState(this));
99+
}
19100
/// Releases memory allocated by the app.
20-
~AppImpl() {}
101+
~AppImpl()
102+
{
103+
closeImage(image);
104+
}
105+
/// Gets a pointer the log.
106+
Log* getLog() noexcept override
107+
{
108+
return &log;
109+
}
110+
/// Gets a pointer to the current document snapshot.
111+
Document* getDocument() noexcept override
112+
{
113+
return history.getDocument();
114+
}
115+
/// Gets a pointer to the current document snapshot.
116+
const Document* getDocument() const noexcept override
117+
{
118+
return history.getDocument();
119+
}
120+
/// Takes a snapshot of the current document.
121+
void snapshotDocument() override
122+
{
123+
history.snapshot();
124+
}
125+
/// Gets a pointer to the menu bar.
126+
Image* getImage() noexcept override
127+
{
128+
return image;
129+
}
130+
/// Gets a pointer to the menu bar.
131+
const Image* getImage() const noexcept override
132+
{
133+
return image;
134+
}
135+
/// Gets a pointer to the menu bar.
136+
const MenuBar* getMenuBar() const noexcept override
137+
{
138+
return &menuBar;
139+
}
140+
/// Gets a pointer to the menu bar.
141+
MenuBar* getMenuBar() noexcept override
142+
{
143+
return &menuBar;
144+
}
21145
/// Checks for any non-options that may be interpreted
22146
/// as a document to be opened.
23147
bool parseArgs(int, char**) override
24148
{
25149
return true;
26150
}
151+
/// Pushes a state to the stack.
152+
void pushAppState(AppState* state) override
153+
{
154+
stateStack.emplace_back(state);
155+
}
27156
/// Renders a frame of the application.
28157
bool frame() override
29158
{
@@ -35,14 +164,85 @@ class AppImpl final : public App
35164

36165
return true;
37166
}
167+
/// Handles mouse motion events.
168+
void mouseMotion(const MouseMotion&) override
169+
{
170+
//std::printf("mouse: %d %d\n", x, y);
171+
}
172+
/// Handles mouse button state changes.
173+
void mouseButton(const MouseButton& button) override
174+
{
175+
(void)button;
176+
std::printf("here\n");
177+
}
38178
protected:
39179
/// This function renders a frame without checking for
40180
/// exceptions.
41181
///
42182
/// Exceptions are checked by the calling function.
43183
void uncheckedFrame()
44184
{
45-
platform->clear(1, 1, 1, 1);
185+
getPlatform()->getRenderer()->clear(1, 1, 1, 1);
186+
187+
menuBar.frame(this);
188+
189+
if (menuBar.documentPropertiesVisible()) {
190+
docProperties.frame();
191+
}
192+
193+
if (menuBar.logVisible()) {
194+
log.frame();
195+
}
196+
197+
for (auto& state : stateStack) {
198+
state->frame();
199+
}
200+
}
201+
/// Observes a menu bar event.
202+
void observe(MenuBar::Event event) override
203+
{
204+
switch (event) {
205+
case MenuBar::Event::ClickedClose:
206+
break;
207+
case MenuBar::Event::ClickedOpen:
208+
break;
209+
case MenuBar::Event::ClickedSave:
210+
break;
211+
case MenuBar::Event::ClickedSaveAs:
212+
break;
213+
case MenuBar::Event::ClickedExportSpriteSheet:
214+
break;
215+
case MenuBar::Event::ClickedExportZip:
216+
break;
217+
case MenuBar::Event::ClickedExportCurrentFrame:
218+
break;
219+
case MenuBar::Event::ClickedRedo:
220+
break;
221+
case MenuBar::Event::ClickedUndo:
222+
break;
223+
case MenuBar::Event::ClickedQuit:
224+
break;
225+
case MenuBar::Event::ClickedTheme:
226+
break;
227+
case MenuBar::Event::ClickedCustomTheme:
228+
break;
229+
}
230+
}
231+
/// Observers a document properties event.
232+
void observe(DocumentProperties::Event event) override
233+
{
234+
switch (event) {
235+
case DocumentProperties::Event::ChangeBackgroundColor:
236+
break;
237+
case DocumentProperties::Event::ChangeDirectory:
238+
break;
239+
case DocumentProperties::Event::ChangeSize:
240+
break;
241+
case DocumentProperties::Event::ChangeName:
242+
break;
243+
case DocumentProperties::Event::ClickedDirectoryBrowse:
244+
break;
245+
}
46246
}
47247
};
48248

editor/App.hpp

+63-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33

44
namespace px {
55

6+
struct Document;
7+
struct Image;
8+
9+
struct MouseButton;
10+
struct MouseMotion;
11+
12+
class AppState;
13+
class Log;
14+
class MenuBar;
615
class Platform;
716

817
/// This is the interface for the application.
918
/// The @ref Platform and @ref App class most communicate
1019
/// with each other via their interfaces.
1120
class App
1221
{
22+
/// A pointer to the platform that is hosting the app.
23+
Platform* platform = nullptr;
1324
public:
1425
/// Creates a new instance of the app.
1526
///
@@ -18,19 +29,68 @@ class App
1829
///
1930
/// @return A pointer to the app.
2031
static App* init(Platform* platform);
32+
/// Constructs the base interface to the app.
33+
///
34+
/// @param p A pointer to the platform hosting the app.
35+
inline App(Platform* p) : platform(p) {}
2136
/// Just a stub.
2237
virtual ~App() {}
38+
/// Gets a pointer to the current document snapshot.
39+
virtual Document* getDocument() noexcept = 0;
40+
/// Gets a pointer to the current document snapshot.
41+
virtual const Document* getDocument() const noexcept = 0;
42+
/// Gets a pointer to the menu bar.
43+
virtual MenuBar* getMenuBar() noexcept = 0;
44+
/// Gets a pointer to the menu bar.
45+
virtual const MenuBar* getMenuBar() const noexcept = 0;
46+
/// Gets a pointer to the latest rendered image.
47+
virtual Image* getImage() noexcept = 0;
48+
/// Gets a pointer to the latest rendered image.
49+
virtual const Image* getImage() const noexcept = 0;
50+
/// Gets a pointer to the platform hosting the app.
51+
///
52+
/// @return A pointer to the platform for non-const access.
53+
inline Platform* getPlatform() noexcept
54+
{
55+
return platform;
56+
}
57+
/// Gets a pointer to the platform hosting the app.
58+
///
59+
/// @return A pointer to the platform for const access.
60+
inline const Platform* getPlatform() const noexcept
61+
{
62+
return platform;
63+
}
64+
/// Gets a pointer to the log.
65+
///
66+
/// @return A pointer to the log.
67+
virtual Log* getLog() noexcept = 0;
68+
/// Called to render a frame.
69+
///
70+
/// @return True on success, false on failure.
71+
virtual bool frame() = 0;
72+
/// This function is called by the platform
73+
/// implementation to notify the application of
74+
/// a mouse motion event.
75+
virtual void mouseMotion(const MouseMotion& event) = 0;
76+
/// This function is called by the platform
77+
/// implementation to notify the app of a mouse
78+
/// button event.
79+
virtual void mouseButton(const MouseButton& event) = 0;
2380
/// Parses arguments from the command line.
2481
///
2582
/// @param argc The number of arguments to parse.
2683
/// @param argv The argument array.
2784
///
2885
/// @return True on success, false on failure.
2986
virtual bool parseArgs(int argc, char** argv) = 0;
30-
/// Called to render a frame.
87+
/// Pushes an app state to the app.
3188
///
32-
/// @return True on success, false on failure.
33-
virtual bool frame() = 0;
89+
/// @param appState The app state to push.
90+
virtual void pushAppState(AppState* appState) = 0;
91+
/// Takes a snapshot of the current document.
92+
/// This is primarily for undo/redo operations.
93+
virtual void snapshotDocument() = 0;
3494
/// Gets the window title.
3595
///
3696
/// @return A pointer to the window title to display.

editor/AppState.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "AppState.hpp"
2+
3+
#include "App.hpp"
4+
5+
namespace px {
6+
7+
Platform* AppState::getPlatform() noexcept
8+
{
9+
return app->getPlatform();
10+
}
11+
12+
MenuBar* AppState::getMenuBar() noexcept
13+
{
14+
return app->getMenuBar();
15+
}
16+
17+
const MenuBar* AppState::getMenuBar() const noexcept
18+
{
19+
return app->getMenuBar();
20+
}
21+
22+
Log* AppState::getLog() noexcept
23+
{
24+
return app->getLog();
25+
}
26+
27+
const Log* AppState::getLog() const noexcept
28+
{
29+
return app->getLog();
30+
}
31+
32+
} // namespace px

0 commit comments

Comments
 (0)