Compare commits
No commits in common. "2af28b72c88e6af8a045f38d9d8a52f262654c8a" and "2398053c61edf60bc2f0e5f456709ff993087712" have entirely different histories.
2af28b72c8
...
2398053c61
|
@ -2,11 +2,10 @@ use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage};
|
|||
use super::{GUIElementsRef, MainWindowRef};
|
||||
|
||||
use slint::Model;
|
||||
use std::{rc::Rc, sync::{Arc, Mutex}};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct MainTab {
|
||||
main_window: MainWindowRef,
|
||||
mtx: Arc<Mutex<i32>>,
|
||||
vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
|
||||
vram_image_list: Rc<slint::VecModel<VRAMImage>>,
|
||||
}
|
||||
|
@ -20,39 +19,23 @@ impl MainTab {
|
|||
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into());
|
||||
main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
|
||||
|
||||
MainTab{main_window, mtx: Arc::new(Mutex::new(0)), vram_file_list, vram_image_list}
|
||||
MainTab{main_window, vram_file_list, vram_image_list}
|
||||
}
|
||||
|
||||
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, image_palette: Option<slint::Image>) {
|
||||
let add_new_image = |file_name: &String, image: slint::Image| {
|
||||
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image) {
|
||||
let vram_image = VRAMImage{
|
||||
img: image,
|
||||
x: 0,
|
||||
y: 0,
|
||||
palette_count: 0,
|
||||
is_palette: false,
|
||||
y: 0
|
||||
};
|
||||
|
||||
self.vram_file_list.push(slint::StandardListViewItem::from(file_name.as_str()));
|
||||
self.vram_image_list.push(vram_image);
|
||||
};
|
||||
|
||||
let _lock = self.mtx.lock().unwrap();
|
||||
add_new_image(file_name, image);
|
||||
}
|
||||
|
||||
pub fn remove_vram_file(&mut self, idx: usize) -> bool {
|
||||
let _lock = self.mtx.lock().unwrap();
|
||||
|
||||
if let Some(element) = self.vram_image_list.iter().skip(idx).next() {
|
||||
if element.is_palette {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_vram_file(&mut self, idx: usize) {
|
||||
self.vram_file_list.remove(idx);
|
||||
self.vram_image_list.remove(idx);
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) {
|
||||
|
|
|
@ -16,11 +16,7 @@ pub const VRAM_HEIGHT:usize = 512;
|
|||
type MainWindowRef = Rc<RefCell<MainWindow>>;
|
||||
type GUIElementsRef = Rc<RefCell<GUIElements>>;
|
||||
|
||||
pub fn display_information(title: impl Into<String>, text: impl Into<String>) {
|
||||
MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Info).set_description(text).show();
|
||||
}
|
||||
|
||||
pub fn display_error(title: impl Into<String>, text: impl Into<String>) {
|
||||
fn display_error(title: &str, text: &String) {
|
||||
MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Error).set_description(text).show();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ impl Logic {
|
|||
Logic{unadded_tim: None}
|
||||
}
|
||||
|
||||
pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<(Image, Option<Image>), Error> {
|
||||
pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<Image, Error> {
|
||||
let tim_info = TIMInfo::from_image(path)?;
|
||||
let image = tim_info.get_slint_images();
|
||||
|
||||
|
@ -22,7 +22,7 @@ impl Logic {
|
|||
Ok(image)
|
||||
}
|
||||
|
||||
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<(Image, Option<Image>), Error> {
|
||||
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<Image, Error> {
|
||||
if let Some(unadded_tim) = &self.unadded_tim {
|
||||
let image = unadded_tim.get_slint_images();
|
||||
|
||||
|
|
|
@ -9,10 +9,10 @@ pub struct TIMInfo {
|
|||
|
||||
impl TIMInfo {
|
||||
pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> {
|
||||
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>, load_alpha: bool) -> Option<Rgba8Pixel> {
|
||||
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>) -> Option<Rgba8Pixel> {
|
||||
Some(Rgba8Pixel::new(
|
||||
*iter.next()?, *iter.next()?, *iter.next()?,
|
||||
if load_alpha {*iter.next()?} else {0xFF}))
|
||||
0xFF))
|
||||
}
|
||||
|
||||
let mut reader = png::Decoder::new(File::open(path)?).read_info().or_else(|error| {Err(Error::from_error(error))})?;
|
||||
|
@ -20,20 +20,19 @@ impl TIMInfo {
|
|||
|
||||
let mut buffer = vec![0; reader.output_buffer_size()];
|
||||
let frame_info = reader.next_frame(&mut buffer).or_else(|error| {Err(Error::from_error(error))})?;
|
||||
let bytes_per_pixel = info.bytes_per_pixel();
|
||||
let bit_depth = frame_info.bit_depth;
|
||||
let mut image_data = SharedPixelBuffer::new(frame_info.width, frame_info.height);
|
||||
let mut dst_pixels = image_data.make_mut_slice();
|
||||
|
||||
if info.color_type == png::ColorType::Indexed {
|
||||
let palette = info.palette.ok_or(Error::from_str("Found indexed PNG without palette"))?;
|
||||
let mut palette_colors = Vec::new();
|
||||
let mut iter = palette.iter();
|
||||
|
||||
while let Some(color) = make_color(&mut iter, false) {
|
||||
while let Some(color) = make_color(&mut iter) {
|
||||
palette_colors.push(color);
|
||||
}
|
||||
|
||||
let mut dst_pixels = image_data.make_mut_slice();
|
||||
for byte in buffer.into_iter() {
|
||||
match bit_depth {
|
||||
png::BitDepth::Four => {
|
||||
|
@ -45,32 +44,19 @@ impl TIMInfo {
|
|||
dst_pixels[0] = palette_colors[byte as usize];
|
||||
dst_pixels = &mut dst_pixels[1..];
|
||||
},
|
||||
_ => {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"));}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(TIMInfo{image_data, palette: Some(palette_colors)})
|
||||
}
|
||||
|
||||
else {
|
||||
if bytes_per_pixel != 3 && bytes_per_pixel != 4 {
|
||||
return Err(Error::from_text(format!("Image has {} bytes per pixel, but only 3 and 4 bytes per pixel are supported", bytes_per_pixel)));
|
||||
}
|
||||
|
||||
let mut byte_iter = buffer.iter();
|
||||
while let Some(color) = make_color(&mut byte_iter, bytes_per_pixel == 4) {
|
||||
match bit_depth {
|
||||
png::BitDepth::Eight => {
|
||||
dst_pixels[0] = color;
|
||||
dst_pixels = &mut dst_pixels[1..];
|
||||
}
|
||||
_ => {return Err(Error::from_str("Only 8bit color depth are supported for direct color images"));}
|
||||
}
|
||||
}
|
||||
Ok(TIMInfo{image_data, palette: None})
|
||||
Err(Error::not_implemented("Support for non indexed images"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_slint_images(&self) -> (slint::Image, Option<slint::Image>) {
|
||||
(slint::Image::from_rgba8_premultiplied(self.image_data.clone()), None)
|
||||
pub fn get_slint_images(&self) -> slint::Image {
|
||||
slint::Image::from_rgba8_premultiplied(self.image_data.clone())
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
mod gui;
|
||||
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information};
|
||||
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT};
|
||||
use rfd::FileDialog;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use slint::SharedString;
|
||||
|
@ -33,9 +33,7 @@ fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) {
|
|||
|
||||
gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| {
|
||||
if idx >= 0 {
|
||||
if !main_tab.remove_vram_file(idx as usize) {
|
||||
display_information("Removing VRAM file", "Can not remove palette. Delete image instead");
|
||||
}
|
||||
main_tab.remove_vram_file(idx as usize);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -58,7 +56,7 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
let file_tab = &gui_elements.file_tab;
|
||||
let file_name = if let Some(name) = file.file_name() {Some(name.to_string_lossy().to_string())} else {None};
|
||||
|
||||
let (image, palette_image) = logic.borrow_mut().set_unadded_tim(&file)?;
|
||||
let image = logic.borrow_mut().set_unadded_tim(&file)?;
|
||||
|
||||
let img_size = image.size();
|
||||
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
|
||||
|
@ -76,9 +74,8 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
let file_tab = &gui_elements.file_tab;
|
||||
|
||||
let file_name = file_tab.get_file_name();
|
||||
let (image, palette_image) = logic.borrow_mut().add_unadded_tim_as(&file_name)?;
|
||||
main_tab.add_new_vram_file(&file_name, logic.borrow_mut().add_unadded_tim_as(&file_name)?);
|
||||
|
||||
main_tab.add_new_vram_file(&file_name, image, palette_image);
|
||||
file_tab.clear_load();
|
||||
main_window.invoke_change_to_main();
|
||||
Ok(())
|
||||
|
|
|
@ -5,8 +5,6 @@ struct VRAMImage {
|
|||
img: image,
|
||||
x: int,
|
||||
y: int,
|
||||
palette_count: int,
|
||||
is_palette: bool,
|
||||
}
|
||||
|
||||
export component MainTab inherits Rectangle {
|
||||
|
|
Loading…
Reference in New Issue