ScummVM API documentation
gui.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Based on
24  * WebVenture (c) 2010, Sean Kasun
25  * https://github.com/mrkite/webventure, http://seancode.com/webventure/
26  *
27  * Used with explicit permission from the author
28  */
29 
30 #ifndef MACVENTURE_GUI_H
31 #define MACVENTURE_GUI_H
32 
33 #include "graphics/macgui/macwindowmanager.h"
34 #include "graphics/macgui/macwindow.h"
35 #include "graphics/macgui/macmenu.h"
36 
37 #include "graphics/font.h"
38 
39 #include "common/timer.h"
40 
41 #include "macventure/macventure.h"
42 #include "macventure/container.h"
43 #include "macventure/image.h"
44 #include "macventure/prebuilt_dialogs.h"
45 #include "macventure/dialog.h"
46 #include "macventure/controls.h"
47 #include "macventure/windows.h"
48 
49 namespace MacVenture {
50 
51 using namespace Graphics::MacGUIConstants;
52 using namespace Graphics::MacWindowConstants;
53 class MacVentureEngine;
54 typedef uint32 ObjID;
55 
56 class Cursor;
57 class ConsoleText;
58 class CommandButton;
59 class ImageAsset;
60 class Dialog;
61 
62 BorderBounds borderBounds(MVWindowType type);
63 Graphics::BorderOffsets borderOffsets(MVWindowType type);
64 
65 enum MenuAction {
66  kMenuActionAbout,
67  kMenuActionNew,
68  kMenuActionOpen,
69  kMenuActionSave,
70  kMenuActionSaveAs,
71  kMenuActionQuit,
72  kMenuActionUndo,
73  kMenuActionCut,
74  kMenuActionCopy,
75  kMenuActionPaste,
76  kMenuActionClear,
77  kMenuActionCleanUp,
78  kMenuActionMessUp,
79 
80  kMenuActionCommand
81 };
82 
83 struct DraggedObj {
84  ObjID id;
85  Common::Point pos;
86  Common::Point mouseOffset;
87  Common::Point startPos;
88  WindowReference startWin;
89  bool hasMoved;
90 };
91 
92 class Gui {
93 
94 public:
95  Gui(MacVentureEngine *engine, Common::MacResManager *resman);
96  ~Gui();
97 
98  void reloadInternals();
99 
100  void draw();
101  void drawMenu();
102  void drawTitle();
103 
104  void clearControls();
105  bool processEvent(Common::Event &event);
106  void handleMenuAction(MenuAction action);
107  void updateWindow(WindowReference winID, bool containerOpen);
108  void invertWindowColors(WindowReference winID);
109 
110  WindowReference createInventoryWindow(ObjID objRef);
111  bool tryCloseWindow(WindowReference winID);
112 
113  Common::Point getObjMeasures(ObjID obj);
114 
115  WindowReference getObjWindow(ObjID objID);
116  WindowReference findObjWindow(ObjID objID);
117 
118  // Event processors
119  bool processCommandEvents(WindowClick click, Common::Event &event);
120  bool processMainGameEvents(WindowClick click, Common::Event &event);
121  bool processOutConsoleEvents(WindowClick click, Common::Event &event);
122  bool processSelfEvents(WindowClick click, Common::Event &event);
123  bool processExitsEvents(WindowClick click, Common::Event &event);
124  bool processDiplomaEvents(WindowClick click, Common::Event &event);
125  bool processInventoryEvents(WindowClick click, Common::Event &event);
126 
127  const WindowData& getWindowData(WindowReference reference);
128 
129  const Graphics::Font& getCurrentFont();
130 
131  // Clicks
132  void selectForDrag(Common::Point cursorPosition);
133  void handleSingleClick();
134  void handleDoubleClick();
135 
136  // Modifiers
137  void bringToFront(WindowReference window);
138  void setWindowTitle(WindowReference winID, const Common::String &string);
139  void updateWindowInfo(WindowReference ref, ObjID objID, const Common::Array<ObjID> &children);
140  void ensureInventoryOpen(WindowReference reference, ObjID id);
141 
142  void addChild(WindowReference target, ObjID child);
143  void removeChild(WindowReference target, ObjID child);
144 
145  void clearExits();
146  void unselectExits();
147  void updateExit(ObjID id);
148 
149  void printText(const Common::String &text);
150 
151  //Dialog interactions
152  void showPrebuiltDialog(PrebuiltDialogs type);
153  bool isDialogOpen();
154 
155  void getTextFromUser();
156  void setTextInput(const Common::String &str);
157  void closeDialog();
158 
159  void loadGame();
160  void saveGame();
161  void newGame();
162  void quitGame();
163 
164  void createInnerSurface(Graphics::ManagedSurface *innerSurface, Graphics::ManagedSurface *outerSurface, const BorderBounds &borders);
165 
166 
167 private: // Attributes
168 
169  MacVentureEngine *_engine;
170  Common::MacResManager *_resourceManager;
171 
172  Graphics::ManagedSurface _screen;
174 
175  Common::List<WindowData> *_windowData;
176  Common::Array<CommandButton> *_controlData;
177  Common::Array<CommandButton> *_exitsData;
178 
179  Graphics::MacWindow *_controlsWindow;
180  Graphics::MacWindow *_mainGameWindow;
181  Graphics::MacWindow *_outConsoleWindow;
182  Graphics::MacWindow *_selfWindow;
183  Graphics::MacWindow *_exitsWindow;
184  Graphics::MacWindow *_diplomaWindow;
185  Common::Array<Graphics::MacWindow*> _inventoryWindows;
186  Graphics::MacMenu *_menu;
187  Dialog *_dialog;
188 
189  Container *_graphics;
191 
192  Graphics::ManagedSurface _draggedSurface;
193  DraggedObj _draggedObj;
194 
195  Cursor *_cursor;
196 
197  ConsoleText *_consoleText;
198 
199 private: // Methods
200 
201  // Initializers
202  void initGUI();
203  void initWindows();
204  void assignObjReferences(); // Mainly guesswork
205 
206  // Loaders
207  bool loadMenus();
208  bool loadWindows();
209  bool loadControls();
210  void loadBorders(Graphics::MacWindow *target, MVWindowType type);
211  void loadBorder(Graphics::MacWindow *target, MVWindowType type, bool active);
212  void loadGraphics();
213  void clearAssets();
214 
215  // Drawers
216  void drawWindows();
217  void drawCommandsWindow();
218  void drawMainGameWindow();
219  void drawSelfWindow();
220  void drawInventories();
221  void drawExitsWindow();
222  void drawConsoleWindow();
223 
224  void drawDraggedObject();
225  void drawObjectsInWindow(const WindowData &targetData, Graphics::ManagedSurface *surface);
226  void drawWindowTitle(WindowReference target, Graphics::ManagedSurface *surface);
227  void drawDialog();
228 
229  void moveDraggedObject(Common::Point target);
230 
231  // Finders
232  WindowReference findWindowAtPoint(Common::Point point);
233  Common::Point getGlobalScrolledSurfacePosition(WindowReference reference);
234  WindowData& findWindowData(WindowReference reference);
235  Graphics::MacWindow *findWindow(WindowReference reference);
236 
237  // Utils
238  void checkSelect(const WindowData &data, Common::Point pos, const Common::Rect &clickRect, WindowReference ref);
239  bool canBeSelected(ObjID obj, const Common::Rect &clickRect, WindowReference ref);
240  bool isRectInsideObject(Common::Rect target, ObjID obj);
241  void selectDraggable(ObjID child, WindowReference origin, Common::Point startPos);
242  void handleDragRelease(bool shiftPressed, bool isDoubleClick);
243  Common::Rect calculateClickRect(Common::Point clickPos, Common::Rect windowBounds);
244  Common::Point localizeTravelledDistance(Common::Point point, WindowReference origin, WindowReference target);
245  void removeInventoryWindow(WindowReference ref);
246 
247  void ensureAssetLoaded(ObjID obj);
248 
249 };
250 
251 enum ClickState {
252  kCursorIdle = 0,
253  kCursorSCStart = 1,
254  kCursorSCDrag = 2,
255  kCursorDCStart = 3,
256  kCursorDCDo = 4,
257  kCursorSCSink = 5,
258  kCursorStateCount
259 };
260 
261 enum CursorInput { // Columns for the FSM transition table
262  kButtonDownCol = 0,
263  kButtonUpCol = 1,
264  kTickCol = 2,
265  kCursorInputCount
266 };
267 
268 class Cursor {
269 
270 public:
271  Cursor(Gui *gui);
272  ~Cursor();
273 
274  void tick();
275  bool processEvent(const Common::Event &event);
276  Common::Point getPos();
277  bool canSelectDraggable();
278 
279 private:
280 
281  void changeState(CursorInput input);
282  void executeStateIn();
283  void executeStateOut();
284 
285 
286 private:
287  Gui *_gui;
288 
289  Common::Point _pos;
290  ClickState _state;
291 };
292 
293 
294 
295 enum {
296  kConsoleLeftOffset = 2
297 };
298 
299 class ConsoleText {
300 
301 public:
302 
303  ConsoleText(Gui *gui) {
304  _gui = gui;
305  _lines.push_back("");
306  updateScroll();
307  }
308 
309  ~ConsoleText() {
310 
311  }
312 
313  void printLine(const Common::String &str, int maxW) {
314  Common::StringArray wrappedLines;
315  int textW = maxW;
316  const Graphics::Font *font = &_gui->getCurrentFont();
317 
318  font->wordWrapText(str, textW, wrappedLines);
319 
320  if (wrappedLines.empty()) // Sometimes we have empty lines
321  _lines.push_back("");
322 
323  for (Common::StringArray::const_iterator j = wrappedLines.begin(); j != wrappedLines.end(); ++j) {
324  _lines.push_back(*j);
325  }
326 
327  updateScroll();
328  }
329 
330  void renderInto(Graphics::ManagedSurface *target, const BorderBounds borders, int textOffset) {
331  target->fillRect(target->getBounds(), kColorWhite);
332 
333  Graphics::ManagedSurface *composeSurface = new Graphics::ManagedSurface();
334  _gui->createInnerSurface(composeSurface, target, borders);
335  composeSurface->clear(kColorGreen);
336 
337  const Graphics::Font *font = &_gui->getCurrentFont();
338  int y = target->h - font->getFontHeight();
339  for (uint i = _scrollPos; i != 0; i--) {
340  font->drawString(target, _lines[i], textOffset, y, font->getStringWidth(_lines[i]), kColorBlack);
341 
342  if (y < font->getFontHeight()) // Do not draw off-screen
343  break;
344 
345  y -= font->getFontHeight();
346  }
347 
348  Common::Point composePosition = Common::Point(borders.leftOffset, borders.topOffset);
349  target->transBlitFrom(*composeSurface, composePosition, kColorGreen);
350  delete composeSurface;
351  }
352 
353  void updateScroll() {
354  _scrollPos = _lines.size() - 1;
355  }
356 
357  void scrollDown() {
358  if (_scrollPos < (int)(_lines.size() - 1)) {
359  _scrollPos++;
360  }
361  }
362 
363  void scrollUp() {
364  if (_scrollPos > 0) {
365  _scrollPos--;
366  }
367  }
368 
369 
370 private:
371 
372  Gui *_gui;
373 
374  Common::StringArray _lines;
375  int _scrollPos;
376 
377 };
378 
379 } // End of namespace MacVenture
380 
381 #endif
void clear(uint32 color=0)
Definition: managed_surface.h:51
Definition: dialog.h:44
Definition: macresman.h:125
const Common::Rect getBounds() const
Definition: managed_surface.h:326
Definition: str.h:59
Definition: macwindow.h:40
Definition: font.h:83
Definition: gui.h:92
int wordWrapText(const Common::String &str, int maxWidth, Common::Array< Common::String > &lines, int initWidth=0, uint32 mode=kWordWrapOnExplicitNewLines) const
Definition: macwindow.h:192
Definition: windows.h:83
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
Definition: list.h:44
Definition: gui.h:299
Definition: macwindowmanager.h:45
Definition: rect.h:144
void transBlitFrom(const Surface &src, uint32 transColor=0, bool flipped=false, uint32 overrideColor=0, uint32 srcAlpha=0xff, const Palette *srcPalette=nullptr)
int getStringWidth(const Common::String &str) const
Definition: macwindowmanager.h:149
Definition: macwindowborder.h:57
Definition: windows.h:97
int16 & h
Definition: managed_surface.h:118
bool empty() const
Definition: array.h:351
Definition: container.h:48
Definition: hashmap.h:85
Definition: atari-cursor.h:38
Definition: events.h:199
Definition: macventure.h:185
Definition: rect.h:45
Definition: macmenu.h:94
Definition: container.h:38
Definition: gui.h:268
void fillRect(Common::Rect r, uint32 color)
Definition: managed_surface.h:754
Definition: gui.h:83
virtual int getFontHeight() const =0
void drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align=kTextAlignLeft, int deltax=0, bool useEllipsis=false) const