Debian Codegraph @

src/cdogsed/destroy_objective_dialog.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 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 "destroy_objective_dialog.h"
29
30 #include "editor_ui_common.h"
31 #include "nk_window.h"
32
33 #define WIDTH 500
34 #define HEIGHT 500
35 #define SIDE_WIDTH 150
36 #define ROW_HEIGHT 25
37
38 typedef struct
39 {
40 struct nk_context *ctx;
41 const PicManager *pm;
42 Objective *o;
43 const MapObject *mo; // last selected
44 CArray texIdsMapObjects; // of GLuint
45 } DestroyObjectiveData;
46
47 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
48 EditorResult DestroyObjectiveDialog(
49 PicManager *pm, EventHandlers *handlers, Objective *o)
50 {
51 NKWindowConfig cfg;
52 memset(&cfg, 0, sizeof cfg);
53 cfg.Title = "Select Destroy Objectives";
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 cfg.WindowFlags = SDL_WINDOW_RESIZABLE;
60
61 NKWindowInit(&cfg);
62
63 DestroyObjectiveData data;
64 memset(&data, 0, sizeof data);
65 data.pm = pm;
66 data.o = o;
67 CArrayInitFill(
68 &data.texIdsMapObjects, sizeof(GLuint),
69 MapObjectsCount(&gMapObjects), NULL);
70 glGenTextures(
71 MapObjectsCount(&gMapObjects),
72 (GLuint *)data.texIdsMapObjects.data);
73 cfg.DrawData = &data;
74 data.ctx = cfg.ctx;
75
76 NKWindow(cfg);
77
78 glDeleteTextures(
79 (GLsizei)data.texIdsMapObjects.size,
80 (const GLuint *)data.texIdsMapObjects.data);
81 CArrayTerminate(&data.texIdsMapObjects);
82 return EDITOR_RESULT_CHANGE_TOOL;
83 }
84
85 static void DrawOpsRow(
86 struct nk_context *ctx, DestroyObjectiveData *dData, bool *result);
87 static void DrawMapObject(
88 DestroyObjectiveData *data, const MapObject *mo, const int idx);
89 static void MapObjectDrawPropsSidebar(
90 struct nk_context *ctx, const MapObject *mo, const float rh);
91 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
92 {
93 bool result = true;
94 DestroyObjectiveData *dData = data;
95 struct vec2i windowSize;
96 SDL_GetWindowSize(win, &windowSize.x, &windowSize.y);
97 const float mainWidth = (float)(windowSize.x - SIDE_WIDTH);
98
99 if (nk_begin(
100 ctx, "", nk_rect(0, 0, mainWidth, (float)windowSize.y),
101 NK_WINDOW_BORDER))
102 {
103 DrawOpsRow(ctx, dData, & result);
104
105 nk_layout_row_dynamic(ctx, 30 * PIC_SCALE, (int)(mainWidth / 90));
106 for (int i = 0; i < MapObjectsCount(&gMapObjects); i++)
107 {
108 const MapObject *mo = IndexMapObject(i);
109 DrawMapObject(dData, mo, i);
110 }
111 nk_end(ctx);
112 }
113
114 if (nk_begin(
115 ctx, "Properties",
116 nk_rect(mainWidth, 0, SIDE_WIDTH, (float)windowSize.y),
117 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
118 {
119 if (dData->mo != NULL)
120 {
121 MapObjectDrawPropsSidebar(ctx, dData->mo, ROW_HEIGHT);
122 }
123 nk_end(ctx);
124 }
125
126 return result;
127 }
128 static void DrawOpsRow(
129 struct nk_context *ctx, DestroyObjectiveData *dData, bool *result)
130 {
131 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 3);
132 if (nk_button_label(ctx, "Select All"))
133 {
134 CArrayClear(&dData->o->u.MapObjects);
135 for (int i = 0; i < MapObjectsCount(&gMapObjects); i++)
136 {
137 const MapObject *mo = IndexMapObject(i);
138 CArrayPushBack(&dData->o->u.MapObjects, &mo);
139 }
140 }
141 if (nk_button_label(ctx, "Select None"))
142 {
143 CArrayClear(&dData->o->u.MapObjects);
144 }
145 if (nk_button_label(ctx, "Done"))
146 {
147 *result = false;
148 }
149 }
150
151 static void DrawMapObject(
152 DestroyObjectiveData *data, const MapObject *mo, const int idx)
153 {
154 bool selected = false;
155 CA_FOREACH(const MapObject *, m, data->o->u.MapObjects)
156 if (*m == mo)
157 {
158 selected = true;
159 break;
160 }
161 CA_FOREACH_END()
162 if (nk_select_label(
163 data->ctx, mo->Name, NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED,
164 selected) != selected)
165 {
166 data->mo = mo;
167 if (selected)
168 {
169 // deselect
170 CA_FOREACH(const MapObject *, m, data->o->u.MapObjects)
171 if (*m == mo)
172 {
173 CArrayDelete(&data->o->u.MapObjects, _ca_index);
174 break;
175 }
176 CA_FOREACH_END()
177 }
178 else
179 {
180 // select (add)
181 CArrayPushBack(&data->o->u.MapObjects, &mo);
182 // Sort and remove duplicates
183 qsort(
184 data->o->u.MapObjects.data, data->o->u.MapObjects.size,
185 data->o->u.MapObjects.elemSize, CompareIntsAsc);
186 CArrayUnique(&data->o->u.MapObjects, IntsEqual);
187 }
188 }
189 const GLuint *texid = CArrayGet(&data->texIdsMapObjects, idx);
190 const Pic *pic = CPicGetPic(&mo->Pic, 0);
191 DrawPic(data->ctx, pic, *texid, svec2i(-40, 5), PIC_SCALE);
192 }
193
194 static void MapObjectDrawPropsSidebar(
195 struct nk_context *ctx, const MapObject *mo, const float rh)
196 {
197 nk_layout_row_dynamic(ctx, rh, 1);
198
199 char buf[256];
200
201 nk_label(ctx, mo->Name, NK_TEXT_LEFT);
202
203 sprintf(buf, "Health: %d", mo->Health);
204 nk_label(ctx, buf, NK_TEXT_LEFT);
205
206 char pbuf[256];
207 MapObjectGetPlacementFlagNames(mo, pbuf, "\n");
208 sprintf(buf, "Placement:\n%s", pbuf);
209 nk_label(ctx, buf, NK_TEXT_LEFT);
210
211 if (mo->DestroyGuns.size > 0)
212 {
213 char exBuf[256];
214 MapObjectGetExplosionGunNames(mo, exBuf, "\n");
215 sprintf(buf, "Explodes:\n%s", exBuf);
216 nk_label(ctx, buf, NK_TEXT_LEFT);
217 }
218 }