Merge pull request #337 from ChancellorIkseew/add-settings-reset

Add settings reset
This commit is contained in:
MihailRis 2024-11-01 12:07:06 +03:00 committed by GitHub
commit a54cfec9f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 109 additions and 8 deletions

4
.gitignore vendored
View File

@ -48,3 +48,7 @@ appimage-build/
/res/content/*
!/res/content/base
*.mtl
# libs
/libs/
/vcpkg_installed/

View File

@ -11,6 +11,7 @@
<panel margin='6' gravity='bottom-left' size='250' color='0' interval='1'>
<button onclick='menu.page="languages"' id='langs_btn'>-</button>
<button onclick='core.open_folder("user:")'>@Open data folder</button>
<button id='s_rst' onclick='set_page("s_rst", "settings_reset")'>@Reset settings</button>
<button onclick='menu:back()'>@Back</button>
</panel>
</container>

View File

@ -11,6 +11,7 @@ function set_page(btn, page)
document.s_dsp.enabled = true
document.s_gfx.enabled = true
document.s_ctl.enabled = true
document.s_rst.enabled = true
document[btn].enabled = false
document.menu.page = page
end

View File

@ -59,4 +59,5 @@ function on_open()
create_checkbox("display.fullscreen", "Fullscreen")
create_checkbox("camera.shaking", "Camera Shaking")
create_checkbox("camera.inertia", "Camera Inertia")
create_checkbox("camera.fov-effects", "Camera FOV Effects")
end

View File

@ -0,0 +1,7 @@
<panel size='380, 0' color='#00000080' padding='8' context='menu'>
<label multiline='false'>@Reset settings</label>
<button onclick='reset("aud")'>@Audio</button>
<button onclick='reset("dsp")'>@Display</button>
<button onclick='reset("gfx")'>@Graphics</button>
<button onclick='reset("ctl")'>@Controls</button>
</panel>

View File

@ -0,0 +1,44 @@
function reset(category)
if category == "aud" then
reset_audio()
elseif category == "dsp" then
reset_display()
elseif category == "gfx" then
reset_graphics()
elseif category == "ctl" then
reset_control()
end
end
function reset_setting(name)
core.set_setting(name, core.get_setting_info(name).def)
end
function reset_audio()
reset_setting("audio.volume-master")
reset_setting("audio.volume-regular")
reset_setting("audio.volume-ui")
reset_setting("audio.volume-ambient")
reset_setting("audio.volume-music")
end
function reset_display()
reset_setting("camera.fov")
reset_setting("display.framerate")
reset_setting("display.fullscreen")
reset_setting("camera.shaking")
reset_setting("camera.inertia")
reset_setting("camera.fov-effects")
end
function reset_graphics()
reset_setting("chunks.load-distance")
reset_setting("chunks.load-speed")
reset_setting("graphics.fog-curve")
reset_setting("graphics.gamma")
reset_setting("graphics.backlight")
end
function reset_control()
input.reset_bindings()
end

View File

@ -38,6 +38,7 @@ menu.Page not found=Страница не найдена
menu.Quit=Выход
menu.Save and Quit to Menu=Сохранить и Выйти в Меню
menu.Settings=Настройки
menu.Reset settings=Сбросить настройки
menu.Contents Menu=Меню контентпаков
menu.Open data folder=Открыть папку данных
menu.Open content folder=Открыть папку [content]
@ -59,6 +60,7 @@ settings.Ambient=Фон
settings.Backlight=Подсветка
settings.Camera Shaking=Тряска Камеры
settings.Camera Inertia=Инерция Камеры
settings.Camera FOV Effects=Эффекты поля зрения
settings.Fog Curve=Кривая Тумана
settings.FOV=Поле Зрения
settings.Fullscreen=Полный экран

View File

@ -31,7 +31,7 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
auto bindsFile = paths->getResourcesFolder()/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) {
Events::loadBindings(
bindsFile.u8string(), files::read_string(bindsFile)
bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND
);
}

View File

@ -131,7 +131,7 @@ void Engine::loadControls() {
if (fs::is_regular_file(controls_file)) {
logger.info() << "loading controls";
std::string text = files::read_string(controls_file);
Events::loadBindings(controls_file.u8string(), text);
Events::loadBindings(controls_file.u8string(), text, BindType::BIND);
}
}
@ -287,7 +287,7 @@ static void load_configs(const fs::path& root) {
auto bindsFile = configFolder/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) {
Events::loadBindings(
bindsFile.u8string(), files::read_string(bindsFile)
bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND
);
}
}

View File

@ -1,4 +1,7 @@
#include <filesystem>
#include "engine.hpp"
#include "files/files.hpp"
#include "frontend/hud.hpp"
#include "frontend/screens/Screen.hpp"
#include "graphics/ui/GUI.hpp"
@ -109,6 +112,27 @@ static int l_is_pressed(lua::State* L) {
}
}
static void resetPackBindings(fs::path& packFolder) {
auto configFolder = packFolder/fs::path("config");
auto bindsFile = configFolder/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) {
Events::loadBindings(
bindsFile.u8string(),
files::read_string(bindsFile),
BindType::REBIND
);
}
}
static int l_reset_bindings(lua::State*) {
auto resFolder = engine->getPaths()->getResourcesFolder();
resetPackBindings(resFolder);
for (auto& pack : engine->getContentPacks()) {
resetPackBindings(pack.folder);
}
return 0;
}
const luaL_Reg inputlib[] = {
{"keycode", lua::wrap<l_keycode>},
{"mousecode", lua::wrap<l_mousecode>},
@ -118,4 +142,5 @@ const luaL_Reg inputlib[] = {
{"get_binding_text", lua::wrap<l_get_binding_text>},
{"is_active", lua::wrap<l_is_active>},
{"is_pressed", lua::wrap<l_is_pressed>},
{"reset_bindings", lua::wrap<l_reset_bindings>},
{NULL, NULL}};

View File

@ -106,9 +106,9 @@ void Events::pollEvents() {
}
Binding& Events::getBinding(const std::string& name) {
auto found = bindings.find(name);
const auto found = bindings.find(name);
if (found == bindings.end()) {
throw std::runtime_error("binding '" + name + "' does not exists");
throw std::runtime_error("binding '" + name + "' does not exist");
}
return found->second;
}
@ -126,6 +126,10 @@ void Events::bind(const std::string& name, inputtype type, int code) {
}
void Events::rebind(const std::string& name, inputtype type, int code) {
const auto& found = bindings.find(name);
if (found == bindings.end()) {
throw std::runtime_error("binding '" + name + "' does not exist");
}
bindings[name] = Binding(type, code);
}
@ -193,7 +197,8 @@ std::string Events::writeBindings() {
}
void Events::loadBindings(
const std::string& filename, const std::string& source
const std::string& filename, const std::string& source,
BindType bindType
) {
auto map = toml::parse(filename, source);
for (auto& [sectionName, section] : map.asObject()) {
@ -214,7 +219,12 @@ void Events::loadBindings(
<< util::quote(key) << ")";
continue;
}
Events::bind(key, type, code);
if (bindType == BindType::BIND) {
Events::bind(key, type, code);
} else if (bindType == BindType::REBIND) {
Events::rebind(key, type, code);
}
}
}
}

View File

@ -9,6 +9,11 @@
inline constexpr short KEYS_BUFFER_SIZE = 1036;
enum class BindType {
BIND = 0,
REBIND = 1
};
class Events {
static bool keys[KEYS_BUFFER_SIZE];
static uint frames[KEYS_BUFFER_SIZE];
@ -52,6 +57,7 @@ public:
static std::string writeBindings();
static void loadBindings(
const std::string& filename, const std::string& source
const std::string& filename, const std::string& source,
BindType bindType
);
};