-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_panel.c
416 lines (341 loc) · 11.1 KB
/
p_panel.c
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//
// panel.c
// de
//
// Created by Thomas Foster on 5/27/23.
//
#include "p_panel.h"
#include "p_stack.h"
#include "common.h"
#include "text.h"
#include "e_editor.h"
#include "e_defaults.h"
#include <string.h>
static int textColor = 15;
static int backgroundColor = 1;
void SetPanelColor(int text, int background)
{
textColor = text;
backgroundColor = background;
}
void SetPanelRenderColor(int index)
{
SDL_Color c = Int24ToSDL(palette[index]);
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 255);
}
void UpdatePanelConsole(const Panel * panel, int x, int y, u8 ch)
{
SDL_Texture * oldTarget = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, panel->texture);
// Write ch, textColor, and backgroundColor to console.
int i = y * panel->width + x;
u8 attr = (backgroundColor << 4) | textColor;
panel->consoleData[i] = (attr << 8) | ch;
// Render background color rect.
static SDL_Rect r = { .w = FONT_WIDTH, .h = FONT_HEIGHT };
r.x = x * FONT_WIDTH;
r.y = y * FONT_HEIGHT;
SetPanelRenderColor(backgroundColor);
SDL_RenderFillRect(renderer, &r);
// Render character
SetPanelRenderColor(textColor);
RenderChar(r.x, r.y, ch);
SDL_SetRenderTarget(renderer, oldTarget); // Restore previous target.
}
void ConsolePrint(const Panel * panel, int x, int y, const char * string)
{
const char * c = string;
while ( *c != '\0' )
{
UpdatePanelConsole(panel, x++, y, *c);
c++;
}
}
Panel NewPanel(int x, int y, int width, int height, int numItems)
{
Panel panel = { 0 };
panel.location.x = x;
panel.location.y = y;
panel.location.w = width * FONT_WIDTH;
panel.location.h = height * FONT_HEIGHT;
panel.width = width;
panel.height = height;
panel.consoleData = calloc(width * height, sizeof(*panel.consoleData));
panel.texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
panel.location.w,
panel.location.h);
panel.numItems = numItems;
panel.items = calloc(numItems, sizeof(*panel.items));
return panel;
}
void FreePanel(const Panel * panel)
{
if ( panel->dynamicItems )
free(panel->items);
free(panel->consoleData);
SDL_DestroyTexture(panel->texture);
}
void LoadPanelConsole(Panel * panel, const char * path)
{
FILE * file = fopen(path, "rb");
if ( file == NULL )
Error("Error: '%s' does not exist\n", path);
u8 width;
u8 height;
fread(&width, sizeof(width), 1, file);
fread(&height, sizeof(height), 1, file);
u16 * data = malloc(sizeof(*data) * width * height);
fread(data, sizeof(*data), width * height, file);
panel->consoleData = data;
fclose(file);
panel->texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
width * FONT_WIDTH,
height * FONT_HEIGHT);
if ( panel->texture == NULL )
Error("Error: could not load line panel (%s)\n", SDL_GetError());
for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++ )
{
BufferCell cell = GetCell(data[y * width + x]);
textColor = cell.foreground;
backgroundColor = cell.background;
UpdatePanelConsole(panel, x, y, cell.character);
}
}
panel->width = width;
panel->height = height;
panel->location.w = width * FONT_WIDTH;
panel->location.h = height * FONT_HEIGHT;
}
SDL_Rect PanelRenderLocation(const Panel * panel)
{
SDL_Rect rect = panel->location;
if ( panel->parent ) {
rect.x += panel->parent->location.x;
rect.y += panel->parent->location.y;
}
return rect;
}
void RenderMark(const PanelItem * item, int value)
{
if ( value != 0 ) {
SetPanelRenderColor(15);
RenderChar((item->x - 3) * FONT_WIDTH, item->y * FONT_HEIGHT, 7);
}
}
void RenderPanelTexture(const Panel * panel)
{
SDL_Rect dest = PanelRenderLocation(panel);
// Shadow
SDL_Rect shadow = dest;
shadow.x += 16;
shadow.y += 16;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 160);
SDL_RenderFillRect(renderer, &shadow);
// Texture
SDL_RenderCopy(renderer, panel->texture, NULL, &dest);
// Outline
SetPanelRenderColor(11);
SDL_RenderDrawRect(renderer, &dest);
}
/// Render a colored horizontal bar at panel item by extracting the console
/// information under the bar, and rerendering it on top with the given color.
void RenderBar(const Panel * panel, const PanelItem * item, int color)
{
for ( int x = item->x; x < item->x + item->width; x++ )
{
int y = item->y;
int i = y * panel->width + x;
BufferCell cell = GetCell(panel->consoleData[i]);
SDL_Rect r = {
x * FONT_WIDTH,
item->y * FONT_HEIGHT,
FONT_WIDTH,
FONT_HEIGHT
};
SetPanelRenderColor(color);
SDL_RenderFillRect(renderer, &r);
SetPanelRenderColor(15);
RenderChar(x * FONT_WIDTH, y * FONT_HEIGHT, cell.character);
}
}
void RenderPanelSelection(const Panel * panel)
{
if ( panel->items == NULL )
return;
SDL_Rect oldViewport;
SDL_RenderGetViewport(renderer, &oldViewport); // Save current viewport.
SDL_Rect renderLocation = PanelRenderLocation(panel);
SDL_RenderSetViewport(renderer, &renderLocation);
// Only render selection if the mouse is actually over an item.
if ( panel->selection != -1 )
RenderBar(panel, &panel->items[panel->selection], 7);
SDL_RenderSetViewport(renderer, &oldViewport); // Restore viewport.
}
void RenderPanelTextInput(const Panel * panel)
{
SetPanelRenderColor(15);
int x = panel->items[panel->textItem].x;
int y = panel->items[panel->textItem].y;
PANEL_RENDER_STRING(x, y, panel->text);
if ( SDL_GetTicks() % 600 < 300 )
PANEL_RENDER_STRING(x + panel->cursor, y, "_");
}
bool ShouldRenderInactiveTextField(const Panel * panel, int itemIndex)
{
return !panel->isTextEditing || (panel->isTextEditing && panel->textItem != itemIndex);
}
void RenderPanel(const Panel * panel)
{
// Texture
RenderPanelTexture(panel);
// Text input / selection / etc.
SDL_RenderSetViewport(renderer, &panel->location);
if ( panel->isTextEditing )
RenderPanelTextInput(panel);
else if ( panel->selection != -1 )
RenderPanelSelection(panel);
// Per-panel rendering?
if ( panel->render )
panel->render();
SDL_RenderSetViewport(renderer, NULL);
}
// TODO: confirm that itemIndex is not necessary (use panel->selection?)
void StartTextEditing(Panel * panel, int itemIndex, void * value, int type)
{
// if ( type == VALUE_INT )
// SDL_itoa(*(int *)value, panel->text, 10);
// else if ( type == VALUE_STRING )
// strncpy(panel->text, (char *)value, sizeof(panel->text));
memset(panel->text, 0, sizeof(panel->text));
panel->text[0] = '\0';
panel->cursor = 0;
panel->isTextEditing = true;
panel->valueType = type;
panel->textItem = itemIndex;
// panel->cursor = (int)strnlen(panel->text, MAX_TEXT);
panel->value = value;
SDL_StartTextInput();
}
void StopTextEditing(Panel * panel)
{
panel->isTextEditing = false;
SDL_StopTextInput();
}
void ProcessPanelTextInput(Panel * panel, const SDL_Event * event)
{
int len = (int)strlen(panel->text);
switch ( event->type )
{
case SDL_KEYDOWN:
switch ( event->key.keysym.sym )
{
case SDLK_RETURN:
if ( panel->valueType == VALUE_INT )
{
// Convert `text` back to an int and set value if valid.
char * end;
long value = strtol(panel->text, &end, 10);
if ( *end == '\0' )
*(int *)panel->value = (int)value;
}
else if ( panel->valueType == VALUE_STRING )
{
strcpy((char *)panel->value, panel->text);
}
StopTextEditing(panel);
if ( panel->textEditingCompletionHandler )
panel->textEditingCompletionHandler();
break;
case SDLK_ESCAPE:
StopTextEditing(panel);
break;
case SDLK_BACKSPACE:
if ( panel->cursor >= 1 )
{
for ( int x = panel->cursor; x <= len; x++ )
panel->text[x - 1] = panel->text[x];
panel->cursor--;
}
break;
case SDLK_LEFT:
if ( panel->cursor >= 0 )
panel->cursor--;
break;
case SDLK_RIGHT:
if ( panel->cursor < len )
panel->cursor++;
break;
default:
break;
}
break;
case SDL_TEXTINPUT:
for ( int x = len; x > panel->cursor; x-- )
panel->text[x] = panel->text[x - 1];
panel->text[panel->cursor] = event->text.text[0];
panel->cursor++;
break;
default:
break;
}
}
bool ProcessPanelEvent(Panel * panel, const SDL_Event * event)
{
if ( panel->isTextEditing )
{
ProcessPanelTextInput(panel, event);
return true;
}
if ( panel->processEvent )
{
// Clicked on a panel that's in a lower layer?
if ( PanelIsUnderneath(panel)
&& event->type == SDL_MOUSEBUTTONDOWN
&& event->button.button == SDL_BUTTON_LEFT )
{
CloseAllPanelsAbove(panel);
return true;
}
if ( panel->processEvent(event) )
return true;
}
switch ( event->type )
{
case SDL_KEYDOWN:
switch ( event->key.keysym.sym )
{
case SDLK_ESCAPE:
CloseTopPanel();
return true;
default:
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if ( event->button.button == SDL_BUTTON_LEFT ) {
if ( panel->mouseLocation.x != -1
&& panel->mouseLocation.y != -1 )
{
int stackPosition = GetPanelStackPosition(panel);
while ( TopPanelStackPosition() > stackPosition )
CloseTopPanel();
return true;
}
}
default:
break;
}
return false;
}
bool DidClickOnItem(const SDL_Event * event, const Panel * panel)
{
return event->type == SDL_MOUSEBUTTONDOWN
&& event->button.button == SDL_BUTTON_LEFT
&& panel->selection != -1;
}