Debian Codegraph @

src/cdogsed/pickup_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 "pickup_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 PickupClass *pc; // last selected
44 CArray texIdsPickupClasses; // of GLuint
45 } PickupObjectiveData;
46
47 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
48 EditorResult PickupObjectiveDialog(
49 PicManager *pm, EventHandlers *handlers, Objective *o)
50 {
51 NKWindowConfig cfg;
52 memset(&cfg, 0, sizeof cfg);
53 cfg.Title = "Select Objective Pickups";
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 PickupObjectiveData data;
64 memset(&data, 0, sizeof data);
65 data.pm = pm;
66 data.o = o;
67 CArrayInitFill(
68 &data.texIdsPickupClasses, sizeof(GLuint),
69 PickupClassesCount(&gPickupClasses), NULL);
70 glGenTextures(
71 PickupClassesCount(&gPickupClasses),
72 (GLuint *)data.texIdsPickupClasses.data);
73 cfg.DrawData = &data;
74 data.ctx = cfg.ctx;
75
76 NKWindow(cfg);
77
78 glDeleteTextures(
79 (GLsizei)data.texIdsPickupClasses.size,
80 (const GLuint *)data.texIdsPickupClasses.data);
81 CArrayTerminate(&data.texIdsPickupClasses);
82 return EDITOR_RESULT_CHANGE_TOOL;
83 }
84
85 static void DrawOpsRow(
86 struct nk_context *ctx, PickupObjectiveData *pData, bool *result);
87 static void DrawPickup(
88 PickupObjectiveData *data, const PickupClass *pc, const int idx);
89 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
90 {
91 bool result = true;
92 PickupObjectiveData *pData = data;
93 struct vec2i windowSize;
94 SDL_GetWindowSize(win, &windowSize.x, &windowSize.y);
95 const float mainWidth = (float)(windowSize.x - SIDE_WIDTH);
96
97 if (nk_begin(
98 ctx, "", nk_rect(0, 0, mainWidth, (float)windowSize.y),
99 NK_WINDOW_BORDER))
100 {
101 DrawOpsRow(ctx, pData, & result);
102
103 nk_layout_row_dynamic(ctx, 30 * PIC_SCALE, (int)(mainWidth / 90));
104 for (int i = 0; i < PickupClassesCount(&gPickupClasses); i++)
105 {
106 const PickupClass *pc = PickupClassGetById(&gPickupClasses, i);
107 DrawPickup(pData, pc, i);
108 }
109 nk_end(ctx);
110 }
111
112 if (nk_begin(
113 ctx, "Properties",
114 nk_rect(mainWidth, 0, SIDE_WIDTH, (float)windowSize.y),
115 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
116 {
117 if (pData->pc != NULL)
118 {
119 PickupClassDrawPropsSidebar(ctx, pData->pc, ROW_HEIGHT);
120 }
121 nk_end(ctx);
122 }
123
124 return result;
125 }
126 static void DrawOpsRow(
127 struct nk_context *ctx, PickupObjectiveData *pData, bool *result)
128 {
129 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 3);
130 if (nk_button_label(ctx, "Select All"))
131 {
132 CArrayClear(&pData->o->u.Pickups);
133 for (int i = 0; i < PickupClassesCount(&gPickupClasses); i++)
134 {
135 const PickupClass *pc = PickupClassGetById(&gPickupClasses, i);
136 CArrayPushBack(&pData->o->u.Pickups, &pc);
137 }
138 }
139 if (nk_button_label(ctx, "Select None"))
140 {
141 CArrayClear(&pData->o->u.Pickups);
142 }
143 if (nk_button_label(ctx, "Done"))
144 {
145 *result = false;
146 }
147 }
148
149 static void DrawPickup(
150 PickupObjectiveData *data, const PickupClass *pc, const int idx)
151 {
152 bool selected = false;
153 CA_FOREACH(const PickupClass *, p, data->o->u.Pickups)
154 if (*p == pc)
155 {
156 selected = true;
157 break;
158 }
159 CA_FOREACH_END()
160 if (nk_select_label(
161 data->ctx, pc->Name, NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED,
162 selected) != selected)
163 {
164 data->pc = pc;
165 if (selected)
166 {
167 // deselect
168 CA_FOREACH(const PickupClass *, p, data->o->u.Pickups)
169 if (*p == pc)
170 {
171 CArrayDelete(&data->o->u.Pickups, _ca_index);
172 break;
173 }
174 CA_FOREACH_END()
175 }
176 else
177 {
178 // select (add)
179 CArrayPushBack(&data->o->u.Pickups, &pc);
180 // Sort and remove duplicates
181 qsort(
182 data->o->u.Pickups.data, data->o->u.Pickups.size,
183 data->o->u.Pickups.elemSize, CompareIntsAsc);
184 CArrayUnique(&data->o->u.Pickups, IntsEqual);
185 }
186 }
187 const GLuint *texid = CArrayGet(&data->texIdsPickupClasses, idx);
188 const Pic *pic = CPicGetPic(&pc->Pic, 0);
189 DrawPic(data->ctx, pic, *texid, svec2i(-40, 5), PIC_SCALE);
190 }