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 loadDiploma();
105 
106  void clearControls();
107  bool processEvent(Common::Event &event);
108  void handleMenuAction(MenuAction action);
109  void updateWindow(WindowReference winID, bool containerOpen);
110  void invertWindowColors(WindowReference winID);
111 
112  WindowReference createInventoryWindow(ObjID objRef);
113  bool tryCloseWindow(WindowReference winID);
114  bool tryCloseWindowRec(WindowReference winID, bool runControl = false);
115  void resetWindows(); // Close and destroy inventory and main game windows
116  void closeAllWindows();
117 
118  void highlightExitButton(ObjID objID);
119 
120  Common::Point getObjMeasures(ObjID obj);
121 
122  WindowReference getObjWindow(ObjID objID);
123  WindowReference findObjWindow(ObjID objID);
124 
125  // Event processors
126  bool processCommandEvents(WindowClick click, Common::Event &event);
127  bool processMainGameEvents(WindowClick click, Common::Event &event);
128  bool processOutConsoleEvents(WindowClick click, Common::Event &event);
129  bool processSelfEvents(WindowClick click, Common::Event &event);
130  bool processExitsEvents(WindowClick click, Common::Event &event);
131  bool processDiplomaEvents(WindowClick click, Common::Event &event);
132  bool processInventoryEvents(WindowReference ref, WindowClick click, Common::Event &event);
133 
134  const WindowData& getWindowData(WindowReference reference);
135  Graphics::MacWindow *findWindow(WindowReference reference);
136 
137  Graphics::MacWindowManager *getMacWindowManager() { return &_wm; }
138  const Graphics::Font& getCurrentFont();
139 
140  // Clicks
141  void select(Common::Point cursorPosition, bool shiftPressed, bool isDoubleClick);
142  void handleSingleClick();
143  void handleDoubleClick();
144 
145  // Modifiers
146  void bringToFront(WindowReference window);
147  void setWindowTitle(WindowReference winID, const Common::String &string);
148  void updateWindowInfo(WindowReference ref, ObjID objID, const Common::Array<ObjID> &children);
149  void ensureInventoryOpen(WindowReference reference, ObjID id);
150 
151  void addChild(WindowReference target, ObjID child);
152  void removeChild(WindowReference target, ObjID child);
153 
154  void clearDraggedObjects();
155 
156  void clearExits();
157  void unselectExits();
158  void updateExit(ObjID id);
159 
160  Common::String getConsoleText() const;
161  void setConsoleText(const Common::String &text);
162 
163  void printText(const Common::String &text);
164 
165  //Dialog interactions
166  void showPrebuiltDialog(PrebuiltDialogs type, const Common::String &title = "");
167  bool isDialogOpen();
168 
169  void getTextFromUser(Common::String &title);
170  void setTextInput(const Common::String &str);
171  void closeDialog();
172 
173  void loadGame();
174  void saveGame();
175  void newGame();
176  void quitGame();
177 
178  void createInnerSurface(Graphics::ManagedSurface *innerSurface, Graphics::ManagedSurface *outerSurface, const BorderBounds &borders);
179 
180 
181 private: // Attributes
182 
183  MacVentureEngine *_engine;
184  Common::MacResManager *_resourceManager;
185 
186  Graphics::ManagedSurface _screen;
188 
189  WindowReference _activeWinRef;
190 
191  Common::List<WindowData> *_windowData;
192  Common::Array<CommandButton> *_controlData;
193  Common::Array<CommandButton> *_exitsData;
194 
195  Graphics::MacWindow *_controlsWindow;
196  Graphics::MacWindow *_mainGameWindow;
197  Graphics::MacTextWindow *_outConsoleWindow;
198  Graphics::MacWindow *_selfWindow;
199  Graphics::MacWindow *_exitsWindow;
200  Graphics::MacWindow *_diplomaWindow;
201 
202  struct InventoryWindowData {
203  Graphics::MacWindow *win;
204  WindowReference ref;
205  };
206  Common::Array<InventoryWindowData> _inventoryWindows;
208 
209  Graphics::MacMenu *_menu;
210  Dialog *_dialog;
211 
212  Container *_graphics;
214  ImageAsset *_diplomaImage;
215 
216  Common::Array<DraggedObj> _draggedObjects;
218 
219  Cursor *_cursor;
220 
221  ConsoleText *_consoleText;
222 
223  WindowReference _lassoWinRef;
224  Common::Point _lassoStart;
225  Common::Point _lassoEnd;
226  bool _lassoBeingDrawn;
227 
228 private: // Methods
229 
230  // Initializers
231  void initGUI();
232  void initWindows();
233  void assignObjReferences(); // Mainly guesswork
234 
235  // Loaders
236  bool loadMenus();
237  bool loadWindows();
238  bool loadControls();
239  void loadBorders(Graphics::MacWindow *target, MVWindowType type);
240  void loadBorder(Graphics::MacWindow *target, MVWindowType type, bool active);
241  void loadGraphics();
242  void clearAssets();
243 
244  // Drawers
245  void drawWindows();
246  void drawCommandsWindow();
247  void drawDiplomaWindow();
248  void drawMainGameWindow();
249  void drawSelfWindow();
250  void drawInventories();
251  void drawExitsWindow();
252  void drawConsoleWindow();
253 
254  void drawDraggedObjects();
255  void drawObjectsInWindow(const WindowData &targetData, Graphics::ManagedSurface *surface);
256  void drawWindowTitle(WindowReference target, Graphics::ManagedSurface *surface);
257  void drawDialog();
258 
259  void moveDraggedObjects(Common::Point target);
260 
261  // Finders
262  WindowReference findWindowAtPoint(Common::Point point);
263  Common::Point getGlobalScrolledSurfacePosition(WindowReference reference);
264  WindowData& findWindowData(WindowReference reference);
265 
266  // Utils
267  void checkSelect(const WindowData &data, Common::Point pos, const Common::Rect &clickRect, WindowReference ref, bool shiftPressed, bool isDoubleClick);
268  bool canBeSelected(ObjID obj, const Common::Rect &clickRect, WindowReference ref);
269  bool isRectInsideObject(Common::Rect target, ObjID obj);
270  void selectDraggable(ObjID child, WindowReference origin, Common::Point startPos);
271  void handleDragRelease(bool shiftPressed, bool isDoubleClick);
272  Common::Rect calculateClickRect(Common::Point clickPos, Common::Rect windowBounds);
273  Common::Point localizeTravelledDistance(Common::Point point, WindowReference origin, WindowReference target);
274  void removeInventoryWindow(WindowReference ref);
275 
276  void ensureAssetLoaded(ObjID obj);
277 
278 };
279 
281  Gui *gui;
282  WindowReference ref;
283 };
284 
285 enum ClickState {
286  kCursorIdle = 0,
287  kCursorSCStart = 1,
288  kCursorSCDrag = 2,
289  kCursorDCStart = 3,
290  kCursorDCDo = 4,
291  kCursorSCSink = 5,
292  kCursorStateCount
293 };
294 
295 enum CursorInput { // Columns for the FSM transition table
296  kButtonDownCol = 0,
297  kButtonUpCol = 1,
298  kTickCol = 2,
299  kCursorInputCount
300 };
301 
302 class Cursor {
303 
304 public:
305  Cursor(Gui *gui);
306  ~Cursor();
307 
308  void tick();
309  bool processEvent(const Common::Event &event);
310  Common::Point getPos();
311  bool canSelectDraggable();
312 
313 private:
314 
315  void changeState(CursorInput input);
316  void executeStateIn();
317  void executeStateOut();
318 
319 
320 private:
321  Gui *_gui;
322 
323  Common::Point _pos;
324  ClickState _state;
325 };
326 
327 
328 
329 enum {
330  kConsoleLeftOffset = 2
331 };
332 
333 class ConsoleText {
334 
335 public:
336 
337  ConsoleText(Gui *gui) {
338  _gui = gui;
339  _lines.push_back("");
340  updateScroll();
341  }
342 
343  ~ConsoleText() {
344 
345  }
346 
347  void printLine(const Common::String &str, int maxW) {
348  Common::StringArray wrappedLines;
349  int textW = maxW;
350  const Graphics::Font *font = &_gui->getCurrentFont();
351 
352  font->wordWrapText(str, textW, wrappedLines);
353 
354  if (wrappedLines.empty()) // Sometimes we have empty lines
355  _lines.push_back("");
356 
357  for (Common::StringArray::const_iterator j = wrappedLines.begin(); j != wrappedLines.end(); ++j) {
358  _lines.push_back(*j);
359  }
360 
361  updateScroll();
362  }
363 
364  void renderInto(Graphics::ManagedSurface *target, const BorderBounds borders, int textOffset) {
365  target->fillRect(target->getBounds(), kColorWhite);
366 
367  Graphics::ManagedSurface *composeSurface = new Graphics::ManagedSurface();
368  _gui->createInnerSurface(composeSurface, target, borders);
369  composeSurface->clear(kColorGreen);
370 
371  const Graphics::Font *font = &_gui->getCurrentFont();
372  int y = target->h - font->getFontHeight();
373  for (uint i = _scrollPos; i != 0; i--) {
374  font->drawString(target, _lines[i], textOffset, y, font->getStringWidth(_lines[i]), kColorBlack);
375 
376  if (y < font->getFontHeight()) // Do not draw off-screen
377  break;
378 
379  y -= font->getFontHeight();
380  }
381 
382  Common::Point composePosition = Common::Point(borders.leftOffset, borders.topOffset);
383  target->transBlitFrom(*composeSurface, composePosition, kColorGreen);
384  delete composeSurface;
385  }
386 
387  void updateScroll() {
388  _scrollPos = _lines.size() - 1;
389  }
390 
391  void scrollDown() {
392  if (_scrollPos < (int)(_lines.size() - 1)) {
393  _scrollPos++;
394  }
395  }
396 
397  void scrollUp() {
398  if (_scrollPos > 0) {
399  _scrollPos--;
400  }
401  }
402 
403 
404 private:
405 
406  Gui *_gui;
407 
408  Common::StringArray _lines;
409  int _scrollPos;
410 
411 };
412 
413 } // End of namespace MacVenture
414 
415 #endif
void clear(uint32 color=0)
Definition: managed_surface.h:51
Definition: dialog.h:48
Definition: macresman.h:126
const Common::Rect getBounds() const
Definition: managed_surface.h:325
Definition: str.h:59
Definition: macwindow.h:40
Definition: font.h:83
Definition: gui.h:92
Definition: image.h:63
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:382
iterator begin()
Definition: array.h:377
Definition: mactextwindow.h:30
Definition: list.h:44
Definition: gui.h:333
Definition: macwindowmanager.h:45
Definition: rect.h:524
int getStringWidth(const Common::String &str) const
void fillRect(const Common::Rect &r, uint32 color)
Definition: managed_surface.h:933
Definition: macwindowmanager.h:147
Definition: macwindowborder.h:57
Definition: windows.h:97
int16 & h
Definition: managed_surface.h:132
bool empty() const
Definition: array.h:354
Definition: container.h:48
Definition: atari-cursor.h:35
Definition: events.h:210
Definition: macventure.h:218
Definition: rect.h:144
Definition: macmenu.h:94
Definition: container.h:38
Definition: gui.h:302
void transBlitFrom(const Surface &src, uint32 transColor=0, bool flipped=false, uint32 srcAlpha=0xff, const Palette *srcPalette=nullptr)
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