Support changing palette size
This commit is contained in:
parent
3cdfe4a677
commit
4d62d79cbd
|
@ -21,10 +21,25 @@ impl FileTab {
|
||||||
main_window.set_file_tab_enable(false);
|
main_window.set_file_tab_enable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_new_load(&self, file_name: Option<String>, image: Image, palette: Option<Image>) {
|
pub fn update_new_loaded_file(&self, file_name: Option<String>, image: Image, palette: Option<Image>) {
|
||||||
|
self.update_palette(palette);
|
||||||
|
|
||||||
let main_window = self.main_window.borrow();
|
let main_window = self.main_window.borrow();
|
||||||
|
|
||||||
main_window.set_file_tab_image_data(image);
|
main_window.set_file_tab_image_data(image);
|
||||||
|
if let Some(file_name) = file_name {
|
||||||
|
main_window.set_file_tab_image_name(file_name.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
main_window.set_file_tab_image_name("".into());
|
||||||
|
}
|
||||||
|
main_window.set_file_tab_enable(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_palette(&self, palette: Option<Image>) {
|
||||||
|
let main_window = self.main_window.borrow();
|
||||||
|
|
||||||
if let Some(palette) = palette {
|
if let Some(palette) = palette {
|
||||||
let size = palette.size();
|
let size = palette.size();
|
||||||
|
|
||||||
|
@ -39,15 +54,6 @@ impl FileTab {
|
||||||
main_window.set_file_tab_palette_height(0);
|
main_window.set_file_tab_palette_height(0);
|
||||||
main_window.set_file_tab_palette_visible(false);
|
main_window.set_file_tab_palette_visible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(file_name) = file_name {
|
|
||||||
main_window.set_file_tab_image_name(file_name.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
main_window.set_file_tab_image_name("".into());
|
|
||||||
}
|
|
||||||
main_window.set_file_tab_enable(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_file_name(&self) -> String {
|
pub fn get_file_name(&self) -> String {
|
||||||
|
@ -65,6 +71,17 @@ impl FileTab {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_update_palette_size(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow, u32, u32) -> Result<(), Error> + 'static) {
|
||||||
|
let main_window_cloned = self.main_window.clone();
|
||||||
|
let gui_cloned = gui_elements.clone();
|
||||||
|
|
||||||
|
self.main_window.borrow().on_file_tab_update_palette_size(move |width, height| {
|
||||||
|
if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow(), width as u32, height as u32) {
|
||||||
|
display_error("Loadind file failed", &error.to_string());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_add_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + 'static) {
|
pub fn on_add_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + 'static) {
|
||||||
let main_window_cloned = self.main_window.clone();
|
let main_window_cloned = self.main_window.clone();
|
||||||
let gui_cloned = gui_elements.clone();
|
let gui_cloned = gui_elements.clone();
|
||||||
|
|
|
@ -34,4 +34,14 @@ impl Logic {
|
||||||
Err(Error::from_str("No data found for loaded image"))
|
Err(Error::from_str("No data found for loaded image"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn change_unadded_tim_palette_size(&mut self, width: u32, height: u32) -> Result<Option<Image>, Error> {
|
||||||
|
if let Some(unadded_tim) = &mut self.unadded_tim {
|
||||||
|
unadded_tim.change_palette_size(width, height)
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@ use tool_helper::Error;
|
||||||
|
|
||||||
pub struct TIMInfo {
|
pub struct TIMInfo {
|
||||||
image_data: SharedPixelBuffer<Rgba8Pixel>,
|
image_data: SharedPixelBuffer<Rgba8Pixel>,
|
||||||
palette: Option<Vec<Rgba8Pixel>>,
|
palette: Option<PaletteInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TIMInfo {
|
impl TIMInfo {
|
||||||
|
@ -48,7 +48,7 @@ impl TIMInfo {
|
||||||
_ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported for indexed color images"));}
|
_ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported for indexed color images"));}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(TIMInfo{image_data, palette: Some(palette_colors)})
|
Ok(TIMInfo{image_data, palette: Some(PaletteInfo::new(palette_colors))})
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
|
@ -70,21 +70,49 @@ impl TIMInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn change_palette_size(&mut self, width: u32, height: u32) -> Result<Option<slint::Image>, Error> {
|
||||||
|
if let Some(palette) = &mut self.palette {
|
||||||
|
if width == 0 || height == 0 {
|
||||||
|
return Err(Error::from_text(format!("{}px x {}px is not a valid size for palette", width, height)));
|
||||||
|
}
|
||||||
|
|
||||||
|
palette.width = width;
|
||||||
|
palette.height = height;
|
||||||
|
Ok(Some(palette.get_image()))
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_slint_images(&self) -> (slint::Image, Option<slint::Image>) {
|
pub fn get_slint_images(&self) -> (slint::Image, Option<slint::Image>) {
|
||||||
(slint::Image::from_rgba8_premultiplied(self.image_data.clone()), if let Some(palette) = &self.palette {
|
(slint::Image::from_rgba8_premultiplied(self.image_data.clone()), if let Some(palette) = &self.palette {
|
||||||
let width = if palette.len() <= 16 {16} else {256};
|
Some(palette.get_image())
|
||||||
Some(Self::get_palette_image(palette, width, 1))
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_palette_image(palette: &Vec<Rgba8Pixel>, width: u32, height: u32) -> slint::Image {
|
struct PaletteInfo {
|
||||||
let mut image_data = SharedPixelBuffer::new(width, height);
|
data: Vec<Rgba8Pixel>,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PaletteInfo {
|
||||||
|
pub fn new(data: Vec<Rgba8Pixel>) -> PaletteInfo {
|
||||||
|
let width = if data.len() <= 16 {16} else {256};
|
||||||
|
PaletteInfo{data, width, height: 1}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_image(&self) -> slint::Image {
|
||||||
|
let mut image_data = SharedPixelBuffer::new(self.width, self.height);
|
||||||
let dst_pixels = image_data.make_mut_slice();
|
let dst_pixels = image_data.make_mut_slice();
|
||||||
|
|
||||||
for (idx, byte) in dst_pixels.iter_mut().enumerate() {
|
for (idx, byte) in dst_pixels.iter_mut().enumerate() {
|
||||||
*byte = if idx < palette.len() {palette[idx]} else {Rgba8Pixel::new(0, 0, 0, 0xFF)};
|
*byte = if idx < self.data.len() {self.data[idx]} else {Rgba8Pixel::new(0, 0, 0, 0xFF)};
|
||||||
}
|
}
|
||||||
|
|
||||||
slint::Image::from_rgba8_premultiplied(image_data.clone())
|
slint::Image::from_rgba8_premultiplied(image_data.clone())
|
||||||
|
|
|
@ -43,6 +43,14 @@ fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) {
|
||||||
fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
|
fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
|
||||||
let gui_elements = gui_elements_ref.borrow();
|
let gui_elements = gui_elements_ref.borrow();
|
||||||
|
|
||||||
|
let logic = logic_ref.clone();
|
||||||
|
gui_elements.file_tab.on_update_palette_size(gui_elements_ref.clone(), move |gui_elements, _main_window, width, height| -> Result<(), Error> {
|
||||||
|
let file_tab = &gui_elements.file_tab;
|
||||||
|
|
||||||
|
file_tab.update_palette(logic.borrow_mut().change_unadded_tim_palette_size(width, height)?);
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
let logic = logic_ref.clone();
|
let logic = logic_ref.clone();
|
||||||
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> Result<(), Error> {
|
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> Result<(), Error> {
|
||||||
let file = FileDialog::new()
|
let file = FileDialog::new()
|
||||||
|
@ -64,7 +72,7 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
||||||
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
|
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
|
||||||
return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
|
return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
|
||||||
}
|
}
|
||||||
file_tab.update_new_load(file_name, image, palette);
|
file_tab.update_new_loaded_file(file_name, image, palette);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -13,16 +13,17 @@ export component MainWindow inherits Window {
|
||||||
callback move_vram_image <=> main_tab.move_vram_image;
|
callback move_vram_image <=> main_tab.move_vram_image;
|
||||||
|
|
||||||
// Convert Image values
|
// Convert Image values
|
||||||
in-out property file_tab_browse_path <=> file_tab.conv_image_path;
|
in-out property file_tab-browse_path <=> file_tab.conv-image_path;
|
||||||
in-out property file_tab_image_data <=> file_tab.conv_image_data;
|
in-out property file_tab-image_data <=> file_tab.conv-image_data;
|
||||||
in-out property file_tab_palette_data <=> file_tab.conv_palette_data;
|
in-out property file_tab-palette_data <=> file_tab.conv-palette_data;
|
||||||
in-out property file_tab_palette_width <=> file_tab.conv_palette_width;
|
in-out property file_tab-palette_width <=> file_tab.conv-palette_width;
|
||||||
in-out property file_tab_palette_height <=> file_tab.conv_palette_height;
|
in-out property file_tab-palette_height <=> file_tab.conv-palette_height;
|
||||||
in-out property file_tab_palette_visible <=> file_tab.conv_palette_enable;
|
in-out property file_tab-palette_visible <=> file_tab.conv-palette_enable;
|
||||||
in-out property file_tab_image_name <=> file_tab.conv_image_name;
|
in-out property file_tab-image_name <=> file_tab.conv-image_name;
|
||||||
in-out property file_tab_enable <=> file_tab.conv_enable_view;
|
in-out property file_tab-enable <=> file_tab.conv-enable_view;
|
||||||
callback file_tab_browse_convert_image <=> file_tab.conv_image_browse_clicked;
|
callback file_tab-update_palette_size <=> file_tab.conv-image_update_palette_size;
|
||||||
callback file_tab_add_convert_image <=> file_tab.conv_image_add_clicked;
|
callback file_tab-browse_convert_image <=> file_tab.conv-image_browse_clicked;
|
||||||
|
callback file_tab-add_convert_image <=> file_tab.conv-image_add_clicked;
|
||||||
|
|
||||||
title: "TIM Tool 0.1.0";
|
title: "TIM Tool 0.1.0";
|
||||||
width: tab_widget.width;
|
width: tab_widget.width;
|
||||||
|
|
|
@ -23,6 +23,7 @@ component ConvertImageWidget inherits Rectangle {
|
||||||
|
|
||||||
callback browse_clicked();
|
callback browse_clicked();
|
||||||
callback add_clicked();
|
callback add_clicked();
|
||||||
|
callback update_palette_size(int, int);
|
||||||
|
|
||||||
background: #D0D0D0;
|
background: #D0D0D0;
|
||||||
|
|
||||||
|
@ -110,15 +111,25 @@ component ConvertImageWidget inherits Rectangle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
VerticalLayout {
|
VerticalLayout {
|
||||||
LineEdit {
|
palette_width_edit := LineEdit {
|
||||||
width: 40pt;
|
width: 40pt;
|
||||||
enabled: root.palette_visible;
|
input-type: number;
|
||||||
text: root.palette_width;
|
enabled: root.palette_visible;
|
||||||
|
text: root.palette_width;
|
||||||
|
|
||||||
|
accepted(text) => {
|
||||||
|
update_palette_size(palette_width_edit.text.to-float(), palette_height_edit.text.to-float());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LineEdit {
|
palette_height_edit := LineEdit {
|
||||||
width: 40pt;
|
width: 40pt;
|
||||||
enabled: root.palette_visible;
|
input-type: number;
|
||||||
text: root.palette_height;
|
enabled: root.palette_visible;
|
||||||
|
text: root.palette_height;
|
||||||
|
|
||||||
|
accepted(text) => {
|
||||||
|
update_palette_size(palette_width_edit.text.to-float(), palette_height_edit.text.to-float());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,19 +172,21 @@ component ConvertImageWidget inherits Rectangle {
|
||||||
}
|
}
|
||||||
|
|
||||||
export component FileTab inherits Rectangle {
|
export component FileTab inherits Rectangle {
|
||||||
in-out property <string> conv_image_path;
|
// TODO: Names are messed up here!
|
||||||
in-out property <string> conv_image_name;
|
in-out property <string> conv-image_path;
|
||||||
in-out property <image> conv_image_data;
|
in-out property <string> conv-image_name;
|
||||||
in-out property <image> conv_palette_data;
|
in-out property <image> conv-image_data;
|
||||||
in-out property <int> conv_palette_width;
|
in-out property <image> conv-palette_data;
|
||||||
in-out property <int> conv_palette_height;
|
in-out property <int> conv-palette_width;
|
||||||
in-out property <bool> conv_palette_enable;
|
in-out property <int> conv-palette_height;
|
||||||
in-out property <bool> conv_enable_view;
|
in-out property <bool> conv-palette_enable;
|
||||||
|
in-out property <bool> conv-enable_view;
|
||||||
|
|
||||||
|
|
||||||
in-out property <State> state;
|
in-out property <State> state;
|
||||||
callback conv_image_browse_clicked;
|
callback conv-image_update_palette_size(int, int);
|
||||||
callback conv_image_add_clicked;
|
callback conv-image_browse_clicked;
|
||||||
|
callback conv-image_add_clicked;
|
||||||
|
|
||||||
x: 0px;
|
x: 0px;
|
||||||
y: 0px;
|
y: 0px;
|
||||||
|
@ -205,21 +218,25 @@ export component FileTab inherits Rectangle {
|
||||||
if root.state == State.Project : ProjectWidget {
|
if root.state == State.Project : ProjectWidget {
|
||||||
}
|
}
|
||||||
if root.state == State.ConvertImage : ConvertImageWidget {
|
if root.state == State.ConvertImage : ConvertImageWidget {
|
||||||
image_path <=> root.conv_image_path;
|
image_path <=> root.conv-image_path;
|
||||||
image_data <=> root.conv_image_data;
|
image_data <=> root.conv-image_data;
|
||||||
palette_data <=> root.conv_palette_data;
|
palette_data <=> root.conv-palette_data;
|
||||||
palette_width <=> root.conv_palette_width;
|
palette_width <=> root.conv-palette_width;
|
||||||
palette_height <=> root.conv_palette_height;
|
palette_height <=> root.conv-palette_height;
|
||||||
palette_visible <=> root.conv_palette_enable;
|
palette_visible <=> root.conv-palette_enable;
|
||||||
image_name <=> root.conv_image_name;
|
image_name <=> root.conv-image_name;
|
||||||
enable_view <=> root.conv_enable_view;
|
enable_view <=> root.conv-enable_view;
|
||||||
|
|
||||||
|
update_palette_size(width, height) => {
|
||||||
|
root.conv-image_update_palette_size(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
browse_clicked => {
|
browse_clicked => {
|
||||||
root.conv_image_browse_clicked();
|
root.conv-image_browse_clicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
add_clicked => {
|
add_clicked => {
|
||||||
root.conv_image_add_clicked();
|
root.conv-image_add_clicked();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue