From a60c245b6341b781bc6583409529fed81d695f5a Mon Sep 17 00:00:00 2001 From: crumblingstatue Date: Wed, 16 Oct 2024 01:11:22 +0200 Subject: [PATCH] Implement saving and loading custom colors --- src/config.rs | 10 +++++++++ src/gui.rs | 2 +- src/gui/windows/preferences_window.rs | 32 ++++++++++++++++++++++----- src/main.rs | 22 +++++++++++++++++- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0ad3583..3308cb9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -130,4 +130,14 @@ pub fn project_dirs() -> Option { ProjectDirs::from("", "crumblingstatue", "hexerator") } +pub trait ProjectDirsExt { + fn color_theme_path(&self) -> PathBuf; +} + +impl ProjectDirsExt for ProjectDirs { + fn color_theme_path(&self) -> PathBuf { + self.config_dir().join("egui_colors_theme.pal") + } +} + const FILENAME: &str = "hexerator.cfg"; diff --git a/src/gui.rs b/src/gui.rs index ff09a11..8dcc8a9 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -54,7 +54,7 @@ pub struct Gui { pub cmd: GCommandQueue, pub fileops: FileOps, pub win: Windows, - colorix: Option, + pub colorix: Option, } pub trait Dialog { diff --git a/src/gui/windows/preferences_window.rs b/src/gui/windows/preferences_window.rs index d95161d..caffabc 100644 --- a/src/gui/windows/preferences_window.rs +++ b/src/gui/windows/preferences_window.rs @@ -2,7 +2,7 @@ use { super::{WinCtx, WindowOpen}, crate::{ app::{backend_command::BackendCmd, App}, - config::{self, Config}, + config::{self, Config, ProjectDirsExt}, gui::message_dialog::{Icon, MessageDialog}, }, egui_colors::{tokens::ThemeColor, Colorix}, @@ -75,7 +75,7 @@ impl super::Window for PreferencesWindow { ui.separator(); match self.tab { Tab::Video => video_ui(ui, app), - Tab::Style => style_ui(app, ui, &mut gui.colorix), + Tab::Style => style_ui(app, ui, &mut gui.colorix, &mut gui.msg_dialog), Tab::Fonts => fonts_ui( ui, &mut self.font_cfg, @@ -105,7 +105,12 @@ fn video_ui(ui: &mut egui::Ui, app: &mut App) { }); } -fn style_ui(app: &mut App, ui: &mut egui::Ui, opt_colorix: &mut Option) { +fn style_ui( + app: &mut App, + ui: &mut egui::Ui, + opt_colorix: &mut Option, + msg_dia: &mut MessageDialog, +) { ui.group(|ui| { let style = &mut app.cfg.style; ui.heading("Font sizes"); @@ -173,11 +178,26 @@ fn style_ui(app: &mut App, ui: &mut egui::Ui, opt_colorix: &mut Option) std::array::from_fn(|_| ThemeColor::Custom(rng.gen::<[u8; 3]>())), ); } - if ui.button("Reset to default (dark)").clicked() { - clear = true; - } }); + ui.separator(); colorix.ui_combo_12(ui); + if let Some(dirs) = crate::config::project_dirs() { + ui.separator(); + ui.horizontal(|ui| { + if ui.button("Save").clicked() { + let data: [[u8; 3]; 12] = colorix.theme().map(|theme| theme.rgb()); + if let Err(e) = std::fs::write(dirs.color_theme_path(), data.as_flattened()) { + msg_dia.open(Icon::Error, "Failed to save theme", e.to_string()); + } + }; + if ui.button("Remove custom colors").clicked() { + if let Err(e) = std::fs::remove_file(dirs.color_theme_path()) { + msg_dia.open(Icon::Error, "Failed to delete theme file", e.to_string()); + } + clear = true; + } + }); + } if clear { ui.ctx().set_visuals(egui::Visuals::dark()); *opt_colorix = None; diff --git a/src/main.rs b/src/main.rs index 5580e35..35d4f77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,8 @@ #![windows_subsystem = "windows"] use { - config::LoadedConfig, + config::{LoadedConfig, ProjectDirsExt as _}, + egui_colors::{tokens::ThemeColor, Colorix}, egui_file_dialog::{DialogState, DirectoryEntry}, egui_sfml::sfml::graphics::RenderStates, gamedebug_core::{IMMEDIATE, PERSISTENT}, @@ -182,6 +183,25 @@ fn try_main() -> anyhow::Result<()> { let lua = Lua::default(); crate::gui::set_font_sizes_style(&mut style, &app.cfg.style); sf_egui.context().set_style(style); + // Custom egui_colors theme load + if let Some(project_dirs) = crate::config::project_dirs() { + let path = project_dirs.color_theme_path(); + if path.exists() { + match std::fs::read(path) { + Ok(data) => { + eprintln!("Okay, reading theme"); + let mut chunks = data.array_chunks().copied(); + let theme = std::array::from_fn(|_| { + ThemeColor::Custom(chunks.next().unwrap_or_default()) + }); + gui.colorix = Some(Colorix::init(sf_egui.context(), theme)); + } + Err(e) => { + eprintln!("Failed to load custom theme: {e}"); + } + } + } + } let mut vertex_buffer = Vec::new(); while window.is_open() {