Skip to content

Commit 0d33cb3

Browse files
committed
Transformations
1 parent 3c29339 commit 0d33cb3

File tree

6 files changed

+204
-103
lines changed

6 files changed

+204
-103
lines changed

lesson-24-x/render_gl/src/debug_lines/mod.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl DebugLines {
7777
let mut offset = 0;
7878
for container in shared_debug_lines.containers.values() {
7979
buffers.multi_draw_items.push(MultiDrawItem {
80-
model_matrix: container.isometry.to_homogeneous(),
80+
model_matrix: na::convert(container.transform),
8181
starting_index: offset,
8282
index_count: container.data.len() as i32,
8383
});
@@ -132,7 +132,7 @@ impl DebugLines {
132132

133133
let new_id = self.containers.borrow_mut()
134134
.new_container(
135-
na::Isometry3::from_parts(na::Translation3::from_vector(pos.coords), na::UnitQuaternion::identity()),
135+
na::convert(na::Isometry3::from_parts(na::Translation3::from_vector(pos.coords), na::UnitQuaternion::identity())),
136136
vec![
137137
LinePoint { pos: render_p3(pos + na::Vector3::x() * half), color: (0.0, 1.0, 0.0, 1.0).into() },
138138
LinePoint { pos: render_p3(pos + na::Vector3::x() * -half), color: (0.0, 1.0, 0.0, 1.0).into() },
@@ -153,7 +153,7 @@ impl DebugLines {
153153
let half = size / 2.0;
154154

155155
let new_id = self.containers.borrow_mut()
156-
.new_container(na::Isometry3::from_parts(na::Translation3::from_vector(pos.coords), na::UnitQuaternion::identity()),
156+
.new_container(na::convert(na::Isometry3::from_parts(na::Translation3::from_vector(pos.coords), na::UnitQuaternion::identity())),
157157
vec![
158158
LinePoint { pos: render_p3(pos + na::Vector3::x() * half), color: render_color_vec4(color) },
159159
LinePoint { pos: render_p3(pos + na::Vector3::x() * -half), color: render_color_vec4(color) },
@@ -169,7 +169,7 @@ impl DebugLines {
169169
}
170170
}
171171

172-
pub fn ray_markers(&self, isometry: na::Isometry3<f32>, pos_direction_colors: impl Iterator<Item=(na::Point3<f32>, na::Vector3<f32>, na::Vector4<f32>)>) -> RayMarkers {
172+
pub fn ray_markers(&self, transform: na::Projective3<f32>, pos_direction_colors: impl Iterator<Item=(na::Point3<f32>, na::Vector3<f32>, na::Vector4<f32>)>) -> RayMarkers {
173173
struct PositionsIter {
174174
pos: na::Point3<f32>,
175175
dir: na::Vector3<f32>,
@@ -197,7 +197,7 @@ impl DebugLines {
197197

198198
let new_id = self.containers.borrow_mut()
199199
.new_container(
200-
isometry,
200+
transform,
201201
pos_direction_colors
202202
.flat_map(|(pos, dir, color)|
203203
PositionsIter {
@@ -215,12 +215,12 @@ impl DebugLines {
215215
}
216216
}
217217

218-
pub fn aabb_marker(&self, isometry: na::Isometry3<f32>, aabb: ncollide3d::bounding_volume::aabb::AABB<f32>, color: na::Vector4<f32>) -> AabbMarker {
218+
pub fn aabb_marker(&self, transform: na::Projective3<f32>, aabb: ncollide3d::bounding_volume::aabb::AABB<f32>, color: na::Vector4<f32>) -> AabbMarker {
219219
let a = aabb.mins();
220220
let b = aabb.maxs();
221221

222222
let new_id = self.containers.borrow_mut()
223-
.new_container(isometry,
223+
.new_container(transform,
224224
vec![
225225
LinePoint { pos: render_p3([a.x, a.y, a.z].into()), color: render_color_vec4(color) },
226226
LinePoint { pos: render_p3([b.x, a.y, a.z].into()), color: render_color_vec4(color) },
@@ -254,11 +254,11 @@ impl DebugLines {
254254
}
255255
}
256256

257-
pub fn rect_marker(&self, isometry: na::Isometry3<f32>, size: na::Vector2<f32>, color: na::Vector4<f32>) -> RectMarker {
258-
RectMarker::new(self.containers.clone(), isometry, size, color)
257+
pub fn rect_marker(&self, transform: na::Projective3<f32>, size: na::Vector2<f32>, color: na::Vector4<f32>) -> RectMarker {
258+
RectMarker::new(self.containers.clone(), transform, size, color)
259259
}
260260

261-
pub fn grid_marker(&self, isometry: na::Isometry3<f32>, spacing: f32, count: i32, color: na::Vector4<f32>) -> GridMarker {
261+
pub fn grid_marker(&self, transform: na::Projective3<f32>, spacing: f32, count: i32, color: na::Vector4<f32>) -> GridMarker {
262262
let mut lines = Vec::with_capacity((count * 2 + 4) as usize);
263263

264264
let mut half_count = count / 2;
@@ -283,7 +283,7 @@ impl DebugLines {
283283
}
284284

285285
let new_id = self.containers.borrow_mut()
286-
.new_container(isometry, lines);
286+
.new_container(transform, lines);
287287

288288
GridMarker {
289289
containers: self.containers.clone(),
@@ -298,9 +298,9 @@ pub struct AabbMarker {
298298
}
299299

300300
impl AabbMarker {
301-
pub fn update_isometry(&self, isometry: na::Isometry3<f32>) {
301+
pub fn update_transform(&self, transform: na::Projective3<f32>) {
302302
if let Some(data) = self.containers.borrow_mut().get_container_mut(self.id) {
303-
data.isometry = isometry;
303+
data.transform = transform;
304304
}
305305
}
306306
}
@@ -317,9 +317,9 @@ pub struct GridMarker {
317317
}
318318

319319
impl GridMarker {
320-
pub fn update_isometry(&self, isometry: na::Isometry3<f32>) {
320+
pub fn update_transform(&self, transform: na::Projective3<f32>) {
321321
if let Some(data) = self.containers.borrow_mut().get_container_mut(self.id) {
322-
data.isometry = isometry;
322+
data.transform = transform;
323323
}
324324
}
325325
}
@@ -336,9 +336,9 @@ pub struct RectMarker {
336336
}
337337

338338
impl RectMarker {
339-
fn new(containers: Rc<RefCell<SharedDebugLines>>, isometry: na::Isometry3<f32>, size: na::Vector2<f32>, color: na::Vector4<f32>) -> RectMarker {
339+
fn new(containers: Rc<RefCell<SharedDebugLines>>, transform: na::Projective3<f32>, size: na::Vector2<f32>, color: na::Vector4<f32>) -> RectMarker {
340340
let id = containers.borrow_mut()
341-
.new_container(isometry, Vec::with_capacity(8));
341+
.new_container(transform, Vec::with_capacity(8));
342342

343343
let marker = RectMarker {
344344
id,
@@ -377,9 +377,9 @@ impl RectMarker {
377377
}
378378
}
379379

380-
pub fn update_isometry(&self, isometry: na::Isometry3<f32>) {
380+
pub fn update_transform(&self, transform: na::Projective3<f32>) {
381381
if let Some(data) = self.containers.borrow_mut().get_container_mut(self.id) {
382-
data.isometry = isometry;
382+
data.transform = transform;
383383
}
384384
}
385385

@@ -409,9 +409,9 @@ impl RayMarkers {
409409
}
410410
}
411411

412-
pub fn update_isometry(&self, isometry: na::Isometry3<f32>) {
412+
pub fn update_transform(&self, transform: na::Projective3<f32>) {
413413
if let Some(data) = self.containers.borrow_mut().get_container_mut(self.id) {
414-
data.isometry = isometry;
414+
data.transform = transform;
415415
}
416416
}
417417
}
@@ -430,7 +430,7 @@ pub struct PointMarker {
430430
impl PointMarker {
431431
pub fn update_position(&self, pos: na::Point3<f32>) {
432432
if let Some(data) = self.containers.borrow_mut().get_container_mut(self.id) {
433-
data.isometry = na::Isometry3::from_parts(na::Translation3::from_vector(pos.coords), na::UnitQuaternion::identity());
433+
data.transform = na::convert(na::Isometry3::from_parts(na::Translation3::from_vector(pos.coords), na::UnitQuaternion::identity()));
434434
}
435435
}
436436
}

lesson-24-x/render_gl/src/debug_lines/shared_debug_lines.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
33
use super::buffers::LinePoint;
44

55
pub struct Container {
6-
pub isometry: na::Isometry3<f32>,
6+
pub transform: na::Projective3<f32>,
77
pub data: Vec<LinePoint>,
88
}
99

@@ -28,10 +28,10 @@ impl SharedDebugLines {
2828
id
2929
}
3030

31-
pub fn new_container(&mut self, isometry: na::Isometry3<f32>, data: Vec<LinePoint>) -> i32 {
31+
pub fn new_container(&mut self, transform: na::Projective3<f32>, data: Vec<LinePoint>) -> i32 {
3232
let next_id = self.get_next_id();
3333
self.containers.insert(next_id, Container {
34-
isometry,
34+
transform,
3535
data,
3636
});
3737
self.invalidated = true;

lesson-24-x/src/interface.rs

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,65 @@ use na;
55
use render_gl::ColorBuffer;
66
use render_gl::{DebugLines, RectMarker};
77
use std::collections::HashMap;
8-
9-
struct Location {
10-
size: (i32, i32),
11-
pos: (i32, i32),
12-
}
13-
8+
use std::collections::BTreeSet;
149

1510
struct ControlInfo {
16-
parent_id: Option<Ix>,
17-
loc: Option<Location>,
11+
_id: Ix,
12+
size: Option<(i32, i32)>,
13+
absolute_transform: Option<na::Projective3<f32>>,
1814
marker: Option<RectMarker>,
1915
}
2016

2117
impl ControlInfo {
22-
pub fn new(parent_id: Option<Ix>, debug_lines: &DebugLines) -> ControlInfo {
18+
pub fn new(id: Ix, _parent_id: Option<Ix>) -> ControlInfo {
2319
ControlInfo {
24-
parent_id,
25-
loc: None,
20+
_id: id,
21+
size: None,
22+
absolute_transform: None,
2623
marker: None,
2724
}
2825
}
2926

30-
pub fn update(&mut self, debug_lines: &DebugLines, size: Option<(i32, i32)>) {
31-
self.loc = size.map(|wh| Location { size: wh, pos: (0, 0) });
32-
match (self.marker.is_some(), size) {
33-
(false, None) => (),
34-
(false, Some(wh)) => self.marker = Some(debug_lines.rect_marker(
35-
na::Isometry3::from_parts(na::Translation3::from_vector(
36-
[0.5, 0.5, 0.0].into()
37-
), na::UnitQuaternion::identity()),
38-
na::Vector2::new(wh.0 as f32, wh.1 as f32),
39-
na::Vector4::new(1.0, 0.5, 0.2, 1.0)
40-
)),
41-
(true, None) => self.marker = None,
42-
(true, Some(wh)) => self.marker.as_mut().unwrap().update_size_and_color(na::Vector2::new(wh.0 as f32, wh.1 as f32), na::Vector4::new(1.0, 0.5, 0.2, 1.0)),
27+
pub fn update_size(&mut self, size: Option<(i32, i32)>) {
28+
self.size = size;
29+
}
30+
31+
pub fn update_transform(&mut self, absolute_transform: &na::Projective3<f32>) {
32+
self.absolute_transform = Some(absolute_transform.clone());
33+
}
34+
35+
pub fn flush_updates(&mut self, debug_lines: &DebugLines) {
36+
match (self.marker.is_some(), self.size, self.absolute_transform) {
37+
(false, Some(wh), Some(t)) => {
38+
self.marker = Some(debug_lines.rect_marker(
39+
t * na::Translation3::new(1.0, 0.0, 0.0),
40+
na::Vector2::new((wh.0 - 1) as f32, (wh.1 - 1) as f32),
41+
na::Vector4::new(1.0, 0.5, 0.2, 1.0),
42+
))
43+
},
44+
(true, Some(wh), Some(t)) => {
45+
let marker = self.marker.as_mut().unwrap();
46+
marker.update_size_and_color(
47+
na::Vector2::new((wh.0 - 1) as f32, (wh.1 - 1) as f32),
48+
na::Vector4::new(1.0, 0.5, 0.2, 1.0),
49+
);
50+
marker.update_transform(t * na::Translation3::new(1.0, 0.0, 0.0));
51+
},
52+
(false, _, _) => {
53+
},
54+
(true, _, _) => {
55+
self.marker = None
56+
},
4357
}
4458
}
4559
}
4660

4761
pub struct Interface {
4862
fill: Leaf<controls::Fill>,
4963
events: Events,
50-
// button: Leaf<controls::Button>,
5164
controls: HashMap<Ix, ControlInfo>,
5265
event_read_buffer: Vec<Effect>,
66+
flush_updates_set: BTreeSet<Ix>,
5367
}
5468

5569
impl Interface {
@@ -64,9 +78,9 @@ impl Interface {
6478
Interface {
6579
fill,
6680
events,
67-
// button,
6881
controls: HashMap::new(),
6982
event_read_buffer: Vec::new(),
83+
flush_updates_set: BTreeSet::new(),
7084
}
7185
}
7286

@@ -76,39 +90,42 @@ impl Interface {
7690

7791
fn process_events(&mut self, debug_lines: &DebugLines) {
7892
self.events.drain_into(&mut self.event_read_buffer);
93+
self.flush_updates_set.clear();
7994

8095
for event in self.event_read_buffer.drain(..) {
8196
match event {
8297
Effect::Add { id, parent_id } => {
83-
self.controls.insert(id, ControlInfo::new(parent_id, debug_lines));
84-
},
98+
self.controls.insert(id, ControlInfo::new(id, parent_id));
99+
self.flush_updates_set.insert(id);
100+
}
85101
Effect::Resize { id, size } => {
86-
self.controls.get_mut(&id).map(|c| c.update(debug_lines, size));
87-
},
102+
self.controls.get_mut(&id).map(|c| c.update_size(size));
103+
self.flush_updates_set.insert(id);
104+
}
105+
Effect::Transform { id, absolute_transform } => {
106+
self.controls.get_mut(&id).map(|c| c.update_transform(&absolute_transform));
107+
self.flush_updates_set.insert(id);
108+
}
88109
Effect::Remove { id } => {
89-
self.controls.remove(&id);
90-
},
110+
self.controls.remove(&id).expect("process_events: self.controls.remove(&id)");
111+
}
91112
}
92113
}
114+
115+
for id in &self.flush_updates_set {
116+
self.controls.get_mut(id).map(|c| c.flush_updates(debug_lines));
117+
}
93118
}
94119

95120
pub fn update(&mut self, _delta: f32, debuglines: &DebugLines) {
96121
self.process_events(debuglines)
97122
}
98123

99-
pub fn mouse_move(&mut self, _x: i32, _y: i32) {
124+
pub fn mouse_move(&mut self, _x: i32, _y: i32) {}
100125

101-
}
102-
103-
pub fn mouse_down(&mut self, _x: i32, _y: i32) {
104-
105-
}
126+
pub fn mouse_down(&mut self, _x: i32, _y: i32) {}
106127

107-
pub fn mouse_up(&mut self, _x: i32, _y: i32) {
128+
pub fn mouse_up(&mut self, _x: i32, _y: i32) {}
108129

109-
}
110-
111-
pub fn render(&mut self, _gl: &gl::Gl, _target: &ColorBuffer, _vp_matrix: &na::Matrix4<f32>) {
112-
113-
}
130+
pub fn render(&mut self, _gl: &gl::Gl, _target: &ColorBuffer, _vp_matrix: &na::Matrix4<f32>) {}
114131
}

lesson-24-x/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ extern crate failure;
66
extern crate lesson_24_x_render as render;
77
extern crate resources;
88
extern crate lesson_24_x_render_gl as render_gl;
9-
#[macro_use]
10-
extern crate lesson_24_x_render_gl_derive as render_gl_derive;
119
extern crate floating_duration;
1210

1311
pub mod profiling;

lib/ui/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub enum Effect {
8484
Add { id: Ix, parent_id: Option<Ix> },
8585
Remove { id: Ix },
8686
Resize { id: Ix, size: Option<(i32, i32)> },
87+
Transform { id: Ix, absolute_transform: na::Projective3<f32> },
8788
}
8889

8990
pub enum ResizeDecision {

0 commit comments

Comments
 (0)