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