-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
toolkit库引入基本的事件处理框架 资源管理器初步实现交互 (#11)
- Loading branch information
1 parent
b026285
commit 8a41b76
Showing
12 changed files
with
363 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,164 @@ | ||
use self::asset_item::AssetItem; | ||
use crate::starry_toolkit::traits::focus::Focus; | ||
use starry_client::base::color::Color; | ||
use starry_server::base::image::Image as ImageResource; | ||
use starry_server::core::{SCREEN_HEIGHT, SCREEN_WIDTH}; | ||
use starry_toolkit::{ | ||
base::{panel::Panel, rect::Rect}, | ||
layout::grid::{Grid, GridArrangeType}, | ||
traits::enter::Enter, | ||
widgets::{image::Image, widget_add_child}, | ||
}; | ||
use std::{cell::RefCell, fs, sync::Arc}; | ||
|
||
use self::asset_item::AssetItem; | ||
use crate::starry_server::base::image::Image as ImageResource; | ||
use std::{collections::BTreeMap, fs, sync::Arc}; | ||
|
||
pub mod asset_item; | ||
|
||
const DESKTOP_BG_PATH: &[u8] = include_bytes!("../resource/desktop_bg.png"); | ||
|
||
pub struct AssetViewer { | ||
pub struct AssetManager { | ||
cur_path: String, | ||
asset_grid: RefCell<Arc<Grid>>, | ||
asset_grid: Arc<Grid>, | ||
items: BTreeMap<(usize, usize), Arc<AssetItem>>, | ||
panel: Panel, | ||
} | ||
|
||
impl AssetViewer { | ||
impl AssetManager { | ||
pub fn new() -> Self { | ||
AssetViewer { | ||
AssetManager { | ||
cur_path: String::from("/"), | ||
asset_grid: RefCell::new(Grid::new()), | ||
asset_grid: Grid::new(), | ||
items: BTreeMap::new(), | ||
panel: Panel::new( | ||
Rect::new(0, 0, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32), | ||
"Title", | ||
Color::rgb(0, 0, 0), | ||
), | ||
} | ||
} | ||
|
||
pub fn init(&self) { | ||
let grid = self.asset_grid.borrow(); | ||
pub fn init(&mut self) { | ||
let grid = self.asset_grid.clone(); | ||
grid.set_upper_limit(5); | ||
grid.set_space(20, 20); | ||
grid.set_arrange_type(GridArrangeType::Horizontal); | ||
|
||
let self_ptr = self as *mut AssetManager; | ||
grid.set_enter_callback(move |grid, char, redraw| { | ||
if char == ' ' { | ||
let asset_manager: &mut AssetManager = unsafe { &mut *self_ptr }; | ||
|
||
if let Some(item) = asset_manager.items.get(&grid.focused_id.get().unwrap()) { | ||
asset_manager.cur_path.push_str(&item.file_path.borrow()); | ||
asset_manager.cur_path.push_str(&"/"); | ||
asset_manager.refresh(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
let row_offset: i32 = match char { | ||
'a' => 0, | ||
'A' => 0, | ||
'd' => 0, | ||
'D' => 0, | ||
'w' => -1, | ||
'W' => -1, | ||
's' => 1, | ||
'S' => 1, | ||
_ => 0, | ||
}; | ||
|
||
let col_offset: i32 = match char { | ||
'a' => -1, | ||
'A' => -1, | ||
'd' => 1, | ||
'D' => 1, | ||
'w' => 0, | ||
'W' => 0, | ||
's' => 0, | ||
'S' => 0, | ||
_ => 0, | ||
}; | ||
|
||
if row_offset == 0 && col_offset == 0 { | ||
return; | ||
} | ||
let mut nxt_row = grid.focused_id.get().unwrap().0 as i32 + row_offset; | ||
let mut nxt_col = grid.focused_id.get().unwrap().1 as i32 + col_offset; | ||
loop { | ||
if nxt_row < 0 | ||
|| nxt_row >= grid.max_row.get() as i32 | ||
|| nxt_col < 0 | ||
|| nxt_col >= grid.max_column.get() as i32 | ||
{ | ||
return; | ||
} | ||
|
||
if grid | ||
.elements | ||
.borrow() | ||
.contains_key(&(nxt_row as usize, nxt_col as usize)) | ||
{ | ||
break; | ||
} | ||
|
||
nxt_row += row_offset; | ||
nxt_col += col_offset; | ||
} | ||
|
||
grid.focus( | ||
grid.elements | ||
.borrow() | ||
.get(&(nxt_row as usize, nxt_col as usize)) | ||
.unwrap(), | ||
); | ||
grid.focused_id | ||
.set(Some((nxt_row as usize, nxt_col as usize))); | ||
|
||
*redraw = true; | ||
}); | ||
|
||
self.panel.add_child(&Image::from_image( | ||
ImageResource::from_path(DESKTOP_BG_PATH).unwrap(), | ||
)); | ||
|
||
self.panel.add_child(&(self.asset_grid)); | ||
} | ||
|
||
pub fn refresh(&self) { | ||
pub fn refresh(&mut self) { | ||
self.items.clear(); | ||
self.asset_grid.clear(); | ||
|
||
// 读取目录中的文件列表 | ||
if let Ok(entries) = fs::read_dir(&self.cur_path) { | ||
for entry in entries { | ||
if let Ok(item) = entry { | ||
let item = AssetItem::new( | ||
let asset_item = AssetItem::new( | ||
item.file_name().to_str().unwrap(), | ||
item.metadata().unwrap().is_dir(), | ||
); | ||
self.asset_grid.borrow_mut().add(&item); | ||
widget_add_child(self.asset_grid.borrow().clone(), item); | ||
let (row, col) = self.asset_grid.add(&asset_item); | ||
self.items.insert((row, col), asset_item.clone()); | ||
widget_add_child(self.asset_grid.clone(), asset_item); | ||
} | ||
} | ||
} else { | ||
println!( | ||
"[Error] AssetManager failed to read dir {:?}", | ||
self.cur_path | ||
); | ||
} | ||
|
||
// TODO 代码整理 | ||
let grid = self.asset_grid.borrow_mut(); | ||
let elements = grid.elements.borrow(); | ||
if let Some(widget) = elements.get(&(0, 0)) { | ||
let grid = self.asset_grid.clone(); | ||
if let Some(widget) = grid.elements.borrow().get(&(0, 0)) { | ||
grid.focused_id.set(Some((0, 0))); | ||
grid.focus(widget); | ||
} | ||
} | ||
|
||
pub fn draw(&self) { | ||
let panel = Panel::new( | ||
Rect::new(0, 0, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32), | ||
"Title", | ||
Color::rgb(0, 0, 0), | ||
); | ||
|
||
panel.add_child(&Image::from_image( | ||
ImageResource::from_path(DESKTOP_BG_PATH).unwrap(), | ||
)); | ||
self.panel.draw(); | ||
} | ||
|
||
panel.add_child(&(self.asset_grid.borrow())); | ||
panel.draw(); | ||
pub fn exec(&mut self) { | ||
self.panel.exec(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,40 @@ | ||
extern crate starry_client; | ||
extern crate starry_server; | ||
extern crate starry_toolkit; | ||
extern crate termios; | ||
|
||
pub mod asset_manager; | ||
|
||
use std::io; | ||
use std::os::unix::io::AsRawFd; | ||
use termios::{tcsetattr, Termios}; | ||
|
||
// TODO | ||
#[allow(dead_code)] | ||
pub fn set_terminal() -> io::Result<()> { | ||
let stdin = io::stdin().as_raw_fd(); | ||
let stored_settings = Termios::from_fd(stdin)?; | ||
|
||
let mut new_settings = stored_settings.clone(); | ||
new_settings.c_lflag &= !(termios::ICANON) & !(termios::ECHO); | ||
new_settings.c_cc[termios::VMIN] = 1; | ||
new_settings.c_cc[termios::VTIME] = 0; | ||
|
||
tcsetattr(stdin, termios::TCSANOW, &new_settings)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// TODO | ||
#[allow(dead_code)] | ||
pub fn reset_terminal() -> io::Result<()> { | ||
let stdin = io::stdin().as_raw_fd(); | ||
let stored_settings = Termios::from_fd(stdin)?; | ||
|
||
let mut new_settings = stored_settings.clone(); | ||
new_settings.c_lflag &= (termios::ICANON) & (termios::ECHO); | ||
|
||
tcsetattr(stdin, termios::TCSANOW, &new_settings)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use starry_apps::asset_manager::code::AssetViewer; | ||
use starry_apps::asset_manager::code::AssetManager; | ||
|
||
fn main() { | ||
let viewer = AssetViewer::new(); | ||
// set_terminal(); | ||
|
||
// TODO | ||
let mut viewer = AssetManager::new(); | ||
viewer.init(); | ||
viewer.refresh(); | ||
viewer.draw(); | ||
|
||
loop {} | ||
viewer.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use super::vector2::Vector2; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub enum Event { | ||
Init, | ||
|
||
Mouse { | ||
point: Vector2, | ||
left_button: bool, | ||
middle_button: bool, | ||
right_button: bool, | ||
}, | ||
|
||
KeyPressed { | ||
character: Option<char>, | ||
scancode: u8, | ||
}, | ||
|
||
KeyReleased { | ||
character: Option<char>, | ||
scancode: u8, | ||
}, | ||
|
||
Scroll { | ||
x: i32, | ||
y: i32, | ||
}, | ||
|
||
Resize { | ||
width: u32, | ||
height: u32, | ||
}, | ||
|
||
Unknown, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod event; | ||
pub mod panel; | ||
pub mod rect; | ||
pub mod vector2; |
Oops, something went wrong.