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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ags/shared/ac/common.h" // quit
24 #include "ags/shared/gui/gui_main.h"
25 #include "ags/shared/gui/gui_object.h"
26 #include "ags/shared/util/stream.h"
27 
28 namespace AGS3 {
29 namespace AGS {
30 namespace Shared {
31 
GUIObject()32 GUIObject::GUIObject() {
33 	Id = 0;
34 	ParentId = 0;
35 	Flags = kGUICtrl_DefFlags;
36 	X = 0;
37 	Y = 0;
38 	Width = 0;
39 	Height = 0;
40 	ZOrder = -1;
41 	IsActivated = false;
42 	_scEventCount = 0;
43 }
44 
GetEventCount() const45 int GUIObject::GetEventCount() const {
46 	return _scEventCount;
47 }
48 
GetEventName(int event) const49 String GUIObject::GetEventName(int event) const {
50 	if (event < 0 || event >= _scEventCount)
51 		return "";
52 	return _scEventNames[event];
53 }
54 
GetEventArgs(int event) const55 String GUIObject::GetEventArgs(int event) const {
56 	if (event < 0 || event >= _scEventCount)
57 		return "";
58 	return _scEventArgs[event];
59 }
60 
IsDeleted() const61 bool GUIObject::IsDeleted() const {
62 	return (Flags & kGUICtrl_Deleted) != 0;
63 }
64 
IsEnabled() const65 bool GUIObject::IsEnabled() const {
66 	return (Flags & kGUICtrl_Enabled) != 0;
67 }
68 
IsOverControl(int x,int y,int leeway) const69 bool GUIObject::IsOverControl(int x, int y, int leeway) const {
70 	return x >= X && y >= Y && x < (X + Width + leeway) && y < (Y + Height + leeway);
71 }
72 
IsTranslated() const73 bool GUIObject::IsTranslated() const {
74 	return (Flags & kGUICtrl_Translated) != 0;
75 }
76 
IsVisible() const77 bool GUIObject::IsVisible() const {
78 	return (Flags & kGUICtrl_Visible) != 0;
79 }
80 
SetClickable(bool on)81 void GUIObject::SetClickable(bool on) {
82 	if (on)
83 		Flags |= kGUICtrl_Clickable;
84 	else
85 		Flags &= ~kGUICtrl_Clickable;
86 }
87 
SetEnabled(bool on)88 void GUIObject::SetEnabled(bool on) {
89 	if (on)
90 		Flags |= kGUICtrl_Enabled;
91 	else
92 		Flags &= ~kGUICtrl_Enabled;
93 	NotifyParentChanged();
94 }
95 
SetTranslated(bool on)96 void GUIObject::SetTranslated(bool on) {
97 	if (on)
98 		Flags |= kGUICtrl_Translated;
99 	else
100 		Flags &= ~kGUICtrl_Translated;
101 	NotifyParentChanged();
102 }
103 
SetVisible(bool on)104 void GUIObject::SetVisible(bool on) {
105 	if (on)
106 		Flags |= kGUICtrl_Visible;
107 	else
108 		Flags &= ~kGUICtrl_Visible;
109 }
110 
111 // TODO: replace string serialization with StrUtil::ReadString and WriteString
112 // methods in the future, to keep this organized.
WriteToFile(Stream * out) const113 void GUIObject::WriteToFile(Stream *out) const {
114 	out->WriteInt32(Flags);
115 	out->WriteInt32(X);
116 	out->WriteInt32(Y);
117 	out->WriteInt32(Width);
118 	out->WriteInt32(Height);
119 	out->WriteInt32(ZOrder);
120 	Name.Write(out);
121 	out->WriteInt32(_scEventCount);
122 	for (int i = 0; i < _scEventCount; ++i)
123 		EventHandlers[i].Write(out);
124 }
125 
ReadFromFile(Stream * in,GuiVersion gui_version)126 void GUIObject::ReadFromFile(Stream *in, GuiVersion gui_version) {
127 	Flags = in->ReadInt32();
128 	// reverse particular flags from older format
129 	if (gui_version < kGuiVersion_350)
130 		Flags ^= kGUICtrl_OldFmtXorMask;
131 	X = in->ReadInt32();
132 	Y = in->ReadInt32();
133 	Width = in->ReadInt32();
134 	Height = in->ReadInt32();
135 	ZOrder = in->ReadInt32();
136 	if (gui_version < kGuiVersion_350) { // NOTE: reading into actual variables only for old savegame support
137 		IsActivated = in->ReadInt32() != 0;
138 	}
139 
140 	if (gui_version >= kGuiVersion_unkn_106)
141 		Name.Read(in);
142 	else
143 		Name.Free();
144 
145 	for (int i = 0; i < _scEventCount; ++i) {
146 		EventHandlers[i].Free();
147 	}
148 
149 	if (gui_version >= kGuiVersion_unkn_108) {
150 		int evt_count = in->ReadInt32();
151 		if (evt_count > _scEventCount)
152 			quit("Error: too many control events, need newer version");
153 		for (int i = 0; i < evt_count; ++i) {
154 			EventHandlers[i].Read(in);
155 		}
156 	}
157 }
158 
ReadFromSavegame(Stream * in,GuiSvgVersion svg_ver)159 void GUIObject::ReadFromSavegame(Stream *in, GuiSvgVersion svg_ver) {
160 	// Properties
161 	Flags = in->ReadInt32();
162 	// reverse particular flags from older format
163 	if (svg_ver < kGuiSvgVersion_350)
164 		Flags ^= kGUICtrl_OldFmtXorMask;
165 	X = in->ReadInt32();
166 	Y = in->ReadInt32();
167 	Width = in->ReadInt32();
168 	Height = in->ReadInt32();
169 	ZOrder = in->ReadInt32();
170 	// Dynamic state
171 	IsActivated = in->ReadBool() ? 1 : 0;
172 }
173 
WriteToSavegame(Stream * out) const174 void GUIObject::WriteToSavegame(Stream *out) const {
175 	// Properties
176 	out->WriteInt32(Flags);
177 	out->WriteInt32(X);
178 	out->WriteInt32(Y);
179 	out->WriteInt32(Width);
180 	out->WriteInt32(Height);
181 	out->WriteInt32(ZOrder);
182 	// Dynamic state
183 	out->WriteBool(IsActivated != 0);
184 }
185 
186 
ConvertLegacyGUIAlignment(LegacyGUIAlignment align)187 HorAlignment ConvertLegacyGUIAlignment(LegacyGUIAlignment align) {
188 	switch (align) {
189 	case kLegacyGUIAlign_Left:
190 		return kHAlignLeft;
191 	case kLegacyGUIAlign_Right:
192 		return kHAlignRight;
193 	case kLegacyGUIAlign_Center:
194 		return kHAlignCenter;
195 	}
196 	return kHAlignNone;
197 }
198 
199 } // namespace Shared
200 } // namespace AGS
201 } // namespace AGS3
202