1 /* 2 * DSFML - The Simple and Fast Multimedia Library for D 3 * 4 * Copyright (c) 2013 - 2017 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 /** 26 * This class inherits all the functions of $(TRANSFORMABLE_LINK) (position, 27 * rotation, scale, bounds, ...) as well as the functions of $(SHAPE_LINK) 28 * (outline, color, texture, ...). 29 * 30 * Example: 31 * --- 32 * auto rectangle = new RectangleShape(); 33 * rectangle.size = Vector2f(100, 50); 34 * rectangle.outlineColor = Color.Red; 35 * rectangle.outlineThickness = 5; 36 * rectangle.position = Vector2f(10, 20); 37 * ... 38 * window.draw(rectangle); 39 * --- 40 * See_Also: 41 * $(SHAPE_LINK), $(CIRCLESHAPE_LINK), $(CONVEXSHAPE_LINK) 42 */ 43 module dsfml.graphics.rectangleshape; 44 45 import dsfml.graphics.shape; 46 import dsfml.system.vector2; 47 48 /** 49 * Specialized shape representing a rectangle. 50 */ 51 class RectangleShape:Shape 52 { 53 private Vector2f m_size; 54 55 /** 56 * Default constructor. 57 * 58 * Params: 59 * theSize = Size of the rectangle 60 */ 61 this(Vector2f theSize = Vector2f(0,0)) 62 { 63 size = theSize; 64 } 65 66 /// Destructor. 67 ~this() 68 { 69 import dsfml.system.config; 70 mixin(destructorOutput); 71 } 72 73 /// The point count for a rectangle is always 4. 74 @property 75 { 76 override uint pointCount() 77 { 78 return 4; 79 } 80 } 81 82 @property 83 { 84 /// The size of the rectangle. 85 Vector2f size(Vector2f theSize) 86 { 87 m_size = theSize; 88 update(); 89 return theSize; 90 } 91 /// ditto 92 Vector2f size() 93 { 94 return m_size; 95 } 96 } 97 98 /** 99 * Get a point of the rectangle. 100 * 101 * The result is undefined if index is out of the valid range. 102 * 103 * Params: 104 * index = Index of the point to get, in range [0 .. pointCount - 1] 105 * 106 * Returns: Index-th point of the shape. 107 */ 108 override Vector2f getPoint(uint index) const 109 { 110 switch (index) 111 { 112 default: 113 case 0: return Vector2f(0, 0); 114 case 1: return Vector2f(m_size.x, 0); 115 case 2: return Vector2f(m_size.x, m_size.y); 116 case 3: return Vector2f(0, m_size.y); 117 } 118 } 119 } 120 121 unittest 122 { 123 version(DSFML_Unittest_Graphics) 124 { 125 import std.stdio; 126 import dsfml.graphics; 127 128 writeln("Unit test for RectangleShape"); 129 auto window = new RenderWindow(VideoMode(800,600), "RectangleShape unittest"); 130 131 auto rectangleShape = new RectangleShape(Vector2f(10, 20)); 132 133 rectangleShape.fillColor = Color.Blue; 134 135 rectangleShape.outlineColor = Color.Green; 136 137 auto clock = new Clock(); 138 139 140 while(window.isOpen()) 141 { 142 Event event; 143 144 while(window.pollEvent(event)) 145 { 146 //no events gonna do stuffs! 147 } 148 149 //draws the shape for a while before closing the window 150 if(clock.getElapsedTime().total!"seconds" > 1) 151 { 152 window.close(); 153 } 154 155 window.clear(); 156 window.draw(rectangleShape); 157 window.display(); 158 } 159 160 writeln(); 161 } 162 }