Lagrange
Viewer.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14
15#include <lagrange/ui/Entity.h>
16#include <lagrange/ui/api.h>
17#include <lagrange/ui/default_components.h>
18#include <lagrange/ui/default_keybinds.h>
19#include <lagrange/ui/panels/ViewportPanel.h>
20#include <lagrange/ui/types/Camera.h>
21#include <lagrange/ui/types/Systems.h>
22#include <lagrange/ui/types/Tools.h>
23#include <lagrange/ui/utils/input.h>
24#include <lagrange/ui/utils/layer.h>
25#include <lagrange/ui/utils/uipanel.h>
26
27#include <imgui.h>
28#include <future>
29#include <memory>
30#include <mutex>
31#include <queue>
32#include <set>
33#include <string>
34
35struct GLFWwindow;
36struct ImGuiContext;
37
38namespace lagrange {
39namespace ui {
40
41
42class Renderer;
43
47class LA_UI_API Viewer
48{
49public:
50 /*
51 IO functions
52 */
53 bool is_key_down(ImGuiKey key);
54 bool is_key_pressed(ImGuiKey key);
55 bool is_key_released(ImGuiKey key);
56 bool is_mouse_down(ImGuiKey key);
57 bool is_mouse_clicked(ImGuiKey key);
58 bool is_mouse_released(ImGuiKey key);
59
61 InputState& get_input() { return lagrange::ui::get_input(registry()); }
62
64 const InputState& get_input() const { return lagrange::ui::get_input(registry()); }
65
70 {
72 std::string window_title = "";
73
75 int pos_x = -1;
77 int pos_y = -1;
79 int width = 1024;
81 int height = 768;
83 bool window_fullscreen = false;
85 bool fullscreen = false;
87 bool vsync = true;
89 int monitor_index = 0;
90
92 int gl_version_major = 3;
94 int gl_version_minor = 3;
96 bool focus_on_show = true;
97
104 bool show_default_ibl = true;
105 size_t default_ibl_resolution = 256;
106
108 bool show_topbar_menu = true;
109
113 std::string imgui_ini_path = "";
114
116 Color selection_color = Color(252.0f / 255.0f, 86.0f / 255.0f, 3.0f / 255.0f, 1.0f);
117
119 bool use_srgb = true;
120 };
121
122
130 Viewer(const std::string& window_title, int window_width, int window_height);
131
132
139 Viewer(int argc, char** argv);
140
146 Viewer(const WindowOptions& window_options);
147
148 virtual ~Viewer();
149
150 operator ui::Registry&() { return registry(); }
151
152 operator const ui::Registry&() const { return registry(); }
153
158 bool should_close() const;
159
163 bool run(const std::function<bool(Registry& r)>& main_loop);
164
168 bool run(const std::function<void(void)>& main_loop = {});
169
171 bool is_initialized() const;
172
174 bool is_show_topbar_menu() const { return m_show_topbar_menu; }
175
177 void set_show_topbar_menu(bool show_topbar_menu) { m_show_topbar_menu = show_topbar_menu; }
178
180 double get_frame_elapsed_time() const;
181
182 // Window width in pixels
183 int get_width() const { return m_width; }
184
185 // Window height in pixels
186 int get_height() const { return m_height; }
187
188 const std::string& get_imgui_config_path() const;
189
191 float get_window_scaling() const;
192
194 Keybinds& get_keybinds();
195
197 const Keybinds& get_keybinds() const;
198
200 Registry& registry() { return m_registry; }
201
203 const Registry& registry() const { return m_registry; }
204
206 const Systems& systems() const { return m_systems; }
207
209 Systems& systems() { return m_systems; }
210
212 std::future<void> run_on_main_thread(std::function<void(void)> fn);
213
216 {
217 return m_main_thread_max_func_per_frame;
218 }
219
222 void set_main_thread_max_func_per_frame(unsigned int limit)
223 {
224 m_main_thread_max_func_per_frame = limit;
225 }
226
227 static std::string get_config_folder();
228
229protected:
230 virtual void draw_menu();
231
232private:
233 void render_one_frame(const std::function<bool(Registry& r)>& main_loop);
234
235 bool init_glfw(const WindowOptions& options);
236 bool init_imgui();
237 bool init_imgui_fonts();
238
239
240 void resize(int window_width, int window_height);
241 void move(int x, int y);
242 void update_scale();
243 void drop(int count, const char** paths);
244
245 void process_input();
246
247 void update_time();
248
249 void start_imgui_frame();
250
251 void end_imgui_frame();
252
253 void make_current();
254
255 void start_dockspace();
256 void end_dockspace();
257
258 void show_last_shader_error();
259
260 WindowOptions m_initial_window_options;
261
262 GLFWwindow* m_window;
263 ImGuiContext* m_imgui_context;
264
265
266 bool m_initialized = false;
267
268 static bool m_instance_initialized;
269
270 std::string m_imgui_ini_path;
271
272 int m_width;
273 int m_height;
274
275 // DPI aware scaling
276 float m_ui_scaling = 1.0f;
277
278
279 std::string m_last_shader_error;
280 std::string m_last_shader_error_desc;
281
282 bool m_show_imgui_demo = false;
283 bool m_show_imgui_style = false;
284 bool m_show_topbar_menu = true;
285
286 entt::registry m_registry;
287 Systems m_systems;
288
289 Entity m_main_viewport;
290
291 // Functions that will be run on next frame on the main thread
292 // Used for code interacting with OpenGL context
293 std::mutex m_mutex;
294 std::queue<std::pair<std::promise<void>, std::function<void(void)>>> m_main_thread_fn;
295 std::atomic_uint m_main_thread_max_func_per_frame = std::numeric_limits<unsigned int>::max();
296};
297
298} // namespace ui
299} // namespace lagrange
Definition: Color.h:24
Container for Systems.
Definition: Systems.h:34
Viewer use systems() to add functions that should be called every frame use registry() or util functi...
Definition: Viewer.h:48
bool is_show_topbar_menu() const
Returns true if the default menu is rendered.
Definition: Viewer.h:174
Systems & systems()
Returns reference to the systems.
Definition: Viewer.h:209
bool run(const std::function< bool(Registry &r)> &main_loop)
Runs the viewer until closed by the user or main_loop returns false.
const Systems & systems() const
Returns reference to the systems.
Definition: Viewer.h:206
InputState & get_input()
Returns keyboard and mouse state.
Definition: Viewer.h:61
const InputState & get_input() const
Returns keyboard and mouse state.
Definition: Viewer.h:64
void set_show_topbar_menu(bool show_topbar_menu)
Set if the default menu is rendered.
Definition: Viewer.h:177
void set_main_thread_max_func_per_frame(unsigned int limit)
Sets the limit of how many enqueued functions can be run during a single frame on the main thread Use...
Definition: Viewer.h:222
Registry & registry()
Returns reference to the registry.
Definition: Viewer.h:200
const Registry & registry() const
Returns reference to the registry.
Definition: Viewer.h:203
unsigned int get_main_thread_max_func_per_frame() const
Returns the limit of how many enqueued functions can be run during a single frame on the main thread.
Definition: Viewer.h:215
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Input.h:21
Window creation options.
Definition: Viewer.h:70