1 /+ 2 The MIT License (MIT) 3 4 Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)> 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 +/ 24 25 module desgui.core.label; 26 27 import desgui.core.widget; 28 import desgui.core.textrect; 29 30 class DiLabelException: DiException 31 { 32 @safe pure nothrow this( string msg, string file=__FILE__, size_t line=__LINE__ ) 33 { super( msg, file, line ); } 34 } 35 36 class DiLabel: DiWidget 37 { 38 protected: 39 DiTextRect textrect; 40 41 DiGlyphRender glyphRender; 42 DiTextRender textRender; 43 44 void repos() 45 { 46 int hoffset = 0; 47 if( textalign != TextAlign.LEFT ) 48 { 49 int sum = textrect.glyph.next.x; 50 hoffset = ( textalign == TextAlign.RIGHT ) ? 51 rect.w - sum : 52 (rect.w - sum) / 2; 53 } 54 textrect.pos = ivec2( hoffset, rect.h * baselineCoef ); 55 } 56 57 final void updateRenders() 58 { textrect.setRenders( textRender, glyphRender ); repos(); } 59 60 TextAlign textalign = TextAlign.LEFT; 61 62 float heightCoef = 0.7; 63 float baselineCoef = 0.8; 64 65 ivec2 oldsize; 66 67 public: 68 69 enum TextAlign { LEFT, CENTER, RIGHT }; 70 71 this( DiWidget par, in irect rr, wstring str="" ) 72 { 73 super( par ); 74 textRender = new DiBaseLineTextRender; 75 glyphRender = ctx.baseGlyphRender(); 76 textrect = new DiTextRect( textRender, glyphRender, ctx.draw.rect() ); 77 78 draw.connect({ textrect.draw(); }); 79 80 reshape.connect( (r) 81 { 82 if( r.size.x == oldsize.x && r.size.y == oldsize.y ) return; 83 DiGlyphParam p = textrect.param; 84 p.height = cast(uint)( rect.h * heightCoef ); 85 textrect.param = p; 86 repos(); 87 oldsize = r.size; 88 }); 89 90 processEventMask = 0; 91 92 textrect.text = str; 93 reshape( rr ); 94 } 95 96 @property 97 { 98 void textAlign( TextAlign ta ) { textalign = ta; repos(); } 99 TextAlign textAlign() const { return textalign; } 100 101 void text( wstring str ) { textrect.text = str; repos(); } 102 wstring text() const { return textrect.text; } 103 104 void color( in col4 clr ) 105 { 106 if( textrect.param.color == clr ) return; 107 DiGlyphParam buf = textrect.param; 108 buf.color = clr; 109 textrect.param = buf; 110 repos(); 111 } 112 col4 color() const { return textrect.param.color; } 113 } 114 115 void setGlyphRender( DiGlyphRender gr ) 116 { 117 if( glyphRender == gr ) return; 118 glyphRender = gr; 119 updateRenders(); 120 } 121 122 void setTextRender( DiTextRender tr ) 123 { 124 if( textRender == tr ) return; 125 textRender = tr; 126 updateRenders(); 127 } 128 }