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
use std::{any::Any, convert::TryFrom};
use dioxus::prelude::*;
use rpgx::{engine::Engine, library::Library};
#[derive(PartialEq, Props, Clone)]
pub struct PawnProps {
pub engine: Signal<Engine>,
pub library: Signal<Library<Box<dyn Any>>>,
pub square_size: u32,
}
#[allow(non_snake_case)]
pub fn Pawn(props: PawnProps) -> Element {
let engine = props.engine.read();
if let Some(scene) = engine.get_active_scene() {
let pawn = scene.pawn.as_ref().unwrap();
let pawn_pos = pawn.pointer;
let library = props.library.read();
let default_texture = String::new();
let pawn_texture = library
.get_by_id(pawn.texture_id)
.and_then(|boxed| boxed.downcast_ref::<String>())
.unwrap_or(&default_texture);
// Safely calculate pixel position
let left = pawn_pos
.x
.checked_mul(props.square_size)
.and_then(|v| i32::try_from(v).ok())
.unwrap_or(0);
let top = pawn_pos
.y
.checked_mul(props.square_size)
.and_then(|v| i32::try_from(v).ok())
.map(|v| v.saturating_sub(props.square_size as i32))
.unwrap_or(0);
rsx! {
div {
id: "pawn",
class: "pawn",
style: format!(
"position: absolute; \
left: {}px; \
top: {}px; \
background-image: url({}); \
background-size: cover; \
background-position: center center; \
z-index: 100; \
width: {}px; \
height: {}px; \
transition: all 0.1s;",
left,
top,
pawn_texture,
props.square_size,
props.square_size * 2,
),
}
}
} else {
rsx! {
div {}
}
}
}