1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 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 use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 module dsfml.graphics.rectangleshape; 21 22 import dsfml.graphics.shape; 23 import dsfml.system.vector2; 24 25 /++ 26 + Specialized shape representing a rectangle. 27 + 28 + This class inherits all the functions of Transformable (position, rotation, scale, bounds, ...) as well as the functions of Shape (outline, color, texture, ...). 29 + 30 + Authors: Laurent Gomila, Jeremy DeHaan 31 + See_Also: http://www.sfml-dev.org/documentation/2.0/classsf_1_1RectangleShape.php#details 32 +/ 33 class RectangleShape:Shape 34 { 35 private Vector2f m_size; 36 37 this(Vector2f theSize = Vector2f(0,0)) 38 { 39 size = theSize; 40 } 41 42 ~this() 43 { 44 import dsfml.system.config; 45 mixin(destructorOutput); 46 } 47 48 /// The point count for a rectangle is always 4. 49 @property 50 { 51 override uint pointCount() 52 { 53 return 4; 54 } 55 } 56 57 /// The size of the rectangle. 58 @property 59 { 60 Vector2f size(Vector2f theSize) 61 { 62 m_size = theSize; 63 update(); 64 return theSize; 65 } 66 Vector2f size() 67 { 68 return m_size; 69 } 70 } 71 72 /** 73 * Get a point of the rectangle. 74 * 75 * The result is undefined if index is out of the valid range. 76 * 77 * Params: 78 * index = Index of the point to get, in range [0 .. pointCount - 1] 79 * 80 * Returns: Index-th point of the shape. 81 */ 82 override Vector2f getPoint(uint index) const 83 { 84 switch (index) 85 { 86 default: 87 case 0: return Vector2f(0, 0); 88 case 1: return Vector2f(m_size.x, 0); 89 case 2: return Vector2f(m_size.x, m_size.y); 90 case 3: return Vector2f(0, m_size.y); 91 } 92 } 93 } 94 95 unittest 96 { 97 version(DSFML_Unittest_Graphics) 98 { 99 import std.stdio; 100 import dsfml.graphics; 101 102 writeln("Unit test for RectangleShape"); 103 auto window = new RenderWindow(VideoMode(800,600), "RectangleShape unittest"); 104 105 auto rectangleShape = new RectangleShape(Vector2f(10, 20)); 106 107 rectangleShape.fillColor = Color.Blue; 108 109 rectangleShape.outlineColor = Color.Green; 110 111 auto clock = new Clock(); 112 113 114 while(window.isOpen()) 115 { 116 Event event; 117 118 while(window.pollEvent(event)) 119 { 120 //no events gonna do stuffs! 121 } 122 123 //draws the shape for a while before closing the window 124 if(clock.getElapsedTime().total!"seconds" > 1) 125 { 126 window.close(); 127 } 128 129 window.clear(); 130 window.draw(rectangleShape); 131 window.display(); 132 } 133 134 writeln(); 135 } 136 }