Unravel Engine C++ Reference
Loading...
Searching...
No Matches
viewport_resolution.cpp
Go to the documentation of this file.
2#include "../hub.h"
3
5
6#include <imgui/imgui.h>
7
8#include <algorithm>
9
11{
12
13namespace
14{
15auto clamp_index(int index, int count) -> int
16{
17 if(count <= 0)
18 {
19 return 0;
20 }
21 return std::clamp(index, 0, count - 1);
22}
23} // namespace
24
25auto compute_viewport_size(const settings::resolution_settings::resolution& res, ImVec2 avail_size) -> ImVec2
26{
27 if(res.aspect == 0.0f)
28 {
29 return avail_size;
30 }
31
32 if(res.width > 0 && res.height > 0)
33 {
34 return ImVec2(static_cast<float>(res.width), static_cast<float>(res.height));
35 }
36
37 return compute_fitted_size(res, avail_size);
38}
39
40auto compute_fitted_size(const settings::resolution_settings::resolution& res, ImVec2 avail_size) -> ImVec2
41{
42 if(res.aspect <= 0.0f)
43 {
44 return avail_size;
45 }
46
47 const float avail_aspect = avail_size.x / std::max(avail_size.y, 1.0f);
48 if(avail_aspect > res.aspect)
49 {
50 return ImVec2(avail_size.y * res.aspect, avail_size.y);
51 }
52 return ImVec2(avail_size.x, avail_size.x / res.aspect);
53}
54
57 ImVec2 avail_size)
58{
59 const ImVec2 viewport = compute_viewport_size(res, avail_size);
60 camera_comp.set_viewport_size({static_cast<std::uint32_t>(viewport.x), static_cast<std::uint32_t>(viewport.y)});
61}
62
64{
65 if(!ctx.has<unravel::settings>())
66 {
67 return nullptr;
68 }
69
70 const auto& resolutions = ctx.get<unravel::settings>().resolution.resolutions;
71 if(resolutions.empty())
72 {
73 return nullptr;
74 }
75
76 return &resolutions[clamp_index(index, static_cast<int>(resolutions.size()))];
77}
78
79auto draw_menu(rtti::context& ctx, int& current_index) -> bool
80{
81 if(!ctx.has<unravel::settings>())
82 {
83 return false;
84 }
85
86 const auto& resolutions = ctx.get<unravel::settings>().resolution.resolutions;
87 if(resolutions.empty())
88 {
89 return false;
90 }
91
92 current_index = clamp_index(current_index, static_cast<int>(resolutions.size()));
93
94 bool changed = false;
95 const auto label = fmt::format("{} {}", resolutions[current_index].name, ICON_MDI_ARROW_DOWN_BOLD);
96 if(ImGui::BeginMenu(label.c_str()))
97 {
98 for(int i = 0; i < static_cast<int>(resolutions.size()); ++i)
99 {
100 if(ImGui::RadioButton(resolutions[i].name.c_str(), &current_index, i))
101 {
102 changed = true;
103 }
104 }
105
106 if(ImGui::MenuItem("Edit ...", "", false))
107 {
108 ctx.get_cached<hub>().open_project_settings(ctx, "Resolution");
109 }
110 ImGui::EndMenu();
111 }
112 ImGui::SetItemTooltipEx("%s", "Resolution Presets");
113
114 return changed;
115}
116
117} // namespace unravel::viewport_resolution
Class that contains core camera data, used for rendering and other purposes.
void set_viewport_size(const usize32_t &size)
Sets the viewport size.
uint16_t index
std::string name
Definition hub.cpp:33
#define ICON_MDI_ARROW_DOWN_BOLD
auto compute_viewport_size(const settings::resolution_settings::resolution &res, ImVec2 avail_size) -> ImVec2
Compute the camera viewport size to use for a resolution preset.
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)....
auto compute_fitted_size(const settings::resolution_settings::resolution &res, ImVec2 avail_size) -> ImVec2
Compute a viewport size that always fits within avail_size by aspect ratio (ignoring any fixed pixel ...
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...