1 /** 2 Grimoire 3 Copyright (c) 2017 Enalye 4 5 This software is provided 'as-is', without any express or implied warranty. 6 In no event will the authors be held liable for any damages arising 7 from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute 11 it freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; 14 you must not claim that you wrote the original software. 15 If you use this software in a product, an acknowledgment 16 in the product documentation would be appreciated but 17 is not required. 18 19 2. Altered source versions must be plainly marked as such, 20 and must not be misrepresented as being the original software. 21 22 3. This notice may not be removed or altered from any source distribution. 23 */ 24 25 module ui.label; 26 27 import std..string; 28 29 import derelict.sdl2.sdl; 30 import derelict.sdl2.ttf; 31 32 import core.all; 33 import common.all; 34 35 import render.texture; 36 import render.sprite; 37 import render.font; 38 39 import ui.widget; 40 41 class Label: Widget { 42 private { 43 string _text; 44 Font _font; 45 Texture _texture; 46 Sprite _sprite; 47 Color _color = Color.white; 48 } 49 50 @property { 51 Vec2f scale() const { 52 return _sprite.scale; 53 } 54 Vec2f scale(Vec2f s) { 55 _sprite.scale = s; 56 return _sprite.scale; 57 } 58 59 string text() const { 60 return _text; 61 } 62 string text(string newText) { 63 _text = newText; 64 reload(); 65 return _text; 66 } 67 68 Font font() const { 69 return cast(Font)_font; 70 } 71 Font font(Font newFont) { 72 _font = newFont; 73 reload(); 74 return _font; 75 } 76 77 Sprite sprite() { 78 return _sprite; 79 } 80 81 Color color() const { return _color; } 82 Color color(Color newColor) { 83 _color = newColor; 84 reload(); 85 return _color; 86 } 87 88 bool isLoaded() const { 89 return _sprite.texture !is null; 90 } 91 } 92 93 this(string newText) { 94 this(); 95 _isInteractable = false; 96 _text = newText; 97 reload(); 98 } 99 100 101 this() { 102 _texture = new Texture; 103 _sprite = _texture; 104 _font = fetch!Font("VeraMoBd"); 105 } 106 107 override void onEvent(Event event) {} 108 override void update(float deltaTime) {} 109 110 override void draw() { 111 if(_text.length > 0) 112 _sprite.draw(_position); 113 } 114 115 void reload() { 116 if(_font is null) 117 return; 118 119 if ((_text.length > 0) && _font.isLoaded) { 120 _texture.loadFromSurface(TTF_RenderUTF8_Blended(_font.font, toStringz(_text), _color.toSDL())); 121 122 version(Windows) { 123 //Hack: On windows, TTF_Render functions for UTF8 strings 124 //will randomly fail and create a 0x0 texture, 125 //So we make sure that the texture is created again. 126 if(_texture.width == 0) 127 _texture.loadFromSurface(TTF_RenderUTF8_Blended(_font.font, toStringz(_text), _color.toSDL())); 128 } 129 } 130 _sprite = _texture; 131 _sprite.size *= _font.scale; 132 _size = _sprite.size; 133 } 134 }