@@ -10,28 +10,69 @@ pub mod controls {
10
10
pub struct Text ;
11
11
12
12
impl Element for Text {
13
- fn inflate ( & mut self , mut base : Base ) {
14
- base . enable_update ( true ) ;
13
+ fn inflate ( & mut self , _base : Base ) {
14
+
15
15
}
16
16
17
- fn resize ( & mut self , mut _base : Base , size : ElementSize ) -> Option < ResolvedSize > {
17
+ fn resize ( & mut self , mut _base : Base , size : BoxSize ) -> Option < ResolvedSize > {
18
18
match size {
19
- ElementSize :: Auto => Some ( ResolvedSize { w : 100 , h : 60 } ) ,
20
- ElementSize :: Fixed { w, h } => Some ( ResolvedSize { w, h } ) ,
19
+ BoxSize :: Hidden => None ,
20
+ BoxSize :: Auto => Some ( ResolvedSize { w : 100 , h : 60 } ) ,
21
+ BoxSize :: Fixed { w, h } => Some ( ResolvedSize { w, h } ) ,
21
22
}
22
23
}
24
+
25
+ fn update ( & mut self , mut _base : Base , _delta : f32 ) {
26
+ println ! ( "update Text" ) ;
27
+ }
23
28
}
24
29
25
- pub struct Button ;
30
+ pub struct Button {
31
+ margin : i32 ,
32
+ step : i32 ,
33
+ delta_acc : f32 ,
34
+ last_size : Option < BoxSize > ,
35
+ }
36
+
37
+ impl Button {
38
+ pub fn new ( ) -> Button {
39
+ Button {
40
+ margin : 10 ,
41
+ step : 1 ,
42
+ delta_acc : 0.0 ,
43
+ last_size : None ,
44
+ }
45
+ }
46
+ }
26
47
27
48
impl Element for Button {
28
49
fn inflate ( & mut self , mut base : Base ) {
29
50
base. add ( Text ) ;
30
51
base. add ( Text ) ;
52
+ base. enable_update ( true ) ;
31
53
}
32
54
33
- fn resize ( & mut self , mut base : Base , size : ElementSize ) -> Option < ResolvedSize > {
34
- base. layout_vertical ( size, 5 )
55
+ fn resize ( & mut self , mut base : Base , size : BoxSize ) -> Option < ResolvedSize > {
56
+ self . last_size = Some ( size) ;
57
+ base. layout_vertical ( size, self . margin )
58
+ }
59
+
60
+ fn update ( & mut self , mut base : Base , delta : f32 ) {
61
+ self . delta_acc += delta;
62
+ if self . delta_acc > 0.05 {
63
+ self . margin += self . step ;
64
+ if self . margin > 20 || self . margin < 1 {
65
+ self . step = -self . step ;
66
+ }
67
+
68
+ if let Some ( BoxSize :: Fixed { w, h } ) = self . last_size {
69
+ base. layout_vertical ( BoxSize :: Fixed { w : w, h : h } , self . margin ) ;
70
+ } else {
71
+ base. layout_vertical ( BoxSize :: Auto , self . margin ) ;
72
+ }
73
+
74
+ self . delta_acc = 0.0 ;
75
+ }
35
76
}
36
77
}
37
78
@@ -50,17 +91,18 @@ pub mod controls {
50
91
impl Element for Fill {
51
92
fn inflate ( & mut self , mut base : Base ) {
52
93
base. add ( Text ) ;
53
- base. add ( Button ) ;
94
+ base. add ( Button :: new ( ) ) ;
54
95
}
55
96
56
- fn resize ( & mut self , mut base : Base , size : ElementSize ) -> Option < ResolvedSize > {
97
+ fn resize ( & mut self , mut base : Base , size : BoxSize ) -> Option < ResolvedSize > {
57
98
base. layout_vertical ( size, 5 )
58
99
}
59
100
}
60
101
}
61
102
62
103
#[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
63
- pub enum ElementSize {
104
+ pub enum BoxSize {
105
+ Hidden ,
64
106
Auto ,
65
107
Fixed { w : i32 , h : i32 }
66
108
}
@@ -81,7 +123,8 @@ pub enum Effect {
81
123
pub trait Element {
82
124
83
125
fn inflate ( & mut self , _base : Base ) { }
84
- fn resize ( & mut self , _base : Base , _size : ElementSize ) -> Option < ResolvedSize > ;
126
+ fn resize ( & mut self , _base : Base , _size : BoxSize ) -> Option < ResolvedSize > ;
127
+ fn update ( & mut self , _base : Base , _delta : f32 ) { }
85
128
86
129
}
87
130
0 commit comments