-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
executable file
·389 lines (329 loc) · 13.1 KB
/
test.cpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <string>
#include <iostream>
class TextBox
{
public:
TextBox(SDL_Renderer *renderer, TTF_Font *font, int x, int y, int width, int height) : m_renderer(renderer),
m_font(font),
m_x(x),
m_y(y),
m_width(width),
m_height(height),
m_selected(false),
m_cursorPos(0),
m_selectionStart(0),
m_text(""),
m_textColor({0, 0, 0, 255}),
m_selectionColor({192, 192, 192, 255}),
m_cursorColor({0, 0, 0, 255}),
m_textOffset(0),
m_maxTextOffset(0)
{
m_rect.x = m_x;
m_rect.y = m_y;
m_rect.w = m_width;
m_rect.h = m_height;
TTF_SizeText(m_font, "H", nullptr, &m_fontHeight);
updateMaxTextOffset();
}
void handleEvent(SDL_Event &event)
{
if (event.type == SDL_MOUSEBUTTONDOWN)
{
int mouseX = event.button.x;
int mouseY = event.button.y;
if (mouseX >= m_x + 10 && mouseX <= m_x + m_width - 10 &&
mouseY >= m_y && mouseY <= m_y + m_height)
{
m_selected = true;
int clickPos = getTextIndexAtPosition(mouseX - (m_x + 10) + m_textOffset);
m_cursorPos = std::max(0, std::min(static_cast<int>(m_text.length()), clickPos + m_textOffset));
updateTextOffset();
}
else
{
m_selected = false;
}
}
else if (event.type == SDL_MOUSEBUTTONUP && m_selected)
{
int mouseX = event.button.x;
int mouseY = event.button.y;
if (mouseX >= m_x + 10 && mouseX <= m_x + m_width - 10 &&
mouseY >= m_y && mouseY <= m_y + m_height)
{
int clickPos = getTextIndexAtPosition(mouseX - (m_x + 10) + m_textOffset);
m_cursorPos = std::max(0, std::min(static_cast<int>(m_text.length()), clickPos));
}
}
else if (event.type == SDL_TEXTINPUT && m_selected)
{
std::string insertedText = event.text.text;
int insertedTextWidth = getTextWidth(insertedText);
if (m_selectionStart != m_cursorPos)
{
m_text.erase(std::min(m_selectionStart, m_cursorPos), std::abs(m_selectionStart - m_cursorPos));
m_cursorPos = m_selectionStart = std::min(m_selectionStart, m_cursorPos);
}
m_text.insert(m_cursorPos, insertedText);
m_selectionStart = m_cursorPos += insertedText.length();
updateTextOffset();
}
else if (event.type == SDL_KEYDOWN && m_selected)
{
switch (event.key.keysym.sym)
{
case SDLK_BACKSPACE:
if (m_selectionStart != m_cursorPos)
{
m_text.erase(std::min(m_selectionStart, m_cursorPos), std::abs(m_selectionStart - m_cursorPos));
m_cursorPos = m_selectionStart;
}
else if (m_cursorPos > 0)
{
m_text.erase(m_cursorPos - 1, 1);
m_cursorPos--;
}
break;
case SDLK_DELETE:
if (m_selectionStart != m_cursorPos)
{
m_text.erase(std::min(m_selectionStart, m_cursorPos), std::abs(m_selectionStart - m_cursorPos));
m_cursorPos = m_selectionStart;
}
else if (m_cursorPos < static_cast<int>(m_text.length()))
{
m_text.erase(m_cursorPos, 1);
}
break;
case SDLK_LEFT:
if (!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LSHIFT] && !SDL_GetKeyboardState(NULL)[SDL_SCANCODE_RSHIFT])
{
if (m_cursorPos > 0)
{
m_cursorPos--;
m_selectionStart = m_cursorPos;
}
}
else
{
if (m_cursorPos > 0)
{
m_cursorPos--;
}
}
updateTextOffset();
break;
case SDLK_RIGHT:
if (!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LSHIFT] && !SDL_GetKeyboardState(NULL)[SDL_SCANCODE_RSHIFT])
{
if (m_cursorPos < static_cast<int>(m_text.length()))
{
m_cursorPos++;
m_selectionStart = m_cursorPos;
}
}
else
{
if (m_cursorPos < static_cast<int>(m_text.length()))
{
m_cursorPos++;
}
}
updateTextOffset();
break;
}
}
}
void render()
{
SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);
SDL_RenderFillRect(m_renderer, &m_rect);
SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255);
SDL_RenderDrawRect(m_renderer, &m_rect);
if (m_selected)
{
int selectionStartX = getTextWidth(m_text.substr(0, m_selectionStart)) + 10 - m_textOffset;
int selectionEndX = getTextWidth(m_text.substr(0, m_cursorPos)) + 10 - m_textOffset;
SDL_SetRenderDrawColor(m_renderer, m_selectionColor.r, m_selectionColor.g, m_selectionColor.b, m_selectionColor.a);
SDL_Rect selectionRect = {m_x + 10 + selectionStartX, m_y + (m_height - m_fontHeight) / 2, selectionEndX - selectionStartX, m_fontHeight};
SDL_RenderFillRect(m_renderer, &selectionRect);
int cursorX = getTextWidth(m_text.substr(0, m_cursorPos)) + 10 - m_textOffset;
SDL_SetRenderDrawColor(m_renderer, m_cursorColor.r, m_cursorColor.g, m_cursorColor.b, m_cursorColor.a);
SDL_RenderDrawLine(m_renderer, m_x + 10 + cursorX, m_y + (m_height - m_fontHeight) / 2, m_x + 10 + cursorX, m_y + (m_height + m_fontHeight) / 2);
}
std::string visibleText = getVisibleText();
renderText(visibleText, m_x + 10, m_y + (m_height - m_fontHeight) / 2, m_textColor);
}
private:
SDL_Renderer *m_renderer;
TTF_Font *m_font;
int m_x;
int m_y;
int m_width;
int m_height;
SDL_Rect m_rect;
bool m_selected;
int m_cursorPos;
int m_selectionStart;
std::string m_text;
SDL_Color m_textColor;
SDL_Color m_selectionColor;
SDL_Color m_cursorColor;
int m_textOffset;
int m_maxTextOffset;
int m_fontHeight;
int getTextWidth(const std::string &text)
{
int textWidth = 0;
TTF_SizeText(m_font, text.c_str(), &textWidth, nullptr);
return textWidth;
}
void updateMaxTextOffset()
{
int textWidth = getTextWidth(m_text);
m_maxTextOffset = std::max(0, textWidth - (m_width - 20));
}
int getTextIndexAtPosition(int positionX)
{
int textIndex = 0;
int textWidth = 0;
int charWidth = 0;
for (char character : m_text)
{
TTF_SizeText(m_font, std::string(1, character).c_str(), &charWidth, nullptr);
if (textWidth + charWidth / 2 >= positionX)
{
break;
}
textWidth += charWidth;
textIndex++;
}
return textIndex;
}
void renderText(const std::string &text, int x, int y, SDL_Color color)
{
if (!text.empty())
{
int textWidth = getTextWidth(text);
int renderWidth = std::min(textWidth - m_textOffset, m_width - 20);
SDL_Surface *surface = TTF_RenderText_Solid(m_font, text.c_str(), color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(m_renderer, surface);
SDL_Rect rect = {x, y, textWidth, surface->h};
SDL_RenderCopy(m_renderer, texture, nullptr, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
}
void updateTextOffset()
{
updateMaxTextOffset();
int cursorX = getTextWidth(m_text.substr(0, m_cursorPos)) + 10 - m_textOffset;
if (cursorX < 0)
{
m_textOffset = std::max(0, getTextWidth(m_text.substr(0, m_cursorPos)) - (m_width - 20) + 10);
}
else if (cursorX > m_width - 20)
{
int offsetChange = cursorX - (m_width - 20);
int maxOffsetChange = std::max(0, m_maxTextOffset - m_textOffset);
m_textOffset += std::min(offsetChange, maxOffsetChange);
}
else
{
int textWidth = getTextWidth(m_text);
int renderWidth = std::min(textWidth, m_width - 20);
if (textWidth > renderWidth)
{
int cursorEndX = getTextWidth(m_text.substr(0, m_cursorPos + 1)) + 10 - m_textOffset;
if (cursorEndX > m_width - 20)
{
int offsetChange = cursorEndX - (m_width - 20);
int maxOffsetChange = std::max(0, m_maxTextOffset - m_textOffset);
m_textOffset += std::min(offsetChange, maxOffsetChange);
}
}
else
{
m_textOffset = 0;
}
}
}
std::string getVisibleText()
{
int availableWidth = m_width - 20;
std::string visibleText = "";
int textWidth = getTextWidth(m_text);
if (textWidth <= availableWidth)
{
// O texto inteiro cabe na caixa de texto
visibleText = m_text;
}
else
{
int startIndex = std::max(0, m_textOffset);
int endIndex = static_cast<int>(m_text.length()) - 1;
int visibleWidth = availableWidth;
// Determinar o índice de início do texto visível
int textOffset = std::min(m_textOffset, textWidth - availableWidth);
int accumulatedWidth = 0;
int currentIndex = 0;
while (currentIndex < static_cast<int>(m_text.length()) && accumulatedWidth < textOffset)
{
int charWidth = getTextWidth(std::string(1, m_text[currentIndex]));
accumulatedWidth += charWidth;
currentIndex++;
}
startIndex = currentIndex;
// Determinar o índice de fim do texto visível
accumulatedWidth = 0;
currentIndex = startIndex;
while (currentIndex < static_cast<int>(m_text.length()) && accumulatedWidth < visibleWidth)
{
int charWidth = getTextWidth(std::string(1, m_text[currentIndex]));
accumulatedWidth += charWidth;
if (accumulatedWidth <= visibleWidth)
{
endIndex = currentIndex;
}
currentIndex++;
}
visibleText = m_text.substr(startIndex, endIndex - startIndex + 1);
}
return visibleText;
}
};
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
SDL_Window *window = SDL_CreateWindow("Text Box", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
TTF_Font *font = TTF_OpenFont("assets/fonts/arial.ttf", 24);
TextBox textBox(renderer, font, 100, 100, 400, 50);
bool quit = false;
SDL_Event event;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
textBox.handleEvent(event);
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
textBox.render();
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}