1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
mod renderer;
use std::cell::RefCell;
use std::any::Any;
use wasm_bindgen::prelude::*;
use crate::core::State;
use crate::span::SpanTrait;
use crate::component::Event;
use dragon::*;
pub struct WorldSpan {
pub name: String,
text: String,
x: f64,
y: f64,
w: f64,
h: f64,
pub width: f32,
pub height: f32,
pub world: World,
state: State,
font_cache: RefCell<Option<String>>,
}
impl WorldSpan {
pub fn new(
state: State,
ctx: web_sys::CanvasRenderingContext2d,
name: &str,
text: &str,
width: f32,
height: f32
) -> Self {
let world = World::new();
world.attach_default_camera();
let renderer = renderer::RenderingSystem::new(world.state.clone(), ctx);
world.state.register_renderer("renderer", renderer);
Self {
name: name.to_string(),
text: text.to_string(),
x: 0.,
y: 0.,
w: 0.,
h: 0.,
width,
height,
world,
state,
font_cache: RefCell::new(None),
}
}
pub fn set_text(&mut self, text: &str) {
self.text = text.to_string();
}
fn draw_outline(&self, ctx: &web_sys::CanvasRenderingContext2d) {
ctx.set_stroke_style(&JsValue::from_str("white"));
ctx.stroke_rect(self.x, self.y, self.w, self.h);
}
}
impl SpanTrait for WorldSpan {
fn get_name(&self) -> &str {
&self.name
}
fn dispatch_event(&mut self, _ev: &mut Event) {
}
fn dispath(&mut self, data: Box<dyn Any>) {
if let Ok(text) = data.downcast::<String>() {
self.text = text.to_string();
}
}
fn render_tick(&self, _ctx: &web_sys::CanvasRenderingContext2d) {
self.world.state.render_tick();
}
fn tick(&mut self) {
self.world.state.tick();
}
fn on_resize(&mut self, left: f64, top: f64, right: f64, bottom: f64) -> (f64, f64, bool) {
self.x = left;
self.y = top;
self.w = self.width as f64 * (right - left);
self.h = self.height as f64 * (bottom - top);
let mut font = self.font_cache.borrow_mut();
*font = None;
let mut systems = self.world.state.renderer_store.borrow_mut();
let renderer = systems.get_mut("renderer").unwrap();
renderer.dispatch(Box::new((self.x, self.y, self.w, self.h)));
(0., 0., true)
}
}