Unravel Engine C++ Reference
Loading...
Searching...
No Matches
game_panel.cpp
Go to the documentation of this file.
1#include "game_panel.h"
2#include "../panel.h"
3#include "../panels_defs.h"
5#include "imgui/imgui.h"
6#include "imgui_widgets/utils.h"
7#include <engine/engine.h>
8
9#include <algorithm>
11#include <engine/ecs/ecs.h>
12#include <engine/play_mode.h>
13#include <engine/input/input.h>
18
19namespace unravel
20{
21
24 , parent_(parent)
25{
26}
27
31
35
37{
38 auto& path = ctx.get_cached<rendering_system>();
39 auto& ec = ctx.get_cached<ecs>();
40 auto& scene = ec.get_scene();
41
42 path.on_frame_update(scene, dt);
43}
44
46{
47 auto& path = ctx.get_cached<rendering_system>();
48 auto& ec = ctx.get_cached<ecs>();
49 auto& scene = ec.get_scene();
50
52}
53
55{
56 auto& ec = ctx.get_cached<ecs>();
57 auto& scene = ec.get_scene();
58 auto& path = ctx.get_cached<rendering_system>();
59
60 if(!is_visible() && !is_visible_force_)
61 {
62 path.release_pipeline_resources(scene);
63 return;
64 }
65
66 if(m_skip_frames_ > 0)
67 {
68 m_skip_frames_--;
69 return;
70 }
71
72 path.render_scene(scene, dt);
73
74 is_visible_force_ = false;
75}
76
78{
79 auto& input = ctx.get_cached<input_system>();
80 input.manager.set_is_input_allowed(is_focused());
81}
82
84{
85 is_visible_force_ = visible;
86}
87
89{
90 // m_skip_frames_ = 100;
91}
92
94{
95 draw_menubar(ctx);
96
97 if(m_skip_frames_ > 0)
98 {
99 auto spinner_size = ImGui::GetContentRegionAvail().y * 0.2f;
100
101 ImGui::SetCursorPosY(ImGui::GetContentRegionAvail().y * 0.5f - spinner_size * 0.5f);
102 ImGui::AlignedItem(0.5f,
103 ImGui::GetContentRegionAvail().x,
104 spinner_size,
105 [spinner_size]()
106 {
107 ImSpinner::Spinner<ImSpinner::SpinnerTypeT::e_st_eclipse>("spinner",
108 ImSpinner::Radius{spinner_size * 0.5f},
109 ImSpinner::Thickness{6.0f},
110 ImSpinner::Color{ImSpinner::white},
111 ImSpinner::Speed{6.0f});
112
113 });
114 return;
115 }
116
117 if(!ctx.has<unravel::settings>())
118 {
119 return;
120 }
121
122 const auto& s = ctx.get<unravel::settings>();
123 const auto* current_res = viewport_resolution::get_resolution(ctx, s.resolution.get_current_resolution_index());
124 if(!current_res)
125 {
126 return;
127 }
128
129 auto& ec = ctx.get_cached<ecs>();
130 auto& play = ctx.get_cached<play_mode>();
131 auto size = ImGui::GetContentRegionAvail();
132 if(size.x > 0 && size.y > 0)
133 {
134 bool rendered = false;
136 ec.get_scene().registry->view<camera_component>().each(
137 [&](auto e, auto&& camera_comp)
138 {
139 viewport_resolution::apply_to_camera(camera_comp, *current_res, size);
140
141 const auto& camera = camera_comp.get_camera();
142 const auto& rview = camera_comp.get_render_view();
143 const auto& obuffer = rview.fbo_safe_get("OBUFFER");
144
145 if(obuffer)
146 {
147 auto tex = obuffer->get_texture(0);
148 auto tex_size = obuffer->get_size();
149 ImVec2 tex_size_v(tex_size.width, tex_size.height);
150 ImGui::ImageWithAspect(ImGui::ToId(tex), tex_size_v, size, ImVec2(0.5f, 0.5f));
151
152 ImVec2 min = ImGui::GetItemRectMin();
153 ImVec2 max = ImGui::GetItemRectMax();
154
155 input::zone work_zone{};
156 work_zone.x = min.x;
157 work_zone.y = min.y;
158 work_zone.w = max.x - min.x;
159 work_zone.h = max.y - min.y;
160
161
162 ctx.get_cached<input_system>().manager.set_work_zone(work_zone);
163 ctx.get_cached<input_system>().manager.set_reference_size({tex_size_v.x, tex_size_v.y});
164
165 if(play.is_active())
166 {
167 ImVec2 padding(2.0f, 2.0f);
168 ImGui::RenderFocusFrame(ImGui::GetItemRectMin() - padding, ImGui::GetItemRectMax() + padding);
169 }
170 rendered = true;
171
172 const auto& pipeline = camera_comp.get_pipeline_data().get_pipeline();
173 pipeline->set_debug_pass(visualize_passes_);
174 pstats.add_stats(pipeline->get_stats());
175 }
176 });
177
178 if(!rendered)
179 {
180 static const auto text = "No cameras rendering";
181 ImGui::SetCursorPosY(size.y * 0.5f);
182 ImGui::AlignedItem(0.5f,
183 size.x,
184 ImGui::CalcTextSize(text).x,
185 []()
186 {
187 ImGui::TextUnformatted(text);
188 });
189 }
190
191
192 viewport_stats_overlay::draw(pstats, stats_overlay_state_, "game");
193
194 if(stats_overlay_state_.open_profiler_requested)
195 {
196 stats_overlay_state_.open_profiler_requested = false;
197 parent_->get_profiler_timeline_panel().show(true);
198 }
199 }
200}
201
202
203auto game_panel::begin_panel(const char* name, ImGuiWindowFlags flags) -> bool
204{
205 auto& ctx = engine::context();
206 auto& play = ctx.get_cached<play_mode>();
207 bool is_playing = play.is_active();
208 ImVec2 padding(is_playing ? 1.0f : 0.0f, is_playing ? 1.0f : 0.0f);
209 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, padding);
210 bool open = panel_base::begin_panel(name, flags);
211 ImGui::PopStyleVar();
212
213 return open;
214}
215
216void game_panel::draw_menubar(rtti::context& ctx)
217{
218 if(ImGui::BeginMenuBar())
219 {
220 ImGui::PushStyleColor(ImGuiCol_Header, ImGui::GetStyleColorVec4(ImGuiCol_TabSelected));
221 ImGui::PushStyleColor(ImGuiCol_HeaderHovered, ImGui::GetStyleColorVec4(ImGuiCol_TabSelected));
222 ImGui::PushStyleColor(ImGuiCol_HeaderActive, ImGui::GetStyleColorVec4(ImGuiCol_TabSelectedOverline));
223
224 if(ctx.has<unravel::settings>())
225 {
226 auto& pm = ctx.get_cached<project_manager>();
227 auto& s = pm.get_settings();
228 int index = s.resolution.get_current_resolution_index();
230 {
231 s.resolution.set_current_resolution_index(index);
232 pm.save_project_settings(ctx);
233 }
234 }
235
237 {
238 ImGui::RadioButton("Full", &visualize_passes_, -1);
239 ImGui::RadioButton("Base Color", &visualize_passes_, 0);
240 ImGui::RadioButton("Diffuse Color", &visualize_passes_, 1);
241 ImGui::RadioButton("Specular Color", &visualize_passes_, 2);
242 ImGui::RadioButton("Radiance", &visualize_passes_, 3);
243 ImGui::RadioButton("Irradiance", &visualize_passes_, 4);
244 ImGui::RadioButton("Ambient Occlusion", &visualize_passes_, 5);
245 ImGui::RadioButton("Normals (World Space)", &visualize_passes_, 6);
246 ImGui::RadioButton("Roughness", &visualize_passes_, 7);
247 ImGui::RadioButton("Metalness", &visualize_passes_, 8);
248 ImGui::RadioButton("Emissive Color", &visualize_passes_, 9);
249 ImGui::RadioButton("Subsurface Color", &visualize_passes_, 10);
250 ImGui::RadioButton("Depth", &visualize_passes_, 11);
251 ImGui::RadioButton("SSIL", &visualize_passes_, 12);
252 ImGui::EndMenu();
253 }
254 ImGui::SetItemTooltipEx("%s", "Visualize Render Passes");
255
256 auto& ui = ctx.get_cached<ui_system>();
257 bool debugger_enabled = ui.is_debugger_enabled();
258 const char* modes[] = {ICON_MDI_BUG_CHECK " UI Debugger", ICON_MDI_BUG " UI Debugger"};
259 const char* debugger_preview = modes[int(!debugger_enabled)];
260
261 if(debugger_enabled)
262 {
263 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 1.0f, 0.0f, 1.0f));
264 }
265 if(ImGui::MenuItem(debugger_preview, "", debugger_enabled))
266 {
267 ui.set_debugger_enabled(!debugger_enabled);
268 }
269 if(debugger_enabled)
270 {
271 ImGui::PopStyleColor();
272 }
273 ImGui::SetItemTooltipEx("%s", debugger_enabled ? "Hide UI Debugger" : "Show UI Debugger");
274
275 viewport_stats_overlay::draw_stats_toggle(stats_overlay_state_);
276
277 ImGui::PopStyleColor(3);
278
279 ImGui::EndMenuBar();
280 }
281}
282
283} // namespace unravel
Class that contains core camera data, used for rendering and other purposes.
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:62
void deinit(rtti::context &ctx)
void init(rtti::context &ctx)
void on_after_render(rtti::context &ctx) override
void on_frame_update(rtti::context &ctx, delta_t dt)
void draw_ui(rtti::context &ctx) override
void on_frame_render(rtti::context &ctx, delta_t dt)
void set_visible_force(bool visible)
game_panel(imgui_panels *parent, const char *name)
void on_frame_before_render(rtti::context &ctx, delta_t dt)
auto get_profiler_timeline_panel() -> profiler_timeline_panel &
Definition panel.cpp:236
virtual bool begin_panel(const char *name, ImGuiWindowFlags flags)
auto is_visible() const -> bool
Definition panel_base.h:17
auto is_focused() const -> bool
Definition panel_base.h:20
Base class for different rendering paths in the ACE framework.
void on_frame_update(scene &scn, delta_t dt)
Prepares the scene for rendering.
void on_frame_before_render(scene &scn, delta_t dt)
auto render_scene(scene &scn, delta_t dt) -> gfx::frame_buffer::ptr
Renders the scene and returns the frame buffer.
float y
float x
std::chrono::duration< float > delta_t
uint16_t index
std::string name
Definition hub.cpp:33
#define ICON_MDI_ARROW_DOWN_BOLD
#define ICON_MDI_BUG_CHECK
#define ICON_MDI_DRAWING_BOX
#define ICON_MDI_BUG
std::vector< size_t > parent_
ImTextureID ToId(gfx::texture_handle _handle, uint8_t _mip=0, uint8_t _flags=IMGUI_FLAGS_ALPHA_BLEND)
Definition imgui.h:103
auto get_resolution(rtti::context &ctx, int index) -> const settings::resolution_settings::resolution *
Get the resolution preset at the given index (clamped against the configured presets)....
void apply_to_camera(camera_component &camera_comp, const settings::resolution_settings::resolution &res, ImVec2 avail_size)
Apply the resolution preset to the camera component using compute_viewport_size. Only viewport size i...
auto draw_menu(rtti::context &ctx, int &current_index) -> bool
Draw the resolution selection drop-down for the current menu bar. The selection is read and written t...
void draw(const rendering::pipeline_stats &pstats, state &overlay_state, const char *id)
Draw a statistics overlay child window at the top-right corner of the current ImGui window....
void draw_stats_toggle(state &overlay_state)
Draw a right-aligned "Stats" toggle button for the menu bar. Toggles the overlay visibility on click.
auto get_cached() -> T &
Definition context.hpp:49
auto has() const -> bool
Definition context.hpp:28
auto get() -> T &
Definition context.hpp:35
Manages the entity-component-system (ECS) operations for the ACE framework.
Definition ecs.h:12
static auto context() -> rtti::context &
Definition engine.cpp:111
Owns play-mode state and orchestrates the splash -> running lifecycle.
Definition play_mode.h:17
auto is_active() const -> bool
Definition play_mode.h:36
void add_stats(const pipeline_stats &stats)
Definition pipeline.cpp:735
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:70
static auto get_scene(entt::handle entity) -> scene *
Gets the scene from an entity handle.
Definition scene.cpp:343