Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
GUIInterface.cpp
Go to the documentation of this file.
1
2
4
5#include "Headers/GUI.h"
6#include "Headers/GUIFlash.h"
7#include "Headers/GUIText.h"
8#include "Headers/GUIButton.h"
11
13
14#include "Core/Headers/Kernel.h"
19
20namespace Divide {
21
23 : _context(&context)
24{
25 Locale::SetChangeLanguageCallback([this](const std::string_view newLanguage) {
26 onLanguageChange(newLanguage);
27 });
28}
29
31{
32 using namespace eastl;
33
34 for (U8 i = 0; i < to_base(GUIType::COUNT); ++i)
35 {
36 for (auto& [nameHash, entry] : _guiElements[i])
37 {
38 delete entry.first;
39 }
40 }
41}
42
43void GUIInterface::onLanguageChange([[maybe_unused]] std::string_view newLanguage) {
44}
45
46void GUIInterface::addElement(const U64 id, GUIElement* element) {
47 assert(Runtime::isMainThread());
48
49 const U8 typeIndex = to_U8(element->type());
50 GUIMap& targetMap = _guiElements[typeIndex];
51
52 const GUIMap::iterator it = targetMap.find(id);
53 if (it != std::end(targetMap))
54 {
55 delete it->second.first;
56 it->second.first = element;
57 it->second.second = element ? element->visible() : false;
58 }
59 else
60 {
61 insert(targetMap, id, std::make_pair(element, element ? element->visible() : false));
62 }
63}
64
65GUIElement* GUIInterface::getGUIElementImpl(const U64 elementName, const GUIType type) const {
66 GUIElement* ret = nullptr;
67 if (type == GUIType::COUNT) {
68 for (U8 i = 0; i < to_base(GUIType::COUNT); ++i) {
69 const GUIMap::const_iterator it = _guiElements[i].find(elementName);
70 if (it != std::cend(_guiElements[i])) {
71 ret = it->second.first;
72 break;
73 }
74 }
75 } else {
76 const GUIMap::const_iterator it = _guiElements[to_U32(type)].find(elementName);
77 if (it != std::cend(_guiElements[to_U32(type)])) {
78 ret = it->second.first;
79 }
80 }
81 return ret;
82}
83
84GUIElement* GUIInterface::getGUIElementImpl(const I64 elementID, const GUIType type) const {
85 GUIElement* ret = nullptr;
86 GUIElement* element = nullptr;
87 if (type == GUIType::COUNT) {
88 for (U8 i = 0; i < to_base(GUIType::COUNT); ++i) {
89 for (const GUIMap::value_type& guiStackIterator : _guiElements[i]) {
90 element = guiStackIterator.second.first;
91 if (element != nullptr && element->getGUID() == elementID) {
92 ret = element;
93 break;
94 }
95 }
96 if (ret != nullptr) {
97 break;
98 }
99 }
100 } else {
101 for (const GUIMap::value_type& guiStackIterator : _guiElements[to_U32(type)]) {
102 element = guiStackIterator.second.first;
103 if (element != nullptr && element->getGUID() == elementID) {
104 ret = element;
105 break;
106 }
107 }
108 }
109
110 return ret;
111}
112
114 const string& text,
115 const RelativePosition2D& offset,
116 const RelativeScale2D& size,
117 const string& rootSheetID) {
118 const U64 guiID = _ID(name);
119
120 assert(getGUIElement<GUIButton>(guiID) == nullptr);
121
122 CEGUI::Window* parent = nullptr;
123 if (_context->getCEGUIContext()) {
124 parent = _context->getCEGUIContext()->getRootWindow();
125 if (!rootSheetID.empty()) {
126 parent = parent->getChild(rootSheetID.c_str());
127 }
128 }
129 ResourceDescriptor<AudioDescriptor> beepSound("buttonClick");
130 beepSound.assetName("beep.wav");
131 beepSound.assetLocation(Paths::g_soundsLocation);
132 Handle<AudioDescriptor> onClickSound = CreateResource(beepSound);
133
134 GUIButton* btn = new GUIButton(name,
135 text,
136 _context->defaultGUIScheme(),
137 offset,
138 size,
139 parent);
140
142
143 addElement(guiID, btn);
144
145 return btn;
146}
147
149 const string& title,
150 const string& message,
151 const vec2<I32> offsetFromCentre) {
152 const U64 guiID = _ID(name);
153
154 assert(getGUIElement<GUIMessageBox>(guiID) == nullptr);
155
156 GUIMessageBox* box = new GUIMessageBox(name,
157 title,
158 message,
159 offsetFromCentre,
161 addElement(guiID, box);
162
163 return box;
164}
165
167 const RelativePosition2D& position,
168 const string& font,
169 const UColour4& colour,
170 const string& text,
171 const bool multiLine,
172 const U8 fontSize) {
173 const U64 guiID = _ID(name);
174
175 assert(getGUIElement<GUIText>(guiID) == nullptr);
176
177 GUIText* t = new GUIText(name,
178 text,
179 multiLine,
180 position,
181 font,
182 colour,
184 fontSize);
185 addElement(guiID, t);
186
187 return t;
188}
189
191 [[maybe_unused]] const RelativePosition2D& position,
192 [[maybe_unused]] const RelativeScale2D& size) {
193 const U64 guiID = _ID(name);
194 assert(getGUIElement<GUIFlash>(guiID) == nullptr);
195
196 GUIFlash* flash = new GUIFlash(name, _context->rootSheet());
197 addElement(guiID, flash);
198
199 return flash;
200}
201
202GUIText* GUIInterface::modifyText(const char* name, const string& text, const bool multiLine) {
203 const U64 guiID = _ID(name);
204
205 const GUIMap::iterator it = _guiElements[to_base(GUIType::GUI_TEXT)].find(guiID);
206
207 if (it == std::cend(_guiElements[to_base(GUIType::GUI_TEXT)])) {
208 return nullptr;
209 }
210
211 GUIText* textElement = static_cast<GUIText*>(it->second.first);
212 assert(textElement != nullptr);
213
214 textElement->text(text.c_str(), multiLine);
215
216 return textElement;
217}
218
219CEGUI::Window* GUIInterface::createWindow(const CEGUI::String& type, const CEGUI::String& name) {
220 CEGUI::Window* window = CEGUI::WindowManager::getSingleton().createWindow(type, name);
221 if (window != nullptr) {
222 _context->rootSheet()->addChild(window);
223 }
224
225 return window;
226}
227
228CEGUI::Window* GUIInterface::loadWindowFromLayoutFile(const char* layoutFileName) {
229 CEGUI::Window* window = CEGUI::WindowManager::getSingleton().loadLayoutFromFile(layoutFileName);
230 if (window != nullptr) {
231 _context->rootSheet()->addChild(window);
232 }
233
234 return window;
235}
236
237bool GUIInterface::unloadWindow(CEGUI::Window*& window) {
238 if (_context->rootSheet()->isChild(window)) {
239 _context->rootSheet()->destroyChild(window);
240 window = nullptr;
241 return true;
242 }
243
244 return false;
245}
246
247}; //namespace Divide
void setEventSound(Event event, Handle< AudioDescriptor > sound)
Definition: GUIButton.cpp:169
FORCE_INLINE I64 getGUID() const noexcept
Definition: GUIDWrapper.h:51
virtual GUIType type() const noexcept
Definition: GUIElement.h:78
Graphical User Interface.
Definition: GUI.h:81
CEGUI::GUIContext * getCEGUIContext() noexcept
Provides direct access to the CEGUI context. Used by plugins (e.g. GUIConsole, GUIInput,...
Definition: GUI.cpp:862
CEGUI::Window * rootSheet() const noexcept
Get a pointer to the root sheet that CEGUI renders into.
Definition: GUI.h:118
virtual GUIMessageBox * addMsgBox(const char *name, const string &title, const string &message, vec2< I32 > offsetFromCentre=vec2< I32 >(0))
virtual CEGUI::Window * createWindow(const CEGUI::String &type, const CEGUI::String &name)
void onLanguageChange(std::string_view newLanguage)
virtual GUIFlash * addFlash(const char *name, const RelativePosition2D &position, const RelativeScale2D &size)
virtual CEGUI::Window * loadWindowFromLayoutFile(const char *layoutFileName)
virtual GUIText * addText(const char *name, const RelativePosition2D &position, const string &font, const UColour4 &colour, const string &text, bool multiLine=false, U8 fontSize=16u)
virtual GUIElement * getGUIElementImpl(U64 elementName, GUIType type) const
virtual GUIButton * addButton(const char *name, const string &text, const RelativePosition2D &offset, const RelativeScale2D &size, const string &rootSheetID="")
virtual bool unloadWindow(CEGUI::Window *&window)
hashMap< U64, std::pair< GUIElement *, bool > > GUIMap
Definition: GUIInterface.h:53
void addElement(U64 id, GUIElement *element)
GUIInterface(GUI &context)
virtual GUIText * modifyText(const char *name, const string &text, bool multiLine)
std::array< GUIMap, to_base(GUIType::COUNT)> _guiElements
Definition: GUIInterface.h:112
void SetChangeLanguageCallback(const DELEGATE< void, std::string_view > &cbk)
Set a function to be called on each language change.
bool isMainThread() noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
void insert(eastl::vector< T, A1 > &target, const eastl::vector< T, A2 > &source)
Definition: Vector.h:97
uint8_t U8
Project & parent
Definition: DefaultScene.h:41
constexpr U64 _ID(const char *const str, const U64 value=val_64_const) noexcept
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
constexpr U8 to_U8(const T value)
int64_t I64
Project const SceneEntry & entry
Definition: DefaultScene.h:41
uint64_t U64
constexpr auto to_base(const Type value) -> Type
void text(const char *text, const bool multiLine)
Definition: TextLabel.cpp:94