1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 
21 #include "ui/controls/interface.h"
22 
23 #include "app/app.h"
24 
25 #include "level/robotmain.h"
26 
27 #include "ui/controls/button.h"
28 #include "ui/controls/check.h"
29 #include "ui/controls/color.h"
30 #include "ui/controls/control.h"
31 #include "ui/controls/edit.h"
32 #include "ui/controls/editvalue.h"
33 #include "ui/controls/enumslider.h"
34 #include "ui/controls/group.h"
35 #include "ui/controls/image.h"
36 #include "ui/controls/key.h"
37 #include "ui/controls/label.h"
38 #include "ui/controls/list.h"
39 #include "ui/controls/map.h"
40 #include "ui/controls/scroll.h"
41 #include "ui/controls/shortcut.h"
42 #include "ui/controls/slider.h"
43 #include "ui/controls/target.h"
44 #include "ui/controls/window.h"
45 
46 #include <boost/range/adaptor/reversed.hpp>
47 
48 
49 namespace Ui
50 {
51 
52 
CInterface()53 CInterface::CInterface()
54 {
55     m_event  = CApplication::GetInstancePointer()->GetEventQueue();
56     m_engine = Gfx::CEngine::GetInstancePointer();
57 }
58 
59 // Object's destructor.
60 
~CInterface()61 CInterface::~CInterface()
62 {
63 }
64 
65 
66 // Purge all controls.
67 
Flush()68 void CInterface::Flush()
69 {
70     for (auto& control : m_controls)
71     {
72         control.reset();
73     }
74 }
75 
GetNextFreeControl()76 int CInterface::GetNextFreeControl()
77 {
78     for (int i = 10; i < static_cast<int>(m_controls.size()) - 1; i++)
79     {
80         if (m_controls[i] == nullptr)
81             return i;
82     }
83     return -1;
84 }
85 
86 template <typename ControlClass>
CreateControl(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)87 ControlClass* CInterface::CreateControl(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
88 {
89     if (eventMsg == EVENT_NULL)
90         eventMsg = GetUniqueEventType();
91 
92     int index = GetNextFreeControl();
93     if (index < 0)
94         return nullptr;
95 
96     auto control = MakeUnique<ControlClass>();
97     control->Create(pos, dim, icon, eventMsg);
98     auto* controlPtr = control.get();
99     m_controls[index] = std::move(control);
100     return controlPtr;
101 }
102 
103 
104 // Creates a new button.
105 
CreateWindows(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)106 CWindow* CInterface::CreateWindows(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
107 {
108     if (eventMsg == EVENT_NULL)
109         eventMsg = GetUniqueEventType();
110 
111     int index = -1;
112     switch (eventMsg)
113     {
114         case EVENT_WINDOW0: index = 0; break;
115         case EVENT_WINDOW1: index = 1; break;
116         case EVENT_WINDOW2: index = 2; break;
117         case EVENT_WINDOW3: index = 3; break;
118         case EVENT_WINDOW4: index = 4; break;
119         case EVENT_WINDOW5: index = 5; break;
120         case EVENT_WINDOW6: index = 6; break;
121         case EVENT_WINDOW7: index = 7; break;
122         case EVENT_WINDOW8: index = 8; break;
123         case EVENT_WINDOW9: index = 9; break;
124         case EVENT_TOOLTIP: index = m_controls.size() - 1; break;
125         default: index = GetNextFreeControl(); break;
126     }
127 
128     if (index < 0)
129         return nullptr;
130 
131     auto window = MakeUnique<CWindow>();
132     window->Create(pos, dim, icon, eventMsg);
133     auto* windowPtr = window.get();
134     m_controls[index] = std::move(window);
135     return windowPtr;
136 }
137 
138 // Creates a new button.
139 
CreateButton(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)140 CButton* CInterface::CreateButton(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
141 {
142     return CreateControl<CButton>(pos, dim, icon, eventMsg);
143 }
144 
145 // Creates a new button.
146 
CreateColor(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)147 CColor* CInterface::CreateColor(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
148 {
149     return CreateControl<CColor>(pos, dim, icon, eventMsg);
150 }
151 
152 // Creates a new button.
153 
CreateCheck(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)154 CCheck* CInterface::CreateCheck(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
155 {
156     return CreateControl<CCheck>(pos, dim, icon, eventMsg);
157 }
158 
159 // Creates a new button.
160 
CreateKey(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)161 CKey* CInterface::CreateKey(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
162 {
163     return CreateControl<CKey>(pos, dim, icon, eventMsg);
164 }
165 
166 // Creates a new button.
167 
CreateGroup(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)168 CGroup* CInterface::CreateGroup(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
169 {
170     return CreateControl<CGroup>(pos, dim, icon, eventMsg);
171 }
172 
173 // Creates a new button.
174 
CreateImage(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)175 CImage* CInterface::CreateImage(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
176 {
177     return CreateControl<CImage>(pos, dim, icon, eventMsg);
178 }
179 
180 // Creates a new label.
181 
CreateLabel(Math::Point pos,Math::Point dim,int icon,EventType eventMsg,std::string name)182 CLabel* CInterface::CreateLabel(Math::Point pos, Math::Point dim, int icon, EventType eventMsg, std::string name)
183 {
184     CLabel* pc = CreateControl<CLabel>(pos, dim, icon, eventMsg);
185     if (pc != nullptr)
186         pc->SetName(name);
187     return pc;
188 }
189 
190 // Creates a new pave editable.
191 
CreateEdit(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)192 CEdit* CInterface::CreateEdit(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
193 {
194     return CreateControl<CEdit>(pos, dim, icon, eventMsg);
195 }
196 
197 // Creates a new editable area.
CreateEditValue(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)198 CEditValue* CInterface::CreateEditValue(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
199 {
200     CEditValue* ev = CreateControl<CEditValue>(pos, dim, icon, eventMsg);
201     ev->SetInterface(this);
202     return ev;
203 }
204 
205 // Creates a new lift.
206 
CreateScroll(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)207 CScroll* CInterface::CreateScroll(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
208 {
209     return CreateControl<CScroll>(pos, dim, icon, eventMsg);
210 }
211 
212 // Creates a new cursor.
213 
CreateSlider(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)214 CSlider* CInterface::CreateSlider(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
215 {
216     return CreateControl<CSlider>(pos, dim, icon, eventMsg);
217 }
218 
CreateEnumSlider(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)219 CEnumSlider* CInterface::CreateEnumSlider(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
220 {
221     return CreateControl<CEnumSlider>(pos, dim, icon, eventMsg);
222 }
223 
224 // Creates a new list.
225 // if expand is less then zero, then the list would try to use expand's absolute value,
226 // and try to scale items to some size, so that dim of the list would not change after
227 // adjusting
228 
CreateList(Math::Point pos,Math::Point dim,int icon,EventType eventMsg,float expand)229 CList* CInterface::CreateList(Math::Point pos, Math::Point dim, int icon, EventType eventMsg, float expand)
230 {
231     if (eventMsg == EVENT_NULL)
232         eventMsg = GetUniqueEventType();
233 
234     int index = GetNextFreeControl();
235     if (index < 0)
236         return nullptr;
237 
238     auto list = MakeUnique<CList>();
239     list->Create(pos, dim, icon, eventMsg, expand);
240     auto* listPtr = list.get();
241     m_controls[index] = std::move(list);
242     return listPtr;
243 }
244 
245 // Creates a new shortcut.
246 
CreateShortcut(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)247 CShortcut* CInterface::CreateShortcut(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
248 {
249     return CreateControl<CShortcut>(pos, dim, icon, eventMsg);
250 }
251 
252 // Creates a new target.
253 
CreateTarget(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)254 CTarget* CInterface::CreateTarget(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
255 {
256     return CreateControl<CTarget>(pos, dim, icon, eventMsg);
257 }
258 
259 // Creates a new map.
260 
CreateMap(Math::Point pos,Math::Point dim,int icon,EventType eventMsg)261 CMap* CInterface::CreateMap(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
262 {
263     return CreateControl<CMap>(pos, dim, icon, eventMsg);
264 }
265 
266 // Removes a control.
267 
DeleteControl(EventType eventMsg)268 bool CInterface::DeleteControl(EventType eventMsg)
269 {
270     for (auto& control : m_controls)
271     {
272         if (control != nullptr &&
273             eventMsg == control->GetEventType())
274         {
275             control.reset();
276             return true;
277         }
278     }
279     return false;
280 }
281 
282 // Gives a control.
283 
SearchControl(EventType eventMsg)284 CControl* CInterface::SearchControl(EventType eventMsg)
285 {
286     for (auto& control : m_controls)
287     {
288         if (control != nullptr &&
289             eventMsg == control->GetEventType())
290         {
291             return control.get();
292         }
293     }
294     return nullptr;
295 }
296 
297 // Management of an event.
298 
EventProcess(const Event & event)299 bool CInterface::EventProcess(const Event &event)
300 {
301     if (event.type == EVENT_MOUSE_MOVE || event.type == EVENT_MOUSE_BUTTON_DOWN || event.type == EVENT_MOUSE_BUTTON_UP)
302     {
303         m_engine->SetMouseType(Gfx::ENG_MOUSE_NORM);
304     }
305 
306     for (auto& control : boost::adaptors::reverse(m_controls))
307     {
308         if (control != nullptr && control->TestState(STATE_ENABLE))
309         {
310             if (! control->EventProcess(event))
311                 return false;
312         }
313     }
314 
315     return true;
316 }
317 
318 
319 // Gives the tooltip binding to the window.
320 
GetTooltip(Math::Point pos,std::string & name)321 bool CInterface::GetTooltip(Math::Point pos, std::string &name)
322 {
323     for (auto& control : boost::adaptors::reverse(m_controls))
324     {
325         if (control != nullptr)
326         {
327             if (control->GetTooltip(pos, name))
328                 return true;
329         }
330     }
331     return false;
332 }
333 
334 
335 // Draws all buttons.
336 
Draw()337 void CInterface::Draw()
338 {
339     for (auto& control : m_controls)
340     {
341         if (control != nullptr)
342             control->Draw();
343     }
344 }
345 
SetFocus(CControl * focusControl)346 void CInterface::SetFocus(CControl* focusControl)
347 {
348     for (auto& control : m_controls)
349     {
350         if (control != nullptr)
351         {
352             control->SetFocus(focusControl);
353         }
354     }
355 }
356 
357 
358 } // namespace Ui
359