ScummVM API documentation
gui_main.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#ifndef AGS_SHARED_GUI_GUI_MAIN_H
23#define AGS_SHARED_GUI_GUI_MAIN_H
24
25#include "common/std/vector.h"
26#include "ags/engine/ac/draw.h"
27#include "ags/shared/ac/common.h"
28#include "ags/shared/ac/common_defines.h" // TODO: split out gui drawing helpers
29#include "ags/shared/gfx/gfx_def.h" // TODO: split out gui drawing helpers
30#include "ags/shared/gui/gui_defines.h"
31#include "ags/shared/util/error.h"
32#include "ags/shared/util/geometry.h"
33#include "ags/shared/util/string.h"
34
35namespace AGS3 {
36
37// Forward declaration
38namespace AGS {
39namespace Shared {
40class Stream;
41}
42}
43using namespace AGS; // FIXME later
44
45class SplitLines;
46
47#define LEGACY_MAX_OBJS_ON_GUI 30
48
49#define GUIMAIN_LEGACY_RESERVED_INTS 5
50#define GUIMAIN_LEGACY_NAME_LENGTH 16
51#define GUIMAIN_LEGACY_EVENTHANDLER_LENGTH 20
52#define GUIMAIN_LEGACY_TW_FLAGS_SIZE 4
53
54namespace AGS {
55namespace Shared {
56
57// Legacy GUIMain visibility state, which combined Visible property and override factor
58enum LegacyGUIVisState {
59 kGUIVisibility_LockedOff = -1, // locked hidden (used by PopupMouseY guis)
60 kGUIVisibility_Off = 0, // hidden
61 kGUIVisibility_On = 1 // shown
62};
63
64class Bitmap;
65class GUIObject;
66
67
68class GUIMain {
69public:
70 static String FixupGUIName(const String &name);
71
72public:
73 GUIMain();
74
75 void InitDefaults();
76
77 // Tells if the gui background supports alpha channel
78 bool HasAlphaChannel() const;
79 // Tells if GUI will react on clicking on it
80 bool IsClickable() const { return (_flags & kGUIMain_Clickable) != 0; }
81 // Tells if GUI's visibility is overridden and it won't be displayed on
82 // screen regardless of Visible property (until concealed mode is off).
83 bool IsConcealed() const { return (_flags & kGUIMain_Concealed) != 0; }
84 // Tells if gui is actually meant to be displayed on screen.
85 // Normally Visible property determines whether GUI is allowed to be seen,
86 // but there may be other settings that override it.
87 bool IsDisplayed() const { return IsVisible() && !IsConcealed(); }
88 // Tells if given coordinates are within interactable area of gui
89 // NOTE: this currently tests for actual visibility and Clickable property
90 bool IsInteractableAt(int x, int y) const;
91 // Tells if gui is a text window
92 bool IsTextWindow() const { return (_flags & kGUIMain_TextWindow) != 0; }
93 // Tells if GUI is *allowed* to be displayed and interacted with.
94 // This does not necessarily mean that it is displayed right now, because
95 // GUI may be hidden for other reasons, including overriding behavior.
96 // For example GUI with kGUIPopupMouseY style will not be shown unless
97 // mouse cursor is at certain position on screen.
98 bool IsVisible() const { return (_flags & kGUIMain_Visible) != 0; }
99
100 // Tells if GUI has graphically changed recently
101 bool HasChanged() const { return _hasChanged; }
102 bool HasControlsChanged() const { return _hasControlsChanged; }
103 // Manually marks GUI as graphically changed
104 // NOTE: this only matters if GUI's own graphic changes (content, size etc),
105 // but not its state (visible) or texture drawing mode (transparency, etc).
106 void MarkChanged();
107 // Marks GUI as having any of its controls changed its looks.
108 void MarkControlChanged();
109 // Clears changed flag
110 void ClearChanged();
111 // Notify GUI about any of its controls changing its location.
112 void NotifyControlPosition();
113 // Notify GUI about one of its controls changing its interactive state.
114 void NotifyControlState(int objid, bool mark_changed);
115 // Resets control-under-mouse detection.
116 void ResetOverControl();
117
118 // Finds a control under given screen coordinates, returns control's child ID.
119 // Optionally allows extra leeway (offset in all directions) to let the user grab tiny controls.
120 // Optionally only allows clickable controls, ignoring non-clickable ones.
121 int32_t FindControlAt(int atx, int aty, int leeway = 0, bool must_be_clickable = true) const;
122 // Gets the number of the GUI child controls
123 int32_t GetControlCount() const;
124 // Gets control by its child's index
125 GUIObject *GetControl(int32_t index) const;
126 // Gets child control's type, looks up with child's index
127 GUIControlType GetControlType(int32_t index) const;
128 // Gets child control's global ID, looks up with child's index
129 int32_t GetControlID(int32_t index) const;
130 // Gets an array of child control indexes in the z-order, from bottom to top
131 const std::vector<int> &GetControlsDrawOrder() const;
132
133 // Child control management
134 // Note that currently GUIMain does not own controls (should not delete them)
135 void AddControl(GUIControlType type, int32_t id, GUIObject *control);
136 void RemoveAllControls();
137
138 // Operations
139 bool BringControlToFront(int32_t index);
140 void DrawSelf(Bitmap *ds);
141 void DrawWithControls(Bitmap *ds);
142 // Polls GUI state, providing current cursor (mouse) coordinates
143 void Poll(int mx, int my);
144 HError RebuildArray();
145 void ResortZOrder();
146 bool SendControlToBack(int32_t index);
147 // Sets whether GUI should react to player clicking on it
148 void SetClickable(bool on);
149 // Override GUI visibility; when in concealed mode GUI won't show up
150 // even if Visible = true
151 void SetConceal(bool on);
152 // Attempts to change control's zorder; returns if zorder changed
153 bool SetControlZOrder(int32_t index, int zorder);
154 // Changes GUI style to the text window or back
155 void SetTextWindow(bool on);
156 // Sets GUI transparency as a percentage (0 - 100) where 100 = invisible
157 void SetTransparencyAsPercentage(int percent);
158 // Sets whether GUI is allowed to be displayed on screen
159 void SetVisible(bool on);
160
161 // Events
162 void OnMouseButtonDown(int mx, int my);
163 void OnMouseButtonUp();
164
165 // Serialization
166 void ReadFromFile(Stream *in, GuiVersion gui_version);
167 void WriteToFile(Stream *out) const;
168 // TODO: move to engine, into gui savegame component unit
169 // (should read/write GUI properties accessing them by interface)
170 void ReadFromSavegame(Stream *in, GuiSvgVersion svg_version);
171 void WriteToSavegame(Stream *out) const;
172
173private:
174 void DrawBlob(Bitmap *ds, int x, int y, color_t draw_color);
175 // Same as FindControlAt but expects local space coordinates
176 int32_t FindControlAtLocal(int atx, int aty, int leeway, bool must_be_clickable) const;
177
178 // TODO: all members are currently public; hide them later
179public:
180 int32_t ID; // GUI identifier
181 String Name; // the name of the GUI
182
183 int32_t X;
184 int32_t Y;
185 int32_t Width;
186 int32_t Height;
187 color_t BgColor; // background color
188 int32_t BgImage; // background sprite index
189 color_t FgColor; // foreground color (used as border color in normal GUIs,
190 // and text color in text windows)
191 int32_t Padding; // padding surrounding a GUI text window
192 GUIPopupStyle PopupStyle; // GUI popup behavior
193 int32_t PopupAtMouseY; // popup when _G(mousey) < this
194 int32_t Transparency; // "incorrect" alpha (in legacy 255-range units)
195 int32_t ZOrder;
196
197 int32_t FocusCtrl; // which control has the focus
198 int32_t HighlightCtrl; // which control has the bounding selection rect
199 int32_t MouseOverCtrl; // which control has the mouse cursor over it
200 int32_t MouseDownCtrl; // which control has the mouse button pressed on it
201 Point MouseWasAt; // last mouse cursor position
202
203 String OnClickHandler; // script function name
204
205private:
206 int32_t _flags; // style and behavior flags
207 bool _hasChanged; // flag tells whether GUI has graphically changed recently
208 bool _hasControlsChanged;
209 bool _polling; // inside the polling process
210
211 // Array of types and control indexes in global GUI object arrays;
212 // maps GUI child slots to actual controls and used for rebuilding Controls array
214 std::vector<ControlRef> _ctrlRefs;
215 // Array of child control references (not exclusively owned!)
216 std::vector<GUIObject *> _controls;
217 // Sorted array of controls in z-order.
218 std::vector<int> _ctrlDrawOrder;
219};
220
221
222namespace GUI {
223extern GuiVersion GameGuiVersion;
224extern GuiOptions Options;
225
226// Applies current text direction setting (may depend on multiple factors)
227String ApplyTextDirection(const String &text);
228// Calculates the text's draw position, given the alignment
229// optionally returns the real graphical rect that the text would occupy
230Point CalcTextPosition(const char *text, int font, const Rect &frame, FrameAlignment align, Rect *gr_rect = nullptr);
231// Calculates the text's draw position and horizontal extent,
232// using strictly horizontal alignment
233Line CalcTextPositionHor(const char *text, int font, int x1, int x2, int y, FrameAlignment align);
234// Calculates the graphical rect that the text would occupy
235// if drawn at the given coordinates
236Rect CalcTextGraphicalRect(const char *text, int font, const Point &at);
237// Calculates the graphical rect that the text would occupy
238// if drawn aligned to the given frame
239Rect CalcTextGraphicalRect(const char *text, int font, const Rect &frame, FrameAlignment align);
240// Calculates a vertical graphical extent for a given font,
241// which is a top and bottom offsets in zero-based coordinates.
242// NOTE: this applies font size fixups.
243Line CalcFontGraphicalVExtent(int font);
244// Draw standart "shading" effect over rectangle
245void DrawDisabledEffect(Bitmap *ds, const Rect &rc);
246// Draw text aligned inside rectangle
247void DrawTextAligned(Bitmap *ds, const char *text, int font, color_t text_color, const Rect &frame, FrameAlignment align);
248// Draw text aligned horizontally inside given bounds
249void DrawTextAlignedHor(Bitmap *ds, const char *text, int font, color_t text_color, int x1, int x2, int y, FrameAlignment align);
250
251// Parses the string and returns combination of label macro flags
252GUILabelMacro FindLabelMacros(const String &text);
253// Applies text transformation necessary for rendering, in accordance to the
254// current game settings, such as right-to-left render, and anything else
255String TransformTextForDrawing(const String &text, bool translate, bool apply_direction);
256// Wraps given text to make it fit into width, stores it in the lines;
257// apply_direction param tells whether text direction setting should be applied
258size_t SplitLinesForDrawing(const char *text, bool apply_direction, SplitLines &lines, int font, int width, size_t max_lines = -1);
259
260// Mark all existing GUI for redraw
261void MarkAllGUIForUpdate(bool redraw, bool reset_over_ctrl);
262// Mark all translatable GUI controls for redraw
263void MarkForTranslationUpdate();
264// Mark all GUI which use the given font for recalculate/redraw;
265// pass -1 to update all the textual controls together
266void MarkForFontUpdate(int font);
267// Mark labels that acts as special text placeholders for redraw
268void MarkSpecialLabelsForUpdate(GUILabelMacro macro);
269// Mark inventory windows for redraw, optionally only ones linked to given character;
270// also marks buttons with inventory icon mode
271void MarkInventoryForUpdate(int char_id, bool is_player);
272
273// Reads all GUIs and their controls.
274// WARNING: the data is read into the global arrays (guis, guibuts, and so on)
275// TODO: remove is_savegame param after dropping support for old saves
276// because only they use ReadGUI to read runtime GUI data
277HError ReadGUI(Stream *in, bool is_savegame = false);
278// Writes all GUIs and their controls.
279// WARNING: the data is written from the global arrays (guis, guibuts, and so on)
280void WriteGUI(Stream *out);
281// Converts legacy GUIVisibility into appropriate GUIMain properties
282void ApplyLegacyVisibility(GUIMain &gui, LegacyGUIVisState vis);
283
284// Rebuilds GUIs, connecting them to the child controls in memory.
285// WARNING: the data is processed in the global arrays (guis, guibuts, and so on)
286HError RebuildGUI();
287}
288
289} // namespace Shared
290} // namespace AGS
291
292extern int get_adjusted_spritewidth(int spr);
293extern int get_adjusted_spriteheight(int spr);
294extern bool is_sprite_alpha(int spr);
295
296extern void set_eip_guiobj(int eip);
297extern int get_eip_guiobj();
298
299} // namespace AGS3
300
301#endif
Definition: allegro_bitmap.h:44
Definition: error.h:110
Definition: gui_main.h:68
Definition: gui_object.h:44
Definition: stream.h:52
Definition: string.h:60
Definition: fonts.h:157
Definition: gui_defines.h:203
Definition: geometry.h:118
Definition: geometry.h:87
Definition: geometry.h:219
Definition: utility.h:39