1 /* 2 Copyright (c) 2014-2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.ui.textline; 30 31 import std.stdio; 32 33 import derelict.opengl.gl; 34 import derelict.opengl.glu; 35 import derelict.freetype.ft; 36 37 import dlib.core.memory; 38 import dlib.math.vector; 39 import dlib.image.color; 40 41 import dgl.core.interfaces; 42 import dgl.ui.font; 43 44 enum Alignment 45 { 46 Left, 47 Right, 48 Center 49 } 50 51 class TextLine: Drawable 52 { 53 Font font; 54 Vector2f position; 55 float scaling; 56 Alignment alignment; 57 Color4f color; 58 string text; 59 float textWidth; 60 61 this(Font font, string text, Vector2f position = Vector2f(0, 0)) 62 { 63 this.font = font; 64 this.text = text; 65 this.position = position; 66 this.scaling = 1.0f; 67 this.textWidth = font.textWidth(text); 68 this.alignment = Alignment.Left; 69 this.color = Color4f(0, 0, 0); 70 } 71 72 override void draw(double dt) 73 { 74 glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT); 75 glDisable(GL_LIGHTING); 76 glEnable(GL_TEXTURE_2D); 77 glDisable(GL_DEPTH_TEST); 78 glEnable(GL_BLEND); 79 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 80 81 glColor4f(color.r, color.g, color.b, color.a); 82 83 glPushMatrix(); 84 glTranslatef(position.x, position.y, 0); 85 if (alignment == Alignment.Center) 86 glTranslatef(-textWidth * 0.5f, 0, 0); 87 if (alignment == Alignment.Right) 88 glTranslatef(-textWidth, 0, 0); 89 glScalef(scaling, scaling, scaling); 90 font.draw(text); 91 glPopMatrix(); 92 93 glPopAttrib(); 94 } 95 96 void free() 97 { 98 Delete(this); 99 } 100 101 void setFont(Font font) 102 { 103 this.font = font; 104 this.textWidth = font.textWidth(text); 105 } 106 107 void setText(string text) 108 { 109 this.text = text; 110 this.textWidth = font.textWidth(text); 111 } 112 113 void setPosition(Vector2f pos) 114 { 115 this.position = pos; 116 } 117 118 void setPosition(float x, float y) 119 { 120 this.position.x = x; 121 this.position.y = y; 122 } 123 }