Debian Codegraph @

src/cdogsed/editor_ui_static.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2016, 2019-2020 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_static.h"
29
30 #include <assert.h>
31
32 #include <cdogs/draw/draw.h>
33 #include <cdogs/events.h>
34 #include <cdogs/font.h>
35 #include <cdogs/log.h>
36 #include <cdogs/map.h>
37
38 #include "editor_ui_common.h"
39 #include "editor_ui_static_additem.h"
40 #include "exit_brush.h"
41 #include "tile_brush.h"
42
43 MISSION_CHECK_TYPE_FUNC(MAPTYPE_STATIC)
44
45 static const char *BrushGetTypeStr(EditorBrush *brush, int isMain)
46 {
47 static char s[128];
48 const int idx = isMain ? brush->MainType : brush->SecondaryType;
49 char tcName[64];
50 sprintf(tcName, "%d", idx);
51 TileClass *tc;
52 if (hashmap_get(
53 gMission.missionData->u.Static.TileClasses, tcName,
54 (any_t *)&tc) != MAP_OK)
55 {
56 LOG(LM_EDIT, LL_ERROR, "cannot find tile class key: %s", tcName);
57 return "";
58 }
59 TileClassGetBrushName(tcName, tc);
60 sprintf(s, "Brush %d: %s", isMain ? 1 : 2, tcName);
61 return s;
62 }
63 static const char *BrushGetMainTypeStr(UIObject *o, void *data)
64 {
65 UNUSED(o);
66 EditorBrush *brush = data;
67 return BrushGetTypeStr(brush, 1);
68 }
69 static const char *BrushGetSecondaryTypeStr(UIObject *o, void *data)
70 {
71 UNUSED(o);
72 EditorBrush *brush = data;
73 return BrushGetTypeStr(brush, 0);
74 }
75 static const char *BrushGetSizeStr(UIObject *o, void *data)
76 {
77 static char s[128];
78 UNUSED(o);
79 EditorBrush *brush = data;
80 sprintf(s, "Brush Size: %d", brush->BrushSize);
81 return s;
82 }
83 static char *BrushGetGuideImageStr(UIObject *o, void *data)
84 {
85 UNUSED(o);
86 EditorBrush *brush = data;
87 return brush->GuideImage;
88 }
89 static const char *BrushGetGuideImageAlphaStr(UIObject *o, void *data)
90 {
91 static char s[128];
92 UNUSED(o);
93 EditorBrush *brush = data;
94 sprintf(s, "Guide Alpha: %d%%", (int)brush->GuideImageAlpha * 100 / 255);
95 return s;
96 }
97 static void MissionDrawAltFloorsEnabled(
98 UIObject *o, GraphicsDevice *g, struct vec2i pos, void *data)
99 {
100 UNUSED(o);
101 UNUSED(g);
102 Campaign *co = data;
103 Mission *m = CampaignGetCurrentMission(co);
104 if (!m)
105 return;
106 DisplayFlag(
107 svec2i_add(pos, o->Pos), "AltFloors",
108 m->u.Static.AltFloorsEnabled,
109 UIObjectIsHighlighted(o));
110 }
111
112 static EditorResult BrushChangeMainType(void *data, int d)
113 {
114 UNUSED(d);
115 EditorBrush *b = data;
116 return TileBrush(&gPicManager, &gEventHandlers, &gCampaign, &b->MainType);
117 }
118 static EditorResult BrushChangeSecondaryType(void *data, int d)
119 {
120 UNUSED(d);
121 EditorBrush *b = data;
122 return TileBrush(
123 &gPicManager, &gEventHandlers, &gCampaign, &b->SecondaryType);
124 }
125 static EditorResult BrushChangeSize(void *data, int d)
126 {
127 EditorBrush *b = data;
128 b->BrushSize = CLAMP(b->BrushSize + d, 1, 5);
129 return EDITOR_RESULT_NONE;
130 }
131 static EditorResult BrushLoadGuideImage(void *data, int d)
132 {
133 UNUSED(d);
134 EditorBrush *b = data;
135 EditorBrushTryLoadGuideImage(b, b->GuideImage);
136 return EDITOR_RESULT_NONE;
137 }
138 static EditorResult BrushChangeGuideImageAlpha(void *data, int d)
139 {
140 EditorBrush *b = data;
141 d *= 4;
142 if (gEventHandlers.keyboard.modState & KMOD_SHIFT)
143 {
144 d *= 8;
145 }
146 b->GuideImageAlpha = (Uint8)CLAMP((int)b->GuideImageAlpha + d, 0, 255);
147 return EDITOR_RESULT_NONE;
148 }
149 static EditorResult MissionChangeAltFloorsEnabled(void *data, int d)
150 {
151 UNUSED(d);
152 Campaign *co = data;
153 Mission *m = CampaignGetCurrentMission(co);
154 m->u.Static.AltFloorsEnabled = !m->u.Static.AltFloorsEnabled;
155 return EDITOR_RESULT_CHANGED;
156 }
157 static int BrushIsBrushTypePoint(void *data)
158 {
159 EditorBrush *b = data;
160 return b->Type == BRUSHTYPE_POINT;
161 }
162 static int BrushIsBrushTypeLine(void *data)
163 {
164 EditorBrush *b = data;
165 return b->Type == BRUSHTYPE_LINE;
166 }
167 static int BrushIsBrushTypeBox(void *data)
168 {
169 EditorBrush *b = data;
170 return b->Type == BRUSHTYPE_BOX;
171 }
172 static int BrushIsBrushTypeBoxFilled(void *data)
173 {
174 EditorBrush *b = data;
175 return b->Type == BRUSHTYPE_BOX_FILLED;
176 }
177 static int BrushIsBrushTypeBoxAndFill(void *data)
178 {
179 EditorBrush *b = data;
180 return b->Type == BRUSHTYPE_BOX_AND_FILL;
181 }
182 static int BrushIsBrushTypeSelect(void *data)
183 {
184 EditorBrush *b = data;
185 return b->Type == BRUSHTYPE_SELECT;
186 }
187 static int BrushIsBrushTypeFill(void *data)
188 {
189 EditorBrush *b = data;
190 return b->Type == BRUSHTYPE_FILL;
191 }
192 static int BrushIsBrushTypeAddItem(void *data)
193 {
194 EditorBrush *b = data;
195 return b->Type == BRUSHTYPE_SET_PLAYER_START ||
196 b->Type == BRUSHTYPE_ADD_ITEM ||
197 b->Type == BRUSHTYPE_ADD_CHARACTER ||
198 b->Type == BRUSHTYPE_ADD_KEY || b->Type == BRUSHTYPE_ADD_PICKUP;
199 }
200 static int BrushIsBrushTypeSetKey(void *data)
201 {
202 EditorBrush *b = data;
203 return b->Type == BRUSHTYPE_SET_KEY;
204 }
205 static int BrushIsBrushTypeSetExit(void *data)
206 {
207 EditorBrush *b = data;
208 return b->Type == BRUSHTYPE_SET_EXIT;
209 }
210 static EditorResult BrushSetBrushTypePoint(void *data, int d)
211 {
212 UNUSED(d);
213 EditorBrush *b = data;
214 b->Type = BRUSHTYPE_POINT;
215 MouseSetPicCursor(
216 &gEventHandlers.mouse,
217 PicManagerGetPic(&gPicManager, "editor/cursors/pencil"));
218 return EDITOR_RESULT_NONE;
219 }
220 static EditorResult BrushSetBrushTypeLine(void *data, int d)
221 {
222 UNUSED(d);
223 EditorBrush *b = data;
224 b->Type = BRUSHTYPE_LINE;
225 MouseSetPicCursor(
226 &gEventHandlers.mouse,
227 PicManagerGetPic(&gPicManager, "editor/cursors/line"));
228 return EDITOR_RESULT_NONE;
229 }
230 static EditorResult BrushSetBrushTypeBox(void *data, int d)
231 {
232 UNUSED(d);
233 EditorBrush *b = data;
234 b->Type = BRUSHTYPE_BOX;
235 MouseSetPicCursor(
236 &gEventHandlers.mouse,
237 PicManagerGetPic(&gPicManager, "editor/cursors/box"));
238 return EDITOR_RESULT_NONE;
239 }
240 static EditorResult BrushSetBrushTypeBoxFilled(void *data, int d)
241 {
242 UNUSED(d);
243 EditorBrush *b = data;
244 b->Type = BRUSHTYPE_BOX_FILLED;
245 MouseSetPicCursor(
246 &gEventHandlers.mouse,
247 PicManagerGetPic(&gPicManager, "editor/cursors/box_filled"));
248 return EDITOR_RESULT_NONE;
249 }
250 static EditorResult BrushSetBrushTypeBoxAndFill(void *data, int d)
251 {
252 UNUSED(d);
253 EditorBrush *b = data;
254 b->Type = BRUSHTYPE_BOX_AND_FILL;
255 MouseSetPicCursor(
256 &gEventHandlers.mouse,
257 PicManagerGetPic(&gPicManager, "editor/cursors/box_n_fill"));
258 return EDITOR_RESULT_NONE;
259 }
260 static EditorResult BrushSetBrushTypeSelect(void *data, int d)
261 {
262 UNUSED(d);
263 EditorBrush *b = data;
264 b->Type = BRUSHTYPE_SELECT;
265 MouseSetPicCursor(
266 &gEventHandlers.mouse,
267 PicManagerGetPic(&gPicManager, "editor/cursors/select"));
268 return EDITOR_RESULT_NONE;
269 }
270 static EditorResult BrushSetBrushTypeFill(void *data, int d)
271 {
272 UNUSED(d);
273 EditorBrush *b = data;
274 b->Type = BRUSHTYPE_FILL;
275 MouseSetPicCursor(
276 &gEventHandlers.mouse,
277 PicManagerGetPic(&gPicManager, "editor/cursors/bucket"));
278 return EDITOR_RESULT_NONE;
279 }
280 static EditorResult BrushSetBrushTypeSetExit(void *data, int d)
281 {
282 UNUSED(d);
283 EditorBrush *b = data;
284 const EditorResult result =
285 ExitBrush(&gEventHandlers, &gCampaign, &b->u.ItemIndex);
286 b->Type = BRUSHTYPE_SET_EXIT;
287 MouseSetPicCursor(
288 &gEventHandlers.mouse,
289 PicManagerGetPic(&gPicManager, "editor/cursors/set_exit"));
290 return result;
291 }
292 static EditorResult BrushSetBrushTypeSetKey(void *data, int d)
293 {
294 UNUSED(d);
295 IndexedEditorBrush *b = data;
296 b->Brush->Type = BRUSHTYPE_SET_KEY;
297 b->Brush->u.ItemIndex = b->u.ItemIndex;
298 MouseSetPicCursor(
299 &gEventHandlers.mouse,
300 PicManagerGetPic(&gPicManager, "editor/cursors/set_key"));
301 return EDITOR_RESULT_CHANGE_TOOL;
302 }
303 static void ActivateBrush(UIObject *o, void *data)
304 {
305 UNUSED(o);
306 EditorBrush *b = data;
307 b->IsActive = 1;
308 }
309 static bool DeactivateBrush(void *data)
310 {
311 EditorBrush *b = data;
312 b->IsActive = 0;
313 return false;
314 }
315 static void ActivateIndexedEditorBrush(UIObject *o, void *data)
316 {
317 UNUSED(o);
318 IndexedEditorBrush *b = data;
319 b->Brush->IsActive = true;
320 }
321 static bool DeactivateIndexedEditorBrush(void *data)
322 {
323 IndexedEditorBrush *b = data;
324 b->Brush->IsActive = false;
325 return false;
326 }
327
328 static UIObject *CreateSetKeyObjs(struct vec2i pos, EditorBrush *brush);
329 UIObject *CreateStaticMapObjs(
330 struct vec2i pos, Campaign *co, EditorBrush *brush)
331 {
332 int x = pos.x;
333 const int th = FontH();
334 UIObject *c = UIObjectCreate(UITYPE_NONE, 0, svec2i_zero(), svec2i_zero());
335 UIObject *o2;
336 // Check whether the map type matches, and set visibility
337 c->CheckVisible = MissionCheckTypeFunc;
338 c->Data = co;
339
340 UIObject *o =
341 UIObjectCreate(UITYPE_BUTTON, 0, svec2i_zero(), svec2i_zero());
342 o->Data = brush;
343 o->OnFocusFunc = ActivateBrush;
344 o->OnUnfocusFunc = DeactivateBrush;
345 o2 = UIObjectCopy(o);
346 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/pencil"));
347 o2->u.Button.IsDownFunc = BrushIsBrushTypePoint;
348 o2->ChangeFunc = BrushSetBrushTypePoint;
349 CSTRDUP(o2->Tooltip, "Point");
350 o2->Pos = pos;
351 UIObjectAddChild(c, o2);
352 pos.x += o2->Size.x;
353 o2 = UIObjectCopy(o);
354 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/line"));
355 o2->u.Button.IsDownFunc = BrushIsBrushTypeLine;
356 o2->ChangeFunc = BrushSetBrushTypeLine;
357 CSTRDUP(o2->Tooltip, "Line");
358 o2->Pos = pos;
359 UIObjectAddChild(c, o2);
360 pos.x += o2->Size.x;
361 o2 = UIObjectCopy(o);
362 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/box"));
363 o2->u.Button.IsDownFunc = BrushIsBrushTypeBox;
364 o2->ChangeFunc = BrushSetBrushTypeBox;
365 CSTRDUP(o2->Tooltip, "Box");
366 o2->Pos = pos;
367 UIObjectAddChild(c, o2);
368 pos.x += o2->Size.x;
369 o2 = UIObjectCopy(o);
370 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/box_filled"));
371 o2->u.Button.IsDownFunc = BrushIsBrushTypeBoxFilled;
372 o2->ChangeFunc = BrushSetBrushTypeBoxFilled;
373 CSTRDUP(o2->Tooltip, "Box filled");
374 o2->Pos = pos;
375 UIObjectAddChild(c, o2);
376 pos.x += o2->Size.x;
377 o2 = UIObjectCopy(o);
378 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/box_n_fill"));
379 o2->u.Button.IsDownFunc = BrushIsBrushTypeBoxAndFill;
380 o2->ChangeFunc = BrushSetBrushTypeBoxAndFill;
381 CSTRDUP(o2->Tooltip, "Box & Fill");
382 o2->Pos = pos;
383 UIObjectAddChild(c, o2);
384 pos.x += o2->Size.x;
385 o2 = UIObjectCopy(o);
386 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/select"));
387 o2->u.Button.IsDownFunc = BrushIsBrushTypeSelect;
388 o2->ChangeFunc = BrushSetBrushTypeSelect;
389 CSTRDUP(o2->Tooltip, "Select and move");
390 o2->Pos = pos;
391 UIObjectAddChild(c, o2);
392 pos.x += o2->Size.x;
393 o2 = UIObjectCopy(o);
394 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/bucket"));
395 o2->u.Button.IsDownFunc = BrushIsBrushTypeFill;
396 o2->ChangeFunc = BrushSetBrushTypeFill;
397 CSTRDUP(o2->Tooltip, "Flood fill");
398 o2->Pos = pos;
399 UIObjectAddChild(c, o2);
400 pos.x += o2->Size.x;
401 o2 = UIObjectCopy(o);
402 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/add"));
403 o2->u.Button.IsDownFunc = BrushIsBrushTypeAddItem;
404 CSTRDUP(o2->Tooltip, "Add items\nRight click to remove");
405 o2->Pos = pos;
406 o2->OnFocusFunc = NULL;
407 o2->OnUnfocusFunc = NULL;
408 UIObjectAddChild(o2, CreateAddItemObjs(o2->Size, brush, co));
409 UIObjectAddChild(c, o2);
410 pos.x += o2->Size.x;
411 o2 = UIObjectCopy(o);
412 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/set_key"));
413 o2->u.Button.IsDownFunc = BrushIsBrushTypeSetKey;
414 CSTRDUP(o2->Tooltip, "Set key required for door");
415 o2->Pos = pos;
416 o2->OnFocusFunc = NULL;
417 o2->OnUnfocusFunc = NULL;
418 UIObjectAddChild(o2, CreateSetKeyObjs(o2->Size, brush));
419 UIObjectAddChild(c, o2);
420 pos.x += o2->Size.x;
421 o2 = UIObjectCopy(o);
422 UIButtonSetPic(o2, PicManagerGetPic(&gPicManager, "editor/set_exit"));
423 o2->u.Button.IsDownFunc = BrushIsBrushTypeSetExit;
424 o2->ChangeFunc = BrushSetBrushTypeSetExit;
425 CSTRDUP(o2->Tooltip, "Set exit areas (box drag)");
426 o2->Pos = pos;
427 UIObjectAddChild(c, o2);
428
429 UIObjectDestroy(o);
430 o = UIObjectCreate(UITYPE_LABEL, 0, svec2i_zero(), svec2i(90, th));
431 pos.x = x;
432 pos.y += o2->Size.y;
433 o2 = UIObjectCopy(o);
434 o2->u.LabelFunc = BrushGetMainTypeStr;
435 o2->Data = brush;
436 o2->ChangeFunc = BrushChangeMainType;
437 o2->OnFocusFunc = ActivateBrush;
438 o2->OnUnfocusFunc = DeactivateBrush;
439 CSTRDUP(o2->Tooltip, "Left click to paint the map with this tile type");
440 o2->Pos = pos;
441 UIObjectAddChild(c, o2);
442 pos.x += o2->Size.x;
443 o2 = UIObjectCopy(o);
444 o2->u.LabelFunc = BrushGetSecondaryTypeStr;
445 o2->Data = brush;
446 o2->ChangeFunc = BrushChangeSecondaryType;
447 o2->OnFocusFunc = ActivateBrush;
448 o2->OnUnfocusFunc = DeactivateBrush;
449 CSTRDUP(o2->Tooltip, "Right click to paint the map with this tile type");
450 o2->Pos = pos;
451 UIObjectAddChild(c, o2);
452
453 pos.x = x;
454 pos.y += th;
455 o2 = UIObjectCopy(o);
456 o2->u.LabelFunc = BrushGetSizeStr;
457 o2->Data = brush;
458 o2->ChangeFunc = BrushChangeSize;
459 o2->OnFocusFunc = ActivateBrush;
460 o2->OnUnfocusFunc = DeactivateBrush;
461 o2->Pos = pos;
462 UIObjectAddChild(c, o2);
463
464 UIObjectDestroy(o);
465 o = UIObjectCreate(UITYPE_TEXTBOX, 0, svec2i_zero(), svec2i(100, th));
466 pos.x = x;
467 pos.y += th;
468 o2 = UIObjectCopy(o);
469 o2->u.Textbox.TextLinkFunc = BrushGetGuideImageStr;
470 o2->u.Textbox.MaxLen = sizeof((EditorBrush *)0)->GuideImage - 1;
471 o2->Data = brush;
472 o2->ChangeFunc = BrushLoadGuideImage;
473 CSTRDUP(o2->u.Textbox.Hint, "(Tracing guide image)");
474 o2->Pos = pos;
475 UIObjectAddChild(c, o2);
476
477 UIObjectDestroy(o);
478 o = UIObjectCreate(UITYPE_LABEL, 0, svec2i_zero(), svec2i(100, th));
479 pos.x = x;
480 pos.y += th;
481 o2 = UIObjectCopy(o);
482 o2->u.LabelFunc = BrushGetGuideImageAlphaStr;
483 o2->Data = brush;
484 o2->ChangeFunc = BrushChangeGuideImageAlpha;
485 o2->Pos = pos;
486 UIObjectAddChild(c, o2);
487
488 pos.x = x;
489 pos.y += th;
490 o2 = UIObjectCreate(UITYPE_CUSTOM, 0, pos, o->Size);
491 o2->u.CustomDrawFunc = MissionDrawAltFloorsEnabled;
492 o2->Data = co;
493 o2->ChangeFunc = MissionChangeAltFloorsEnabled;
494 o2->Pos = pos;
495 UIObjectAddChild(c, o2);
496
497 UIObjectDestroy(o);
498 return c;
499 }
500
501 static UIObject *CreateSetKeyObjs(struct vec2i pos, EditorBrush *brush)
502 {
503 UIObject *o2;
504 UIObject *c = UIObjectCreate(UITYPE_CONTEXT_MENU, 0, pos, svec2i_zero());
505
506 UIObject *o = UIObjectCreate(
507 UITYPE_CUSTOM, 0, svec2i_zero(),
508 svec2i(TILE_WIDTH + 4, TILE_HEIGHT + 4));
509 o->ChangeFunc = BrushSetBrushTypeSetKey;
510 o->u.CustomDrawFunc = DrawKey;
511 o->OnFocusFunc = ActivateIndexedEditorBrush;
512 o->OnUnfocusFunc = DeactivateIndexedEditorBrush;
513 pos = svec2i_zero();
514 for (int i = -1; i < KEY_COUNT; i++)
515 {
516 o2 = UIObjectCopy(o);
517 o2->IsDynamicData = 1;
518 CMALLOC(o2->Data, sizeof(IndexedEditorBrush));
519 ((IndexedEditorBrush *)o2->Data)->Brush = brush;
520 ((IndexedEditorBrush *)o2->Data)->u.ItemIndex = i;
521 o2->Pos = pos;
522 if (i == -1)
523 {
524 // -1 means no key
525 CSTRDUP(o2->Tooltip, "no key");
526 }
527 UIObjectAddChild(c, o2);
528 pos.x += o->Size.x;
529 }
530
531 UIObjectDestroy(o);
532 return c;
533 }