Debian Codegraph @

src/cdogsed/exit_brush.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 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 "exit_brush.h"
29
30 #include "editor_ui_common.h"
31 #include "nk_window.h"
32
33 #define WIDTH 500
34 #define HEIGHT 200
35 #define MAIN_WIDTH 300
36 #define SIDE_WIDTH (WIDTH - MAIN_WIDTH)
37 #define ROW_HEIGHT 25
38
39 typedef struct
40 {
41 struct nk_context *ctx;
42 Campaign *co;
43 Mission *m;
44 int *exitIdx;
45 EditorResult result;
46 } ExitBrushData;
47
48 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
49 EditorResult ExitBrush(EventHandlers *handlers, Campaign *co, int *exitIdx)
50 {
51 NKWindowConfig cfg;
52 memset(&cfg, 0, sizeof cfg);
53 cfg.Title = "Exit Brush";
54 cfg.Size = svec2i(WIDTH, HEIGHT);
55 color_t bg = {41, 26, 26, 255};
56 cfg.BG = bg;
57 cfg.Handlers = handlers;
58 cfg.Draw = Draw;
59
60 NKWindowInit(&cfg);
61
62 ExitBrushData data;
63 memset(&data, 0, sizeof data);
64 data.co = co;
65 data.m = CampaignGetCurrentMission(co);
66 CASSERT(data.m->Type == MAPTYPE_STATIC, "unexpected map type");
67 *exitIdx = CLAMP(*exitIdx, 0, (int)data.m->u.Static.Exits.size - 1);
68 data.exitIdx = exitIdx;
69 cfg.DrawData = &data;
70 data.ctx = cfg.ctx;
71
72 NKWindow(cfg);
73
74 return data.result;
75 }
76
77 static void DrawOpsRow(
78 struct nk_context *ctx, ExitBrushData *eData, bool *result);
79 static void DrawPropsSidebar(
80 struct nk_context *ctx, ExitBrushData *eData, Exit *selectedExit);
81 static void DrawExit(ExitBrushData *eData, Exit *e, const int idx);
82 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
83 {
84 UNUSED(win);
85 bool result = true;
86 ExitBrushData *eData = data;
87 Exit *selectedExit = NULL;
88 if (*eData->exitIdx > 0 &&
89 *eData->exitIdx < (int)eData->m->u.Static.Exits.size)
90 {
91 selectedExit = CArrayGet(&eData->m->u.Static.Exits, *eData->exitIdx);
92 }
93
94 if (nk_begin(
95 ctx, "Properties", nk_rect(0, 0, SIDE_WIDTH, HEIGHT),
96 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
97 {
98 if (selectedExit != NULL)
99 {
100 DrawPropsSidebar(ctx, eData, selectedExit);
101 }
102 nk_end(ctx);
103 }
104
105 if (nk_begin(
106 ctx, "", nk_rect(SIDE_WIDTH, 0, MAIN_WIDTH, HEIGHT),
107 NK_WINDOW_BORDER))
108 {
109 DrawOpsRow(ctx, eData, &result);
110
111 nk_layout_row_dynamic(ctx, 15 * PIC_SCALE, 1);
112 CA_FOREACH(Exit, e, eData->m->u.Static.Exits)
113 DrawExit(eData, e, _ca_index);
114 CA_FOREACH_END()
115 nk_end(ctx);
116 }
117 return result;
118 }
119 static void DrawOpsRow(
120 struct nk_context *ctx, ExitBrushData *eData, bool *result)
121 {
122 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 3);
123 if (nk_button_label(ctx, "Done"))
124 {
125 *result = false;
126 }
127 if (nk_button_label(ctx, "Add"))
128 {
129 Exit e;
130 memset(&e, 0, sizeof e);
131 e.Mission = eData->co->MissionIndex + 1;
132 *eData->exitIdx = (int)eData->m->u.Static.Exits.size;
133 CArrayPushBack(&eData->m->u.Static.Exits, &e);
134 eData->result |= EDITOR_RESULT_CHANGED;
135 }
136 if (eData->m->u.Static.Exits.size > 0 && nk_button_label(ctx, "Remove"))
137 {
138 CArrayDelete(&eData->m->u.Static.Exits, *eData->exitIdx);
139 *eData->exitIdx =
140 MIN(*eData->exitIdx, (int)eData->m->u.Static.Exits.size - 1);
141 eData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
142 }
143 }
144 static void DrawPropsSidebar(
145 struct nk_context *ctx, ExitBrushData *eData, Exit *selectedExit)
146 {
147 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
148
149 char buf[256];
150
151 // Location
152 sprintf(
153 buf, "Location: %d, %d", selectedExit->R.Pos.x, selectedExit->R.Pos.y);
154 nk_label(ctx, buf, NK_TEXT_LEFT);
155 if (nk_widget_is_hovered(ctx))
156 {
157 nk_tooltip(ctx, "Click and drag location in main editor");
158 }
159
160 // Size
161 sprintf(
162 buf, "Size: %d x %d", selectedExit->R.Size.x, selectedExit->R.Size.y);
163 nk_label(ctx, buf, NK_TEXT_LEFT);
164
165 // Mission
166 // Note: show 1-indexed mission
167 int mission = selectedExit->Mission;
168 mission++;
169 nk_property_int(
170 ctx, "Mission", 1, &mission, (int)eData->co->Setting.Missions.size + 1,
171 1, 1.f);
172 selectedExit->Mission = mission - 1;
173 if (nk_widget_is_hovered(ctx))
174 {
175 sprintf(buf, "Current mission: %d", eData->co->MissionIndex + 1);
176 nk_tooltip(ctx, buf);
177 }
178 if (mission != selectedExit->Mission)
179 {
180 eData->result |= EDITOR_RESULT_CHANGED;
181 }
182
183 // Hidden
184 if (DrawCheckbox(
185 ctx, "Hidden", "Whether this exit shows up in compass and automap",
186 &selectedExit->Hidden))
187 {
188 eData->result |= EDITOR_RESULT_CHANGED;
189 }
190 }
191
192 static void DrawExit(ExitBrushData *eData, Exit *e, const int idx)
193 {
194 char buf[256];
195 sprintf(
196 buf, "%d: mission %d (%d,%d %dx%d)", idx, e->Mission + 1, e->R.Pos.x,
197 e->R.Pos.y, e->R.Size.x, e->R.Size.y);
198 const int selected = *eData->exitIdx == idx;
199 if (nk_select_label(
200 eData->ctx, buf, NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED,
201 selected))
202 {
203 *eData->exitIdx = idx;
204 }
205 }