Skip to content

Commit 31e8580

Browse files
committed
Merge #136
136: Implement multiple traits for all objects r=alteous a=vitvakatu
2 parents e5cd473 + 753e600 commit 31e8580

File tree

21 files changed

+134
-67
lines changed

21 files changed

+134
-67
lines changed

src/animation.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
use cgmath;
104104
use froggy;
105105
use mint;
106+
use std::hash::{Hash, Hasher};
106107
use std::sync::mpsc;
107108

108109
use Object;
@@ -226,6 +227,7 @@ enum Operation {
226227
type Message = (froggy::WeakPointer<ActionData>, Operation);
227228

228229
/// Controls the playback properties of an animation
230+
#[derive(Clone, Debug)]
229231
pub struct Action {
230232
/// Message channel to parent mixer.
231233
tx: mpsc::Sender<Message>,
@@ -234,6 +236,26 @@ pub struct Action {
234236
pointer: froggy::Pointer<ActionData>,
235237
}
236238

239+
impl PartialEq for Action {
240+
fn eq(
241+
&self,
242+
other: &Action,
243+
) -> bool {
244+
self.pointer == other.pointer
245+
}
246+
}
247+
248+
impl Eq for Action {}
249+
250+
impl Hash for Action {
251+
fn hash<H: Hasher>(
252+
&self,
253+
state: &mut H,
254+
) {
255+
self.pointer.hash(state);
256+
}
257+
}
258+
237259
/// Internal data for an animation action.
238260
struct ActionData {
239261
/// The animation data for this action.

src/audio.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl AudioData {
110110
///
111111
/// You must add it to the scene to play sounds.
112112
/// You may create several `Source`s to play sounds simultaneously.
113+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
113114
pub struct Source {
114115
pub(crate) object: Object,
115116
}
@@ -125,27 +126,27 @@ impl Source {
125126
clip: &Clip,
126127
) {
127128
let msg = HubOperation::SetAudio(Operation::Append(clip.clone()));
128-
let _ = self.object.tx.send((self.as_ref().downgrade(), msg));
129+
let _ = self.object.tx.send((self.object.node.downgrade(), msg));
129130
}
130131

131132
/// Pause current sound.
132133
///
133134
/// You can [`resume`](struct.Source.html#method.resume) playback.
134135
pub fn pause(&self) {
135136
let msg = HubOperation::SetAudio(Operation::Pause);
136-
let _ = self.object.tx.send((self.as_ref().downgrade(), msg));
137+
let _ = self.object.tx.send((self.object.node.downgrade(), msg));
137138
}
138139

139140
/// Resume playback after [`pausing`](struct.Source.html#method.pause).
140141
pub fn resume(&self) {
141142
let msg = HubOperation::SetAudio(Operation::Resume);
142-
let _ = self.object.tx.send((self.as_ref().downgrade(), msg));
143+
let _ = self.object.tx.send((self.object.node.downgrade(), msg));
143144
}
144145

145146
/// Stop the playback by emptying the queue.
146147
pub fn stop(&self) {
147148
let msg = HubOperation::SetAudio(Operation::Stop);
148-
let _ = self.object.tx.send((self.as_ref().downgrade(), msg));
149+
let _ = self.object.tx.send((self.object.node.downgrade(), msg));
149150
}
150151

151152
/// Adjust playback volume.
@@ -156,7 +157,7 @@ impl Source {
156157
volume: f32,
157158
) {
158159
let msg = HubOperation::SetAudio(Operation::SetVolume(volume));
159-
let _ = self.object.tx.send((self.as_ref().downgrade(), msg));
160+
let _ = self.object.tx.send((self.object.node.downgrade(), msg));
160161
}
161162
}
162163

src/camera.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ use cgmath;
6060
use mint;
6161
use std::ops;
6262

63-
use node::NodePointer;
6463
use object::Object;
6564

6665
/// The Z values of the near and far clipping planes of a camera's projection.
@@ -86,6 +85,7 @@ impl From<ops::RangeFrom<f32>> for ZRange {
8685
}
8786

8887
/// A camera's projection.
88+
#[derive(Clone, Debug, PartialEq)]
8989
pub enum Projection {
9090
/// An orthographic projection.
9191
Orthographic(Orthographic),
@@ -96,6 +96,7 @@ pub enum Projection {
9696
/// Camera is used to render Scene with specific [`Projection`].
9797
///
9898
/// [`Projection`]: enum.Projection.html
99+
#[derive(Clone, Debug, PartialEq)]
99100
pub struct Camera {
100101
pub(crate) object: Object,
101102

@@ -157,24 +158,7 @@ impl Projection {
157158
}
158159
}
159160

160-
impl AsRef<NodePointer> for Camera {
161-
fn as_ref(&self) -> &NodePointer {
162-
&self.object.node
163-
}
164-
}
165-
166-
impl ops::Deref for Camera {
167-
type Target = Object;
168-
fn deref(&self) -> &Object {
169-
&self.object
170-
}
171-
}
172-
173-
impl ops::DerefMut for Camera {
174-
fn deref_mut(&mut self) -> &mut Object {
175-
&mut self.object
176-
}
177-
}
161+
three_object_wrapper!(Camera);
178162

179163
/// Orthographic projection parameters.
180164
#[derive(Clone, Debug, PartialEq)]

src/controls/first_person.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use input::{axis, Input, Key};
77
use object::Object;
88
use std::f32::consts::PI;
99

10-
#[derive(Clone, Debug)]
10+
#[derive(Clone, Debug, PartialEq)]
1111
struct Axes {
1212
pub forward: Option<axis::Key>,
1313
pub strafing: Option<axis::Key>,
@@ -31,6 +31,7 @@ impl Default for Axes {
3131
}
3232

3333
/// Controls for first person camera.
34+
#[derive(Clone, Debug, PartialEq)]
3435
pub struct FirstPerson {
3536
object: Object,
3637
position: mint::Point3<f32>,
@@ -45,6 +46,7 @@ pub struct FirstPerson {
4546
}
4647

4748
/// Constructs custom [`FirstPerson`](struct.FirstPerson.html) controls.
49+
#[derive(Clone, Debug, PartialEq)]
4850
pub struct Builder {
4951
object: Object,
5052
position: mint::Point3<f32>,

src/controls/orbit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use object::Object;
1111
/// Camera is rotating around the fixed point without any restrictions.
1212
/// By default, it uses left mouse button as control button (hold it to rotate) and mouse wheel
1313
/// to adjust distance to the central point.
14+
#[derive(Clone, Debug)]
1415
pub struct Orbit {
1516
object: Object,
1617
transform: TransformInternal,
@@ -20,6 +21,7 @@ pub struct Orbit {
2021
}
2122

2223
/// Helper struct to construct [`Orbit`](struct.Orbit.html) with desired settings.
24+
#[derive(Clone, Debug)]
2325
pub struct Builder {
2426
object: Object,
2527
position: mint::Point3<f32>,

src/factory/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::path::{Path, PathBuf};
88

99
use animation;
1010
use camera;
11-
use color;
1211
use cgmath::Vector3;
12+
use color;
1313
use genmesh::{Polygon, Triangulate};
1414
use gfx;
1515
use gfx::format::I8Norm;
@@ -124,7 +124,11 @@ impl Factory {
124124
let object = self.hub.lock().unwrap().spawn_scene();
125125
let hub = self.hub.clone();
126126
let background = scene::Background::Color(color::BLACK);
127-
Scene { object, hub, background }
127+
Scene {
128+
object,
129+
hub,
130+
background,
131+
}
128132
}
129133

130134
/// Create new [Orthographic] Camera.

src/hub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Hub {
132132
let tx = self.message_tx.clone();
133133
let node = self.nodes.create(NodeInternal {
134134
scene_id: Some(uid),
135-
.. SubNode::Scene.into()
135+
..SubNode::Scene.into()
136136
});
137137
Object { node, tx }
138138
}

src/input/axis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use glutin::VirtualKeyCode as KeyCode;
44

55
/// Two buttons responsible for opposite directions along specific axis.
6-
#[derive(Clone, Copy, Debug, PartialEq)]
6+
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
77
pub struct Key {
88
/// Key for "negative" direction
99
pub neg: KeyCode,
@@ -19,7 +19,7 @@ pub struct Key {
1919
/// - `id = 2` for mouse wheel moves.
2020
///
2121
/// However, these `id`s depend on hardware and may vary on different machines.
22-
#[derive(Clone, Copy, Debug, PartialEq)]
22+
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
2323
pub struct Raw {
2424
/// Axis id.
2525
pub id: u8,

src/light.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use render::{BackendResources, ShadowFormat};
1010

1111
/// `ShadowMap` is used to render shadows from [`PointLight`](struct.PointLight.html)
1212
/// and [`DirectionalLight`](struct.DirectionalLight.html).
13-
#[derive(Clone, Debug)]
13+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1414
pub struct ShadowMap {
1515
pub(crate) resource: gfx::handle::ShaderResourceView<BackendResources, f32>,
1616
pub(crate) target: gfx::handle::DepthStencilView<BackendResources, ShadowFormat>,
1717
}
1818

19-
#[derive(Clone, Debug)]
19+
#[derive(Clone, Debug, PartialEq)]
2020
pub(crate) enum ShadowProjection {
2121
Orthographic(Orthographic),
2222
}
@@ -33,6 +33,7 @@ impl ShadowMap {
3333

3434
/// Omni-directional, fixed-intensity and fixed-color light source that affects
3535
/// all objects in the scene equally.
36+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
3637
pub struct Ambient {
3738
pub(crate) object: Object,
3839
}
@@ -47,6 +48,7 @@ impl Ambient {
4748
/// The light source that illuminates all objects equally from a given direction,
4849
/// like an area light of infinite size and infinite distance from the scene;
4950
/// there is shading, but cannot be any distance falloff.
51+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
5052
pub struct Directional {
5153
pub(crate) object: Object,
5254
pub(crate) shadow: Option<ShadowMap>,
@@ -93,6 +95,7 @@ impl Directional {
9395
/// takes color of the "ground". In other cases, color is determined as
9496
/// interpolation between colors of upper and lower hemispheres, depending on
9597
/// how much the normal is oriented to the upper and the lower hemisphere.
98+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
9699
pub struct Hemisphere {
97100
pub(crate) object: Object,
98101
}
@@ -105,6 +108,7 @@ impl Hemisphere {
105108
}
106109

107110
/// Light originates from a single point, and spreads outward in all directions.
111+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
108112
pub struct Point {
109113
pub(crate) object: Object,
110114
}

src/macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ macro_rules! three_object_wrapper {
4343
};
4444
($($name:ident::$field:ident),*) => {
4545
$(
46+
impl AsRef<$crate::Object> for $name {
47+
fn as_ref(&self) -> &$crate::Object {
48+
&self.$field
49+
}
50+
}
51+
4652
impl ::std::ops::Deref for $name {
4753
type Target = $crate::Object;
4854
fn deref(&self) -> &$crate::Object {

0 commit comments

Comments
 (0)