From 2993fa661ac96f6804e3dd4866b6cb22ea4ee80a Mon Sep 17 00:00:00 2001 From: jaby Date: Mon, 3 Feb 2025 22:35:15 +0000 Subject: [PATCH] Get moving tiles sorted --- src/Tools/tim_tool/src/main.rs | 59 ++++++++++++++++++++---- src/Tools/tim_tool/ui/app-window.slint | 2 + src/Tools/tim_tool/ui/tab/main-tab.slint | 43 +++++++++++++++-- 3 files changed, 92 insertions(+), 12 deletions(-) diff --git a/src/Tools/tim_tool/src/main.rs b/src/Tools/tim_tool/src/main.rs index fb352245..f4c192b3 100644 --- a/src/Tools/tim_tool/src/main.rs +++ b/src/Tools/tim_tool/src/main.rs @@ -2,29 +2,67 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use slint::Model; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, path::Path, rc::Rc}; slint::include_modules!(); struct GUIElements { - tab_vram_file_list: Rc> + vram_file_list: Rc>, + vram_image_list: Rc> } impl GUIElements { pub fn new(main_window: &MainWindow) -> GUIElements { - let tab_vram_file_list:Vec = main_window.get_main_tab_vram_file_list().iter().collect(); - let tab_vram_file_list = Rc::new(slint::VecModel::from(tab_vram_file_list)); + let vram_file_list:Vec = main_window.get_main_tab_vram_file_list().iter().collect(); + let vram_file_list = Rc::new(slint::VecModel::from(vram_file_list)); - main_window.set_main_tab_vram_file_list(tab_vram_file_list.clone().into()); - GUIElements{tab_vram_file_list} + let vram_image_list = Rc::new(slint::VecModel::from(Vec::::new())); + + main_window.set_main_tab_vram_file_list(vram_file_list.clone().into()); + main_window.set_main_tab_vram_images(vram_image_list.clone().into()); + GUIElements{vram_file_list, vram_image_list} } pub fn add_new_vram_file(&mut self, file: &str) { - self.tab_vram_file_list.push(slint::StandardListViewItem::from(file)); + let vram_image = VRAMImage{ + img: slint::Image::load_from_path(Path::new("/home/jaby/Desktop/PSX_Dev/jabyengine/examples/PoolBox/assets/IMG_6921.png")).unwrap(), + x: 0, + y: 0 + }; + + self.vram_file_list.push(slint::StandardListViewItem::from(file)); + self.vram_image_list.push(vram_image); } pub fn remove_vram_file(&mut self, idx: usize) { - self.tab_vram_file_list.remove(idx); + self.vram_file_list.remove(idx); + self.vram_image_list.remove(idx); + } + + pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) { + if let Some(mut vram_info) = self.vram_image_list.row_data(idx) { + vram_info.x += dx; + vram_info.y += dy; + + if vram_info.x < 0 { + vram_info.x = 0; + } + + if vram_info.y < 0 { + vram_info.y = 0; + } + + let (vram_img_width, vram_img_height) = (vram_info.img.size().width as i32, vram_info.img.size().height as i32); + if (vram_info.x + vram_img_width) > 1024 { + vram_info.x = 1024 - vram_img_width; + } + + if (vram_info.y + vram_img_width) > 512 { + vram_info.y = 512 - vram_img_height; + } + + self.vram_image_list.set_row_data(idx, vram_info); + } } } @@ -42,5 +80,10 @@ fn main() -> Result<(), slint::PlatformError> { gui_elements_cloned.borrow_mut().remove_vram_file(idx as usize); } }); + let gui_elements_cloned = gui_elements.clone(); + main_window.on_move_vram_image(move |idx: i32, dx: i32, dy: i32| { + gui_elements_cloned.borrow_mut().move_vram_image(idx as usize, dx, dy); + }); + main_window.run() } \ No newline at end of file diff --git a/src/Tools/tim_tool/ui/app-window.slint b/src/Tools/tim_tool/ui/app-window.slint index 060a7444..fb165e95 100644 --- a/src/Tools/tim_tool/ui/app-window.slint +++ b/src/Tools/tim_tool/ui/app-window.slint @@ -5,9 +5,11 @@ import { TabWidget } from "std-widgets.slint"; export component MainWindow inherits Window { in-out property main_tab_vram_file_list <=> tab.vram_files; + in-out property main_tab_vram_images <=> tab.vram_images; callback main_tab_add_file_clicked <=> tab.add_file_clicked; callback main_tab_remove_file_clicked <=> tab.remove_file_clicked; + callback move_vram_image <=> tab.move_vram_image; title: "TIM Tool 0.1.0"; width: tab_widget.width; diff --git a/src/Tools/tim_tool/ui/tab/main-tab.slint b/src/Tools/tim_tool/ui/tab/main-tab.slint index 76a1c739..10a44f31 100644 --- a/src/Tools/tim_tool/ui/tab/main-tab.slint +++ b/src/Tools/tim_tool/ui/tab/main-tab.slint @@ -2,9 +2,9 @@ import { VRAMArea } from "../vram-components.slint"; import { Button, ComboBox, GroupBox, StandardListView } from "std-widgets.slint"; struct VRAMImage { - img: image, - x: int, - y: int, + img: image, + x: int, + y: int, } export component MainTab inherits Rectangle { @@ -13,6 +13,7 @@ export component MainTab inherits Rectangle { callback add_file_clicked(); callback remove_file_clicked(int); + callback move_vram_image(int, int, int); width: group.width + group.x*2; height: group.height + group.y*2 + 32px; @@ -40,9 +41,43 @@ export component MainTab inherits Rectangle { for vram_image[i] in root.vram_images: VRAMArea { x: root.get_border_width()*1px; y: root.get_border_width()*1px; - img: vram-image.img; + img: vram-image.img; img_x: vram-image.x; img_y: vram-image.y; + TouchArea { + x: (parent.img_x + self.lines_crossed_x())*1px; + y: (parent.img_y + self.lines_crossed_y())*1px; + width: (parent.img.width + self.lines_crossed_width())*1px; + height: (parent.img.height + self.lines_crossed_height())*1px; + mouse-cursor: grab; + moved => { + self.mouse-cursor = MouseCursor.grabbing; + root.move_vram_image(i, (self.mouse-x - self.pressed-x)/1px, (self.mouse-y - self.pressed-y)/1px); + + } + pointer-event(event) => { + if event.kind == PointerEventKind.up { + self.mouse-cursor = MouseCursor.grab; + } + } + + // Thanks to Cody the white tiger + function lines_crossed_x() -> int { + return parent.img_x/64; + } + + function lines_crossed_y() -> int { + return parent.img_y/256; + } + + function lines_crossed_width() -> int { + return ((parent.img_x + parent.img.width)/64) - self.lines_crossed_x(); + } + + function lines_crossed_height() -> int { + return ((parent.img_y + parent.img.height)/256) - self.lines_crossed_y(); + } + } } } HorizontalLayout {