1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14 
15 #ifndef __AC_GUIMAIN_H
16 #define __AC_GUIMAIN_H
17 
18 #include <vector>
19 #include "gui/guiobject.h"
20 #include "ac/common_defines.h"       // AGS_INLINE
21 #include "gfx/gfx_def.h"
22 #include "util/geometry.h"
23 #include "util/string.h"
24 
25 // Forward declaration
26 namespace AGS { namespace Common { class Stream; } }
27 using namespace AGS; // FIXME later
28 
29 // There were issues when including header caused conflicts
30 struct GameSetupStruct;
31 
32 #define LEGACY_MAX_OBJS_ON_GUI      30
33 
34 #define GUIMAIN_RESERVED_INTS       5
35 #define GUIMAIN_LEGACY_NAME_LENGTH         16
36 #define GUIMAIN_LEGACY_EVENTHANDLER_LENGTH 20
37 #define GUIMAIN_LEGACY_TW_FLAGS_SIZE 4
38 
39 namespace AGS
40 {
41 namespace Common
42 {
43 
44 enum GUIVisibilityState
45 {
46     kGUIVisibility_Concealed = -1, // gui is hidden by command
47     kGUIVisibility_Off       =  0, // gui is disabled (won't show up by command)
48     kGUIVisibility_On        =  1  // gui is shown by command
49 };
50 
51 
52 class GUIMain
53 {
54 public:
55     static String FixupGUIName(const String &name);
56 
57 public:
58     GUIMain();
59 
60     void Init();
61 
62     // Tells if the gui background supports alpha channel
63     bool        HasAlphaChannel() const;
64     // Tells if gui is allowed to be displayed, but is currently hidden off-screen
IsConcealed()65     inline bool IsConcealed() const { return _visibility == kGUIVisibility_Concealed; }
66     // Tells if given coordinates are within interactable area of gui
67     bool        IsInteractableAt(int x, int y) const;
68     // Tells if gui visibility is disabled
IsOff()69     inline bool IsOff() const { return _visibility == kGUIVisibility_Off; }
70     // Tells if gui is a text window
71     bool        IsTextWindow() const;
72     // Tells if gui is allowed to be displayed on screen
IsVisible()73     inline bool IsVisible() const { return _visibility == kGUIVisibility_On; }
74 
75     int32_t FindControlUnderMouse() const;
76     // this version allows some extra leeway in the Editor so that
77     // the user can grab tiny controls
78     int32_t FindControlUnderMouse(int leeway) const;
79     int32_t FindControlUnderMouse(int leeway, bool must_be_clickable) const;
80     GUIControlType GetControlType(int index) const;
81 
82     // Operations
83     bool    BringControlToFront(int index);
84     void    Draw(Bitmap *ds);
85     void    DrawAt(Bitmap *ds, int x, int y);
86     void    Poll();
87     void    RebuildArray();
88     void    ResortZOrder();
89     bool    SendControlToBack(int index);
90     // attempts to change control's zorder; returns if zorder changed
91     bool    SetControlZOrder(int index, int zorder);
92     void    SetTransparencyAsPercentage(int percent);
93     void    SetVisibility(GUIVisibilityState visibility);
94 
95     // Events
96     void    OnMouseButtonDown();
97     void    OnMouseButtonUp();
98     void    OnControlPositionChanged();
99 
100     // Serialization
101     void    ReadFromFile(Common::Stream *in, GuiVersion gui_version);
102     void    WriteToFile(Common::Stream *out, GuiVersion gui_version) const;
103 
104 private:
105     void    DrawBlob(Bitmap *ds, int x, int y, color_t draw_color);
106 
107     // TODO: all members are currently public; hide them later
108 public:
109     int32_t Id;             // GUI identifier
110     String  Name;           // the name of the GUI
111     int32_t Flags;          // style and behavior flags
112 
113     int32_t X;
114     int32_t Y;
115     int32_t Width;
116     int32_t Height;
117     color_t BgColor;        // background color
118     int32_t BgImage;        // background sprite index
119     color_t FgColor;        // foreground color
120     int32_t Padding;        // padding surrounding a GUI text window
121     GUIPopupStyle PopupStyle; // GUI popup behavior
122     int32_t PopupAtMouseY;  // popup when mousey < this
123     int32_t Transparency;   // "incorrect" alpha (in legacy 255-range units)
124     int32_t ZOrder;
125 
126     int32_t FocusCtrl;      // which control has the focus
127     int32_t HighlightCtrl;  // which control has the bounding selection rect
128     int32_t MouseOverCtrl;  // which control has the mouse cursor over it
129     int32_t MouseDownCtrl;  // which control has the mouse button pressed on it
130     Point   MouseWasAt;     // last mouse cursor position
131 
132     String  OnClickHandler; // script function name
133 
134     std::vector<GUIObject*> Controls; // array of child controls
135     std::vector<int32_t>    CtrlRefs; // for re-building controls array
136     std::vector<int16_t>    CtrlDrawOrder;
137 
138     // TODO: remove these later
139     int32_t ControlCount;   // number of objects on gui
140 
141 private:
142     GUIVisibilityState _visibility;
143 };
144 
145 } // namespace Common
146 } // namespace AGS
147 
148 extern GuiVersion GameGuiVersion;
149 extern std::vector<Common::GUIMain> guis;
150 extern int all_buttons_disabled, gui_inv_pic;
151 extern int gui_disabled_style;
152 extern char lines[MAXLINE][200];
153 extern int  numlines;
154 
155 extern void read_gui(Common::Stream *in, std::vector<Common::GUIMain> &guiread, GameSetupStruct * gss);
156 extern void write_gui(Common::Stream *out, const std::vector<Common::GUIMain> &guiwrite, GameSetupStruct * gss, bool savedgame);
157 
158 extern int mousex, mousey;
159 
160 extern int get_adjusted_spritewidth(int spr);
161 extern int get_adjusted_spriteheight(int spr);
162 extern bool is_sprite_alpha(int spr);
163 
164 // This function has distinct implementations in Engine and Editor
165 extern void draw_gui_sprite(Common::Bitmap *ds, int spr, int x, int y, bool use_alpha = true,
166                             Common::BlendMode blend_mode = Common::kBlendMode_Alpha);
167 
168 extern AGS_INLINE int divide_down_coordinate(int coord);
169 extern AGS_INLINE int multiply_up_coordinate(int coord);
170 extern AGS_INLINE void multiply_up_coordinates(int *x, int *y);
171 extern AGS_INLINE int get_fixed_pixel_size(int pixels);
172 
173 // Those function have distinct implementations in Engine and Editor
174 extern void wouttext_outline(Common::Bitmap *ds, int xxp, int yyp, int usingfont, color_t text_color, const char *texx);
175 extern int wgettextwidth_compensate(Common::Bitmap *ds, const char *tex, int font) ;
176 extern void check_font(int *fontnum);
177 
178 extern void set_our_eip(int eip);
179 #define SET_EIP(x) set_our_eip(x);
180 extern void set_eip_guiobj(int eip);
181 extern int get_eip_guiobj();
182 
183 extern bool outlineGuiObjects;
184 
185 #endif // __AC_GUIMAIN_H
186