Display encoding on main screen

This commit is contained in:
Jaby 2025-03-06 22:21:56 +01:00
parent 72b97ad7d3
commit 62289bc07e
5 changed files with 49 additions and 31 deletions

View File

@ -11,15 +11,11 @@ pub struct FileTab {
} }
impl FileTab { impl FileTab {
const FOUR_BIT_NAME:&str = "4 bit";
const EIGHT_BIT_NAME:&str = "8 bit";
const FULL_COLOR_NAME:&str = "Full color";
fn update_encoding_options(&self, width: u32, height: u32, has_palette: bool) -> Result<&str, Error> { fn update_encoding_options(&self, width: u32, height: u32, has_palette: bool) -> Result<&str, Error> {
self.encoding_options.clear(); self.encoding_options.clear();
if has_palette { if has_palette {
let mut selected_str = Self::EIGHT_BIT_NAME; let mut selected_str = Encoding::EightBit.to_str();
let has_4bit = width%4 == 0; let has_4bit = width%4 == 0;
let has_8bit = width%2 == 0; let has_8bit = width%2 == 0;
@ -28,19 +24,19 @@ impl FileTab {
} }
if has_4bit { if has_4bit {
self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Self::FOUR_BIT_NAME, width/4, height))); self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Encoding::FourBit.to_str(), width/4, height)));
selected_str = Self::FOUR_BIT_NAME; selected_str = Encoding::FourBit.to_str();
} }
if has_8bit { if has_8bit {
self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Self::EIGHT_BIT_NAME, width/2, height))); self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Encoding::EightBit.to_str(), width/2, height)));
} }
Ok(selected_str) Ok(selected_str)
} }
else { else {
self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Self::FULL_COLOR_NAME, width, height))); self.encoding_options.push(SharedString::from(format!("{} ({}/{})", Encoding::FullColor.to_str(), width, height)));
Ok(Self::FULL_COLOR_NAME) Ok(Encoding::FullColor.to_str())
} }
} }
@ -116,15 +112,15 @@ impl FileTab {
pub fn get_encoding(&self) -> Result<Encoding, Error> { pub fn get_encoding(&self) -> Result<Encoding, Error> {
let selected_encoding = self.main_window.borrow().get_file_tab_selected_encoding(); let selected_encoding = self.main_window.borrow().get_file_tab_selected_encoding();
if selected_encoding.starts_with(Self::FOUR_BIT_NAME) { if selected_encoding.starts_with(Encoding::FourBit.to_str()) {
Ok(Encoding::FourBit) Ok(Encoding::FourBit)
} }
else if selected_encoding.starts_with(Self::EIGHT_BIT_NAME) { else if selected_encoding.starts_with(Encoding::EightBit.to_str()) {
Ok(Encoding::EightBit) Ok(Encoding::EightBit)
} }
else if selected_encoding.starts_with(Self::FULL_COLOR_NAME) { else if selected_encoding.starts_with(Encoding::FullColor.to_str()) {
Ok(Encoding::FullColor) Ok(Encoding::FullColor)
} }

View File

@ -1,7 +1,8 @@
use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage}; use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage};
use super::{GUIElementsRef, MainWindowRef}; use super::{GUIElementsRef, MainWindowRef};
use slint::Model; use slint::{Model, SharedString};
use tim_tool::logic::tim::Encoding;
use std::{rc::Rc, sync::{Arc, Mutex}}; use std::{rc::Rc, sync::{Arc, Mutex}};
struct VRAM { struct VRAM {
@ -26,13 +27,15 @@ impl MainTab {
MainTab{main_window, vram: Arc::new(Mutex::new(VRAM{file_list: vram_file_list, image_list: vram_image_list}))} MainTab{main_window, vram: Arc::new(Mutex::new(VRAM{file_list: vram_file_list, image_list: vram_image_list}))}
} }
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, image_palette: Option<slint::Image>) { pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, encoding: Encoding, image_palette: Option<slint::Image>) {
let vram_data = self.vram.lock().expect("VRAM already locked"); let vram_data = self.vram.lock().expect("VRAM already locked");
let add_new_image = |file_name: &String, image: slint::Image, palette_count: i32, is_palette: bool| { let add_new_image = |file_name: &String, image: slint::Image, encoding: Encoding, palette_count: i32, is_palette: bool| {
let vram_image = VRAMImage{ let encoding_str = if is_palette {"<Palette>"} else {encoding.to_str()};
let vram_image = VRAMImage{
img: image, img: image,
x: 0, x: 0,
y: 0, y: 0,
encoding_str: SharedString::from(encoding_str),
palette_count, palette_count,
is_palette, is_palette,
}; };
@ -41,10 +44,10 @@ impl MainTab {
vram_data.image_list.push(vram_image); vram_data.image_list.push(vram_image);
}; };
add_new_image(file_name, image, if image_palette.is_some() {1} else {0}, false); add_new_image(file_name, image, encoding, if image_palette.is_some() {1} else {0}, false);
if let Some(image_palette) = image_palette { if let Some(image_palette) = image_palette {
let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)"; let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)";
add_new_image(&file_name, image_palette, 0, true); add_new_image(&file_name, image_palette, encoding, 0, true);
} }
} }

View File

@ -2,12 +2,27 @@ use std::{fs::File, path::PathBuf};
use slint::{Rgba8Pixel, SharedPixelBuffer}; use slint::{Rgba8Pixel, SharedPixelBuffer};
use tool_helper::Error; use tool_helper::Error;
#[derive(Clone, Copy)]
pub enum Encoding { pub enum Encoding {
FourBit, FourBit,
EightBit, EightBit,
FullColor, FullColor,
} }
impl Encoding {
const FOUR_BIT_NAME:&str = "4 bit";
const EIGHT_BIT_NAME:&str = "8 bit";
const FULL_COLOR_NAME:&str = "Full color";
pub const fn to_str(&self) -> &str {
match self {
Encoding::FourBit => Self::FOUR_BIT_NAME,
Encoding::EightBit => Self::EIGHT_BIT_NAME,
Encoding::FullColor => Self::FULL_COLOR_NAME,
}
}
}
pub struct TIMInfo { pub struct TIMInfo {
image_data: SharedPixelBuffer<Rgba8Pixel>, image_data: SharedPixelBuffer<Rgba8Pixel>,
palette: Option<PaletteInfo>, palette: Option<PaletteInfo>,

View File

@ -87,7 +87,7 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
let encoding = file_tab.get_encoding()?; let encoding = file_tab.get_encoding()?;
let (image, palette_image) = logic.borrow_mut().add_unadded_tim_as(&file_name, encoding)?; let (image, palette_image) = logic.borrow_mut().add_unadded_tim_as(&file_name, encoding)?;
main_tab.add_new_vram_file(&file_name, image, palette_image); main_tab.add_new_vram_file(&file_name, image, encoding, palette_image);
file_tab.clear_load(); file_tab.clear_load();
main_window.invoke_change_to_main(); main_window.invoke_change_to_main();
Ok(()) Ok(())

View File

@ -5,6 +5,7 @@ struct VRAMImage {
img: image, img: image,
x: int, x: int,
y: int, y: int,
encoding_str: string,
palette_count: int, palette_count: int,
is_palette: bool, is_palette: bool,
} }
@ -78,10 +79,12 @@ export component MainTab inherits Rectangle {
} }
if event.kind == PointerEventKind.down { if event.kind == PointerEventKind.down {
cur_sel_x.text = parent.img_x; cur_sel_x.text = parent.img_x;
cur_sel_y.text = parent.img_y; cur_sel_y.text = parent.img_y;
cur_sel_img.source = parent.img; cur_sel_img.source = parent.img;
cur_sel_img.visible = true; encoding_text.encoding_str = vram-image.encoding_str;
cur_sel_img.visible = true;
vram_files_list.current-item = i; vram_files_list.current-item = i;
} }
} }
@ -150,10 +153,11 @@ export component MainTab inherits Rectangle {
model: root.vram_files; model: root.vram_files;
current-item-changed(current-item) => { current-item-changed(current-item) => {
cur_sel_x.text = root.vram_images[current-item].x; cur_sel_x.text = root.vram_images[current-item].x;
cur_sel_y.text = root.vram_images[current-item].y; cur_sel_y.text = root.vram_images[current-item].y;
cur_sel_img.source = root.vram_images[current-item].img; cur_sel_img.source = root.vram_images[current-item].img;
cur_sel_img.visible = true; encoding_text.encoding_str = root.vram_images[current-item].encoding_str;
cur_sel_img.visible = true;
} }
} }
HorizontalLayout { HorizontalLayout {
@ -232,9 +236,9 @@ export component MainTab inherits Rectangle {
} }
} }
} }
ComboBox { encoding_text := Text {
model: ["4-bit", "16-bit", "24-bit"]; in-out property <string> encoding_str;
current-value: "4-bit"; text: "Encoding: " + encoding_str;
} }
} }
} }