Debian Codegraph @

src/cdogsed/tile_brush.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2019-2021 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "tile_brush.h"
29
30 #include "editor_ui_common.h"
31 #include "nk_window.h"
32
33 #define WIDTH 600
34 #define HEIGHT 500
35 #define MAIN_WIDTH 400
36 #define SIDE_WIDTH (WIDTH - MAIN_WIDTH)
37 #define ROW_HEIGHT 25
38 #define OPS_HEIGHT 35
39
40 static const char *IndexBulletClassName(const int i)
41 {
42 CASSERT(
43 i >= 0 && i < (int)gBulletClasses.Classes.size +
44 (int)gBulletClasses.CustomClasses.size,
45 "Bullet index out of bounds");
46 const BulletClass *b;
47 if (i < (int)gBulletClasses.Classes.size)
48 {
49 b = CArrayGet(&gBulletClasses.Classes, i);
50 }
51 else
52 {
53 b = CArrayGet(
54 &gBulletClasses.CustomClasses, i - gBulletClasses.Classes.size);
55 }
56 return b->Name;
57 }
58 static int BulletClassIndex(const BulletClass *b)
59 {
60 if (b == NULL)
61 {
62 return -1;
63 }
64 int i = 0;
65 CA_FOREACH(const BulletClass, b2, gBulletClasses.Classes)
66 if (b == b2)
67 {
68 return i;
69 }
70 i++;
71 CA_FOREACH_END()
72 CA_FOREACH(const BulletClass, b2, gBulletClasses.CustomClasses)
73 if (b == b2)
74 {
75 return i;
76 }
77 i++;
78 CA_FOREACH_END()
79 CASSERT(false, "cannot find bullet");
80 return -1;
81 }
82
83 typedef struct
84 {
85 struct nk_context *ctx;
86 PicManager *pm;
87 Mission *m;
88 int *brushIdx;
89 CArray texIdsTileClasses; // of GLuint
90 char *tcTypes;
91 char *floorStyles;
92 char *wallStyles;
93 char *doorStyles;
94 char *bullets;
95 CArray texIdsFloorStyles;
96 CArray texIdsWallStyles;
97 CArray texIdsDoorStyles;
98 EditorResult result;
99 } TileBrushData;
100
101 static void ResetTexIds(TileBrushData *data);
102 static const char *IndexFloorStyleName(const int idx);
103 static const char *IndexWallStyleName(const int idx);
104 static const char *IndexDoorStyleName(const int idx);
105 static const char *IndexTileStyleName(const TileClassType type, const int idx);
106 static int FloorStyleIndex(const char *style);
107 static int WallStyleIndex(const char *style);
108 static int DoorStyleIndex(const char *style);
109 static const TileClass *GetOrAddTileClass(
110 map_t c, const TileClass *base, PicManager *pm, const char *style,
111 const color_t mask, const color_t maskAlt);
112 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
113 EditorResult TileBrush(
114 PicManager *pm, EventHandlers *handlers, Campaign *co, int *brushIdx)
115 {
116 NKWindowConfig cfg;
117 memset(&cfg, 0, sizeof cfg);
118 cfg.Title = "Tile Brush";
119 cfg.Size = svec2i(WIDTH, HEIGHT);
120 color_t bg = {41, 26, 26, 255};
121 cfg.BG = bg;
122 cfg.Handlers = handlers;
123 cfg.Draw = Draw;
124
125 NKWindowInit(&cfg);
126
127 TileBrushData data;
128 memset(&data, 0, sizeof data);
129 data.pm = pm;
130 data.m = CampaignGetCurrentMission(co);
131 CASSERT(data.m->Type == MAPTYPE_STATIC, "unexpected map type");
132 data.brushIdx = brushIdx;
133 CArrayInit(&data.texIdsTileClasses, sizeof(GLuint));
134 ResetTexIds(&data);
135 cfg.DrawData = &data;
136 data.ctx = cfg.ctx;
137 data.tcTypes = GetClassNames(
138 TILE_CLASS_COUNT, (const char *(*)(const int))TileClassTypeStr);
139 data.floorStyles =
140 GetClassNames(pm->tileStyleNames.size, IndexFloorStyleName);
141 data.wallStyles =
142 GetClassNames(pm->wallStyleNames.size, IndexWallStyleName);
143 data.doorStyles =
144 GetClassNames(pm->doorStyleNames.size, IndexDoorStyleName);
145 // TODO: tile previews show wrong icons; possibly due to clashes between
146 // SDL2 textures and OGL textures. Load SDL2 textures first and in a second
147 // run, load OGL textures
148 data.bullets = GetClassNames(
149 BulletClassesCount(&gBulletClasses), IndexBulletClassName);
150 TexArrayInit(&data.texIdsFloorStyles, pm->tileStyleNames.size);
151 CA_FOREACH(const GLuint, texid, data.texIdsFloorStyles)
152 const char *style = *(char **)CArrayGet(&pm->tileStyleNames, _ca_index);
153 const TileClass *styleClass = GetOrAddTileClass(
154 gMap.TileClasses, &gTileFloor, pm, style, colorBattleshipGrey,
155 colorOfficeGreen);
156 LoadTexFromPic(*texid, styleClass->Pic);
157 CA_FOREACH_END()
158 TexArrayInit(&data.texIdsWallStyles, pm->wallStyleNames.size);
159 CA_FOREACH(const GLuint, texid, data.texIdsWallStyles)
160 const char *style = *(char **)CArrayGet(&pm->wallStyleNames, _ca_index);
161 const TileClass *styleClass = GetOrAddTileClass(
162 gMap.TileClasses, &gTileWall, pm, style, colorGravel,
163 colorOfficeGreen);
164 LoadTexFromPic(*texid, styleClass->Pic);
165 CA_FOREACH_END()
166 TexArrayInit(&data.texIdsDoorStyles, pm->doorStyleNames.size);
167 CA_FOREACH(const GLuint, texid, data.texIdsDoorStyles)
168 const char *style = *(char **)CArrayGet(&pm->doorStyleNames, _ca_index);
169 const TileClass *styleClass = GetOrAddTileClass(
170 gMap.TileClasses, &gTileDoor, pm, style, colorWhite, colorOfficeGreen);
171 LoadTexFromPic(*texid, styleClass->Pic);
172 CA_FOREACH_END()
173
174 NKWindow(cfg);
175
176 glDeleteTextures(
177 (GLsizei)data.texIdsTileClasses.size,
178 (const GLuint *)data.texIdsTileClasses.data);
179 CArrayTerminate(&data.texIdsTileClasses);
180 CFREE(data.tcTypes);
181 CFREE(data.floorStyles);
182 CFREE(data.wallStyles);
183 CFREE(data.doorStyles);
184 CFREE(data.bullets);
185 TexArrayTerminate(&data.texIdsFloorStyles);
186 TexArrayTerminate(&data.texIdsWallStyles);
187 TexArrayTerminate(&data.texIdsDoorStyles);
188 return data.result;
189 }
190
191 static void DrawTileOpsRow(
192 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC,
193 bool *result);
194 static void DrawTilePropsSidebar(
195 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
196 static void DrawTileType(
197 TileBrushData *tbData, TileClass *tc, const int tileId,
198 const int tilesDrawn);
199 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
200 {
201 UNUSED(win);
202 bool result = true;
203 TileBrushData *tbData = data;
204 TileClass *selectedTC =
205 MissionStaticIdTileClass(&tbData->m->u.Static, *tbData->brushIdx);
206
207 if (nk_begin(
208 ctx, "Properties", nk_rect(0, 0, SIDE_WIDTH, HEIGHT),
209 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
210 {
211 if (selectedTC != NULL)
212 {
213 DrawTilePropsSidebar(ctx, tbData, selectedTC);
214 }
215 nk_end(ctx);
216 }
217
218 if (nk_begin(
219 ctx, "Ops", nk_rect(SIDE_WIDTH, 0, MAIN_WIDTH, OPS_HEIGHT),
220 NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
221 {
222 DrawTileOpsRow(ctx, tbData, selectedTC, &result);
223 nk_end(ctx);
224 }
225
226 if (nk_begin(
227 ctx, "Tiles",
228 nk_rect(SIDE_WIDTH, OPS_HEIGHT, MAIN_WIDTH, HEIGHT - OPS_HEIGHT),
229 NK_WINDOW_BORDER))
230 {
231 nk_layout_row_dynamic(ctx, 40 * PIC_SCALE, MAIN_WIDTH / 120);
232 int tilesDrawn = 0;
233 for (int i = 0;
234 tilesDrawn < hashmap_length(tbData->m->u.Static.TileClasses); i++)
235 {
236 TileClass *tc = MissionStaticIdTileClass(&tbData->m->u.Static, i);
237 if (tc != NULL)
238 {
239 DrawTileType(tbData, tc, i, tilesDrawn);
240 tilesDrawn++;
241 }
242 }
243 nk_end(ctx);
244 }
245
246 return result;
247 }
248 static void DrawTileOpsRow(
249 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC,
250 bool *result)
251 {
252 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 4);
253 if (nk_button_label(ctx, "Done"))
254 {
255 *result = false;
256 }
257 if (nk_button_label(ctx, "Add"))
258 {
259 TileClass tc;
260 TileClassInit(
261 &tc, &gPicManager, &gTileFloor, "tile", "normal", colorGray,
262 colorGray);
263 if (MissionStaticAddTileClass(&tbData->m->u.Static, &tc) != NULL)
264 {
265 ResetTexIds(tbData);
266 }
267 TileClassTerminate(&tc);
268 tbData->result |= EDITOR_RESULT_CHANGED;
269 }
270 if (nk_button_label(ctx, "Duplicate"))
271 {
272 if (MissionStaticAddTileClass(&tbData->m->u.Static, selectedTC) !=
273 NULL)
274 {
275 ResetTexIds(tbData);
276 }
277 tbData->result |= EDITOR_RESULT_CHANGED;
278 }
279 if (hashmap_length(tbData->m->u.Static.TileClasses) > 0 &&
280 nk_button_label(ctx, "Remove"))
281 {
282 if (MissionStaticRemoveTileClass(
283 &tbData->m->u.Static, *tbData->brushIdx))
284 {
285 ResetTexIds(tbData);
286 // Set selected tile index to an existing tile
287 if (hashmap_length(tbData->m->u.Static.TileClasses) > 0)
288 {
289 for (*tbData->brushIdx = 0;
290 MissionStaticIdTileClass(
291 &tbData->m->u.Static, *tbData->brushIdx) == NULL;
292 (*tbData->brushIdx)++)
293 ;
294 }
295 }
296 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
297 }
298 }
299 static void DrawTileTypeSelect(
300 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
301 static void DrawTileStyleSelect(
302 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
303 static void DrawTileBulletSelect(
304 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
305 static void DrawTilePropsSidebar(
306 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
307 {
308 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
309
310 DrawTileTypeSelect(ctx, tbData, selectedTC);
311
312 if (selectedTC->Type != TILE_CLASS_NOTHING)
313 {
314 DrawTileStyleSelect(ctx, tbData, selectedTC);
315 if (selectedTC->Type == TILE_CLASS_FLOOR)
316 {
317 bool hasDamage = StrBulletClass(selectedTC->DamageBullet) != NULL;
318 if (DrawCheckbox(
319 ctx, "Damaging", "Whether actors take damage on this tile",
320 &hasDamage))
321 {
322 CFREE(selectedTC->DamageBullet);
323 if (hasDamage)
324 {
325 CSTRDUP(selectedTC->DamageBullet, IndexBulletClassName(0));
326 }
327 else
328 {
329 selectedTC->DamageBullet = NULL;
330 }
331 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
332 }
333 if (hasDamage)
334 {
335 DrawTileBulletSelect(ctx, tbData, selectedTC);
336 }
337 }
338 // Changing colour requires regenerating tile mask pics
339 if (ColorPicker(ctx, ROW_HEIGHT, "Primary Color", &selectedTC->Mask))
340 {
341 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
342 }
343 if (ColorPicker(ctx, ROW_HEIGHT, "Alt Color", &selectedTC->MaskAlt))
344 {
345 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
346 }
347
348 // Changing tile props can affect map object placement
349 if (DrawCheckbox(
350 ctx, "Can Walk", "Whether actors can walk through this tile",
351 &selectedTC->canWalk))
352 {
353 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
354 }
355 if (DrawCheckbox(
356 ctx, "Opaque", "Whether this tile cannot be seen through",
357 &selectedTC->isOpaque))
358 {
359 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
360 }
361 if (DrawCheckbox(
362 ctx, "Shootable", "Whether bullets will hit this tile",
363 &selectedTC->shootable))
364 {
365 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
366 }
367 if (DrawCheckbox(
368 ctx, "IsRoom",
369 "Affects random placement of indoor/outdoor map objects",
370 &selectedTC->IsRoom))
371 {
372 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
373 }
374 }
375 }
376 static void DrawTileTypeSelect(
377 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
378 {
379 nk_label(ctx, "Type:", NK_TEXT_LEFT);
380 const TileClassType newType = nk_combo_separator(
381 ctx, tbData->tcTypes, '\0', selectedTC->Type, TILE_CLASS_COUNT,
382 ROW_HEIGHT, nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT));
383 if (newType != selectedTC->Type)
384 {
385 TileClassTerminate(selectedTC);
386 const TileClass *base = &gTileFloor;
387 switch (newType)
388 {
389 case TILE_CLASS_FLOOR:
390 break;
391 case TILE_CLASS_WALL:
392 base = &gTileWall;
393 break;
394 case TILE_CLASS_DOOR:
395 base = &gTileDoor;
396 break;
397 case TILE_CLASS_NOTHING:
398 base = &gTileNothing;
399 break;
400 default:
401 CASSERT(false, "unknown tile class");
402 break;
403 }
404 TileClassInitDefault(
405 selectedTC, tbData->pm, base, NULL, &selectedTC->Mask);
406 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
407 }
408 }
409 static void DrawTileStyleSelect(
410 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
411 {
412 nk_label(ctx, "Style:", NK_TEXT_LEFT);
413 const GLuint *styleTexIds = NULL;
414 const char *styles = "";
415 int styleSelected = -1;
416 int styleCount = 0;
417 switch (selectedTC->Type)
418 {
419 case TILE_CLASS_FLOOR:
420 styles = tbData->floorStyles;
421 styleSelected = FloorStyleIndex(selectedTC->Style);
422 styleTexIds = tbData->texIdsFloorStyles.data;
423 styleCount = (int)tbData->texIdsFloorStyles.size;
424 break;
425 case TILE_CLASS_WALL:
426 styles = tbData->wallStyles;
427 styleSelected = WallStyleIndex(selectedTC->Style);
428 styleTexIds = tbData->texIdsWallStyles.data;
429 styleCount = (int)tbData->texIdsWallStyles.size;
430 break;
431 case TILE_CLASS_DOOR:
432 styles = tbData->doorStyles;
433 styleSelected = DoorStyleIndex(selectedTC->Style);
434 styleTexIds = tbData->texIdsDoorStyles.data;
435 styleCount = (int)tbData->texIdsDoorStyles.size;
436 break;
437 default:
438 break;
439 }
440 const int newStyle = nk_combo_separator_image(
441 ctx, styleTexIds, styles, '\0', styleSelected, styleCount, ROW_HEIGHT,
442 nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT));
443 if (newStyle != styleSelected)
444 {
445 CFREE(selectedTC->Style);
446 CSTRDUP(
447 selectedTC->Style, IndexTileStyleName(selectedTC->Type, newStyle));
448 TileClassReloadPic(selectedTC, tbData->pm);
449 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
450 }
451 }
452
453 static void DrawTileBulletSelect(
454 struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
455 {
456 nk_label(ctx, "Damage Bullet:", NK_TEXT_LEFT);
457 const int selectedIndex =
458 BulletClassIndex(StrBulletClass(selectedTC->DamageBullet));
459 const int newBullet = nk_combo_separator(
460 ctx, tbData->bullets, '\0', selectedIndex,
461 BulletClassesCount(&gBulletClasses), ROW_HEIGHT,
462 nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT));
463 if (newBullet != selectedIndex)
464 {
465 CFREE(selectedTC->DamageBullet);
466 CSTRDUP(selectedTC->DamageBullet, IndexBulletClassName(newBullet));
467 tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
468 }
469 if (nk_widget_is_hovered(ctx))
470 {
471 nk_tooltip(ctx, "Bullet type to hit player with when stepped on");
472 }
473 }
474
475 static void DrawTileClass(
476 struct nk_context *ctx, PicManager *pm, TileClass *tc,
477 const struct vec2i pos, const GLuint texid);
478 static void DrawTileType(
479 TileBrushData *tbData, TileClass *tc, const int tileId,
480 const int tilesDrawn)
481 {
482 char name[256];
483 TileClassGetBrushName(name, tc);
484 const int selected = *tbData->brushIdx == tileId;
485 if (nk_select_label(
486 tbData->ctx, name, NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED,
487 selected))
488 {
489 *tbData->brushIdx = tileId;
490 }
491 const GLuint *texid = CArrayGet(&tbData->texIdsTileClasses, tilesDrawn);
492 DrawTileClass(tbData->ctx, tbData->pm, tc, svec2i(-40, 5), *texid);
493 }
494 static void DrawTileClass(
495 struct nk_context *ctx, PicManager *pm, TileClass *tc,
496 const struct vec2i pos, const GLuint texid)
497 {
498 const Pic *pic = TileClassGetPic(pm, tc);
499 if (pic == NULL)
500 {
501 // Attempt to reload pic (we may have changed its colour for example)
502 TileClassReloadPic(tc, pm);
503 pic = TileClassGetPic(pm, tc);
504 }
505 DrawPic(ctx, pic, texid, pos, PIC_SCALE);
506 }
507
508 static void ResetTexIds(TileBrushData *data)
509 {
510 if (data->texIdsTileClasses.size > 0)
511 {
512 glDeleteTextures(
513 (GLsizei)data->texIdsTileClasses.size,
514 (const GLuint *)data->texIdsTileClasses.data);
515 }
516 const int nTileClasses = hashmap_length(data->m->u.Static.TileClasses);
517 CArrayResize(&data->texIdsTileClasses, nTileClasses, NULL);
518 glGenTextures(nTileClasses, (GLuint *)data->texIdsTileClasses.data);
519 }
520
521 static const char *IndexFloorStyleName(const int idx)
522 {
523 return *(const char **)CArrayGet(&gPicManager.tileStyleNames, idx);
524 }
525 static const char *IndexWallStyleName(const int idx)
526 {
527 return *(const char **)CArrayGet(&gPicManager.wallStyleNames, idx);
528 }
529 static const char *IndexDoorStyleName(const int idx)
530 {
531 return *(const char **)CArrayGet(&gPicManager.doorStyleNames, idx);
532 }
533 static const char *IndexTileStyleName(const TileClassType type, const int idx)
534 {
535 switch (type)
536 {
537 case TILE_CLASS_FLOOR:
538 return IndexFloorStyleName(idx);
539 case TILE_CLASS_WALL:
540 return IndexWallStyleName(idx);
541 case TILE_CLASS_DOOR:
542 return IndexDoorStyleName(idx);
543 default:
544 return NULL;
545 }
546 }
547 static int TileStyleIndex(const CArray *styles, const char *style)
548 {
549 if (style == NULL)
550 {
551 return -1;
552 }
553 CA_FOREACH(const char *, styleName, *styles)
554 if (strcmp(*styleName, style) == 0)
555 {
556 return _ca_index;
557 }
558 CA_FOREACH_END()
559 return -1;
560 }
561 static int FloorStyleIndex(const char *style)
562 {
563 return TileStyleIndex(&gPicManager.tileStyleNames, style);
564 }
565 static int WallStyleIndex(const char *style)
566 {
567 return TileStyleIndex(&gPicManager.wallStyleNames, style);
568 }
569 static int DoorStyleIndex(const char *style)
570 {
571 return TileStyleIndex(&gPicManager.doorStyleNames, style);
572 }
573
574 static const TileClass *GetOrAddTileClass(
575 map_t c, const TileClass *base, PicManager *pm, const char *style,
576 const color_t mask, const color_t maskAlt)
577 {
578 const TileClass *styleClass = TileClassesGetMaskedTile(
579 c, base, style, TileClassBaseStyleType(base->Type), mask, maskAlt);
580 if (styleClass == &gTileNothing)
581 {
582 styleClass = TileClassesAdd(
583 c, pm, base, style, TileClassBaseStyleType(base->Type), mask,
584 maskAlt);
585 }
586 return styleClass;
587 }