1 /** 2 * Window 3 */ 4 module d2d.sdl2.Window; 5 6 import std.algorithm; 7 import std.array; 8 import std.conv; 9 import std..string; 10 import d2d.sdl2; 11 12 /** 13 * A window is, as it name suggests, a window 14 * This window class is extremely minimalistic and is a port of the SDL_Window 15 */ 16 class Window { 17 18 private SDL_Window* window; 19 20 /** 21 * Returns the raw SDL data of this object 22 */ 23 @property SDL_Window* handle() { 24 return this.window; 25 } 26 27 /** 28 * Sets the window's brightness or gama multiplier 29 */ 30 @property void brightness(float b) { 31 ensureSafe(SDL_SetWindowBrightness(this.window, b)); 32 } 33 34 /** 35 * Gets the window's brightness or gama multiplier 36 */ 37 @property float brightness() { 38 return SDL_GetWindowBrightness(this.window); 39 } 40 41 /** 42 * Sets the display mode of this window 43 */ 44 @property void displayMode(SDL_DisplayMode dMode) { 45 ensureSafe(SDL_SetWindowDisplayMode(this.window, &dMode)); 46 } 47 48 /** 49 * Gets the display mode of this window 50 */ 51 @property SDL_DisplayMode displayMode() { 52 SDL_DisplayMode dMode; 53 ensureSafe(SDL_GetWindowDisplayMode(this.window, &dMode)); 54 return dMode; 55 } 56 57 /** 58 * Sets whether the window grabs input or not 59 */ 60 @property void grab(bool g) { 61 SDL_SetWindowGrab(this.window, g ? SDL_TRUE : SDL_FALSE); 62 } 63 64 /** 65 * Gets whether the window grabs input or not 66 */ 67 @property bool grab() { 68 return SDL_GetWindowGrab(this.window) == SDL_TRUE; 69 } 70 71 /** 72 * Sets the window's maximum size 73 */ 74 @property void maximumSize(iVector maxSize) { 75 SDL_SetWindowMaximumSize(this.window, maxSize.x, maxSize.y); 76 } 77 78 /** 79 * Gets the window's maximum size 80 */ 81 @property iVector maximumSize() { 82 iVector maxSize = new iVector(0, 0); 83 SDL_GetWindowMaximumSize(this.window, &maxSize.x, &maxSize.y); 84 return maxSize; 85 } 86 87 /** 88 * Sets the window's minimum size 89 */ 90 @property void minimumSize(iVector minSize) { 91 SDL_SetWindowMinimumSize(this.window, minSize.x, minSize.y); 92 } 93 94 /** 95 * Gets the window's minimum size 96 */ 97 @property iVector minimumSize() { 98 iVector minSize = new iVector(0, 0); 99 SDL_GetWindowMinimumSize(this.window, &minSize.x, &minSize.y); 100 return minSize; 101 } 102 103 /** 104 * Sets the window's size 105 */ 106 @property void size(iVector s) { 107 SDL_SetWindowSize(this.window, s.x, s.y); 108 } 109 110 /** 111 * Gets the window's size 112 */ 113 @property iVector size() { 114 iVector s = new iVector(0, 0); 115 SDL_GetWindowSize(this.window, &s.x, &s.y); 116 return s; 117 } 118 119 /** 120 * Sets the window's opacity 121 */ 122 @property void opacity(float o) { 123 ensureSafe(SDL_SetWindowOpacity(this.window, o)); 124 } 125 126 /** 127 * Gets the window's opacity 128 */ 129 @property float opacity() { 130 float o; 131 ensureSafe(SDL_GetWindowOpacity(this.window, &o)); 132 return o; 133 } 134 135 /** 136 * Sets the window's screen position 137 */ 138 @property position(iVector pos) { 139 SDL_SetWindowPosition(this.window, pos.x, pos.y); 140 } 141 142 /** 143 * Gets the window's screen position 144 */ 145 @property iVector position() { 146 iVector pos; 147 SDL_GetWindowPosition(this.window, &pos.x, &pos.y); 148 return pos; 149 } 150 151 /** 152 * Sets the window's title 153 */ 154 @property void title(string newTitle) { 155 SDL_SetWindowTitle(this.window, newTitle.toStringz); 156 } 157 158 /** 159 * Gets the window's title 160 */ 161 @property string title() { 162 return SDL_GetWindowTitle(this.window).to!string; 163 } 164 165 /** 166 * Sets the window's icon 167 */ 168 @property void icon(Surface i) { 169 SDL_SetWindowIcon(this.window, i.handle); 170 } 171 172 /** 173 * Gets the information about the window 174 * Returns an array of all the flags that describe this window 175 */ 176 @property SDL_WindowFlags[] info() { 177 immutable SDL_WindowFlags[] allPossibleFlags = [ 178 SDL_WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN_DESKTOP, SDL_WINDOW_OPENGL, 179 SDL_WINDOW_SHOWN, SDL_WINDOW_HIDDEN, SDL_WINDOW_BORDERLESS, 180 SDL_WINDOW_RESIZABLE, SDL_WINDOW_MINIMIZED, SDL_WINDOW_MAXIMIZED, 181 SDL_WINDOW_INPUT_GRABBED, 182 SDL_WINDOW_INPUT_FOCUS, 183 SDL_WINDOW_MOUSE_FOCUS, SDL_WINDOW_FOREIGN, SDL_WINDOW_ALLOW_HIGHDPI, 184 SDL_WINDOW_MOUSE_CAPTURE, SDL_WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_SKIP_TASKBAR, 185 SDL_WINDOW_UTILITY, SDL_WINDOW_TOOLTIP, SDL_WINDOW_POPUP_MENU 186 ]; 187 immutable uint windowFlags = SDL_GetWindowFlags(this.window); 188 return allPossibleFlags.filter!(flag => flag & windowFlags).array.dup; 189 } 190 191 /** 192 * Gets the window's renderer 193 * This function shouldn't be used because this function returns a renderer object 194 * Once the returned object gets garbage collected, it deletes all instances of this window's renderer 195 */ 196 @property Renderer renderer() { 197 return new Renderer(ensureSafe(SDL_GetRenderer(this.window))); 198 } 199 200 /** 201 * Constructor for a window; needs at least a width and a height 202 */ 203 this(int w, int h, SDL_WindowFlags flags = SDL_WINDOW_SHOWN, string title = "", 204 int x = SDL_WINDOWPOS_CENTERED, int y = SDL_WINDOWPOS_CENTERED) { 205 loadLibSDL(); 206 this.window = ensureSafe(SDL_CreateWindow(title.toStringz, x, y, w, h, flags)); 207 } 208 209 /** 210 * Creates the window from an already existing SDL_Window 211 */ 212 this(SDL_Window* alreadyExisting) { 213 this.window = alreadyExisting; 214 } 215 216 /** 217 * Ensures that SDL can properly dispose of the window 218 */ 219 ~this() { 220 SDL_DestroyWindow(this.window); 221 } 222 223 /** 224 * Marks the current window as a modal for the given parent window 225 */ 226 void setAsModalFor(Window parent) { 227 ensureSafe(SDL_SetWindowModalFor(this.window, parent.handle)); 228 } 229 230 /** 231 * Sets whether the window should be bordered or not 232 * Is not a property method because it is a setter without a getter 233 */ 234 void setBordered(bool borderedStatus) { 235 SDL_SetWindowBordered(this.window, borderedStatus ? SDL_TRUE : SDL_FALSE); 236 } 237 238 /** 239 * Sets whether the window should be resizable or not 240 * Is not a property method because it is a setter without a getter 241 */ 242 void setResizable(bool resizability) { 243 SDL_SetWindowResizable(this.window, resizability ? SDL_TRUE : SDL_FALSE); 244 } 245 246 /** 247 * Hides the window 248 */ 249 void hide() { 250 SDL_HideWindow(this.window); 251 } 252 253 /** 254 * Shows the window 255 */ 256 void show() { 257 SDL_ShowWindow(this.window); 258 } 259 260 /** 261 * Maximizes the window 262 */ 263 void maximize() { 264 SDL_MaximizeWindow(this.window); 265 } 266 267 /** 268 * Minimizes window in terms of size 269 */ 270 void minimize() { 271 SDL_MinimizeWindow(this.window); 272 } 273 274 /** 275 * Restores a window to its original state before a maximize or minimize 276 */ 277 void restore() { 278 SDL_RestoreWindow(this.window); 279 } 280 281 /** 282 * Moves the window to be on top of everything else and to have the focus 283 */ 284 void raise() { 285 SDL_RaiseWindow(this.window); 286 } 287 288 }