Skip to content

Commit

Permalink
Add copy all to icon source
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Jan 12, 2025
1 parent 4d1566d commit 5877e9a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
17 changes: 17 additions & 0 deletions reffect/src/action/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ impl<T> DynAction<T> {
}
});
}

pub fn render_copy_all_cloned(
&mut self,
ui: &Ui,
id: impl Into<String>,
value: &T,
mut action: impl FnMut(&mut T, &T) + 'static,
) where
T: Clone + 'static,
{
item_context_menu(id, || {
if MenuItem::new("Copy to all siblings").build(ui) {
let cloned = value.clone();
self.set(move |target| action(target, &cloned));
}
});
}
}

impl<T> fmt::Debug for DynAction<T> {
Expand Down
3 changes: 2 additions & 1 deletion reffect/src/elements/icon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ impl Icon {
pub fn render_options(&mut self, ui: &Ui, ctx: &Context) -> DynAction<Self> {
let mut action = DynAction::<Self>::empty();

self.source.render_select(ui, ctx);
let source_action = self.source.render_select(ui, ctx);
action.or(source_action.map(|icon: &mut Self| &mut icon.source));

ui.spacing();

Expand Down
24 changes: 16 additions & 8 deletions reffect/src/elements/icon/source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
action::DynAction,
addon::Addon,
context::Context,
lockbox::Lockbox,
Expand Down Expand Up @@ -35,8 +36,8 @@ pub enum IconSource {

Empty,

#[serde(alias = "Automatic")]
Dynamic,
#[serde(alias = "Dynamic")]
Automatic,

Url(String),

Expand All @@ -63,7 +64,7 @@ impl IconSource {
pub fn get_texture(&self, skill: Skill) -> Option<TextureId> {
match self {
Self::Empty => None,
Self::Dynamic => match skill {
Self::Automatic => match skill {
Skill::Unknown => TextureManager::get_texture(&IconSource::Unknown),
Skill::WeaponSwap => TextureManager::get_weapon_swap(),
Skill::BundleDrop => TextureManager::get_bundle_drop(),
Expand All @@ -79,7 +80,7 @@ impl IconSource {
pub fn generate_id(&self) -> String {
match self {
Self::Unknown => Self::UNKNOWN_ID.into(),
Self::Empty | Self::Dynamic => String::new(),
Self::Empty | Self::Automatic => String::new(),
Self::File(path) => format!("REFFECT_ICON_FILE_\"{}\"", path.display()),
Self::Url(url) => format!("REFFECT_ICON_URL_\"{url}\""),
}
Expand All @@ -89,26 +90,31 @@ impl IconSource {
match self {
Self::Unknown => "unknown".into(),
Self::Empty => "empty".into(),
Self::Dynamic => "dynamic".into(),
Self::Automatic => "automatic".into(),
Self::File(path) => format!("file \"{}\"", path.display()),
Self::Url(url) => format!("url \"{url}\""),
}
}

pub fn render_select(&mut self, ui: &Ui, ctx: &Context) {
pub fn render_select(&mut self, ui: &Ui, ctx: &Context) -> DynAction<Self> {
let mut action = DynAction::empty();

let validation = match self {
Self::Dynamic if !ctx.settings.use_game_icons => {
Self::Automatic if !ctx.settings.use_game_icons => {
Validation::Error("Requires experimental reuse game icons setting")
}
_ => Validation::Ok,
};
validation.for_item(ui, || enum_combo(ui, "Icon", self, ComboBoxFlags::empty()));
action.render_copy_all_cloned(ui, "iconsrc", self, |target, source| {
*target = source.clone()
});

// we assume this stays in place, otherwise we consider the file dialog invalidated
let id = self as *mut _ as usize;

match self {
Self::Unknown | Self::Empty | Self::Dynamic => {}
Self::Unknown | Self::Empty | Self::Automatic => {}
Self::File(path) => {
let validation = if path.is_absolute() {
Validation::Warn("Non-shareable absolute icon path")
Expand Down Expand Up @@ -166,5 +172,7 @@ impl IconSource {
}
}
}

action
}
}
2 changes: 1 addition & 1 deletion reffect/src/texture_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl TextureManager {

if !textures.exists(&source) {
match &source {
IconSource::Unknown | IconSource::Empty | IconSource::Dynamic => {}
IconSource::Unknown | IconSource::Empty | IconSource::Automatic => {}
IconSource::File(path) => {
let id = textures.add_pending(source.clone());
drop(textures); // drop to avoid recursive locking
Expand Down

0 comments on commit 5877e9a

Please # to comment.