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 dialogue box is the box that is used to display the dialogues.
12 */
13 module novelate.ui.dialoguebox;
14 
15 import novelate.ui.component;
16 import novelate.buildstate;
17 
18 /// A dialogue box component.
19 final class DialogueBox : Component
20 {
21   private:
22   /// The rectangle shape used for the dialogue box.
23   ExternalRectangleShape _rect;
24   /// The color of the dialogue box.
25   Paint _color;
26 
27   public:
28   final:
29   package (novelate)
30   {
31     /// Creates a new dialogue box.
32     this()
33     {
34       super();
35 
36       _rect = new ExternalRectangleShape(FloatVector(cast(float)0, cast(float)0));
37     }
38   }
39 
40   @property
41   {
42     /// Gets the color of the dialogue box.
43     Paint color() { return _color; }
44 
45     /// Sets the color of the dialogue box.
46     void color(Paint newColor)
47     {
48       _color = newColor;
49 
50       _rect.fillColor = _color;
51     }
52   }
53 
54   /// See: Component.render()
55   override void render(ExternalWindow window)
56   {
57     _rect.draw(window);
58   }
59 
60   /// See: Component.refresh()
61   override void refresh(size_t width, size_t height)
62   {
63     import novelate.config;
64 
65     if (width == 800)
66     {
67       super.size = FloatVector(width - (config.defaultDialogueMargin * 2), config.defaultDialogueHeight800);
68     }
69     else if (width == 1024)
70     {
71       super.size = FloatVector(width - (config.defaultDialogueMargin * 2), config.defaultDialogueHeight1024);
72     }
73     else if (width == 1280)
74     {
75       super.size = FloatVector(width - (config.defaultDialogueMargin * 2), config.defaultDialogueHeight1280);
76     }
77 
78     super.position = FloatVector
79     (
80       (width / 2) - (super.width / 2),
81       height - (super.height + config.defaultDialogueMargin)
82     );
83   }
84 
85   /// See: Component.updateSize()
86   override void updateSize()
87   {
88     static if (isManualMemory)
89     {
90       if (_rect)
91       {
92         _rect.clean();
93       }
94     }
95 
96     _rect = new ExternalRectangleShape(FloatVector(cast(float)super.width, cast(float)super.height));
97     _rect.fillColor = _color;
98 
99     updatePosition();
100   }
101 
102   /// See: Component.updatePosition()
103   override void updatePosition()
104   {
105     _rect.position = FloatVector(cast(float)super.x, cast(float)super.y);
106   }
107 
108   static if (isManualMemory)
109   {
110     /// See: Component.clean()
111     override void clean()
112     {
113       if (_rect)
114       {
115         _rect.clean();
116       }
117     }
118   }
119 }