-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextHelp.h
322 lines (287 loc) · 11.8 KB
/
TextHelp.h
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
#pragma once
#include <SDL3/SDL.h>
#include <SDL3/SDL_render.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <string>
#include <vector>
#include "Vector2.h"
#include "MoreMaths.h"
#define elif else if
typedef int Alignment;
#define Top 0
#define Left 0
#define Right 2
#define Bottom 2
#define Center 1
// Character cache
class TextCharacters{
public:
TextCharacters(SDL_Renderer * renderer, TTF_Font * font, std::string characters);
SDL_Renderer * GetRenderer();
SDL_Texture * GetCharacter(std::string character);
float GetTotalLength(std::string characters);
TTF_Font * GetFont();
private:
TTF_Font * Font;
std::string Charin;
SDL_Renderer * Renderer;
std::vector<SDL_Texture *> Characters;
};
inline TextCharacters::TextCharacters(SDL_Renderer * renderer, TTF_Font * font, std::string characters){
SDL_Surface * tempsurf;
Font = font;
Renderer = renderer;
Charin = characters;
for (int i = 0; i < characters.length(); i++){
tempsurf = TTF_RenderText_Blended(Font, (std::string() + characters[i]).c_str(), 1, {255, 255, 255, 255});
Characters.push_back(SDL_CreateTextureFromSurface(Renderer, tempsurf));
SDL_DestroySurface(tempsurf);
}
}
inline SDL_Texture * TextCharacters::GetCharacter(std::string character){
return Characters[Charin.find(character)];
}
inline float TextCharacters::GetTotalLength(std::string characters){
float value = 0;
float addvalue = 0;
int kerning;
for (int i = 0; i < characters.length(); i++){
SDL_GetTextureSize(Characters[Charin.find(characters[i])], &addvalue, nullptr);
TTF_GetGlyphKerning(GetFont(), (int)characters[i], (int)characters[i+1], &kerning);
value += addvalue + kerning;
}
return value;
}
inline SDL_Renderer * TextCharacters::GetRenderer(){
return Renderer;
}
inline TTF_Font * TextCharacters::GetFont(){
return Font;
}
// Actual object to render text
class TextObject{
public:
TextObject(std::string text, Alignment horizontal, Alignment vertical, Vector2 position, SDL_Color mod, bool editable, bool visibleWhenEmpty = true, int editStart = 0, int editEnd = 0);
void Render(SDL_Renderer * renderer, TextCharacters Characters);
void MoveCursor(bool Shift, bool Control, bool MoveLeft);
void Edit(std::string InputChars, bool Modifier);
void Delete(bool Reverse);
void Destroy();
void TrySelect(Vector2 CursorPosition, bool Shift, TextCharacters Characters);
void ConTrySelect(Vector2 CursorPosition, TextCharacters Characters);
void ChangeHorizontalAlignment(Alignment horizontal, TextCharacters Characters);
void ChangeVerticalAlignment(Alignment vertical, TextCharacters Characters);
bool Editable;
int EditStart = 0;
int EditEnd = 0;
bool VisibleWhenEmpty;
bool Selected = false;
std::string Text;
Alignment Horizontal;
Alignment Vertical;
Vector2 Position;
SDL_Color Mod;
private:
int Cursor;
int Selection;
};
inline TextObject::TextObject(std::string text, Alignment horizontal, Alignment vertical, Vector2 position, SDL_Color mod, bool editable, bool visibleWhenEmpty, int editStart, int editEnd){
Text = text;
Horizontal = horizontal;
Vertical = vertical;
Position = position;
Editable = editable;
EditStart = editStart;
EditEnd = editEnd;
VisibleWhenEmpty = visibleWhenEmpty;
Mod = mod;
Cursor = -1;
Selection = 0;
}
inline void TextObject::TrySelect(Vector2 CursorPosition, bool Shift, TextCharacters Characters){
float FontHeight = (float)TTF_GetFontHeight(Characters.GetFont());
float TotaLength = Characters.GetTotalLength(Text);
float BasePos = (TotaLength*((float)Horizontal/2));
SDL_FRect TestRange = {Position.x - BasePos-8, Position.y - (FontHeight*((float)Vertical/2)), TotaLength+16, FontHeight};
if (Text.length() == 0) TestRange = {Position.x - 8, Position.y - 8, 16, 16};
if (contained(CursorPosition, TestRange)){
float distance = TotaLength;
for (int i = 0; i < Text.length()+1; i++){
if (CursorPosition.x > (Position.x - BasePos + Characters.GetTotalLength(slice(Text, 0, i)))){
distance = abs(CursorPosition.x - (Position.x - BasePos + Characters.GetTotalLength(slice(Text, 0, i))));
}
else{
if (Shift && Cursor != -1){
Selection = i-(distance < abs(CursorPosition.x - (Position.x - BasePos + Characters.GetTotalLength(slice(Text, 0, i)))))-Cursor;
}
else{
Cursor = i-(distance < abs(CursorPosition.x - (Position.x - BasePos + Characters.GetTotalLength(slice(Text, 0, i)))));
Selection = 0;
}
break;
}
if (i == Text.length()-1){
if (Shift && Cursor != -1){
Selection = i+1-Cursor;
}
else{
Cursor = i+1;
Selection = 0;
}
}
}
Selected = inlimit(Cursor, EditStart, Text.length()-EditEnd+1);
}
else{
Selected = false;
}
}
inline void TextObject::ConTrySelect(Vector2 CursorPosition, TextCharacters Characters){
float TotaLength = Characters.GetTotalLength(Text);
float BasePos = (TotaLength*((float)Horizontal/2));
float distance = TotaLength;
for (int i = 0; i < Text.length()+1; i++){
TotaLength = Characters.GetTotalLength(slice(Text, 0, i));
if (CursorPosition.x > (Position.x - BasePos + TotaLength)){
distance = abs(CursorPosition.x - (Position.x - BasePos + TotaLength));
}
else{
Selection = i-(distance < abs(CursorPosition.x - (Position.x - BasePos + TotaLength))) - Cursor;
break;
}
if (i == Text.length()-1) Selection = i+1 - Cursor;
}
if (Cursor+Selection < EditStart){
Selection = EditStart-Cursor;
}
if (Cursor+Selection > Text.length()-EditEnd){
Selection = Text.length()-EditEnd-Cursor;
}
}
inline void TextObject::MoveCursor(bool Shift, bool Control, bool MoveLeft){
if (Selected){
if (Shift){
Selection -= (MoveLeft-0.5)*2;
Selection = limit(Selection, -Cursor, Text.length()-Cursor);
if (Cursor+Selection < EditStart){
Selection = EditStart-Cursor;
}
if (Cursor+Selection > Text.length()-EditEnd){
Selection = Text.length()-EditEnd-Cursor;
}
}
else{
Cursor -= (MoveLeft-0.5)*2 - Selection;
Cursor = limit(Cursor, EditStart, Text.length()-EditEnd);
Selection = 0;
}
}
}
inline void TextObject::Edit(std::string InputChars, bool Modifier){
if (Selected){
if (Modifier){
if (InputChars == "a"){
Cursor = EditStart;
Selection = Text.length()-EditEnd-Cursor;
}
elif (InputChars == "c"){
SDL_SetClipboardText(slice(Text, (Selection<0)?Cursor+Selection:Cursor, (Selection>0)?Cursor+Selection:Cursor).c_str());
}
elif (InputChars == "x"){
if (Selection != 0){
SDL_SetClipboardText(slice(Text, (Selection<0)?Cursor+Selection:Cursor, (Selection>0)?Cursor+Selection:Cursor).c_str());
Delete(false);
}
}
elif (InputChars == "v"){
Edit(SDL_GetClipboardText(), false);
}
}
else{
if (Selection == 0){
Text = slice(Text, 0, Cursor)+InputChars+slice(Text, Cursor, Text.length());
Cursor += InputChars.length();
}
else{
Text = slice(Text, 0, (Selection<0)?Cursor+Selection:Cursor)+InputChars+slice(Text, (Selection>0)?Cursor+Selection:Cursor, Text.length());
Cursor = (Selection<0)?Cursor+Selection+InputChars.length():Cursor+InputChars.length();
Selection = 0;
}
}
}
}
inline void TextObject::Delete(bool Reverse){
if (Selected){
if (Selection == 0){
if (Reverse){
if (Cursor<Text.length()-EditEnd) Text = slice(Text, 0, Cursor)+slice(Text, Cursor+1, Text.length());
}
else{
if (Cursor>EditStart){
Text = slice(Text, 0, Cursor-1)+slice(Text, Cursor, Text.length());
Cursor--;
}
}
}
else{
Text = slice(Text, 0, (Selection<0)?Cursor+Selection:Cursor)+slice(Text, (Selection>0)?Cursor+Selection:Cursor, Text.length());
Cursor = (Selection<0)?Cursor+Selection:Cursor;
Selection = 0;
}
}
}
inline void TextObject::ChangeHorizontalAlignment(Alignment horizontal, TextCharacters Characters){
float FontWidth = Characters.GetTotalLength(Text);
Position.x += (FontWidth*((float)horizontal/2)) - (FontWidth*((float)Horizontal/2));
Horizontal = horizontal;
}
inline void TextObject::ChangeVerticalAlignment(Alignment vertical, TextCharacters Characters){
float FontHeight = (float)TTF_GetFontHeight(Characters.GetFont());
Position.y += ((FontHeight/2) + (FontHeight*((float)vertical/2))) - ((FontHeight/2) + (FontHeight*((float)Vertical/2)));
Vertical = vertical;
}
inline void TextObject::Render(SDL_Renderer * renderer, TextCharacters Characters){
float FontHeight = (float)TTF_GetFontHeight(Characters.GetFont());
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_Texture * curchar;
SDL_FRect charect;
charect.x = Position.x - (Characters.GetTotalLength(Text)*((float)Horizontal/2));
charect.y = Position.y - (FontHeight*((float)Vertical/2));
// Render editing stuffs like text cursor
if (Selected && Editable){
Cursor = limit(Cursor, 0, Text.length());
if (Text.length() == 0){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderLine(renderer, Position.x, Position.y - (FontHeight/4), Position.x, Position.y + (FontHeight/4));
}
else{
if (Selection != 0){
SDL_SetRenderDrawColor(renderer, 50, 50, 128, 128);
SDL_FRect SelecRect = {charect.x+Characters.GetTotalLength(slice(Text, 0, Cursor + Selection)), charect.y, ((Selection>0)?-1:1)*Characters.GetTotalLength(slice(Text, ((Selection<0)?Cursor+Selection:Cursor), ((Selection>0)?Cursor+Selection:Cursor))), FontHeight+1};
SDL_RenderFillRect(renderer, &SelecRect);
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderLine(renderer, charect.x+Characters.GetTotalLength(slice(Text, 0, Cursor + Selection)), charect.y, charect.x+Characters.GetTotalLength(slice(Text, 0, Cursor + Selection)), charect.y+FontHeight);
}
}
else{
if (Text.length() == 0 && VisibleWhenEmpty){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 64);
SDL_FRect TempRectForFillWhyDoesItNeedAPointerThatsSoSillyAndStupidButIGuessItMakesSenseConsideringYouDontWantToConstantlyCopyDataLikeIDoAllTheTimeInMyCPPProgramsOopsiesLol = {Position.x - (FontHeight/4), Position.y - (FontHeight/4), FontHeight/2, FontHeight/2};
SDL_RenderFillRect(renderer, &TempRectForFillWhyDoesItNeedAPointerThatsSoSillyAndStupidButIGuessItMakesSenseConsideringYouDontWantToConstantlyCopyDataLikeIDoAllTheTimeInMyCPPProgramsOopsiesLol);
}
Cursor = -1;
Selection = 0;
}
// Render text!1!
int kerning;
for (int i = 0; i < Text.length(); i++){
curchar = Characters.GetCharacter(std::string() + Text[i]);
SDL_SetTextureColorMod(curchar, Mod.r, Mod.g, Mod.b);
SDL_SetTextureAlphaMod(curchar, Mod.a);
SDL_GetTextureSize(curchar, &charect.w, &charect.h);
SDL_RenderTexture(renderer, curchar, NULL, &charect);
TTF_GetGlyphKerning(Characters.GetFont(), (int)Text[i], (int)Text[i+1], &kerning);
charect.x += charect.w + kerning;
}
}