Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added menu toggle key option. Fixed general settings loading correctly #2

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package/SKSE/Plugins/CommunityShaders.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"Menu": {
"Toggle Key": 35
},
"General": {
"Enable Async": true,
"Enable Disk Cache": true,
Expand Down
85 changes: 83 additions & 2 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Features/DistantTreeLighting.h"
#include "Features/GrassCollision.h"

#define SETTING_MENU_TOGGLEKEY "Toggle Key"

void SetupImGuiStyle()
{
Expand All @@ -18,6 +19,21 @@ void SetupImGuiStyle()
}
bool IsEnabled = false;

void Menu::Load(json& o_json)
{
if (o_json[SETTING_MENU_TOGGLEKEY].is_number_unsigned()) {
toggleKey = o_json[SETTING_MENU_TOGGLEKEY];
}
}

void Menu::Save(json& o_json)
{
json menu;
menu[SETTING_MENU_TOGGLEKEY] = toggleKey;

o_json["Menu"] = menu;
}

RE::BSEventNotifyControl Menu::ProcessEvent(RE::InputEvent* const* a_event, RE::BSTEventSource<RE::InputEvent*>* a_eventSource)
{
if (!a_event || !a_eventSource)
Expand All @@ -32,6 +48,7 @@ RE::BSEventNotifyControl Menu::ProcessEvent(RE::InputEvent* const* a_event, RE::

auto scan_code = button->GetIDCode();
uint32_t key = MapVirtualKeyEx(scan_code, MAPVK_VSC_TO_VK_EX, GetKeyboardLayout(0));

switch (scan_code) {
case DIK_LEFTARROW:
key = VK_LEFT;
Expand Down Expand Up @@ -117,7 +134,14 @@ RE::BSEventNotifyControl Menu::ProcessEvent(RE::InputEvent* const* a_event, RE::

switch (button->device.get()) {
case RE::INPUT_DEVICE::kKeyboard:
if (key == VK_END && !button->IsPressed()) {
if (key == toggleKey && !button->IsPressed()) {

// Avoid closing menu when setting the toggle key
if (settingToggleKey) {
settingToggleKey = false;
break;
}

IsEnabled = !IsEnabled;
if (const auto controlMap = RE::ControlMap::GetSingleton()) {
controlMap->ignoreKeyboardMouse = IsEnabled;
Expand Down Expand Up @@ -186,6 +210,36 @@ void Menu::DrawSettings()

ImGui::Spacing();

if (ImGui::CollapsingHeader("Menu")) {

// Check if we're waiting for input
if (settingToggleKey) {
// Loop over all the keys and check if any of them are pressed
for (int i = 0; i < IM_ARRAYSIZE(ImGui::GetIO().KeysDown); i++) {
if (ImGui::IsKeyPressed(i)) {
// If a key is pressed, set the selected key code and break out of the loop
toggleKey = i;
break;
}
}
}
if (settingToggleKey)
{
ImGui::Text("Press any key to set as toggle key...");
}
else
{
ImGui::Text("Toggle Key:");
ImGui::SameLine();
ImGui::TextColored(ImVec4(1, 1, 0, 1), "%s", KeyIdToString(toggleKey));

ImGui::SameLine();
if (ImGui::Button("Change")) {
settingToggleKey = true;
}
}
}

if (ImGui::CollapsingHeader("General", ImGuiTreeNodeFlags_DefaultOpen)) {
bool useCustomShaders = shaderCache.IsEnabled();
if (ImGui::Checkbox("Enable Shaders", &useCustomShaders)) {
Expand Down Expand Up @@ -273,4 +327,31 @@ void Menu::DrawOverlay()
ImGui::GetIO().MouseDrawCursor = true;
DrawSettings();
}
}
}

const char* Menu::KeyIdToString(uint32_t key)
{
if (key >= 256)
return std::string().c_str();

static const char* keyboard_keys_international[256] = {
"", "Left Mouse", "Right Mouse", "Cancel", "Middle Mouse", "X1 Mouse", "X2 Mouse", "", "Backspace", "Tab", "", "", "Clear", "Enter", "", "",
"Shift", "Control", "Alt", "Pause", "Caps Lock", "", "", "", "", "", "", "Escape", "", "", "", "",
"Space", "Page Up", "Page Down", "End", "Home", "Left Arrow", "Up Arrow", "Right Arrow", "Down Arrow", "Select", "", "", "Print Screen", "Insert", "Delete", "Help",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "", "", "", "", "",
"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Left Windows", "Right Windows", "Apps", "", "Sleep",
"Numpad 0", "Numpad 1", "Numpad 2", "Numpad 3", "Numpad 4", "Numpad 5", "Numpad 6", "Numpad 7", "Numpad 8", "Numpad 9", "Numpad *", "Numpad +", "", "Numpad -", "Numpad Decimal", "Numpad /",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16",
"F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "", "", "", "", "", "", "", "",
"Num Lock", "Scroll Lock", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Left Shift", "Right Shift", "Left Control", "Right Control", "Left Menu", "Right Menu", "Browser Back", "Browser Forward", "Browser Refresh", "Browser Stop", "Browser Search", "Browser Favorites", "Browser Home", "Volume Mute", "Volume Down", "Volume Up",
"Next Track", "Previous Track", "Media Stop", "Media Play/Pause", "Mail", "Media Select", "Launch App 1", "Launch App 2", "", "", "OEM ;", "OEM +", "OEM ,", "OEM -", "OEM .", "OEM /",
"OEM ~", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "OEM [", "OEM \\", "OEM ]", "OEM '", "OEM 8",
"", "", "OEM <", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "Attn", "CrSel", "ExSel", "Erase EOF", "Play", "Zoom", "", "PA1", "OEM Clear", ""
};

return keyboard_keys_international[key];
}
12 changes: 9 additions & 3 deletions src/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ class Menu : public RE::BSTEventSink<RE::InputEvent*>
return &menu;
}

void Load(json& o_json);
void Save(json& o_json);

RE::BSEventNotifyControl ProcessEvent(RE::InputEvent* const* a_event,
RE::BSTEventSource<RE::InputEvent*>* a_eventSource) override;

void DrawSettings();
void DrawOverlay();

private:
Menu()
{
}

uint32_t toggleKey = VK_END;
bool settingToggleKey = false;

Menu() { }
const char* KeyIdToString(uint32_t key);
};
31 changes: 27 additions & 4 deletions src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <magic_enum.hpp>

#include "ShaderCache.h"
#include "Menu.h"

#include "Features/Clustered.h"
#include "Features/GrassLighting.h"
Expand Down Expand Up @@ -57,9 +58,29 @@ void State::Setup()
void State::Load()
{
auto& shaderCache = SIE::ShaderCache::Instance();
std::ifstream i(L"Data\\SKSE\\Plugins\\CommunityShaders.json");

std::string configPath = "Data\\SKSE\\Plugins\\CommunityShaders.json";

std::ifstream i(configPath);
if (!i.is_open()) {
logger::error("Error opening config file ({})\n", configPath);
return;
}

json settings;
i >> settings;
try
{
i >> settings;
}
catch (const nlohmann::json::parse_error& e)
{
logger::error("Error parsing json config file ({}) : {}\n", configPath, e.what());
return;
}

if (settings["Menu"].is_object()) {
Menu::GetSingleton()->Load(settings["Menu"]);
}

if (settings["General"].is_object()) {
json& general = settings["General"];
Expand All @@ -68,10 +89,10 @@ void State::Load()
shaderCache.SetEnabled(general["Enable Shaders"]);

if (general["Enable Disk Cache"].is_boolean())
shaderCache.SetEnabled(general["Enable Disk Cache"]);
shaderCache.SetDiskCache(general["Enable Disk Cache"]);

if (general["Enable Async"].is_boolean())
shaderCache.SetEnabled(general["Enable Async"]);
shaderCache.SetAsync(general["Enable Async"]);
}

if (settings["Replace Original Shaders"].is_object()) {
Expand All @@ -95,6 +116,8 @@ void State::Save()
std::ofstream o(L"Data\\SKSE\\Plugins\\CommunityShaders.json");
json settings;

Menu::GetSingleton()->Save(settings);

json general;
general["Enable Shaders"] = shaderCache.IsEnabled();
general["Enable Disk Cache"] = shaderCache.IsDiskCache();
Expand Down