Skip to content

Commit

Permalink
waddy: refactor between texturetile and wadimage
Browse files Browse the repository at this point in the history
  • Loading branch information
khanghugo committed Sep 25, 2024
1 parent 8d06063 commit 4e6a372
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 61 deletions.
116 changes: 58 additions & 58 deletions src/gui/programs/waddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,39 @@ impl Default for SearchBar {
}
}

struct LoadedImage {
image: WadImage,
}

struct TextureTile {
index: usize,
name: String,
image: LoadedImage,
dimensions: (u32, u32),
wad_image: WadImage,
in_rename: bool,
prev_name: String,
}

impl TextureTile {
fn new(
index: usize,
name: impl AsRef<str> + Into<String>,
image: LoadedImage,
dimensions: (u32, u32),
) -> Self {
fn new(index: usize, wad_image: WadImage) -> Self {
Self {
index,
name: name.into(),
image,
dimensions,
wad_image,
in_rename: false,
prev_name: String::new(),
}
}

fn name(&self) -> &String {
self.wad_image.name()
}

fn name_mut(&mut self) -> &mut String {
self.wad_image.name_mut()
}

fn dimensions(&self) -> (u32, u32) {
self.wad_image.dimensions()
}

#[allow(dead_code)]
fn texture(&self) -> &egui::TextureHandle {
self.wad_image.texture()
}
}

const BASE_IMAGE_TILE_SIZE: f32 = 96.0;
Expand Down Expand Up @@ -119,7 +123,7 @@ impl WaddyGui {
let current_id = egui::Id::new(format!(
"{}{}",
self.instances[instance_index].texture_tiles[texture_tile_index].index,
self.instances[instance_index].texture_tiles[texture_tile_index].name
self.instances[instance_index].texture_tiles[texture_tile_index].name()
));

let is_selected = self.instances[instance_index]
Expand All @@ -140,8 +144,7 @@ impl WaddyGui {
})
.show(ui, |ui| {
let texture = self.instances[instance_index].texture_tiles[texture_tile_index]
.image
.image
.wad_image
.texture();
let dimensions = if self.fit_texture {
let dimensions = texture.size_vec2();
Expand All @@ -168,22 +171,22 @@ impl WaddyGui {
// if clicked then copy the name of the texture
if ui
.add(
egui::Label::new(&current_tile.name)
egui::Label::new(current_tile.name())
.selectable(false)
.sense(Sense::click()),
)
.on_hover_text("Click to copy name")
.clicked()
{
ui.output_mut(|o| o.copied_text = current_tile.name.to_string());
ui.output_mut(|o| o.copied_text = current_tile.name().to_string());
ui.close_menu();
}

ui.separator();

if ui.button("View").clicked() {
self.extra_image_viewports
.push(WadImage::new(current_tile.image.image.texture()));
.push(current_tile.wad_image.clone());
ui.close_menu();
}

Expand All @@ -193,7 +196,9 @@ impl WaddyGui {
current_tile.in_rename = true;
context_menu_clicked = true;

current_tile.prev_name.clone_from(&current_tile.name);
current_tile
.prev_name
.clone_from(&current_tile.name().clone());
ui.close_menu();
}

Expand All @@ -202,9 +207,9 @@ impl WaddyGui {
if ui.button("Export").clicked() {
if let Some(path) = rfd::FileDialog::new()
.set_file_name(
&self.instances[instance_index].texture_tiles
self.instances[instance_index].texture_tiles
[texture_tile_index]
.name,
.name(),
)
.add_filter("All Files", &["bmp"])
.save_file()
Expand Down Expand Up @@ -240,7 +245,7 @@ impl WaddyGui {
let current_tile = &self.instances[instance_index]
.texture_tiles[texture_tile_index];

let texture_file_name = &current_tile.name;
let texture_file_name = &current_tile.name();

// tODO TOAST
if let Err(err) = self.instances[instance_index]
Expand Down Expand Up @@ -437,21 +442,20 @@ impl WaddyGui {

// middle click wound bring a new viewport
if clickable_image.middle_clicked() {
self.extra_image_viewports.push(WadImage::new(
self.extra_image_viewports.push(
self.instances[instance_index].texture_tiles[texture_tile_index]
.image
.image
.texture(),
));
.wad_image
.clone(),
);
};

ui.end_row();

if self.instances[instance_index].texture_tiles[texture_tile_index].in_rename {
let widget = ui.add(
egui::TextEdit::singleline(
&mut self.instances[instance_index].texture_tiles[texture_tile_index]
.name,
self.instances[instance_index].texture_tiles[texture_tile_index]
.name_mut(),
)
.font(egui::TextStyle::Small),
);
Expand All @@ -466,8 +470,10 @@ impl WaddyGui {
let current_tile =
&mut self.instances[instance_index].texture_tiles[texture_tile_index];

let prev_name = current_tile.prev_name.clone();

current_tile.in_rename = false;
current_tile.name.clone_from(&current_tile.prev_name);
current_tile.name_mut().clone_from(&prev_name);
} else if ui.input(|i| i.key_pressed(egui::Key::Enter)) {
let current_instance = &mut self.instances[instance_index];
let current_tile = &mut current_instance.texture_tiles[texture_tile_index];
Expand All @@ -477,16 +483,20 @@ impl WaddyGui {

if let Err(err) = current_instance
.waddy
.rename_texture(texture_tile_index, current_tile.name.clone())
.rename_texture(texture_tile_index, current_tile.name().clone())
{
// TODO learn how to do toast
println!("{:?}", err);

current_tile.name.clone_from(&current_tile.prev_name);
} else if current_tile.name.len() >= 16 {
let prev_name = current_tile.prev_name.clone();

current_tile.name_mut().clone_from(&prev_name);
} else if current_tile.name().len() >= 16 {
println!("Texture name is too long");

current_tile.name.clone_from(&current_tile.prev_name);
let prev_name = current_tile.prev_name.clone();

current_tile.name_mut().clone_from(&prev_name);
} else {
// this means things are good
self.instances[instance_index].is_changed = true;
Expand All @@ -498,22 +508,24 @@ impl WaddyGui {

// beside the context menu, double click on the name would also enter rename mode
if ui
.label(custom_font(current_tile.name.clone()))
.label(custom_font(current_tile.name().clone()))
.double_clicked()
{
current_tile.in_rename = true;
current_tile.prev_name.clone_from(&current_tile.name);
current_tile
.prev_name
.clone_from(&current_tile.name().clone());
};
}

ui.end_row();
ui.label(custom_font(format!(
"{}x{}",
self.instances[instance_index].texture_tiles[texture_tile_index]
.dimensions
.dimensions()
.0,
self.instances[instance_index].texture_tiles[texture_tile_index]
.dimensions
.dimensions()
.1
)));
});
Expand Down Expand Up @@ -544,7 +556,7 @@ impl WaddyGui {
.filter(|&texture_tile| {
if is_search_enabled {
self.instances[instance_index].texture_tiles[texture_tile]
.name
.name()
.to_lowercase()
.contains(search_text.as_str())
} else {
Expand Down Expand Up @@ -833,12 +845,7 @@ impl WaddyGui {

self.instances[instance_index]
.texture_tiles
.push(TextureTile::new(
instance_index,
texture_name,
LoadedImage { image: wad_image },
dimensions,
));
.push(TextureTile::new(instance_index, wad_image));

self.instances[instance_index].is_changed = true;
}
Expand Down Expand Up @@ -876,22 +883,15 @@ impl WaddyGui {
.enumerate()
.filter_map(|(index, entry)| {
if let FileEntry::MipTex(miptex) = &entry.file_entry {
let loaded_image = WadImage::from_wad_image(
let wad_image = WadImage::from_wad_image(
ui,
entry.directory_entry.texture_name.get_string(),
miptex.mip_images[0].data.get_bytes(),
miptex.palette.get_bytes(),
(miptex.width, miptex.height),
);

return Some(TextureTile::new(
index,
waddy.wad().entries[index].texture_name(),
LoadedImage {
image: loaded_image,
},
waddy.wad().entries[index].file_entry.dimensions(),
));
return Some(TextureTile::new(index, wad_image));
// None
}

Expand Down
31 changes: 28 additions & 3 deletions src/gui/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,22 @@ pub fn display_text_in_viewport(ctx: &Context, s: impl Into<String>) -> bool {

#[derive(Clone)]
pub struct WadImage {
name: String,
dimensions: (u32, u32),
texture: egui::TextureHandle,
}

impl WadImage {
pub fn new(handle: &egui::TextureHandle) -> Self {
#[allow(dead_code)]
pub fn new(
handle: &egui::TextureHandle,
name: impl AsRef<str> + Into<String>,
dimensions: (u32, u32),
) -> Self {
Self {
texture: handle.clone(),
name: name.into(),
dimensions,
}
}

Expand All @@ -181,15 +190,31 @@ impl WadImage {
.collect::<Vec<u8>>();
// Load the texture only once.
let handle = ui.ctx().load_texture(
name,
name.as_ref(),
egui::ColorImage::from_rgb([dimensions.0 as usize, dimensions.1 as usize], &image),
Default::default(),
);

Self { texture: handle }
Self {
texture: handle,
name: name.into().to_owned(),
dimensions,
}
}

pub fn texture(&self) -> &egui::TextureHandle {
&self.texture
}

pub fn dimensions(&self) -> (u32, u32) {
self.dimensions
}

pub fn name(&self) -> &String {
&self.name
}

pub fn name_mut(&mut self) -> &mut String {
&mut self.name
}
}

0 comments on commit 4e6a372

Please sign in to comment.