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
NuklearUI.cpp
Go to the documentation of this file.
1
7
8#define NK_GLFW_GL4_IMPLEMENTATION
9
10#include <entt/entity/registry.hpp>
11
13#include "galaxy/core/ServiceLocator.hpp"
14#include "galaxy/core/Window.hpp"
17#include "galaxy/utils/Globals.hpp"
18
19#include "NuklearUI.hpp"
20
21namespace galaxy
22{
23 namespace ui
24 {
26 : m_ctx {nullptr}
27 , m_atlas {nullptr}
28 , m_viewport_backup {0, 0, 0, 0}
29 {
30 auto& window = core::ServiceLocator<core::Window>::ref();
31
32 m_ctx = nk_glfw3_init(window.handle(), NK_GLFW3_DEFAULT, NK_MAX_VERTEX_BUFFER, NK_MAX_ELEMENT_BUFFER);
33
34 toggle_input(false);
35 scale(window.get_content_scale_max());
36 }
37
39 {
40 nk_glfw3_shutdown();
41 m_ctx = nullptr;
42 m_atlas = nullptr;
43 }
44
45 void NuklearUI::on_mouse_pressed(events::MousePressed& e)
46 {
47 if (!e.handled)
48 {
49 nk_glfw3_mouse_button_callback(nullptr, static_cast<int>(e.button), GLFW_PRESS, static_cast<int>(e.mod));
50 e.handled = true;
51 }
52 }
53
54 void NuklearUI::on_mouse_wheel(events::MouseWheel& e)
55 {
56 if (!e.handled)
57 {
58 nk_gflw3_scroll_callback(nullptr, e.xoff, e.yoff);
59 e.handled = true;
60 }
61 }
62
63 void NuklearUI::on_key_char(events::KeyChar& e)
64 {
65 if (!e.handled)
66 {
67 nk_glfw3_char_callback(nullptr, e.codepoint);
68 e.handled = true;
69 }
70 }
71
72 void NuklearUI::on_key_press(events::KeyPress& e)
73 {
74 if (!e.handled)
75 {
76 int action = GLFW_RELEASE;
77
78 if (e.pressed == true)
79 {
80 action = GLFW_PRESS;
81 }
82 else if (e.repeat == true)
83 {
84 action = GLFW_REPEAT;
85 }
86
87 nk_glfw3_key_button_callback(nullptr, input::key_to_int(e.keycode), e.scancode, action, input::mod_to_int(e.mod));
88 e.handled = true;
89 }
90 }
91
92 void NuklearUI::on_content_scale(const events::ContentScale& e)
93 {
94 scale(std::max(e.xscale, e.yscale));
95 }
96
98 {
99 if (glfw.do_input)
100 {
101 nk_input_begin(ctx());
102 }
103 }
104
106 {
107 if (glfw.do_input)
108 {
109 for (auto i = 0; i < glfw.text_len; ++i)
110 {
111 nk_input_unicode(&glfw.ctx, glfw.text[i]);
112 }
113
114 double x = 0.0, y = 0.0;
115 glfwGetCursorPos(glfw.win, &x, &y);
116 nk_input_motion(&glfw.ctx, static_cast<int>(x), static_cast<int>(y));
117
118 nk_input_button(&glfw.ctx,
119 NK_BUTTON_DOUBLE,
120 static_cast<int>(glfw.double_click_pos.x),
121 static_cast<int>(glfw.double_click_pos.y),
122 glfw.is_double_click_down);
123 nk_input_scroll(&glfw.ctx, glfw.scroll);
124 nk_input_end(ctx());
125 glfw.text_len = 0;
126 glfw.scroll = nk_vec2(0, 0);
127 }
128 }
129
131 {
132 begin_input();
133 glfwPollEvents();
134 end_input();
135 }
136
138 {
139 nk_glfw3_new_frame();
140 }
141
143 {
144 glGetIntegerv(GL_VIEWPORT, m_viewport_backup.data());
145
146 nk_glfw3_render(NK_ANTI_ALIASING_ON);
147
148 glUseProgram(0);
149 glBindVertexArray(0);
150 glBindTexture(GL_TEXTURE_2D, 0);
151 glDisable(GL_SCISSOR_TEST);
152 glEnable(GL_DEPTH_TEST);
153
155 }
156
157 void NuklearUI::set_font(const std::string& id)
158 {
159 if (m_fonts.contains(id))
160 {
161 nk_style_set_font(ctx(), &m_fonts[id]->handle);
162 }
163 }
164
165 void NuklearUI::toggle_input(const bool enable)
166 {
167 m_ctx->do_input = enable;
168
169 if (!enable)
170 {
171 m_ctx->mod_held = false;
172 }
173 }
174
175 void NuklearUI::show_loading_bar(const char* text, nk_size total, nk_size current)
176 {
177 auto& window = core::ServiceLocator<core::Window>::ref();
178
179 if (nk_begin(ctx(),
180 "loading_window",
181 nk_rect((window.get_widthf() / 2.0f) - 200, (window.get_heightf() / 2.0f) - 30, 400, 60),
182 NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
183 {
184 nk_layout_row_dynamic(ctx(), 20, 1);
185 nk_label(ctx(), text, NK_TEXT_CENTERED);
186 nk_layout_row_static(ctx(), 20, 400, 1);
187 nk_progress(ctx(), &current, total, NK_FIXED);
188 }
189
190 nk_end(ctx());
191 }
192
194 {
195 auto& window = core::ServiceLocator<core::Window>::ref();
196
197 if (nk_begin(ctx(),
198 "building_window",
199 nk_rect((window.get_widthf() / 2.0f) - 200, (window.get_heightf() / 2.0f) - 20, 400, 40),
200 NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
201 {
202 nk_layout_row_dynamic(ctx(), 20, 1);
203 nk_label(ctx(), "Building Textures...", NK_TEXT_CENTERED);
204 }
205
206 nk_end(ctx());
207 }
208
209 nk_context* NuklearUI::ctx() const
210 {
211 return &m_ctx->ctx;
212 }
213
214 void NuklearUI::scale(const float scale)
215 {
216 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
217 auto& config = core::ServiceLocator<core::Config>::ref();
218
219 if (m_atlas)
220 {
221 nk_font_atlas_clear(m_atlas);
222 }
223
224 nk_glfw3_font_stash_begin(&m_atlas);
225
226 for (const auto& file : fs.list(GALAXY_UI_FONTS_DIR))
227 {
228 auto data = fs.read_binary(file);
229 if (!data.empty())
230 {
231 const auto size = scale * config.get<float>("ui_font_size");
232
233 auto font_cfg = nk_font_config(size);
234 font_cfg.pixel_snap = true;
235
236 auto font = nk_font_atlas_add_from_memory(m_atlas, data.data(), data.size(), size, &font_cfg);
237 m_fonts.emplace(file, font);
238 }
239 }
240
241 nk_glfw3_font_stash_end();
242 nk_style_default(ctx());
243 }
244 } // namespace ui
245} // namespace galaxy
void on_mouse_pressed(events::MousePressed &e)
On mouse pressed event handler.
Definition NuklearUI.cpp:45
void show_building_atlas()
Part of the loading screen. Informs the user that the atlas is being built.
void on_mouse_wheel(events::MouseWheel &e)
On mouse wheel event handler.
Definition NuklearUI.cpp:54
void poll_input() const
Calls begin, end and glfwPollEvents.
void toggle_input(const bool enable)
Control the input.
void show_loading_bar(const char *text, nk_size total, nk_size current)
Shows a progress bar for loading screens.
void on_key_press(events::KeyPress &e)
On keyboad press event.
Definition NuklearUI.cpp:72
void end_input() const
Called after glfwPollEvents.
void scale(const float scale)
Scale UI to monitor/window.
~NuklearUI()
Destructor.
Definition NuklearUI.cpp:38
void on_content_scale(const events::ContentScale &e)
On content scale event.
Definition NuklearUI.cpp:92
void on_key_char(events::KeyChar &e)
On typing event handler.
Definition NuklearUI.cpp:63
nk_context * ctx() const
Nuklear Context.
nk_font_atlas * m_atlas
Nuklear atlas pointer.
ankerl::unordered_dense::map< std::string, nk_font * > m_fonts
Fonts used by nuklear.
void begin_input() const
Called before glfwPollEvents.
Definition NuklearUI.cpp:97
nk_glfw * m_ctx
Nuklear context pointer.
void new_frame()
Called before any nuklear code.
void render()
Render nuklear.
std::array< int, 4 > m_viewport_backup
Viewport backup when rendering.
NuklearUI()
Constructor.
Definition NuklearUI.cpp:25
void set_font(const std::string &id)
Change the active font.
Timer.hpp galaxy.
Definition Async.hpp:17