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 nudsfml.graphics.rectangleshape; 47 48 import nudsfml.graphics.shape; 49 import nudsfml.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 } 73 74 /// The point count for a rectangle is always 4. 75 @property 76 { 77 override uint pointCount() const 78 { 79 return 4; 80 } 81 } 82 83 @property 84 { 85 /// The size of the rectangle. 86 Vector2f size(Vector2f theSize) 87 { 88 m_size = theSize; 89 update(); 90 return theSize; 91 } 92 /// ditto 93 Vector2f size() 94 { 95 return m_size; 96 } 97 } 98 99 /** 100 * Get a point of the rectangle. 101 * 102 * The result is undefined if index is out of the valid range. 103 * 104 * Params: 105 * index = Index of the point to get, in range [0 .. pointCount - 1] 106 * 107 * Returns: Index-th point of the shape. 108 */ 109 override Vector2f getPoint(uint index) const 110 { 111 switch (index) 112 { 113 default: 114 case 0: return Vector2f(0, 0); 115 case 1: return Vector2f(m_size.x, 0); 116 case 2: return Vector2f(m_size.x, m_size.y); 117 case 3: return Vector2f(0, m_size.y); 118 } 119 } 120 } 121 122 unittest 123 { 124 version(DSFML_Unittest_Graphics) 125 { 126 import std.stdio; 127 import nudsfml.graphics; 128 129 writeln("Unit test for RectangleShape"); 130 auto window = new RenderWindow(VideoMode(800,600), "RectangleShape unittest"); 131 132 auto rectangleShape = new RectangleShape(Vector2f(10, 20)); 133 134 rectangleShape.fillColor = Color.Blue; 135 136 rectangleShape.outlineColor = Color.Green; 137 138 auto clock = new Clock(); 139 140 141 while(window.isOpen()) 142 { 143 Event event; 144 145 while(window.pollEvent(event)) 146 { 147 //no events gonna do stuffs! 148 } 149 150 //draws the shape for a while before closing the window 151 if(clock.getElapsedTime().asSeconds() > 1) 152 { 153 window.close(); 154 } 155 156 window.clear(); 157 window.draw(rectangleShape); 158 window.display(); 159 } 160 161 writeln(); 162 } 163 }