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

im_text.cpp « IMUI « GUI « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3ecef3c61443150f07fa2d22e8a589b850a6dcca (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//-----------------------------------------------------------------------------
//           Name: im_text.cpp
//      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.
//
//-----------------------------------------------------------------------------
#include "im_text.h"

#include <GUI/IMUI/imgui.h>

/*******************************************************************************************/
/**
 * @brief  Constructor
 *
 */
IMText::IMText() :
    fontObjDirty(false),
    fontObjInit(false)
{
    IMrefCountTracker.addRefCountObject( getElementTypeName() );
}

/*******************************************************************************************/
/**
 * @brief  Constructor
 *
 * @param _name Name for this object (incumbent on the programmer to make sure they're unique)
 *
 */
IMText::IMText(std::string const& _name) :
    IMElement(_name)
{
    IMrefCountTracker.addRefCountObject( getElementTypeName() );
}

/*******************************************************************************************/
/**
 * @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 IMText::getElementTypeName() {
    return "Text";
}

/*******************************************************************************************/
/**
 * @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::IMText(std::string const& _text, std::string const& _fontName, int _fontSize, vec4 _color ) {
    
    IMrefCountTracker.addRefCountObject( getElementTypeName() );
    
    setText( _text );
    setFontByName( _fontName, _fontSize );
    setColor( _color );
}

/*******************************************************************************************/
/**
 * @brief  Constructor
 *
 * @param _text String for the text
 * @param _fontSetup Font parameter structure
 *
 */
IMText::IMText( std::string const& _text, FontSetup _fontSetup )
{
    IMrefCountTracker.addRefCountObject( getElementTypeName() );
    
    setText( _text );
    setFont( _fontSetup );
}


/*******************************************************************************************/
/**
 * @brief  Copy constructor
 *
 */
IMText::IMText( IMText const& other ) {
    
    IMrefCountTracker.addRefCountObject( getElementTypeName() );
    
    fontObjDirty = other.fontObjDirty;
    fontObjInit = other.fontObjInit;
    fontSetup = other.fontSetup;
    text = other.text;
    screenFontSize = other.screenFontSize;
    screenSize = other.screenSize;
}


/*******************************************************************************************/
/**
 * @brief  Derives the various metrics for this text element
 *
 */
void IMText::updateEngineTextObject() {
    
    // only bother if we have text, an owner and we haven't done it yet
    if( text != "" && owner != NULL && fontObjDirty ) {
        
        // update the actual screen resize
        screenFontSize = (int) (screenMetrics.GUItoScreenYScale * float(fontSetup.size));
        
        // get a new text object
		int flags = 0;
		if(fontSetup.fontName == "edosz"){
			flags = TextAtlas::kSmallLowercase;
		}
        imuiText = owner->IMGUI_IMUIContext->makeText( std::string("Data/Fonts/" + fontSetup.fontName + ".ttf"),
                                                     screenFontSize, flags );
        fontObjInit = true;
        
        // update its attributes
        imuiText.setText( text );
        
        if( fontSetup.shadowed ) {
            imuiText.setRenderFlags( TextAtlasRenderer::kTextShadow );
        }
        
        imuiText.setRotation( fontSetup.rotation );
        
        // ask the engine for the text dimensions
        vec2 boundingBox = imuiText.getBoundingBoxDimensions();
        
        screenSize.x() = boundingBox.x() + 0.5f;
        screenSize.y() = boundingBox.y() + 0.5f;
        
        vec2 newSize = vec2( (screenSize.x() / screenMetrics.GUItoScreenXScale) + 0.5f,
                             (screenSize.y() / screenMetrics.GUItoScreenYScale) + 0.5f );
        
        setSize( newSize );
        
        fontObjDirty = false;
        onRelayout();
        
    }
    
    
}

/*******************************************************************************************/
/**
 * @brief  When a resize, move, etc has happened do whatever is necessary
 *
 */
void IMText::doRelayout() {
    
    if( fontObjDirty ) {
        updateEngineTextObject();
    }
    
}

/*******************************************************************************************/
/**
 * @brief  Do whatever is necessary when the resolution changes
 *
 */
void IMText::doScreenResize()  {
    screenFontSize = int(screenMetrics.GUItoScreenYScale * float(fontSetup.size));
    fontObjDirty = true;
}

/*******************************************************************************************/
/**
 * @brief  Sets the font attributes
 *
 * @param _fontSetup font description object
 *
 */
void IMText::setFont( FontSetup _fontSetup ) {
    fontSetup = _fontSetup;
    setColor( fontSetup.color );
    fontObjDirty = true;
}


/*******************************************************************************************/
/**
 * @brief  Sets the font attributes
 *
 * @param _fontName name of the font (assumed to be in Data/Fonts)
 * @param _fontSize height of the font
 *
 */
void IMText::setFontByName( std::string const& _fontName, int _fontSize ) {
    fontSetup.fontName = _fontName;
    fontSetup.size = _fontSize;
    fontObjDirty = true;
}

/*******************************************************************************************/
/**
 * @brief  Sets the actual text
 *
 * @param _text String for the text
 *
 */
void IMText::setText( std::string const& _text ) {
    text = _text;
    fontObjDirty = true;
}

/*******************************************************************************************/
/**
 * @brief  Gets the current text
 *
 * @returns String for the text
 *
 */
std::string IMText::getText() {
    return text;
}

/*******************************************************************************************/
/**
 * @brief  Sets the text to be shadowed
 *
 * @param shadow true (default) if the text should have a shadow, false otherwise
 *
 */
void IMText::setShadowed( bool shouldShadow ) {
    fontSetup.shadowed = shouldShadow;
    fontObjDirty = true;
}

/*******************************************************************************************/
/**
 * @brief Sets the rotation for this image
 *
 * @param Rotation (in degrees)
 *
 */
void IMText::setRotation( float _rotation ) {
    fontSetup.rotation = _rotation;
    fontObjDirty = true;
}

/*******************************************************************************************/
/**
 * @brief Gets the rotation for this text
 *
 * @returns current rotation (in degrees)
 *
 */
float IMText::getRotation() {
    return fontSetup.rotation;
}

/*******************************************************************************************/
/**
 * @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 IMText::update( uint64_t delta, vec2 drawOffset, GUIState& guistate ) {
    IMElement::update( delta, drawOffset, guistate );
    
    // update the text object, if any
    updateEngineTextObject();
    
}

/*******************************************************************************************/
/**
 * @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 IMText::render( vec2 drawOffset, vec2 currentClipPos, vec2 currentClipSize ) {
    
    // Make sure we're supposed draw (and have something to draw)
    if( show && text != "" && fontObjInit ) {
        
        vec2 GUIRenderPos = drawOffset + vec2( paddingL, paddingU ) + drawDisplacement;
        
        vec2 screenRenderPos = screenMetrics.GUIToScreen( GUIRenderPos );
        
        imuiText.setPosition( vec3( screenRenderPos.x(), screenRenderPos.y(), (float) getZOrdering() ) );
        
        if( isColorEffected ) {
            imuiText.setColor( effectColor );
        }
        else {
            imuiText.setColor( color );
        }
        
        if( shouldClip && currentClipSize.x() != UNDEFINEDSIZE && currentClipSize.y() != UNDEFINEDSIZE ) {
            
            vec2 adjustedClipPos = screenMetrics.GUIToScreen( currentClipPos + drawDisplacement );
            
            vec2 screenClipPos( vec2(adjustedClipPos.x(),
                                     adjustedClipPos.y()) );
            vec2 screenClipSize(vec2((currentClipSize.x() * screenMetrics.GUItoScreenXScale),
                                     (currentClipSize.y() * screenMetrics.GUItoScreenYScale) ) );
            
            imuiText.setClipping( screenClipPos, screenClipSize );
            
        }
        
        if( fontSetup.shadowed ) {
            imuiText.setRenderFlags( TextAtlasRenderer::kTextShadow );
        }
        
        if( owner != NULL ) {
            owner->IMGUI_IMUIContext->queueText( imuiText );
        }
        
    }
    
    // Call the superclass to make sure any element specific rendering is done
    IMElement::render( drawOffset, currentClipPos, currentClipSize );
    
}

/*******************************************************************************************/
/**
 * @brief  Remove all referenced object without releaseing references
 *
 */
void IMText::clense() {
    
}

IMText::~IMText() {
    IMrefCountTracker.removeRefCountObject( getElementTypeName() );
}