1 /**
2 * Copyright © Novelate 2020
3 * License: MIT (https://github.com/Novelate/NovelateEngine/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 * Website: https://novelate.com/
6 * ------
7 * Novelate is a free and open-source visual novel engine and framework written in the D programming language.
8 * It can be used freely for both personal and commercial projects.
9 * ------
10 * Module Description:
11 * A label is a simple component for displaying text.
12 */
13 module novelate.ui.label;
14 
15 import std.datetime;
16 
17 import novelate.ui.component;
18 import novelate.fonts;
19 import novelate.config;
20 import novelate.buildstate;
21 
22 /// Wrapper around a label component.
23 final class Label : Component
24 {
25   private:
26   /// The text component.
27   ExternalText _textComponent;
28   /// The text.
29   dstring _text;
30   /// The font name.
31   string _fontName;
32   /// The font.
33   ExternalFont _font;
34   /// The font size.
35   size_t _fontSize;
36   /// The color.
37   Paint _color;
38 
39   public:
40   final:
41   /// Creates a new label.
42   this()
43   {
44     super();
45 
46     _fontName = config.defaultFont;
47     _font = retrieveFont(_fontName, FontStyle.normal);
48     _fontSize = config.defaultFontSize;
49 
50     _textComponent = new ExternalText;
51     _textComponent.setFont(_font);
52     _textComponent.setString("");
53     _textComponent.setCharacterSize(_fontSize);
54   }
55 
56   @property
57   {
58     /// Gets the text.
59     dstring text() { return _text; }
60 
61     /// Sets the text.
62     void text(dstring newText)
63     {
64       _text = newText;
65 
66       _textComponent.setString(_text);
67 
68       auto bounds = _textComponent.bounds;
69 
70       super.size = FloatVector(bounds.x, bounds.y);
71     }
72 
73     /// Gets the font name.
74     string fontName() { return _fontName; }
75 
76     /// Sets the font name.
77     void fontName(string newFontName)
78     {
79       _fontName = newFontName;
80 
81       _font = retrieveFont(_fontName, FontStyle.normal);
82       _textComponent.setFont(_font);
83     }
84 
85     /// Gets the font size.
86     size_t fontSize() { return _fontSize; }
87 
88     /// Sets the font size.
89     void fontSize(size_t newFontSize)
90     {
91       _fontSize = newFontSize;
92 
93       _textComponent.setCharacterSize(_fontSize);
94     }
95 
96     /// Gets the color.
97     Paint color() { return _color; }
98 
99     /// Sets the color.
100     void color(Paint newColor)
101     {
102       _color = newColor;
103 
104       _textComponent.setColor(_color);
105     }
106   }
107 
108   /// See: Component.render()
109   override void render(ExternalWindow window)
110   {
111     _textComponent.draw(window);
112   }
113 
114   /// See: Component.refresh()
115   override void refresh(size_t width, size_t height)
116   {
117     size_t boxHeight;
118     if (width == 800)
119     {
120       boxHeight = config.defaultDialogueHeight800;
121     }
122     else if (width == 1024)
123     {
124       boxHeight = config.defaultDialogueHeight1024;
125     }
126     else if (width == 1280)
127     {
128       boxHeight = config.defaultDialogueHeight1280;
129     }
130 
131     _textComponent.position = FloatVector(config.defaultDialogueMargin + config.defaultDialoguePadding, ((height + config.defaultDialoguePadding) - boxHeight));
132 
133     updateInternalPosition(_textComponent.position);
134   }
135 
136   /// See: Component.updateSize()
137   override void updateSize()
138   {
139   }
140 
141   /// See: Component.updatePosition()
142   override void updatePosition()
143   {
144     _textComponent.position = super.position;
145   }
146 
147   static if (isManualMemory)
148   {
149     /// See: Component.clean()
150     override void clean()
151     {
152       if (_textComponent)
153       {
154         _textComponent.clean();
155       }
156     }
157   }
158 }