1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5  *
6  * This software is provided 'as-is', without any express or implied warranty.
7  * In no event will the authors be held liable for any damages arising from the
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  *
24  *
25  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29  * This class inherits all the functions of $(TRANSFORMABLE_LINK) (position,
30  * rotation, scale, bounds, ...) as well as the functions of $(SHAPE_LINK)
31  * (outline, color, texture, ...).
32  *
33  * Example:
34  * ---
35  * auto rectangle = new RectangleShape();
36  * rectangle.size = Vector2f(100, 50);
37  * rectangle.outlineColor = Color.Red;
38  * rectangle.outlineThickness = 5;
39  * rectangle.position = Vector2f(10, 20);
40  * ...
41  * window.draw(rectangle);
42  * ---
43  * See_Also:
44  * $(SHAPE_LINK), $(CIRCLESHAPE_LINK), $(CONVEXSHAPE_LINK)
45  */
46 module dsfml.graphics.rectangleshape;
47 
48 import dsfml.graphics.shape;
49 import dsfml.system.vector2;
50 
51 /**
52  * Specialized shape representing a rectangle.
53  */
54 class RectangleShape:Shape
55 {
56     private Vector2f m_size;
57 
58     /**
59      * Default constructor.
60      *
61      * Params:
62      *  theSize = Size of the rectangle
63      */
64     this(Vector2f theSize = Vector2f(0,0))
65     {
66         size = theSize;
67     }
68 
69     /// Destructor.
70     ~this()
71     {
72         import dsfml.system.config;
73         mixin(destructorOutput);
74     }
75 
76     /// The point count for a rectangle is always 4.
77     @property
78     {
79         override uint pointCount() const
80         {
81             return 4;
82         }
83     }
84 
85     @property
86     {
87         /// The size of the rectangle.
88         Vector2f size(Vector2f theSize)
89         {
90             m_size = theSize;
91             update();
92             return theSize;
93         }
94         /// ditto
95         Vector2f size()
96         {
97             return m_size;
98         }
99     }
100 
101     /**
102      * Get a point of the rectangle.
103      *
104      * The result is undefined if index is out of the valid range.
105      *
106      * Params:
107      * 		index	= Index of the point to get, in range [0 .. pointCount - 1]
108      *
109      * Returns: Index-th point of the shape.
110      */
111     override Vector2f getPoint(uint index) const
112     {
113         switch (index)
114         {
115             default:
116             case 0: return Vector2f(0, 0);
117             case 1: return Vector2f(m_size.x, 0);
118             case 2: return Vector2f(m_size.x, m_size.y);
119             case 3: return Vector2f(0, m_size.y);
120         }
121     }
122 }
123 
124 unittest
125 {
126     version(DSFML_Unittest_Graphics)
127     {
128         import std.stdio;
129         import dsfml.graphics;
130 
131         writeln("Unit test for RectangleShape");
132         auto window = new RenderWindow(VideoMode(800,600), "RectangleShape unittest");
133 
134         auto rectangleShape = new RectangleShape(Vector2f(10, 20));
135 
136         rectangleShape.fillColor = Color.Blue;
137 
138         rectangleShape.outlineColor = Color.Green;
139 
140         auto clock = new Clock();
141 
142 
143         while(window.isOpen())
144         {
145             Event event;
146 
147             while(window.pollEvent(event))
148             {
149                 //no events gonna do stuffs!
150             }
151 
152             //draws the shape for a while before closing the window
153             if(clock.getElapsedTime().asSeconds() > 1)
154             {
155                 window.close();
156             }
157 
158             window.clear();
159             window.draw(rectangleShape);
160             window.display();
161         }
162 
163         writeln();
164     }
165 }