Hooked up graphics option menu to RT64, updated RT64

This commit is contained in:
Mr-Wiseguy
2024-01-01 00:09:14 -05:00
parent ac131c7835
commit 09bacbe82c
12 changed files with 354 additions and 135 deletions
+85 -1
View File
@@ -2,6 +2,8 @@
#include "recomp_ui.h"
#include "../../ultramodern/ultramodern.hpp"
#include "../../ultramodern/config.hpp"
#include "common/rt64_user_configuration.h"
#include "nfd.h"
#include "RmlUi/Core.h"
@@ -36,15 +38,97 @@ public:
}
};
NLOHMANN_JSON_SERIALIZE_ENUM(ultramodern::Resolution, {
{ultramodern::Resolution::Original, "Original"},
{ultramodern::Resolution::Original2x, "Original2x"},
{ultramodern::Resolution::Auto, "Auto"},
});
NLOHMANN_JSON_SERIALIZE_ENUM(ultramodern::WindowMode, {
{ultramodern::WindowMode::Windowed, "Windowed"},
{ultramodern::WindowMode::Fullscreen, "Fullscreen"}
});
ultramodern::GraphicsConfig cur_options;
ultramodern::GraphicsConfig new_options;
Rml::DataModelHandle options_handle;
template <typename T>
void get_option(const T& input, Rml::Variant& output) {
std::string value = "";
to_json(value, input);
if (value.empty()) {
throw std::runtime_error("Invalid value :" + std::to_string(int(input)));
}
output = value;
}
template <typename T>
void set_option(T& output, const Rml::Variant& input) {
T value = T::OptionCount;
from_json(input.Get<std::string>(), value);
if (value == T::OptionCount) {
throw std::runtime_error("Invalid value :" + input.Get<std::string>());
}
output = value;
}
template <typename T>
void bind_option(Rml::DataModelConstructor& constructor, const std::string& name, T* option) {
constructor.BindFunc(name,
[option](Rml::Variant& out) { get_option(*option, out); },
[option](const Rml::Variant& in) { set_option(*option, in); options_handle.DirtyVariable("options_changed"); }
);
};
void recomp::make_ui_bindings(Rml::Context* context) {
Rml::DataModelConstructor constructor = context->CreateDataModel("graphics_model");
if (!constructor) {
throw std::runtime_error("Failed to make RmlUi data model constructor");
}
bind_option(constructor, "res_option", &new_options.res_option);
bind_option(constructor, "wm_option", &new_options.wm_option);
bind_option(constructor, "ar_option", &new_options.ar_option);
bind_option(constructor, "msaa_option", &new_options.msaa_option);
bind_option(constructor, "rr_option", &new_options.rr_option);
constructor.BindFunc("rr_manual_value",
[](Rml::Variant& out) {
out = new_options.rr_manual_value;
},
[](const Rml::Variant& in) {
new_options.rr_manual_value = in.Get<int>();
options_handle.DirtyVariable("options_changed");
});
constructor.BindFunc("options_changed",
[](Rml::Variant& out) {
out = (cur_options != new_options);
});
options_handle = constructor.GetModelHandle();
}
std::unique_ptr<Rml::EventListenerInstancer> recomp::make_event_listener_instancer() {
std::unique_ptr<UiEventListenerInstancer> ret = std::make_unique<UiEventListenerInstancer>();
ret->register_event("start_game",
[](Rml::Event& event) {
ultramodern::start_game(0);
set_current_menu(Menu::None);
set_current_menu(Menu::Config);
}
);
ret->register_event("apply_options",
[](Rml::Event& event) {
cur_options = new_options;
options_handle.DirtyVariable("options_changed");
update_graphics_config(new_options);
});
return ret;
}
+20 -3
View File
@@ -565,7 +565,7 @@ public:
};
bool can_focus(Rml::Element* element) {
return element->GetProperty(Rml::PropertyId::TabIndex)->Get<Rml::Style::TabIndex>() != Rml::Property(Rml::Style::TabIndex::None);
return element->GetOwnerDocument() != nullptr && element->GetProperty(Rml::PropertyId::TabIndex)->Get<Rml::Style::TabIndex>() != Rml::Property(Rml::Style::TabIndex::None);
}
Rml::Element* get_target(Rml::ElementDocument* document, Rml::Element* element) {
@@ -660,7 +660,20 @@ struct {
// Revert focus to the previous element if focused on anything without a tab index.
// This should prevent the user from losing focus on something that has no navigation.
if (focused && !can_focus(focused)) {
prev_focused->Focus();
// If the previously focused element is still accepting focus, return focus to it.
if (prev_focused && can_focus(prev_focused)) {
prev_focused->Focus();
}
// Otherwise, check if the currently focused element has a "nav-return" attribute and focus that attribute's value if so.
else {
Rml::Variant* nav_return = focused->GetAttribute("nav-return");
if (nav_return && nav_return->GetType() == Rml::Variant::STRING) {
Rml::Element* return_element = current_document->GetElementById(nav_return->Get<std::string>());
if (return_element) {
return_element->Focus();
}
}
}
}
else {
prev_focused = current_document->GetFocusLeafNode();
@@ -693,6 +706,7 @@ void init_hook(RT64::RenderInterface* interface, RT64::RenderDevice* device) {
SDL_GetWindowSizeInPixels(window, &width, &height);
UIContext.rml.context = Rml::CreateContext("main", Rml::Vector2i(width, height));
recomp::make_ui_bindings(UIContext.rml.context);
Rml::Debugger::Initialise(UIContext.rml.context);
@@ -730,7 +744,7 @@ bool recomp::try_deque_event(SDL_Event& out) {
return ui_event_queue.try_dequeue(out);
}
std::atomic<recomp::Menu> open_menu = recomp::Menu::Config;//Launcher;
std::atomic<recomp::Menu> open_menu = recomp::Menu::Launcher;
void draw_hook(RT64::RenderCommandList* command_list, RT64::RenderTexture* swap_chain_texture) {
int num_keys;
@@ -778,6 +792,9 @@ void draw_hook(RT64::RenderCommandList* command_list, RT64::RenderTexture* swap_
int width, height;
SDL_GetWindowSizeInPixels(window, &width, &height);
// Scale the UI based on the window size with 720 vertical resolution as the reference point.
UIContext.rml.context->SetDensityIndependentPixelRatio(height / 720.0f);
UIContext.rml.render_interface->start(command_list, width, height);
static int prev_width = 0;