1
1
#include " App.hpp"
2
2
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"
3
10
#include " Platform.hpp"
11
+ #include " Renderer.hpp"
12
+
13
+ #include < libpx.hpp>
4
14
5
15
#include < imgui.h>
6
16
17
+ #include < glm/glm.hpp>
18
+
19
+ #include < memory>
20
+ #include < vector>
21
+
22
+ #include < cstdio>
23
+
7
24
namespace px {
8
25
9
26
namespace {
10
27
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
+
11
75
// / 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
13
79
{
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;
16
94
public:
17
95
// / 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
+ }
19
100
// / 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
+ }
21
145
// / Checks for any non-options that may be interpreted
22
146
// / as a document to be opened.
23
147
bool parseArgs (int , char **) override
24
148
{
25
149
return true ;
26
150
}
151
+ // / Pushes a state to the stack.
152
+ void pushAppState (AppState* state) override
153
+ {
154
+ stateStack.emplace_back (state);
155
+ }
27
156
// / Renders a frame of the application.
28
157
bool frame () override
29
158
{
@@ -35,14 +164,85 @@ class AppImpl final : public App
35
164
36
165
return true ;
37
166
}
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
+ }
38
178
protected:
39
179
// / This function renders a frame without checking for
40
180
// / exceptions.
41
181
// /
42
182
// / Exceptions are checked by the calling function.
43
183
void uncheckedFrame ()
44
184
{
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
+ }
46
246
}
47
247
};
48
248
0 commit comments