Skip to content

Commit

Permalink
UI: Allow sidebar page to be changed with LB/RB
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Nov 2, 2023
1 parent 779bc2e commit 7972fdf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 31 deletions.
75 changes: 50 additions & 25 deletions src/Framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@ void Framework::draw_ui() {
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Keyboard Menu Key: Insert");
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Gamepad L3 + R3: Toggle Menu");
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Gamepad RT: Shortcuts");
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Gamepad LB/RB: Change Sidebar Page");

ImGui::EndGroup();
ImGui::EndGroup();
Expand All @@ -1180,21 +1181,19 @@ void Framework::draw_ui() {

// Mods:
std::vector<std::string> sidebar_entries{"About"};
static int selected_sidebar_entry = 0;
static bool initialized_default_sidebar_entry = false;

if (ImGui::BeginTable("UEVRTable", 2, ImGuiTableFlags_::ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_::ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_::ImGuiTableFlags_SizingFixedFit)) {
ImGui::TableSetupColumn("UEVRLeftPane", ImGuiTableColumnFlags_WidthFixed, 150.0f);
ImGui::TableSetupColumn("UEVRRightPane", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("UEVRLeftPaneColumn", ImGuiTableColumnFlags_WidthFixed, 150.0f);
ImGui::TableSetupColumn("UEVRRightPaneColumn", ImGuiTableColumnFlags_WidthStretch);

ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0); // Set to the first column

ImGui::BeginChild("UEVRLeftPane", ImVec2(0, 0), true);
auto dcs = [&](const char* label, uint32_t page_value) -> bool {
ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f, 0.5f));
if (ImGui::Selectable(label, selected_sidebar_entry == page_value)) {
selected_sidebar_entry = page_value;
if (ImGui::Selectable(label, m_sidebar_state.selected_entry == page_value)) {
m_sidebar_state.selected_entry = page_value;
ImGui::PopStyleVar();
return true;
}
Expand Down Expand Up @@ -1230,9 +1229,10 @@ void Framework::draw_ui() {
for (const auto& range : mod_sidebar_ranges) {
if (i == range.mn) {
// Set first entry as default ("Runtime" entry of VR mod)
if (range.has_sidebar_entries && !initialized_default_sidebar_entry) {
selected_sidebar_entry = i;
initialized_default_sidebar_entry = true;
if (range.has_sidebar_entries && !m_sidebar_state.initialized) {
m_sidebar_state.selected_entry = i;
m_sidebar_state.initialized = true;
ImGui::SetWindowFocus("UEVRRightPane");
}

ImGui::Text(range.mod->get_name().data());
Expand All @@ -1242,28 +1242,53 @@ void Framework::draw_ui() {
dcs(sidebar_entries[i].c_str(), i);
}

if (selected_sidebar_entry >= sidebar_entries.size()) {
selected_sidebar_entry = 0;
bool wants_focus_right = false;

if (ImGui::IsKeyPressed(ImGuiKey_GamepadL1)) {
decrement_sidebar_page();
wants_focus_right = true;
}

if (ImGui::IsKeyPressed(ImGuiKey_GamepadR1)) {
increment_sidebar_page();
wants_focus_right = true;
}

if (m_sidebar_state.selected_entry < 0) {
m_sidebar_state.selected_entry = sidebar_entries.size() - 1;
}

m_sidebar_state.selected_entry = m_sidebar_state.selected_entry % sidebar_entries.size();

ImGui::EndChild();
ImGui::TableNextColumn(); // Move to the next column (right)

if (selected_sidebar_entry > 0) {
// Find the mod that owns this entry
for (const auto& range : mod_sidebar_ranges) {
if (selected_sidebar_entry >= range.mn && selected_sidebar_entry < range.mx) {
if (range.has_sidebar_entries) {
range.mod->on_draw_sidebar_entry(sidebar_entries[selected_sidebar_entry]);
} else {
range.mod->on_draw_ui();
}
if (wants_focus_right) {
ImGui::SetNextWindowFocus();
}

if (ImGui::BeginChild("UEVRRightPane", ImVec2(0, 0), true, ImGuiWindowFlags_::ImGuiWindowFlags_AlwaysUseWindowPadding)) {
ImGui::BeginGroup();

break;
if (m_sidebar_state.selected_entry > 0) {
// Find the mod that owns this entry
for (const auto& range : mod_sidebar_ranges) {
if (m_sidebar_state.selected_entry >= range.mn && m_sidebar_state.selected_entry < range.mx) {
if (range.has_sidebar_entries) {
range.mod->on_draw_sidebar_entry(sidebar_entries[m_sidebar_state.selected_entry]);
} else {
range.mod->on_draw_ui();
}

break;
}
}
} else {
draw_about();
}
} else {
draw_about();

ImGui::EndGroup();
ImGui::EndChild();
}

/*for (auto& mod : m_mods->get_mods()) {
Expand All @@ -1274,7 +1299,7 @@ void Framework::draw_ui() {
} else if (!m_game_data_initialized) {
ImGui::EndChild();

if (selected_sidebar_entry == 0) {
if (m_sidebar_state.selected_entry == 0) {
ImGui::TableNextColumn();
draw_about();
}
Expand All @@ -1284,7 +1309,7 @@ void Framework::draw_ui() {
} else if (!m_error.empty()) {
ImGui::EndChild();

if (selected_sidebar_entry == 0) {
if (m_sidebar_state.selected_entry == 0) {
ImGui::TableNextColumn();
draw_about();
}
Expand Down
13 changes: 13 additions & 0 deletions src/Framework.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ class Framework {
m_has_engine_thread = true;
}

void increment_sidebar_page() {
++m_sidebar_state.selected_entry;
}

void decrement_sidebar_page() {
--m_sidebar_state.selected_entry;
}

private:
void consume_input();
void update_fonts();
Expand Down Expand Up @@ -286,6 +294,11 @@ class Framework {

RendererType m_renderer_type{RendererType::D3D11};

struct {
int32_t selected_entry{0};
bool initialized{false};
} m_sidebar_state{};

template <typename T> using ComPtr = Microsoft::WRL::ComPtr<T>;

private: // D3D misc
Expand Down
20 changes: 14 additions & 6 deletions src/mods/VR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,6 @@ void VR::update_imgui_state_from_xinput_state(XINPUT_STATE& state, bool is_vr_co
MAP_BUTTON(ImGuiKey_GamepadDpadRight, XINPUT_GAMEPAD_DPAD_RIGHT);
MAP_BUTTON(ImGuiKey_GamepadDpadUp, XINPUT_GAMEPAD_DPAD_UP);
MAP_BUTTON(ImGuiKey_GamepadDpadDown, XINPUT_GAMEPAD_DPAD_DOWN);
MAP_BUTTON(ImGuiKey_GamepadL1, XINPUT_GAMEPAD_LEFT_SHOULDER);
MAP_BUTTON(ImGuiKey_GamepadR1, XINPUT_GAMEPAD_RIGHT_SHOULDER);
MAP_ANALOG(ImGuiKey_GamepadL2, gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255);
MAP_ANALOG(ImGuiKey_GamepadR2, gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255);
MAP_BUTTON(ImGuiKey_GamepadL3, XINPUT_GAMEPAD_LEFT_THUMB);
Expand All @@ -1138,6 +1136,8 @@ void VR::update_imgui_state_from_xinput_state(XINPUT_STATE& state, bool is_vr_co
MAP_ANALOG(ImGuiKey_GamepadLStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
MAP_ANALOG(ImGuiKey_GamepadLStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
MAP_ANALOG(ImGuiKey_GamepadLStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
MAP_BUTTON(ImGuiKey_GamepadL1, XINPUT_GAMEPAD_LEFT_SHOULDER);
MAP_BUTTON(ImGuiKey_GamepadR1, XINPUT_GAMEPAD_RIGHT_SHOULDER);
} else {
// Map it to the dpad
const auto left_stick_left = gamepad.sThumbLX < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE * 2;
Expand Down Expand Up @@ -1860,13 +1860,13 @@ void VR::on_draw_sidebar_entry(std::string_view name) {
// Draw the ui thats always drawn first.
on_draw_ui();

const auto made_child = ImGui::BeginChild("VRChild", ImVec2(0, 0), true, ImGuiWindowFlags_::ImGuiWindowFlags_NavFlattened);
/*const auto made_child = ImGui::BeginChild("VRChild", ImVec2(0, 0), true, ImGuiWindowFlags_::ImGuiWindowFlags_NavFlattened);
utility::ScopeGuard sg([made_child]() {
if (made_child) {
ImGui::EndChild();
}
});
});*/

enum SelectedPage {
PAGE_RUNTIME,
Expand Down Expand Up @@ -2124,6 +2124,12 @@ void VR::on_draw_sidebar_entry(std::string_view name) {
ImGui::DragFloat4("Raw Left", (float*)&m_raw_projections[0], 0.01f, -100.0f, 100.0f);
ImGui::DragFloat4("Raw Right", (float*)&m_raw_projections[1], 0.01f, -100.0f, 100.0f);

const auto left_stick_axis = get_left_stick_axis();
const auto right_stick_axis = get_right_stick_axis();

ImGui::DragFloat2("Left Stick", (float*)&left_stick_axis, 0.01f, -1.0f, 1.0f);
ImGui::DragFloat2("Right Stick", (float*)&right_stick_axis, 0.01f, -1.0f, 1.0f);

ImGui::TextWrapped("Hardware scheduling: %s", m_has_hw_scheduling ? "Enabled" : "Disabled");
}

Expand All @@ -2141,10 +2147,12 @@ void VR::on_draw_ui() {
std::string adjusted_name = get_name().data();
adjusted_name += " (Loading...)";

if (!ImGui::CollapsingHeader(adjusted_name.data())) {
/*if (!ImGui::CollapsingHeader(adjusted_name.data())) {
ImGui::PopID();
return;
}
}*/

ImGui::TextWrapped("Loading...");
} else {
/*if (!ImGui::CollapsingHeader(get_name().data())) {
ImGui::PopID();
Expand Down

0 comments on commit 7972fdf

Please sign in to comment.