This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathCPUTText.cpp
More file actions
353 lines (282 loc) · 11.2 KB
/
Copy pathCPUTText.cpp
File metadata and controls
353 lines (282 loc) · 11.2 KB
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
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Intel Corporation
//
// 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 "CPUTText.h"
#include "CPUTFont.h"
#include <string.h>
#ifdef CPUT_FOR_DX11
#include "CPUTGuiControllerDX11.h"
#else
#error You must supply a target graphics API (ex: #define CPUT_FOR_DX11), or implement the target API for this file.
#endif
// texture atlas information
float gTextAtlasWidth = 750.0f;
float gTextAtlasHeight = 12.0f;
// Constructor
//------------------------------------------------------------------------------
CPUTText::CPUTText(CPUTFont *pFont):mVertexStride(0),
mVertexOffset(0),
mpMirrorBuffer(NULL),
mNumCharsInString(0),
mZDepth(1.0f),
mpFont(pFont)
{
// initialize the state variables
InitialStateSet();
mQuadSize.height=0; mQuadSize.width=0;
mPosition.x=0; mPosition.y=0;
mStaticText.clear();
}
// Constructor
//-----------------------------------------------------------------------------
CPUTText::CPUTText(const cString String, CPUTControlID id, CPUTFont *pFont):mVertexStride(0),
mVertexOffset(0),
mpMirrorBuffer(NULL),
mNumCharsInString(0),
mZDepth(1.0f),
mpFont(pFont)
{
// initialize the state variables
InitialStateSet();
// save the control ID for callbacks
mcontrolID = id;
// set as enabled
CPUTControl::SetEnable(true);
// reset position/sizes
mQuadSize.height=0; mQuadSize.width=0;
mPosition.x=0; mPosition.y=0;
// set the text
SetText(String);
// store the control id
mcontrolID = id;
}
// Initial state of the control's member variables
//-----------------------------------------------------------------------------
void CPUTText::InitialStateSet()
{
mcontrolType = CPUT_STATIC;
mStaticState = CPUT_CONTROL_ACTIVE;
mPosition.x=0; mPosition.y=0;
mQuadSize.width=0; mQuadSize.height=0;
mDimensions.x=0; mDimensions.y=0; mDimensions.width=0; mDimensions.height=0;
}
// Destructor
//------------------------------------------------------------------------------
CPUTText::~CPUTText()
{
ReleaseInstanceData();
}
// Return the dimensions of this static text object (in pixels)
//--------------------------------------------------------------------------------
void CPUTText::GetDimensions(int &width, int &height)
{
width = mQuadSize.width;
height = mQuadSize.height;
}
// Return the screen position of this static text object (in pixels)
//--------------------------------------------------------------------------------
void CPUTText::GetPosition(int &x, int &y)
{
x = mPosition.x;
y = mPosition.y;
}
// fills user defined buffer with static string
//--------------------------------------------------------------------------------
void CPUTText::GetString(cString &ButtonText)
{
// fill user defined buffer with the string
ButtonText = mStaticText;
}
// Enable/disable the text
//--------------------------------------------------------------------------------
void CPUTText::SetEnable(bool in_bEnabled)
{
// set as enabled
CPUTControl::SetEnable(in_bEnabled);
// recalculate
Recalculate();
// position or size may move - force a recalculation of this control's location
// if it is managed by the auto-arrange function
if(this->IsAutoArranged())
{
CPUTGuiControllerDX11::GetController()->Resize();
}
}
// Release all instance data
//--------------------------------------------------------------------------------
void CPUTText::ReleaseInstanceData()
{
SAFE_DELETE_ARRAY(mpMirrorBuffer);
}
//--------------------------------------------------------------------------------
CPUTResult CPUTText::RegisterInstanceData()
{
CPUTResult result = CPUT_SUCCESS;
// ping the font system and tell it to load this font - or add GetFont() to the AssetLibrary(!?)
// get the pertinent mapping data back for that font
// calculate the uv's/etc for each character
// hold onto a retrievable copy of the texture text atlas that can be returned with this object/or make a function that sets that map (material?)
return result;
}
// Register all static assets (used by all CPUTText objects)
//--------------------------------------------------------------------------------
CPUTResult CPUTText::RegisterStaticResources()
{
return CPUT_SUCCESS;
}
//
//--------------------------------------------------------------------------------
CPUTResult CPUTText::UnRegisterStaticResources()
{
// todo: Release font?
//CPUTFontLibraryDX11::GetFontLibrary()->DeleteFont( mFontID );
return CPUT_SUCCESS;
}
//
//--------------------------------------------------------------------------------
void CPUTText::SetPosition(int x, int y)
{
mPosition.x = x;
mPosition.y = y;
Recalculate();
}
//--------------------------------------------------------------------------------
void CPUTText::DrawIntoBuffer(CPUTGUIVertex *pVertexBufferMirror, UINT *pInsertIndex, UINT pMaxBufferSize)
{
if(!mControlVisible)
{
return;
}
if((NULL==pVertexBufferMirror) || (NULL==pInsertIndex))
{
return;
}
if(!mpMirrorBuffer) // || !mpMirrorBufferDisabled)
{
return;
}
// Do we have enough room to put the text vertexes into the output buffer?
int VertexCopyCount = GetOutputVertexCount();
ASSERT( (pMaxBufferSize >= *pInsertIndex + VertexCopyCount), _L("Too many characters to fit in allocated GUI string vertex buffer.\n\nIncrease CPUT_GUI_BUFFER_STRING_SIZE size.") );
// copy the string quads into the target buffer
memcpy(&pVertexBufferMirror[*pInsertIndex], mpMirrorBuffer, sizeof(CPUTGUIVertex)*6*mNumCharsInString);
*pInsertIndex+=6*mNumCharsInString;
}
// Calculate the number of verticies will be needed to display this string
//
//--------------------------------------------------------------------------------
int CPUTText::GetOutputVertexCount()
{
// A string is made of one quad per character (including spaces)
//
// ---
// | 1 |
// ---
//
// calculation: (number of characters in string) * 3 verticies/triangle * 2 triangles/quad * 1 quad/character
return mNumCharsInString * (2*3);
}
// using the supplied font, build up a quad for each character in the string
// and assemble the quads into a vertex buffer ready for drawing/memcpy
//--------------------------------------------------------------------------------
void CPUTText::Recalculate()
{
SAFE_DELETE_ARRAY(mpMirrorBuffer);
mNumCharsInString = (int) mStaticText.size();
mpMirrorBuffer = new CPUTGUIVertex[(mNumCharsInString+1)*6];
bool Enabled = false;
if(CPUT_CONTROL_ACTIVE == mControlState)
{
Enabled = true;
}
// walk each character and build a quad from it
float characterPosition=0;
for(int ii=0; ii<mNumCharsInString; ii++)
{
// todo: unsafe cast from wchar_t to char! Some other way???
char character = (char) mStaticText[ii];
float3 UV1, UV2;
CPUT_SIZE size = mpFont->GetGlyphSize(character);
mpFont->GetGlyphUVS(character, Enabled, UV1, UV2);
if('\t'!=character)
{
AddQuadIntoMirrorBuffer(mpMirrorBuffer, ii*6, (float)mPosition.x+characterPosition, (float)mPosition.y, (float)size.width, (float)size.height, UV1, UV2);
}
else
{
// calculate tab width = # pixels to get to next tabstop
// Tabs are relative from BEGINNNING of the string, not absolute based on x-position of string. So in order for columns to line
// up, you need the starts of the strings to line up too.
// If you want 'absolute' x alignment behavior, use this:
//size.width = size.width - ((mPosition.x+characterPosition) % size.width);
// simply skip the amount of space indicated in the font's tab slot
int CurrentPositionForNextGlyph = (int) characterPosition;
size.width = size.width - (CurrentPositionForNextGlyph % size.width);
AddQuadIntoMirrorBuffer(mpMirrorBuffer, ii*6, (float)mPosition.x+characterPosition, (float)mPosition.y, (float)size.width, (float)size.height, UV1, UV2);
}
// store the max height of the string
mQuadSize.height = max(mQuadSize.height, size.height);
// step to next X location for next character
characterPosition+=size.width;
}
// store the total width of the string
mQuadSize.width = (int)characterPosition; // width of string in pixels
// tell gui system this control image is now dirty
// and needs to rebuild it's draw list
CPUTGuiControllerDX11::GetController()->ControlIsDirty();
// position or size may move - force a recalculation of this control's location
// if it is managed by the auto-arrange function
if(this->IsAutoArranged())
{
CPUTGuiControllerDX11::GetController()->Resize();
}
}
// Register quad for drawing string on
//--------------------------------------------------------------------------------
CPUTResult CPUTText::SetText(const cString String, float depth)
{
HEAPCHECK;
mStaticText = String;
mZDepth = depth;
// call recalculate function to generate new quad
// list to display this text
Recalculate();
HEAPCHECK;
return CPUT_SUCCESS;
}
// This generates a quad with the supplied coordinates/uv's/etc.
//------------------------------------------------------------------------
void CPUTText::AddQuadIntoMirrorBuffer(CPUTGUIVertex *pMirrorBuffer,
int index,
float x,
float y,
float w,
float h,
float3 uv1,
float3 uv2 )
{
pMirrorBuffer[index+0].Pos = float3( x + 0.0f, y + 0.0f, mZDepth);
pMirrorBuffer[index+0].UV = float2(uv1.x, uv1.y);
pMirrorBuffer[index+1].Pos = float3( x + w, y + 0.0f, mZDepth);
pMirrorBuffer[index+1].UV = float2(uv2.x, uv1.y);
pMirrorBuffer[index+2].Pos = float3( x + 0.0f, y + h, mZDepth);
pMirrorBuffer[index+2].UV = float2(uv1.x, uv2.y);
pMirrorBuffer[index+3].Pos = float3( x + w, y + 0.0f, mZDepth);
pMirrorBuffer[index+3].UV = float2(uv2.x, uv1.y);
pMirrorBuffer[index+4].Pos = float3( x + w, y + h, mZDepth);
pMirrorBuffer[index+4].UV = float2(uv2.x, uv2.y);
pMirrorBuffer[index+5].Pos = float3( x + 0.0f, y +h, mZDepth);
pMirrorBuffer[index+5].UV = float2(uv1.x, uv2.y);
}