1 /*
2 *******************************************************************************************
3 * Dgame (a D game framework) - Copyright (c) Randy Schütt
4 * 
5 * This software is provided 'as-is', without any express or implied warranty.
6 * In no event will the authors be held liable for any damages arising from
7 * the use of this software.
8 * 
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 * 
13 * 1. The origin of this software must not be misrepresented; you must not claim
14 *    that you wrote the original software. If you use this software in a product,
15 *    an acknowledgment in the product documentation would be appreciated but is
16 *    not required.
17 * 
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 *    misrepresented as being the original software.
20 * 
21 * 3. This notice may not be removed or altered from any source distribution.
22 *******************************************************************************************
23 */
24 module Dgame.Graphics.Moveable;
25 
26 private {
27 	import derelict.opengl3.gl;
28 	
29 	import Dgame.Math.Vector2;
30 }
31 
32 /**
33  * Basic implementation for moveable objects
34  *
35  * Author: rschuett
36  */
37 abstract class Moveable {
38 private:
39 	Vector2f _position;
40 	
41 protected:
42 	/**
43 	 * Overloadable method if the position is moved.
44 	 */
45 	void _positionMoved(float dx, float dy) {
46 	
47 	}
48 	
49 	/**
50 	 * Overloadable method if the position is reset.
51 	 */
52 	void _positionReset(float nx, float ny) {
53 	
54 	}
55 
56 	/**
57 	* Apply translation to the object.
58 	*/
59 	void _applyTranslation() const {
60 		glTranslatef(this._position.x, this._position.y, 0f);
61 	}
62 
63 public:	
64 	/**
65 	 * Overloadable method to reset the position.
66 	 * The position is set to 0|0.
67 	 */
68 	void resetTranslation() {
69 		this.setPosition(0, 0);
70 	}
71 
72 final:
73 	/**
74 	 * Setting a new position.
75 	 */
76 	void setPosition(ref const Vector2f vec) {
77 		this._position = vec;
78 		this._positionReset(vec.x, vec.y);
79 	}
80 	
81 	/**
82 	 * Setting a new position.
83 	 */
84 	void setPosition(float x, float y) {
85 		this._position.set(x, y);
86 		this._positionReset(x, y);
87 	}
88 	
89 	/**
90 	 * Returns the current position.
91 	 */
92 	ref const(Vector2f) getPosition() const pure nothrow {
93 		return this._position;
94 	}
95 
96 	/**
97 	 * Returns an inout reference to the coordinates.
98 	 * It should be used to set or get the x and y coordinates.
99 	 * If you (re)set the position herewith,
100 	 * it doesn't appear in <b>_positionMoved</b> or <b>_positionReset</b>.
101 	 */
102 	@property
103 	ref inout(Vector2f) position() inout pure nothrow {
104 		return this._position;
105 	}
106 
107 	/**
108 	 * Move the current position by vec.
109 	 */
110 	void move(ref const Vector2f vec) {
111 		this._position += vec;
112 		this._positionMoved(vec.x, vec.y);
113 	}
114 	
115 	/**
116 	 * Move the current position by x|y.
117 	 */
118 	void move(float x, float y) {
119 		this._position.move(x, y);
120 		this._positionMoved(x, y);
121 	}
122 }