Debian Codegraph @

src/cdogsed/editor_ui_common.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014, 2016-2017, 2019-2021, 2023 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 "editor_ui_common.h"
29
30 #include <SDL_opengl.h>
31
32 #include <cdogs/events.h>
33 #include <cdogs/font.h>
34 #include <cdogs/gamedata.h>
35 #include <cdogs/log.h>
36 #include <cdogs/palette.h>
37
38 void DisplayMapItem(const struct vec2i pos, const MapObject *mo)
39 {
40 struct vec2i offset;
41 const Pic *pic = MapObjectGetPic(mo, &offset);
42 PicRender(
43 pic, gGraphicsDevice.gameWindow.renderer, svec2i_add(pos, offset),
44 colorWhite, 0, svec2_one(), SDL_FLIP_NONE, Rect2iZero());
45 }
46
47 void DisplayMapItemWithDensity(
48 const struct vec2i pos, const MapObjectDensity *mod,
49 const bool isHighlighted)
50 {
51 DisplayMapItem(pos, mod->M);
52 if (isHighlighted)
53 {
54 FontCh('>', svec2i_add(pos, svec2i(-8, -4)));
55 }
56 char s[10];
57 sprintf(s, "%d", mod->Density);
58 FontStr(s, svec2i_add(pos, svec2i(-8, 5)));
59 }
60
61 void DisplayPickup(const struct vec2i pos, const PickupClass *p)
62 {
63 const Pic *pic = CPicGetPic(&p->Pic, 0);
64 PicRender(
65 pic, gGraphicsDevice.gameWindow.renderer, svec2i_subtract(pos, svec2i_scale_divide(pic->size, 2)),
66 colorWhite, 0, svec2_one(), SDL_FLIP_NONE, Rect2iZero());
67 }
68
69 void DisplayPickupWithCount(
70 const struct vec2i pos, const PickupCount *pc,
71 const bool isHighlighted)
72 {
73 DisplayPickup(pos, pc->P);
74 if (isHighlighted)
75 {
76 FontCh('>', svec2i_add(pos, svec2i(-8, -4)));
77 }
78 char s[10];
79 sprintf(s, "%d", pc->Count);
80 FontStr(s, svec2i_add(pos, svec2i(-8, 5)));
81 }
82
83 void DrawKey(UIObject *o, GraphicsDevice *g, struct vec2i pos, void *vData)
84 {
85 const IndexedEditorBrush *data = vData;
86 if (data->u.ItemIndex == -1)
87 {
88 // No key; don't draw
89 return;
90 }
91 const Mission *m = CampaignGetCurrentMission(&gCampaign);
92 const Pic *pic =
93 CPicGetPic(&KeyPickupClass(m->KeyStyle, data->u.ItemIndex)->Pic, 0);
94 pos = svec2i_add(svec2i_add(pos, o->Pos), svec2i_scale_divide(o->Size, 2));
95 pos = svec2i_subtract(pos, svec2i_scale_divide(pic->size, 2));
96 PicRender(
97 pic, g->gameWindow.renderer, pos, colorWhite, 0, svec2_one(),
98 SDL_FLIP_NONE, Rect2iZero());
99 }
100
101 void InsertMission(Campaign *co, Mission *mission, int idx)
102 {
103 Mission m;
104 if (mission == NULL)
105 {
106 MissionInit(&m);
107 m.Size = svec2i(48, 48);
108 // Set some default values for the mission
109 m.u.Classic.CorridorWidth = 1;
110 m.u.Classic.Rooms.Min = m.u.Classic.Rooms.Max = 5;
111 m.u.Classic.Rooms.WallLength = 1;
112 m.u.Classic.Rooms.WallPad = 1;
113 m.u.Classic.Doors.Min = m.u.Classic.Doors.Max = 1;
114 m.u.Classic.Pillars.Min = m.u.Classic.Pillars.Max = 1;
115 }
116 else
117 {
118 memset(&m, 0, sizeof m);
119 MissionCopy(&m, mission);
120 }
121 CArrayInsert(&co->Setting.Missions, idx, &m);
122 }
123
124 bool ConfirmScreen(const char *info, const char *msg)
125 {
126 WindowContextPreRender(&gGraphicsDevice.gameWindow);
127 ClearScreen(&gGraphicsDevice);
128 const int w = gGraphicsDevice.cachedConfig.Res.x;
129 const int h = gGraphicsDevice.cachedConfig.Res.y;
130 FontStr(info, svec2i((w - FontStrW(info)) / 2, (h - FontH()) / 2));
131 FontStr(msg, svec2i((w - FontStrW(msg)) / 2, (h + FontH()) / 2));
132 BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen);
133 WindowContextPostRender(&gGraphicsDevice.gameWindow);
134
135 SDL_Keycode k = SDL_GetKeyFromScancode(GetKey(&gEventHandlers));
136 return k == SDLK_y;
137 }
138
139 void ClearScreen(GraphicsDevice *g)
140 {
141 color_t color = {32, 32, 60, 255};
142 const Uint32 pixel = COLOR2PIXEL(color);
143 for (int i = 0; i < GraphicsGetScreenSize(&g->cachedConfig); i++)
144 {
145 g->buf[i] = pixel;
146 }
147 if (SDL_SetRenderTarget(g->gameWindow.renderer, g->bkgTgt) != 0)
148 {
149 LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
150 }
151 BlitUpdateFromBuf(g, g->bkgTgt);
152 if (SDL_SetRenderTarget(g->gameWindow.renderer, NULL) != 0)
153 {
154 LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
155 }
156 BlitClearBuf(g);
157 }
158
159 void DisplayFlag(
160 const struct vec2i pos, const char *s, const bool isOn,
161 const bool isHighlighted)
162 {
163 color_t labelMask = isHighlighted ? colorRed : colorWhite;
164 struct vec2i p = FontStrMask(s, pos, labelMask);
165 p = FontChMask(':', p, labelMask);
166 FontStrMask(isOn ? "On" : "Off", p, isOn ? colorPurple : colorWhite);
167 }
168
169 static const char *CampaignGetSeedStr(UIObject *o, void *data);
170 static EditorResult CampaignChangeSeed(void *data, int d);
171 UIObject *CreateCampaignSeedObj(const struct vec2i pos, Campaign *co)
172 {
173 const int th = FontH();
174 UIObject *o =
175 UIObjectCreate(UITYPE_LABEL, 0, svec2i_zero(), svec2i(50, th));
176 o->u.LabelFunc = CampaignGetSeedStr;
177 o->Data = co;
178 o->ChangeFunc = CampaignChangeSeed;
179 CSTRDUP(o->Tooltip, "Preview with different random seed");
180 o->Pos = pos;
181 return o;
182 }
183 static const char *CampaignGetSeedStr(UIObject *o, void *data)
184 {
185 static char s[128];
186 UNUSED(o);
187 Campaign *co = data;
188 if (!CampaignGetCurrentMission(co))
189 return NULL;
190 sprintf(s, "Seed: %d", ConfigGetInt(&gConfig, "Game.RandomSeed"));
191 return s;
192 }
193 static EditorResult CampaignChangeSeed(void *data, int d)
194 {
195 UNUSED(data);
196 if (gEventHandlers.keyboard.modState & KMOD_SHIFT)
197 {
198 d *= 10;
199 }
200 ConfigSetInt(
201 &gConfig, "Game.RandomSeed",
202 ConfigGetInt(&gConfig, "Game.RandomSeed") + d);
203 return EDITOR_RESULT_RELOAD;
204 }
205
206 typedef struct
207 {
208 bool (*ObjFunc)(UIObject *, MapObject *, void *);
209 void *Data;
210 // Data size required for map item change checking
211 size_t DataSize;
212 struct vec2i GridItemSize;
213 struct vec2i GridSize;
214 struct vec2i PageOffset;
215 } CreateAddMapItemObjsImplData;
216 static UIObject *CreateAddMapItemObjsImpl(
217 struct vec2i pos, CreateAddMapItemObjsImplData data);
218 UIObject *CreateAddMapItemObjs(
219 const struct vec2i pos, bool (*objFunc)(UIObject *, MapObject *, void *),
220 void *data, const size_t dataSize, const bool expandDown)
221 {
222 CreateAddMapItemObjsImplData d;
223 d.ObjFunc = objFunc;
224 d.Data = data;
225 d.DataSize = dataSize;
226 d.GridItemSize = svec2i(TILE_WIDTH + 4, TILE_HEIGHT * 2 + 4);
227 d.GridSize = svec2i(8, 6);
228 if (expandDown)
229 {
230 d.PageOffset = svec2i(-5, 5);
231 }
232 else
233 {
234 d.PageOffset =
235 svec2i(-5, -d.GridItemSize.y * d.GridSize.y - FontH() - 5);
236 }
237 return CreateAddMapItemObjsImpl(pos, d);
238 }
239 UIObject *CreateAddPickupSpawnerObjs(
240 const struct vec2i pos, bool (*objFunc)(UIObject *, MapObject *, void *),
241 void *data, const size_t dataSize)
242 {
243 CreateAddMapItemObjsImplData d;
244 d.ObjFunc = objFunc;
245 d.Data = data;
246 d.DataSize = dataSize;
247 d.GridItemSize = svec2i(TILE_WIDTH + 4, TILE_HEIGHT + 4);
248 d.GridSize = svec2i(5, 9);
249 d.PageOffset = svec2i(-5, 5);
250 return CreateAddMapItemObjsImpl(pos, d);
251 }
252 static void CreateAddMapItemSubObjs(UIObject *c, void *vData);
253 static UIObject *CreateAddMapItemObjsImpl(
254 struct vec2i pos, CreateAddMapItemObjsImplData data)
255 {
256 UIObject *c = UIObjectCreate(UITYPE_CONTEXT_MENU, 0, pos, svec2i_zero());
257 c->OnFocusFunc = CreateAddMapItemSubObjs;
258 c->IsDynamicData = true;
259 CMALLOC(c->Data, sizeof(CreateAddMapItemObjsImplData));
260 memcpy(c->Data, &data, sizeof data);
261
262 return c;
263 }
264 static int CountAddMapItemSubObjs(const UIObject *o);
265 static void CreateAddMapItemSubObjs(UIObject *c, void *vData)
266 {
267 const CreateAddMapItemObjsImplData *data = vData;
268 const int pageSize = data->GridSize.x * data->GridSize.y;
269 // Check if we need to recreate the objs
270 // TODO: this is a very heavyweight way to do it
271 int count = 0;
272 bool allChildrenSame = true;
273 for (int i = 0; i < MapObjectsCount(&gMapObjects); i++)
274 {
275 MapObject *mo = IndexMapObject(i);
276 UIObject *o2 = UIObjectCreate(
277 UITYPE_CUSTOM, 0, svec2i_zero(), data->GridItemSize);
278 o2->IsDynamicData = true;
279 CCALLOC(o2->Data, data->DataSize);
280 if (!data->ObjFunc(o2, mo, data->Data))
281 {
282 UIObjectDestroy(o2);
283 continue;
284 }
285 const int pageIdx = count / pageSize;
286 if (pageIdx >= (int)c->Children.size)
287 {
288 allChildrenSame = false;
289 break;
290 }
291 const UIObject **op = CArrayGet(&c->Children, pageIdx);
292 const UIObject **octx = CArrayGet(&(*op)->Children, 0);
293 const int idx = count % pageSize;
294 if (idx >= (int)(*octx)->Children.size)
295 {
296 allChildrenSame = false;
297 break;
298 }
299 const UIObject **oc = CArrayGet(&(*octx)->Children, idx);
300 if (memcmp(o2->Data, (*oc)->Data, data->DataSize) != 0)
301 {
302 allChildrenSame = false;
303 UIObjectDestroy(o2);
304 break;
305 }
306 count++;
307 UIObjectDestroy(o2);
308 }
309 int cCount = CountAddMapItemSubObjs(c);
310 if (cCount == count && allChildrenSame)
311 {
312 return;
313 }
314
315 // Recreate the child UI objects
316 c->Highlighted = NULL;
317 UIObject **objs = c->Children.data;
318 for (int i = 0; i < (int)c->Children.size; i++, objs++)
319 {
320 UIObjectDestroy(*objs);
321 }
322 CArrayClear(&c->Children);
323
324 // Create pagination
325 int pageNum = 1;
326 UIObject *pageLabel = NULL;
327 UIObject *page = NULL;
328 UIObject *o =
329 UIObjectCreate(UITYPE_CUSTOM, 0, svec2i_zero(), data->GridItemSize);
330 const struct vec2i gridStart = svec2i_zero();
331 struct vec2i pos = svec2i_zero();
332 count = 0;
333 for (int i = 0; i < MapObjectsCount(&gMapObjects); i++)
334 {
335 // Only add normal map objects
336 MapObject *mo = IndexMapObject(i);
337 UIObject *o2 = UIObjectCopy(o);
338 o2->IsDynamicData = true;
339 CCALLOC(o2->Data, data->DataSize);
340 if (!data->ObjFunc(o2, mo, data->Data))
341 {
342 UIObjectDestroy(o2);
343 continue;
344 }
345 o2->Pos = pos;
346 if (count == 0)
347 {
348 pageLabel = UIObjectCreate(
349 UITYPE_LABEL, 0, svec2i((pageNum - 1) * 10, 0),
350 svec2i(10, FontH()));
351 char buf[32];
352 sprintf(buf, "%d", pageNum);
353 UIObjectSetDynamicLabel(pageLabel, buf);
354 page = UIObjectCreate(
355 UITYPE_CONTEXT_MENU, 0,
356 svec2i_add(data->PageOffset, pageLabel->Size), svec2i_zero());
357 UIObjectAddChild(pageLabel, page);
358 pageNum++;
359 }
360 UIObjectAddChild(page, o2);
361 pos.x += o->Size.x;
362 if (((count + 1) % data->GridSize.x) == 0)
363 {
364 pos.x = gridStart.x;
365 pos.y += o->Size.y;
366 }
367 count++;
368 if (count == pageSize)
369 {
370 count = 0;
371 pos = gridStart;
372 UIObjectAddChild(c, pageLabel);
373 }
374 }
375 if (pageLabel != NULL)
376 {
377 UIObjectAddChild(c, pageLabel);
378 }
379
380 UIObjectDestroy(o);
381 }
382 static int CountAddMapItemSubObjs(const UIObject *o)
383 {
384 int count = 0;
385 CA_FOREACH(const UIObject *, page, o->Children)
386 const UIObject **ctx = CArrayGet(&(*page)->Children, 0);
387 count += (int)(*ctx)->Children.size;
388 CA_FOREACH_END()
389 return count;
390 }
391
392 typedef struct
393 {
394 bool (*ObjFunc)(UIObject *, PickupClass *, void *);
395 void *Data;
396 // Data size required for pickup change checking
397 size_t DataSize;
398 struct vec2i GridItemSize;
399 struct vec2i GridSize;
400 struct vec2i PageOffset;
401 } CreateAddPickupObjsImplData;
402 static UIObject *CreateAddPickupObjsImpl(
403 struct vec2i pos, CreateAddPickupObjsImplData data);
404 UIObject *CreateAddPickupObjs(
405 const struct vec2i pos, bool (*objFunc)(UIObject *, PickupClass *, void *),
406 void *data, const size_t dataSize, const bool expandDown)
407 {
408 CreateAddPickupObjsImplData d;
409 d.ObjFunc = objFunc;
410 d.Data = data;
411 d.DataSize = dataSize;
412 d.GridItemSize = svec2i(TILE_WIDTH + 4, TILE_HEIGHT * 2 + 4);
413 d.GridSize = svec2i(8, 6);
414 if (expandDown)
415 {
416 d.PageOffset = svec2i(-5, 5);
417 }
418 else
419 {
420 d.PageOffset =
421 svec2i(-5, -d.GridItemSize.y * d.GridSize.y - FontH() - 5);
422 }
423 return CreateAddPickupObjsImpl(pos, d);
424 }
425 static void CreateAddPickupSubObjs(UIObject *c, void *vData);
426 static UIObject *CreateAddPickupObjsImpl(
427 struct vec2i pos, CreateAddPickupObjsImplData data)
428 {
429 UIObject *c = UIObjectCreate(UITYPE_CONTEXT_MENU, 0, pos, svec2i_zero());
430 c->OnFocusFunc = CreateAddPickupSubObjs;
431 c->IsDynamicData = true;
432 CMALLOC(c->Data, sizeof(CreateAddPickupObjsImplData));
433 memcpy(c->Data, &data, sizeof data);
434
435 return c;
436 }
437 static int CountAddPickupSubObjs(const UIObject *o);
438 static void CreateAddPickupSubObjs(UIObject *c, void *vData)
439 {
440 const CreateAddPickupObjsImplData *data = vData;
441 const int pageSize = data->GridSize.x * data->GridSize.y;
442 // Check if we need to recreate the objs
443 // TODO: this is a very heavyweight way to do it
444 int count = 0;
445 bool allChildrenSame = true;
446 for (int i = 0; i < PickupClassesCount(&gPickupClasses); i++)
447 {
448 PickupClass *p = PickupClassGetById(&gPickupClasses, i);
449 UIObject *o2 = UIObjectCreate(
450 UITYPE_CUSTOM, 0, svec2i_zero(), data->GridItemSize);
451 o2->IsDynamicData = true;
452 CCALLOC(o2->Data, data->DataSize);
453 if (!data->ObjFunc(o2, p, data->Data))
454 {
455 UIObjectDestroy(o2);
456 continue;
457 }
458 const int pageIdx = count / pageSize;
459 if (pageIdx >= (int)c->Children.size)
460 {
461 allChildrenSame = false;
462 break;
463 }
464 const UIObject **op = CArrayGet(&c->Children, pageIdx);
465 const UIObject **octx = CArrayGet(&(*op)->Children, 0);
466 const int idx = count % pageSize;
467 if (idx >= (int)(*octx)->Children.size)
468 {
469 allChildrenSame = false;
470 break;
471 }
472 const UIObject **oc = CArrayGet(&(*octx)->Children, idx);
473 if (memcmp(o2->Data, (*oc)->Data, data->DataSize) != 0)
474 {
475 allChildrenSame = false;
476 UIObjectDestroy(o2);
477 break;
478 }
479 count++;
480 UIObjectDestroy(o2);
481 }
482 int cCount = CountAddPickupSubObjs(c);
483 if (cCount == count && allChildrenSame)
484 {
485 return;
486 }
487
488 // Recreate the child UI objects
489 c->Highlighted = NULL;
490 UIObject **objs = c->Children.data;
491 for (int i = 0; i < (int)c->Children.size; i++, objs++)
492 {
493 UIObjectDestroy(*objs);
494 }
495 CArrayClear(&c->Children);
496
497 // Create pagination
498 int pageNum = 1;
499 UIObject *pageLabel = NULL;
500 UIObject *page = NULL;
501 UIObject *o =
502 UIObjectCreate(UITYPE_CUSTOM, 0, svec2i_zero(), data->GridItemSize);
503 const struct vec2i gridStart = svec2i_zero();
504 struct vec2i pos = svec2i_zero();
505 count = 0;
506 for (int i = 0; i < PickupClassesCount(&gPickupClasses); i++)
507 {
508 PickupClass *p = PickupClassGetById(&gPickupClasses, i);
509 UIObject *o2 = UIObjectCopy(o);
510 o2->IsDynamicData = true;
511 CCALLOC(o2->Data, data->DataSize);
512 if (!data->ObjFunc(o2, p, data->Data))
513 {
514 UIObjectDestroy(o2);
515 continue;
516 }
517 o2->Pos = pos;
518 if (count == 0)
519 {
520 pageLabel = UIObjectCreate(
521 UITYPE_LABEL, 0, svec2i((pageNum - 1) * 10, 0),
522 svec2i(10, FontH()));
523 char buf[32];
524 sprintf(buf, "%d", pageNum);
525 UIObjectSetDynamicLabel(pageLabel, buf);
526 page = UIObjectCreate(
527 UITYPE_CONTEXT_MENU, 0,
528 svec2i_add(data->PageOffset, pageLabel->Size), svec2i_zero());
529 UIObjectAddChild(pageLabel, page);
530 pageNum++;
531 }
532 UIObjectAddChild(page, o2);
533 pos.x += o->Size.x;
534 if (((count + 1) % data->GridSize.x) == 0)
535 {
536 pos.x = gridStart.x;
537 pos.y += o->Size.y;
538 }
539 count++;
540 if (count == pageSize)
541 {
542 count = 0;
543 pos = gridStart;
544 UIObjectAddChild(c, pageLabel);
545 }
546 }
547 if (pageLabel != NULL)
548 {
549 UIObjectAddChild(c, pageLabel);
550 }
551
552 UIObjectDestroy(o);
553 }
554 static int CountAddPickupSubObjs(const UIObject *o)
555 {
556 int count = 0;
557 CA_FOREACH(const UIObject *, page, o->Children)
558 const UIObject **ctx = CArrayGet(&(*page)->Children, 0);
559 count += (int)(*ctx)->Children.size;
560 CA_FOREACH_END()
561 return count;
562 }
563
564 char *MakeMapObjectTooltip(const MapObject *mo)
565 {
566 // Add a descriptive tooltip for the map object
567 char buf[512];
568 // Construct text representing the placement flags
569 char pfBuf[128];
570 MapObjectGetPlacementFlagNames(mo, pfBuf, ", ");
571 // Construct text representing explosion guns
572 char exBuf[256];
573 strcpy(exBuf, "");
574 if (mo->DestroyGuns.size > 0)
575 {
576 sprintf(exBuf, "\nExplodes: ");
577 char exBuf2[256];
578 MapObjectGetExplosionGunNames(mo, exBuf2, ", ");
579 strcat(exBuf, exBuf2);
580 }
581 sprintf(
582 buf, "%s\nHealth: %d\nPlacement: %s%s", mo->Name, mo->Health, pfBuf,
583 exBuf);
584 char *tmp;
585 CSTRDUP(tmp, buf);
586 return tmp;
587 }
588 void MapObjectGetPlacementFlagNames(const MapObject *mo, char *buf, const char *sep)
589 {
590 if (mo->Flags == 0)
591 {
592 sprintf(buf, "anywhere");
593 }
594 else
595 {
596 strcpy(buf, "");
597 for (int i = 1; i < PLACEMENT_COUNT; i++)
598 {
599 if (mo->Flags & (1 << i))
600 {
601 if (strlen(buf) > 0)
602 {
603 strcat(buf, sep);
604 }
605 strcat(buf, PlacementFlagStr(i));
606 }
607 }
608 }
609 }
610 void MapObjectGetExplosionGunNames(const MapObject *mo, char *buf, const char *sep)
611 {
612 buf[0] = '\0';
613 for (int i = 0; i < (int)mo->DestroyGuns.size; i++)
614 {
615 if (i > 0)
616 {
617 strcat(buf, sep);
618 }
619 const WeaponClass **wc = CArrayGet(&mo->DestroyGuns, i);
620 strcat(buf, (*wc)->name);
621 }
622 }
623
624 char *MakePickupTooltip(const PickupClass *pc)
625 {
626 // Add a descriptive tooltip for the pickup
627 char buf[512];
628 strcpy(buf, pc->Name);
629 // Add effect descriptions
630 CA_FOREACH(const PickupEffect, pe, pc->Effects)
631 char peBuf[128];
632 switch (pe->Type)
633 {
634 case PICKUP_JEWEL:
635 sprintf(peBuf, "\n- Score: %d", pe->u.Score);
636 break;
637 case PICKUP_HEALTH:
638 sprintf(peBuf, "\n- Health: %d%s", pe->u.Heal.Amount, pe->u.Heal.ExceedMax ? " (exceed max)" : "");
639 break;
640 case PICKUP_AMMO: {
641 const Ammo *a = AmmoGetById(&gAmmo, pe->u.Ammo.Id);
642 sprintf(peBuf, "\n- Ammo: %s %d", a->Name, (int)pe->u.Ammo.Amount);
643 }
644 break;
645 case PICKUP_KEYCARD:
646 sprintf(peBuf, "\n- Key: %s", KeycardStr(pe->u.Keys));
647 break;
648 case PICKUP_GUN: {
649 const WeaponClass *wc = IdWeaponClass(pe->u.GunId);
650 sprintf(peBuf, "\n- Weapon: %s", wc->name);
651 }
652 break;
653 case PICKUP_SHOW_MAP:
654 strcpy(peBuf, "\n- Show map");
655 break;
656 case PICKUP_LIVES:
657 sprintf(peBuf, "\n- Lives: %d", pe->u.Lives);
658 break;
659 default:
660 CASSERT(false, "Unknown pickup type");
661 break;
662 }
663 strcat(buf, peBuf);
664 CA_FOREACH_END()
665 char *tmp;
666 CSTRDUP(tmp, buf);
667 return tmp;
668 }
669
670 static EditorResult CloseChange(void *data, int d);
671 void CreateCloseLabel(UIObject *c, const struct vec2i pos)
672 {
673 char *closeLabel = "Close";
674 const struct vec2i closeSize = FontStrSize(closeLabel);
675 UIObject *oClose = UIObjectCreate(UITYPE_LABEL, 0, pos, closeSize);
676 oClose->Label = closeLabel;
677 // Have a dummy change func so that the context menu is closed
678 oClose->ChangeFunc = CloseChange;
679 UIObjectAddChild(c, oClose);
680 }
681 static EditorResult CloseChange(void *data, int d)
682 {
683 UNUSED(data);
684 UNUSED(d);
685 return EDITOR_RESULT_RELOAD;
686 }
687
688 void TileClassGetBrushName(char *buf, const TileClass *tc)
689 {
690 sprintf(buf, "%s (%s)", tc->Name, tc->Style);
691 }
692
693 char *GetClassNames(const size_t len, const char *(*indexNameFunc)(const int))
694 {
695 int classLen = 0;
696 for (int i = 0; i < (int)len; i++)
697 {
698 const char *name = indexNameFunc(i);
699 classLen += (int)(strlen(name) + 1);
700 }
701 if (classLen == 0)
702 {
703 return "";
704 }
705 char *names;
706 CMALLOC(names, classLen);
707 char *cp = names;
708 for (int i = 0; i < (int)len; i++)
709 {
710 const char *name = indexNameFunc(i);
711 CASSERT(name != NULL, "Class has no name");
712 strcpy(cp, name);
713 cp += strlen(name) + 1;
714 }
715 return names;
716 }
717
718 void TexArrayInit(CArray *arr, const size_t count)
719 {
720 CArrayInitFill(arr, sizeof(GLuint), count, NULL);
721 glGenTextures((GLsizei)count, (GLuint *)arr->data);
722 }
723 void TexArrayTerminate(CArray *arr)
724 {
725 glDeleteTextures((GLsizei)arr->size, (const GLuint *)arr->data);
726 CArrayTerminate(arr);
727 }