hai_core 0.7.0

Core implementation of Hai game engine, and general 2D rendering library using WebGPU as well.
Documentation
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use csscolorparser::Color;
use hai_pal::sync::RwLock;
use log::warn;
use std::sync::Arc;

use crate::base::*;
use crate::traits::Node;
use crate::utils::constants::{VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
#[cfg(all(not(feature = "web"), feature = "js_runtime"))]
use crate::utils::convert::{from_js, JSValue};
#[cfg(all(not(feature = "web"), feature = "js_runtime"))]
use crate::utils::dispatch_event::{dispatch_event, HaiEvent, HaiEventKind};

pub static mut NODE_ID: u32 = 0;

#[derive(Debug, Default)]
pub struct NodeBase {
    /// Debug label
    label: String,
    /// id
    id: u32,
    /// anchor point
    anchor: Point,
    /// pivot point
    pivot: Point,
    /// translate relative to parent
    translate: Point,
    /// scale relative to parent
    scale: Point,
    /// rotation relative to parent
    rotation: f32,
    /// skew relative to parent
    skew: Point,
    /// visible to render
    visible: bool,
    /// tint color
    tint: Color,
    /// opcaity, aka alpha. Ranges from 0.0 to 1.0.
    opacity: f32,
    /// opacity that has been multiplied with parent
    global_opacity: f32,
    /// if this node will response to user input, will affect itself and all children
    interactive: bool,
    /// cursor style
    cursor: HaiCursor,
    /// for update transform dirty check
    _update_id: u32,
    _current_update_id: u32,
    _need_update_vertices: bool,
    /// transform matrix relative to parent
    transform: Transform,
    /// transform matrix relative to global
    global_transform: Transform,
    /// children
    children: Vec<Arc<RwLock<dyn Node>>>,
}

impl NodeBase {
    pub fn new(label: String) -> Self {
        let id = unsafe {
            NODE_ID += 1;
            NODE_ID
        };
        Self {
            label,
            id,
            anchor: Point::default(),
            pivot: Point::default(),
            translate: Point::default(),
            scale: Point::one(),
            rotation: 0.,
            skew: Point::default(),
            visible: true,
            tint: Color::new(1.0, 1.0, 1.0, 1.0),
            opacity: 1.0,
            global_opacity: 1.0,
            interactive: true,
            cursor: HaiCursor::default(),

            _update_id: 0,
            _current_update_id: 0,
            _need_update_vertices: true,

            transform: Transform::default(),
            global_transform: Transform::default(),
            children: vec![],
        }
    }
}

impl NodeBase {
    pub fn node_type(&self) -> &'static str {
        unreachable!("Should not call Node::node_type, use NodeType::node_type(&node) instead.");
    }

    #[inline]
    pub fn pend_update(&mut self) {
        self._update_id += 1;
    }

    /// pop vertices update flag, returns the current flag value, and set it to false
    #[inline]
    pub fn pop_update_vertices(&mut self) -> bool {
        let flag = self._need_update_vertices;
        self._need_update_vertices = false;
        flag
    }

    #[inline]
    pub fn id(&self) -> &u32 {
        &self.id
    }

    #[inline]
    pub fn label(&self) -> &String {
        &self.label
    }

    #[inline]
    pub fn anchor(&self) -> &Point {
        &self.anchor
    }
    #[inline]
    pub fn pivot(&self) -> &Point {
        &self.pivot
    }
    #[inline]
    pub fn translate(&self) -> &Point {
        &self.translate
    }
    #[inline]
    pub fn scale(&self) -> &Point {
        &self.scale
    }
    #[inline]
    pub fn rotation(&self) -> &f32 {
        &self.rotation
    }
    #[inline]
    pub fn skew(&self) -> &Point {
        &self.skew
    }
    #[inline]
    pub fn visible(&self) -> bool {
        self.visible
    }
    #[inline]
    pub fn tint(&self) -> &Color {
        &self.tint
    }
    #[inline]
    pub fn opacity(&self) -> &f32 {
        &self.opacity
    }
    #[inline]
    pub fn global_opacity(&self) -> &f32 {
        &self.global_opacity
    }
    #[inline]
    pub fn interactive(&self) -> bool {
        self.interactive
    }
    #[inline]
    pub fn cursor(&self) -> &HaiCursor {
        &self.cursor
    }

    #[inline]
    pub fn set_anchor(&mut self, x: f32, y: f32) {
        self.anchor.x = x;
        self.anchor.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_pivot(&mut self, x: f32, y: f32) {
        self.pivot.x = x;
        self.pivot.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_translate(&mut self, x: f32, y: f32) {
        self.translate.x = x;
        self.translate.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_x(&mut self, x: f32) {
        self.translate.x = x;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_y(&mut self, y: f32) {
        self.translate.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_scale(&mut self, x: f32, y: f32) {
        self.scale.x = x;
        self.scale.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_scale_x(&mut self, x: f32) {
        self.scale.x = x;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_scale_y(&mut self, y: f32) {
        self.scale.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_rotation(&mut self, radian: f32) {
        self.rotation = radian;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_skew(&mut self, x: f32, y: f32) {
        self.skew.x = x;
        self.skew.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_skew_x(&mut self, x: f32) {
        self.skew.x = x;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_skew_y(&mut self, y: f32) {
        self.skew.y = y;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_tint(&mut self, color: Color) {
        self.tint = color;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_opacity(&mut self, opacity: f32) {
        self.opacity = opacity;
        self._update_id += 1;
    }
    #[inline]
    pub fn set_interactive(&mut self, interactive: bool) {
        self.interactive = interactive;
    }
    #[inline]
    pub fn set_cursor(&mut self, cursor: HaiCursor) {
        self.cursor = cursor;
    }

    pub fn transform(&self) -> &Transform {
        &self.transform
    }
    pub fn global_transform(&self) -> &Transform {
        &self.global_transform
    }
    pub fn children(&self) -> &Vec<Arc<RwLock<dyn Node>>> {
        &self.children
    }

    // pub fn as_any(&self) -> &dyn Any {
    //     self
    // }

    // pub fn as_any_mut(&mut self) -> &mut dyn Any {
    //     self
    // }

    #[cfg(all(not(feature = "web"), feature = "js_runtime"))]
    #[inline]
    pub fn update_properties(&mut self, props: &mut JSValue) {
        let props: NodeProps = match from_js(props) {
            Ok(v) => v,
            Err(err) => {
                warn!("Failed to convert JSValue to NodeProps: {:?}", err);
                return;
            }
        };

        if let Some(x) = props.x {
            self.set_x(x);
        }

        if let Some(y) = props.y {
            self.set_y(y);
        }

        if let Some(v) = props.scale {
            self.set_scale(v, v);
        }

        if let Some(x) = props.scale_x {
            self.set_scale_x(x);
        }

        if let Some(y) = props.scale_y {
            self.set_scale_y(y);
        }

        if let Some(v) = props.rotation {
            self.set_rotation(v);
        }

        if let Some(v) = props.skew {
            self.set_skew(v, v);
        }

        if let Some(x) = props.skew_x {
            self.set_skew_x(x);
        }

        if let Some(y) = props.skew_y {
            self.set_skew_y(y);
        }

        if let Some(point) = props.anchor {
            self.set_anchor(point[0], point[1]);
        }

        if let Some(point) = props.pivot {
            self.set_pivot(point[0], point[1]);
        }

        if let Some(visible) = props.visible {
            self.set_visible(visible);
        }

        if let Some(tint) = props.tint {
            self.set_tint(tint);
        }

        if let Some(opacity) = props.opacity {
            self.set_opacity(opacity);
        }

        if let Some(interactive) = props.interactive {
            self.set_interactive(interactive);
        }

        if let Some(cursor) = props.cursor {
            self.set_cursor(cursor);
        }
    }

    #[inline]
    pub fn get_child(&self, index: usize) -> Option<Arc<RwLock<dyn Node>>> {
        if let Some(child) = self.children.get(index) {
            return Some(child.clone());
        }
        None
    }

    #[inline]
    pub fn add_child(&mut self, child: Arc<RwLock<dyn Node>>) {
        self.children.push(child);
    }

    #[inline]
    pub fn insert_child(&mut self, index: usize, child: Arc<RwLock<dyn Node>>) {
        self.children.insert(index, child);
    }

    #[inline]
    pub fn insert_child_before(
        &mut self,
        before_child: Arc<RwLock<dyn Node>>,
        child: Arc<RwLock<dyn Node>>,
    ) {
        let index = self.children.iter().position(|item| {
            let l = item.read();
            let r = before_child.read();
            *l == *r
        });
        if index.is_none() {
            warn!("Cannot insert child before another one because the another child does not present in current children.");
        }
        self.children.insert(index.unwrap_or(0), child);
    }

    #[inline]
    pub fn remove_child(&mut self, child: Arc<RwLock<dyn Node>>) -> Option<Arc<RwLock<dyn Node>>> {
        if let Some(index) = self.children.iter().position(|item| {
            let l = item.read();
            let r = child.read();
            *l == *r
        }) {
            return Some(self.children.remove(index));
        }
        None
    }

    #[inline]
    pub fn remove_child_at(&mut self, index: usize) -> Option<Arc<RwLock<dyn Node>>> {
        if index < self.children.len() {
            return Some(self.children.remove(index));
        }
        None
    }

    #[inline]
    pub fn move_to(&mut self, x: f32, y: f32) {
        self.set_translate(x, y);
    }

    #[inline]
    pub fn update(&mut self, parent: &Self, _: &SurfaceSize, force: bool) {
        if force || self._update_id != self._current_update_id {
            let x = self.translate.x;
            let y = self.translate.y;
            let rotation = self.rotation;
            let scale_x = self.scale.x;
            let scale_y = self.scale.y;
            let skew_x = self.skew.x;
            let skew_y = self.skew.y;
            let pivot_x = self.pivot.x;
            let pivot_y = self.pivot.y;

            let a = (rotation + skew_y).cos() * scale_x;
            let b = (rotation + skew_y).sin() * scale_x;
            let c = -(rotation - skew_x).sin() * scale_y;
            let d = (rotation - skew_x).cos() * scale_y;
            let tx = x - ((pivot_x * a) + (pivot_y * c));
            let ty = y - ((pivot_x * b) + (pivot_y * d));

            // use logical size to calculate transform matrix, so that the transform matrix will not be affected by scale ratio
            let tx = tx / VIEWPORT_WIDTH;
            let ty = ty / VIEWPORT_HEIGHT;

            self.transform.x_axis.x = a;
            self.transform.x_axis.y = b;
            self.transform.y_axis.x = c;
            self.transform.y_axis.y = d;
            self.transform.z_axis.x = tx;
            self.transform.z_axis.y = ty;

            // refresh global transform matrix
            let mut global_transform = *parent.global_transform();
            global_transform.multiply(self.transform);
            self.global_transform = global_transform;

            // refresh global opacity
            self.global_opacity = self.opacity * parent.global_opacity;

            self._current_update_id = self._update_id;
            self._need_update_vertices = true;

            // mark all children pend update
            for child in self.children.iter() {
                let mut child = child.write();
                child.base_mut().pend_update();
            }
        }
    }
}

use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeProps {
    pub anchor: Option<[f32; 2]>,
    pub pivot: Option<[f32; 2]>,
    pub x: Option<f32>,
    pub y: Option<f32>,
    pub scale: Option<f32>,
    pub scale_x: Option<f32>,
    pub scale_y: Option<f32>,
    pub rotation: Option<f32>,
    pub skew: Option<f32>,
    pub skew_x: Option<f32>,
    pub skew_y: Option<f32>,
    pub visible: Option<bool>,
    pub tint: Option<Color>,
    pub opacity: Option<f32>,
    pub interactive: Option<bool>,
    pub cursor: Option<HaiCursor>,
}

impl Drop for NodeBase {
    fn drop(&mut self) {
        #[cfg(all(not(feature = "web"), feature = "js_runtime"))]
        dispatch_event(HaiEvent {
            kind: HaiEventKind::NodeDestroyed,
            target_id: self.id,
            bubble_target_ids: vec![],
        });
    }
}