Welcome to mirror list, hosted at ThFree Co, Russian Federation.

im_text.h « IMUI « GUI « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b1a2c1ab74f07fa1cdd7badb08403977236c7658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//-----------------------------------------------------------------------------
//           Name: im_text.h
//      Developer: Wolfire Games LLC
//    Description: Text element class for creating adhoc GUIs as part of the 
//                 UI tools  
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

#include <GUI/IMUI/im_support.h>
#include <GUI/IMUI/im_element.h>

struct FontSetup
{
    std::string fontName;   // name for the font
    int size;               // size in GUI pixels
    vec4 color;             // color for the font
    float rotation; // Rotation for the text
    bool shadowed;  // should this text have a shadow?

    FontSetup() :
        rotation(0.0),
        shadowed(false)
    {
        fontName = "OpenSans-Regular";
        size = 60;
        color = HexColor("#fff");
        
    }

    FontSetup( std::string const& _name, int _size, vec4 _color, bool _shadowed = false ) :
        rotation(0.0)
    {
        fontName = _name;
        size = _size;
        color = _color;
        shadowed = _shadowed;
    }
    
    ~FontSetup() {}
};


static void FontSetup_ASconstructor_default(FontSetup *self) {
    new(self) FontSetup();
}

static void FontSetup_ASconstructor_params(FontSetup *self, std::string const& _name, int32_t _size, vec4 _color, bool _shadowed = false ) {
    new(self) FontSetup(  _name, _size, _color, _shadowed );
}

static void FontSetup_AScopy(FontSetup *self, const FontSetup& other ) {
    new(self) FontSetup( other );
}

static void FontSetup_ASdestructor(FontSetup *self ) {
    self->~FontSetup();
}

static const FontSetup& FontSetup_ASassign(FontSetup *self, const FontSetup& other) {
    return (*self) = other;
}




/*******************************************************************************************/
/**
 * @brief Any styled text element 
 *
 */
class IMText : public IMElement
{
public:
    IMUIText imuiText;  // Engine object for the text
    bool fontObjDirty;  // Does the engine object need updating
    bool fontObjInit;   // Has the font object been initialized, at least once
    FontSetup fontSetup;// Attributes for the current font
    std::string text;   // Actual text to render
    int screenFontSize; // Height of the text (screen space) 
    vec2 screenSize;   // Bit of a hack as we need the screen height for getting the right metrics
    
    /*******************************************************************************************/
    /**
     * @brief  Constructor
     *
     */
    IMText();
    
    /*******************************************************************************************/
    /**
     * @brief  Copy constructor
     *
     */
    IMText( IMText const& other );
    
    /*******************************************************************************************/
    /**
     * @brief  Constructor
     * 
     * @param _name Name for this object (incumbent on the programmer to make sure they're unique)
     *
     */
    IMText(std::string const& _name);
    
    /*******************************************************************************************/
    /**
     * @brief  Constructor
     * 
     * @param _text String for the text
     * @param _fontName name of the font (assumed to be in Data/Fonts)
     * @param _fontSize height of the font
     * @param _color vec4 color rgba
     *
     */
    IMText(std::string const& _text, std::string const& _fontName, int _fontSize, vec4 _color = vec4(1.0f) );
    
    /*******************************************************************************************/
    /**
     * @brief  Constructor
     * 
     * @param _text String for the text
     * @param _fontSetup Font parameter structure
     *
     */
    IMText( std::string const& _text, FontSetup _fontSetup );
    
    /*******
     *
     * Angelscript factory
     *
     */
    static IMText* ASFactory_named( std::string const& name ) {
        return new IMText( name );
    }
    
    static IMText* ASFactory_unnamed( std::string const& _text, std::string const& _fontName, int _fontSize, vec4 _color = vec4(1.0f) ) {
        return new IMText( _text, _fontName, _fontSize, _color );
    }
    
    static IMText* ASFactory_fontsetup( std::string const& _text, FontSetup _fontSetup ) {
        return new IMText( _text, _fontSetup );
    }
    
    
    /*******************************************************************************************/
    /**
     * @brief  Gets the name of the type of this element — for autonaming and debugging
     *
     * @returns name of the element type as a string
     *
     */
    std::string getElementTypeName() override;
    
    /*******************************************************************************************/
    /**
     * @brief  Derives the various metrics for this text element
     * 
     */
    void updateEngineTextObject();
    
    /*******************************************************************************************/
    /**
     * @brief  When a resize, move, etc has happened do whatever is necessary
     * 
     */
    void doRelayout() override;

    /*******************************************************************************************/
    /**
     * @brief  Do whatever is necessary when the resolution changes
     *
     */
    void doScreenResize() override;

    /*******************************************************************************************/
    /**
     * @brief  Sets the font attributes 
     * 
     * @param _fontSetup font description object
     *
     */
    void setFont( FontSetup _fontSetup );

    /*******************************************************************************************/
    /**
     * @brief  Sets the font attributes 
     * 
     * @param _fontName name of the font (assumed to be in Data/Fonts)
     * @param _fontSize height of the font
     *
     */
    void setFontByName( std::string const& _fontName, int _fontSize );

    /*******************************************************************************************/
    /**
     * @brief  Sets the actual text 
     * 
     * @param _text String for the text
     *
     */
    void setText( std::string const& _text );
    
    /*******************************************************************************************/
    /**
     * @brief  Gets the current text
     * 
     * @returns String for the text
     *
     */
    std::string getText();
    
    /*******************************************************************************************/
    /**
     * @brief  Sets the text to be shadowed
     *  
     * @param shadow true (default) if the text should have a shadow, false otherwise
     *
     */
    void setShadowed( bool shouldShadow = true );
    
    /*******************************************************************************************/
    /**
     * @brief Sets the rotation for this image
     * 
     * @param Rotation (in degrees)
     *
     */
    void setRotation( float _rotation );
    
    /*******************************************************************************************/
    /**
     * @brief Gets the rotation for this text
     * 
     * @returns current rotation (in degrees)
     *
     */
    float getRotation();
    
    /*******************************************************************************************/
    /**
     * @brief  Updates the element  
     * 
     * @param delta Number of millisecond elapsed since last update
     * @param drawOffset Absolute offset from the upper lefthand corner (GUI space)
     * @param guistate The state of the GUI at this update
     *
     */
    void update( uint64_t delta, vec2 drawOffset, GUIState& guistate ) override;
    
    /*******************************************************************************************/
    /**
     * @brief  Rather counter-intuitively, this draws this object on the screen
     *
     * @param drawOffset Absolute offset from the upper lefthand corner (GUI space)
     * @param clipPos pixel location of upper lefthand corner of clipping region
     * @param clipSize size of clipping region
     *
     */
    void render( vec2 drawOffset, vec2 currentClipPos, vec2 currentClipSize ) override;
    
    /*******************************************************************************************/
    /**
     * @brief  Remove all referenced object without releaseing references
     *
     */
    void clense() override;
    
    ~IMText() override;
};