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.label;
14 
15 import std.datetime;
16 
17 import dsfml.graphics : Text, RenderWindow, Color;
18 import dsfml.system : Vector2f;
19 
20 import novelate.component;
21 import novelate.fonts;
22 import novelate.config;
23 
24 /// Wrapper around a label component.
25 final class Label : Component
26 {
27   private:
28   /// The text component.
29   Text _textComponent;
30   /// The text.
31   dstring _text;
32   /// The font name.
33   string _fontName;
34   /// The font.
35   Font _font;
36   /// The font size.
37   uint _fontSize;
38   /// The color.
39   Color _color;
40 
41   public:
42   final:
43   /// Creates a new label.
44   this()
45   {
46     super();
47 
48     _fontName = config.defaultFont;
49     _font = retrieveFont(_fontName, FontStyle.normal);
50     _fontSize = config.defaultFontSize;
51 
52     _textComponent = new Text();
53     _textComponent.setFont(_font);
54     _textComponent.setString("");
55     _textComponent.setCharacterSize(_fontSize);
56   }
57 
58   @property
59   {
60     /// Gets the text.
61     dstring text() { return _text; }
62 
63     /// Sets the text.
64     void text(dstring newText)
65     {
66       _text = newText;
67 
68       _textComponent.setString(_text);
69 
70       auto bounds = _textComponent.getLocalBounds();
71 
72       super.size = Vector2f(bounds.width, bounds.height);
73     }
74 
75     /// Gets the font name.
76     string fontName() { return _fontName; }
77 
78     /// Sets the font name.
79     void fontName(string newFontName)
80     {
81       _fontName = newFontName;
82 
83       _font = retrieveFont(_fontName, FontStyle.normal);
84       _textComponent.setFont(_font);
85     }
86 
87     /// Gets the font size.
88     uint fontSize() { return _fontSize; }
89 
90     /// Sets the font size.
91     void fontSize(uint newFontSize)
92     {
93       _fontSize = newFontSize;
94 
95       _textComponent.setCharacterSize(_fontSize);
96     }
97 
98     /// Gets the color.
99     Color color() { return _color; }
100 
101     /// Sets the color.
102     void color(Color newColor)
103     {
104       _color = newColor;
105 
106       _textComponent.setColor(_color);
107     }
108   }
109 
110   /// See: Component.render()
111   override void render(RenderWindow window)
112   {
113     window.draw(_textComponent);
114   }
115 
116   /// See: Component.refresh()
117   override void refresh(size_t width, size_t height)
118   {
119     size_t boxHeight;
120     if (width == 800)
121     {
122       boxHeight = config.defaultDialogueHeight800;
123     }
124     else if (width == 1024)
125     {
126       boxHeight = config.defaultDialogueHeight1024;
127     }
128     else if (width == 1280)
129     {
130       boxHeight = config.defaultDialogueHeight1280;
131     }
132 
133     _textComponent.position = Vector2f(config.defaultDialogueMargin + config.defaultDialoguePadding, ((height + config.defaultDialoguePadding) - boxHeight));
134 
135     updateInternalPosition(_textComponent.position);
136   }
137 
138   /// See: Component.updateSize()
139   override void updateSize()
140   {
141   }
142 
143   /// See: Component.updatePosition()
144   override void updatePosition()
145   {
146     _textComponent.position = super.position;
147   }
148 }