galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
GUI.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
10#include "galaxy/core/ServiceLocator.hpp"
13
14#include "GUI.hpp"
15
16namespace galaxy
17{
18 namespace components
19 {
21 {
22 }
23
24 GUI::GUI(const nlohmann::json& json)
25 {
26 deserialize(json);
27 }
28
30 {
31 this->m_self = std::move(s.m_self);
32 this->m_update = std::move(s.m_update);
33 this->m_file = std::move(s.m_file);
34 }
35
37 {
38 if (this != &s)
39 {
40 this->m_self = std::move(s.m_self);
41 this->m_update = std::move(s.m_update);
42 this->m_file = std::move(s.m_file);
43 }
44
45 return *this;
46 }
47
49 {
50 if (m_self.valid())
51 {
52 m_self.abandon();
53 }
54 }
55
56 void GUI::load(const std::string& file)
57 {
58 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
59
60 auto script = fs.read(file);
61 if (!script.empty())
62 {
63 if (m_self.valid())
64 {
65 m_self.abandon();
66 }
67
68 m_file = file;
69
70 auto& state = core::ServiceLocator<sol::state>::ref();
71 auto& nui = core::ServiceLocator<ui::NuklearUI>::ref();
72
73 if (!script.empty())
74 {
75 auto result = state.load(script);
76
77 if (result.valid())
78 {
79 m_self = result.call();
80
81 if (m_self.valid())
82 {
83 m_self["ctx"] = nui.ctx();
84 m_update = m_self["do_ui"];
85
86 if (!m_update.valid())
87 {
88 GALAXY_LOG(GALAXY_ERROR, "Update function not present in ui script '{0}'.", m_file);
89 }
90 }
91 else
92 {
93 GALAXY_LOG(GALAXY_ERROR, "Failed to validate ui script '{0}'. Make sure its in the correct format.", m_file);
94 }
95 }
96 else
97 {
98 GALAXY_LOG(GALAXY_ERROR, "Failed to load ui script '{0}' because '{1}'.", m_file, magic_enum::enum_name(result.status()));
99 }
100 }
101 else
102 {
103 GALAXY_LOG(GALAXY_ERROR, "Failed to read script '{0}'.", m_file);
104 }
105 }
106 else
107 {
108 GALAXY_LOG(GALAXY_ERROR, "Failed to find ui script '{0}'.", m_file);
109 }
110 }
111
112 const std::string& GUI::file() const
113 {
114 return m_file;
115 }
116
117 nlohmann::json GUI::serialize()
118 {
119 nlohmann::json json = "{}"_json;
120 json["file"] = m_file;
121
122 return json;
123 }
124
125 void GUI::deserialize(const nlohmann::json& json)
126 {
127 load(json.at("file"));
128 }
129 } // namespace components
130} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
Script for running an active UI.
Definition GUI.hpp:23
GUI()
Constructor.
Definition GUI.cpp:20
const std::string & file() const
Get script file.
Definition GUI.cpp:112
GUI & operator=(GUI &&)
Move assignment operator.
Definition GUI.cpp:36
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition GUI.cpp:125
void load(const std::string &file)
Load script and set context.
Definition GUI.cpp:56
virtual ~GUI()
Destructor.
Definition GUI.cpp:48
sol::table m_self
The script object (table) returned by a lua script.
Definition GUI.hpp:95
nlohmann::json serialize() override
Serializes object.
Definition GUI.cpp:117
std::string m_file
Script file.
Definition GUI.hpp:106
sol::function m_update
The update function belonging to the lua table.
Definition GUI.hpp:100
Timer.hpp galaxy.
Definition Async.hpp:17