mirror of
https://github.com/Astatin3/IntroToWebAuthoring.git
synced 2026-06-09 00:28:00 -06:00
Start work on views system
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
#[wasm_bindgen(module = "/js/index.js")]
|
||||
extern "C" {
|
||||
fn cursor(name: &str);
|
||||
}
|
||||
|
||||
pub fn set_cursor(c: Cursor) {
|
||||
cursor(c.value());
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub enum Cursor {
|
||||
Alias,
|
||||
AllScroll,
|
||||
Auto,
|
||||
Cell,
|
||||
ColResize,
|
||||
ContextMenu,
|
||||
Copy,
|
||||
Crosshair,
|
||||
Default,
|
||||
EResize,
|
||||
EWResize,
|
||||
Grab,
|
||||
Grabbing,
|
||||
Help,
|
||||
Move,
|
||||
NResize,
|
||||
NEResize,
|
||||
NESWResize,
|
||||
NSResize,
|
||||
NWResize,
|
||||
NWSEResize,
|
||||
NoDrop,
|
||||
None,
|
||||
NotAllowed,
|
||||
Pointer,
|
||||
Progress,
|
||||
RowResize,
|
||||
SResize,
|
||||
SEResize,
|
||||
SWResize,
|
||||
Text,
|
||||
WResize,
|
||||
Wait,
|
||||
ZoomIn,
|
||||
ZoomOut,
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
fn value(&self) -> &str {
|
||||
match self {
|
||||
Cursor::Alias => "alias",
|
||||
Cursor::AllScroll => "all-scroll",
|
||||
Cursor::Auto => "auto",
|
||||
Cursor::Cell => "cell",
|
||||
Cursor::ColResize => "col-resize",
|
||||
Cursor::ContextMenu => "context-menu",
|
||||
Cursor::Copy => "copy",
|
||||
Cursor::Crosshair => "crosshair",
|
||||
Cursor::Default => "default",
|
||||
Cursor::EResize => "e-resize",
|
||||
Cursor::EWResize => "ew-resize",
|
||||
Cursor::Grab => "grab",
|
||||
Cursor::Grabbing => "grabbing",
|
||||
Cursor::Help => "help",
|
||||
Cursor::Move => "move",
|
||||
Cursor::NResize => "n-resize",
|
||||
Cursor::NEResize => "ne-resize",
|
||||
Cursor::NESWResize => "nesw-resize",
|
||||
Cursor::NSResize => "ns-resize",
|
||||
Cursor::NWResize => "nw-resize",
|
||||
Cursor::NWSEResize => "nwse-resize",
|
||||
Cursor::NoDrop => "no-drop",
|
||||
Cursor::None => "none",
|
||||
Cursor::NotAllowed => "not-allowed",
|
||||
Cursor::Pointer => "pointer",
|
||||
Cursor::Progress => "progress",
|
||||
Cursor::RowResize => "row-resize",
|
||||
Cursor::SResize => "s-resize",
|
||||
Cursor::SEResize => "se-resize",
|
||||
Cursor::SWResize => "sw-resize",
|
||||
Cursor::Text => "text",
|
||||
Cursor::WResize => "w-resize",
|
||||
Cursor::Wait => "wait",
|
||||
Cursor::ZoomIn => "zoom-in",
|
||||
Cursor::ZoomOut => "zoom-out",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
mod cursors;
|
||||
|
||||
pub use cursors::{Cursor, set_cursor};
|
||||
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
use crate::{
|
||||
log,
|
||||
render::Renderer,
|
||||
views::{View, default_view},
|
||||
};
|
||||
|
||||
pub trait Activity {
|
||||
fn new() -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
fn update(&mut self, dt: f32);
|
||||
fn draw(&self, renderer: &mut Renderer, state: &AppState);
|
||||
fn l_click(&mut self, renderer: &mut Renderer, state: &AppState);
|
||||
fn mouse_move(&mut self, renderer: &mut Renderer, state: &AppState);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct App {
|
||||
// pub(crate) activities: Vec<Box<dyn Activity>>,
|
||||
pub(crate) root_view: Option<Box<dyn View>>,
|
||||
pub(crate) renderer: Renderer,
|
||||
// pub(crate) current_activity: Option<usize>,
|
||||
pub(crate) state: AppState,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub(crate) mouse_x: f32,
|
||||
pub(crate) mouse_y: f32,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
AppState {
|
||||
mouse_x: 0.0,
|
||||
mouse_y: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(renderer: Renderer) -> Self {
|
||||
App {
|
||||
root_view: Some(default_view()),
|
||||
renderer,
|
||||
// current_activity: Some(0),
|
||||
state: AppState::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self) {
|
||||
if let Some(view) = &self.root_view {
|
||||
let (width, height) = (
|
||||
self.renderer.actual_width.clone() as f32,
|
||||
self.renderer.actual_height.clone() as f32,
|
||||
);
|
||||
|
||||
view.draw(&mut self.renderer, 0., 0., width, height);
|
||||
self.renderer.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// App events
|
||||
impl App {
|
||||
pub fn resize(&mut self, width: u32, height: u32) {
|
||||
self.renderer.resize(width, height);
|
||||
self.draw();
|
||||
}
|
||||
|
||||
pub fn mouse_move(&mut self, x: f32, y: f32) {
|
||||
self.state.mouse_x = x;
|
||||
self.state.mouse_y = y;
|
||||
|
||||
// if let Some(current_activity) = self.current_activity {
|
||||
// self.activities[current_activity].mouse_move(&mut self.renderer, &self.state);
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn l_click(&mut self, x: f32, y: f32) {
|
||||
self.state.mouse_x = x;
|
||||
self.state.mouse_y = y;
|
||||
|
||||
// if let Some(current_activity) = self.current_activity {
|
||||
// self.activities[current_activity].l_click(&mut self.renderer, &self.state);
|
||||
// }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user