@@ -15,85 +15,117 @@ macro_rules! err {
1515 // there is no way to specify panci message, so we need to do console.error and then panci any value
1616}
1717
18- //to remove and replace with util
19- macro_rules! angle_diff {
20- ( $beta: expr, $alpha: expr) => { {
21- let phi = ( $beta - $alpha) . abs( ) % ( 2.0 * MATH_PI ) ; // This is either the distance or 2*Math.PI - distance
22- if phi > MATH_PI {
23- ( 2.0 * MATH_PI ) - phi
24- } else {
25- phi
26- }
27- } }
28- }
29-
18+ mod line;
3019mod texture;
20+ mod types;
3121
32- use gloo_utils:: format:: JsValueSerdeExt ; // for transforming JsValue into serde
22+ use std:: collections:: HashMap ;
23+
24+ use gloo_utils:: format:: JsValueSerdeExt ;
25+ use line:: Line ;
3326use serde:: { Deserialize , Serialize } ;
34- use texture:: { Texture , VertexPoint } ;
27+ use texture:: Texture ;
28+ use types:: { Point , VertexPoint } ;
3529use wasm_bindgen:: prelude:: * ;
3630
3731#[ wasm_bindgen]
3832pub struct State {
39- textures : Vec < Texture > ,
33+ assets : HashMap < usize , Texture > ,
34+ hovered_asset_id : usize , // 0 -> no asset is hovered
4035}
4136
4237#[ wasm_bindgen]
4338impl State {
4439 pub fn new ( width : f32 , height : f32 ) -> State {
45- State { textures : vec ! [ ] }
40+ State {
41+ assets : HashMap :: new ( ) ,
42+ hovered_asset_id : 0 ,
43+ }
4644 }
4745
48- pub fn add_texture ( & mut self , raw_points : JsValue , id : usize ) {
46+ pub fn add_texture ( & mut self , id : usize , raw_points : JsValue , texture_id : usize ) {
4947 let serde = raw_points. into_serde ( ) ;
5048 let points: Vec < VertexPoint > = if serde. is_ok ( ) {
5149 serde. unwrap ( )
5250 } else {
5351 err ! ( "add_texture received not copatible data from JS. Failed at conversion to Rust types." ) ;
5452 } ;
5553
56- self . textures . push ( Texture :: new ( points, id ) ) ;
54+ self . assets . insert ( id , Texture :: new ( id , points, texture_id ) ) ;
5755 }
5856
59- pub fn get_shader_input ( & self , index : usize ) -> JsValue {
60- let mut vertex_data: Vec < f32 > = vec ! [ ] ;
61- let mut texture_id: usize = 0 ;
62- if index < self . textures . len ( ) {
63- texture_id = self . textures [ index] . id ;
64- self . textures [ index] . add_vertex ( & mut vertex_data) ;
65- }
57+ pub fn get_shader_input ( & self , id : usize ) -> JsValue {
58+ let asset: & Texture = if self . assets . contains_key ( & id) {
59+ self . assets . get ( & id) . unwrap ( )
60+ } else {
61+ err ! ( "asset with id {id} not found" ) ;
62+ } ;
6663
6764 let payload = ShaderInput {
68- texture_id,
69- vertex_data,
65+ texture_id : asset . texture_id ,
66+ vertex_data : asset . get_vertex_data ( ) ,
7067 } ;
68+
7169 serde_wasm_bindgen:: to_value ( & payload) . unwrap ( )
70+ }
71+
72+ pub fn get_shader_pick_input ( & self , id : usize ) -> JsValue {
73+ let asset: & Texture = if self . assets . contains_key ( & id) {
74+ self . assets . get ( & id) . unwrap ( )
75+ } else {
76+ err ! ( "asset with id {id} not found" ) ;
77+ } ;
78+
79+ let payload = ShaderInput {
80+ texture_id : asset. texture_id ,
81+ vertex_data : asset. get_vertex_pick_data ( ) ,
82+ } ;
7283
73- // js_sys::Float32Array::from(&result[..] )
84+ serde_wasm_bindgen :: to_value ( & payload ) . unwrap ( )
7485 }
7586
76- pub fn update_points ( & mut self , texture_id : usize , raw_points : JsValue ) {
87+ pub fn update_points ( & mut self , id : usize , raw_points : JsValue ) {
88+ let asset = self . assets . get_mut ( & id) . unwrap ( ) ;
89+
7790 let serde = raw_points. into_serde ( ) ;
7891 let points: Vec < Point > = if serde. is_ok ( ) {
7992 serde. unwrap ( )
8093 } else {
8194 err ! ( "add_texture received not copatible data from JS. Failed at conversion to Rust types." ) ;
8295 } ;
8396
84- let texture_option = self
85- . textures
86- . iter_mut ( )
87- . find ( |texture| texture. id == texture_id) ;
97+ asset. update_coords ( points) ;
98+ }
8899
89- texture_option. unwrap ( ) . update_coords ( points) ;
100+ pub fn update_hover ( & mut self , id : usize ) {
101+ self . hovered_asset_id = id
90102 }
91- }
92103
93- #[ derive( Serialize , Deserialize ) ]
94- struct Point {
95- x : f32 ,
96- y : f32 ,
104+ pub fn get_border ( & self ) -> Vec < f32 > {
105+ if self . assets . contains_key ( & self . hovered_asset_id ) {
106+ let asset: & Texture = self . assets . get ( & self . hovered_asset_id ) . unwrap ( ) ;
107+
108+ asset
109+ . points
110+ . iter ( )
111+ . enumerate ( )
112+ . flat_map ( |( index, point) | {
113+ Line :: get_vertex_data (
114+ point,
115+ if index == 3 {
116+ & asset. points [ 0 ]
117+ } else {
118+ & asset. points [ index + 1 ]
119+ } ,
120+ 20.0 ,
121+ ( 1.0 , 0.0 , 0.0 , 1.0 ) ,
122+ )
123+ } )
124+ . collect :: < Vec < f32 > > ( )
125+ } else {
126+ vec ! [ ]
127+ }
128+ }
97129}
98130
99131#[ derive( Serialize , Deserialize ) ]
0 commit comments