-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdraw_args.rs
More file actions
141 lines (125 loc) · 3.02 KB
/
draw_args.rs
File metadata and controls
141 lines (125 loc) · 3.02 KB
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
131
132
133
134
135
136
137
138
139
140
141
use crate::engine::*;
use wasm_bindgen::JsValue;
pub enum DrawArgs {
Image(DrawImageArgs),
String(DrawStringArgs),
GradientBox(DrawGradientBoxArgs),
}
impl DrawArgs {
pub fn get_depth(&self) -> &F1 {
return match self {
DrawArgs::Image(image_args) => &image_args.depth,
DrawArgs::String(string_args) => &string_args.depth,
DrawArgs::GradientBox(gradient_box_args) => &gradient_box_args.depth,
};
}
}
pub enum DrawSource {
Canvas(Rc<web_sys::HtmlCanvasElement>),
Texture(Rc<Texture>),
}
pub struct DrawImageArgs {
pub source: DrawSource,
pub position: F2,
pub size: F2,
pub depth: F1,
pub optional: DrawImageOptionalArgs,
}
pub struct DrawImageOptionalArgs {
pub color: DrawColor,
pub rotation: F1,
pub anchor_point: F2,
pub opacity: F1,
pub partial_region_offset: F2,
pub partial_region_size: F2,
pub composite_operation: Option<String>,
pub subpixel_precision: bool,
}
pub const PARTIAL_REGION_OFFSET_DEFAULT: F2 = F2 { x: 0.0, y: 0.0 };
pub const PARTIAL_REGION_SIZE_DEFAULT: F2 = F2 { x: 1.0, y: 1.0 };
impl Default for DrawImageOptionalArgs {
fn default() -> DrawImageOptionalArgs {
return DrawImageOptionalArgs {
color: DrawColor::default(),
rotation: 0.0,
anchor_point: F2 { x: 0.5, y: 0.5 },
opacity: 1.0,
partial_region_offset: PARTIAL_REGION_OFFSET_DEFAULT,
partial_region_size: PARTIAL_REGION_SIZE_DEFAULT,
composite_operation: None,
subpixel_precision: false,
};
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct DrawColor {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl DrawColor {
pub fn new(r: &u8, g: &u8, b: &u8) -> DrawColor {
return DrawColor {
r: *r,
g: *g,
b: *b,
};
}
pub fn as_rgb_js_value(&self) -> JsValue {
return JsValue::from_str(&format!("rgb({}, {}, {})", self.r, self.g, self.b));
}
}
impl Default for DrawColor {
fn default() -> DrawColor {
return DrawColor {
r: 255,
g: 255,
b: 255,
};
}
}
pub struct DrawStringArgs {
pub text: String,
pub position: F2,
pub font_size: F1,
pub depth: F1,
pub optional: DrawStringOptionalArgs,
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum TextAlignment {
Left,
Center,
Right,
}
pub struct DrawStringOptionalArgs {
pub alignment: TextAlignment,
pub color: DrawColor,
pub border: bool,
pub border_color: DrawColor,
pub border_scale: F1,
pub opacity: F1,
pub text_cache: Option<Rc<TextCache>>,
}
impl Default for DrawStringOptionalArgs {
fn default() -> DrawStringOptionalArgs {
return DrawStringOptionalArgs {
alignment: TextAlignment::Left,
color: DrawColor::default(),
border: false,
border_color: DrawColor { r: 0, g: 0, b: 0 },
border_scale: 1.0,
opacity: 1.0,
text_cache: None,
};
}
}
pub struct DrawGradientBoxArgs {
pub position: F2,
pub size: F2,
pub draw_color_start: DrawColor,
pub alpha_start: F1,
pub draw_color_end: DrawColor,
pub alpha_end: F1,
pub depth: F1,
pub anchor_point: F2,
}