Skip to content

Commit 447ce91

Browse files
committed
Simplify Hide
1 parent 518570e commit 447ce91

File tree

4 files changed

+180
-64
lines changed

4 files changed

+180
-64
lines changed

lesson-24-x/src/interface.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl ControlInfo {
6161
}
6262

6363
pub struct Interface {
64+
tree: Tree,
6465
fill: Leaf<controls::Fill>,
6566
events: Events,
6667
controls: HashMap<Ix, ControlInfo>,
@@ -71,7 +72,7 @@ pub struct Interface {
7172
}
7273

7374
impl Interface {
74-
pub fn new(gl: &gl::Gl, resources: &resources::Resources, size: ElementSize) -> Result<Interface, failure::Error> {
75+
pub fn new(gl: &gl::Gl, resources: &resources::Resources, size: BoxSize) -> Result<Interface, failure::Error> {
7576
let tree = Tree::new();
7677

7778
let events = tree.events();
@@ -80,6 +81,7 @@ impl Interface {
8081
fill.resize(size);
8182

8283
Ok(Interface {
84+
tree,
8385
fill,
8486
events,
8587
controls: HashMap::new(),
@@ -89,7 +91,7 @@ impl Interface {
8991
})
9092
}
9193

92-
pub fn resize(&mut self, size: ElementSize) {
94+
pub fn resize(&mut self, size: BoxSize) {
9395
self.fill.resize(size);
9496
}
9597

@@ -123,7 +125,8 @@ impl Interface {
123125
}
124126
}
125127

126-
pub fn update(&mut self, _delta: f32) {
128+
pub fn update(&mut self, delta: f32) {
129+
self.tree.update(delta);
127130
self.process_events()
128131
}
129132

lesson-24-x/src/main.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ fn run() -> Result<(), failure::Error> {
7878
viewport.set_used(&gl);
7979
color_buffer.set_clear_color(&gl, na::Vector3::new(0.3, 0.3, 0.5));
8080

81-
let mut iface = Interface::new(&gl, &resources, ui::ElementSize::Fixed { w: viewport.w, h: viewport.h })?;
81+
let mut iface_auto_size = false;
82+
let mut iface = Interface::new(&gl, &resources, ui::BoxSize::Fixed { w: viewport.w, h: viewport.h })?;
8283

8384
// main loop
8485

@@ -92,15 +93,32 @@ fn run() -> Result<(), failure::Error> {
9293
break 'main;
9394
}
9495

96+
use sdl2::event::Event;
97+
use sdl2::keyboard::Scancode;
98+
9599
match event {
96-
sdl2::event::Event::Window {
100+
Event::Window {
97101
win_event: sdl2::event::WindowEvent::Resized(_w, _h),
98102
..
99103
} => {
100104
let (hdpi_w, hdpi_h) = window.drawable_size();
101105
viewport.update_size(hdpi_w as i32, hdpi_h as i32);
102106
viewport.set_used(&gl);
103-
iface.resize(ui::ElementSize::Fixed { w: hdpi_w as i32, h: hdpi_h as i32 });
107+
if iface_auto_size {
108+
iface.resize(ui::BoxSize::Auto );
109+
} else {
110+
iface.resize(ui::BoxSize::Fixed { w: viewport.w, h: viewport.h });
111+
}
112+
},
113+
Event::KeyDown {
114+
scancode: Some(Scancode::L), ..
115+
} => {
116+
iface_auto_size = !iface_auto_size;
117+
if iface_auto_size {
118+
iface.resize(ui::BoxSize::Auto );
119+
} else {
120+
iface.resize(ui::BoxSize::Fixed { w: viewport.w, h: viewport.h });
121+
}
104122
},
105123
_ => (),
106124
};

lib/ui/src/lib.rs

+55-12
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,69 @@ pub mod controls {
1010
pub struct Text;
1111

1212
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+
1515
}
1616

17-
fn resize(&mut self, mut _base: Base, size: ElementSize) -> Option<ResolvedSize> {
17+
fn resize(&mut self, mut _base: Base, size: BoxSize) -> Option<ResolvedSize> {
1818
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 }),
2122
}
2223
}
24+
25+
fn update(&mut self, mut _base: Base, _delta: f32) {
26+
println!("update Text");
27+
}
2328
}
2429

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+
}
2647

2748
impl Element for Button {
2849
fn inflate(&mut self, mut base: Base) {
2950
base.add(Text);
3051
base.add(Text);
52+
base.enable_update(true);
3153
}
3254

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+
}
3576
}
3677
}
3778

@@ -50,17 +91,18 @@ pub mod controls {
5091
impl Element for Fill {
5192
fn inflate(&mut self, mut base: Base) {
5293
base.add(Text);
53-
base.add(Button);
94+
base.add(Button::new());
5495
}
5596

56-
fn resize(&mut self, mut base: Base, size: ElementSize) -> Option<ResolvedSize> {
97+
fn resize(&mut self, mut base: Base, size: BoxSize) -> Option<ResolvedSize> {
5798
base.layout_vertical(size, 5)
5899
}
59100
}
60101
}
61102

62103
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
63-
pub enum ElementSize {
104+
pub enum BoxSize {
105+
Hidden,
64106
Auto,
65107
Fixed { w: i32, h: i32 }
66108
}
@@ -81,7 +123,8 @@ pub enum Effect {
81123
pub trait Element {
82124

83125
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) {}
85128

86129
}
87130

0 commit comments

Comments
 (0)