Skip to content

Commit

Permalink
Add ability to import and export crafter config to and from clipboard (
Browse files Browse the repository at this point in the history
…#83)

* Add ability to import and export crafter config to and from clipboard

Importing is done in a roundabout way, by having a text input that is used for pasting the config into. egui doesn't seem to expose a way to directly access the contents of the clipboard.

* Use RON instead of JSON
  • Loading branch information
augenfrosch authored Sep 11, 2024
1 parent b00331b commit ba6e452
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ eframe = { version = "0.27.2", features = ["persistence"] }
egui_extras = { version = "0.27.2", features = ["all_loaders"] }
image = { version = "0.24.9", default-features = false, features = ["webp"] }
serde = { version = "1.0.203", features = ["derive"] }
ron = "0.8"
log = "0.4"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
52 changes: 52 additions & 0 deletions src/widgets/stats_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,58 @@ impl<'a> Widget for StatsEdit<'a> {
ui.checkbox(&mut stats.quick_innovation, "Quick Innovation");
});
}

ui.separator().rect.width();
ui.horizontal(|ui| {
let button_text = "🗐 Copy Crafter Config";
let button_response;
if ui
.ctx()
.animate_bool_with_time(egui::Id::new(button_text), false, 0.25)
== 0.0
{
button_response = ui.button(button_text);
} else {
button_response = ui.add_enabled(false, egui::Button::new(button_text));
}
if button_response.clicked() {
ui.output_mut(|output| {
output.copied_text = ron::to_string(self.crafter_config).unwrap()
});
ui.ctx()
.animate_bool_with_time(egui::Id::new(button_text), true, 0.0);
}

ui.add_space(button_response.rect.width() * 0.5);
let selected_job = self.crafter_config.selected_job;
let hint_text = "📋 Paste Config here to Load";
let input_string = &mut String::new();
let input_response;
if ui
.ctx()
.animate_bool_with_time(egui::Id::new(hint_text), false, 0.25)
== 0.0
{
input_response =
ui.add(egui::TextEdit::singleline(input_string).hint_text(hint_text));
} else {
input_response = ui.add_enabled(
false,
egui::TextEdit::singleline(input_string).hint_text(hint_text),
);
}
if input_response.changed() {
match ron::from_str(&input_string) {
Ok(crafter_config) => {
*self.crafter_config = crafter_config;
self.crafter_config.selected_job = selected_job;
ui.ctx()
.animate_bool_with_time(egui::Id::new(hint_text), true, 0.0);
}
Err(_) => {}
}
}
});
})
.response
}
Expand Down

0 comments on commit ba6e452

Please sign in to comment.