Skip to content

Commit

Permalink
Implement saving and loading custom colors
Browse files Browse the repository at this point in the history
  • Loading branch information
crumblingstatue committed Oct 15, 2024
1 parent c25c422 commit a60c245
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ pub fn project_dirs() -> Option<ProjectDirs> {
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";
2 changes: 1 addition & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Gui {
pub cmd: GCommandQueue,
pub fileops: FileOps,
pub win: Windows,
colorix: Option<Colorix>,
pub colorix: Option<Colorix>,
}

pub trait Dialog {
Expand Down
32 changes: 26 additions & 6 deletions src/gui/windows/preferences_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Colorix>) {
fn style_ui(
app: &mut App,
ui: &mut egui::Ui,
opt_colorix: &mut Option<Colorix>,
msg_dia: &mut MessageDialog,
) {
ui.group(|ui| {
let style = &mut app.cfg.style;
ui.heading("Font sizes");
Expand Down Expand Up @@ -173,11 +178,26 @@ fn style_ui(app: &mut App, ui: &mut egui::Ui, opt_colorix: &mut Option<Colorix>)
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;
Expand Down
22 changes: 21 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a60c245

Please sign in to comment.