Debian Codegraph @

src/cdogsed/add_pickup_dialog.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2020-2022 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 "add_pickup_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 const PickupClass **pc;
43 CArray texIdsPickupClasses; // of GLuint
44 } AddPickupData;
45
46 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
47 EditorResult AddPickupDialog(
48 PicManager *pm, EventHandlers *handlers, const PickupClass **pc)
49 {
50 NKWindowConfig cfg;
51 memset(&cfg, 0, sizeof cfg);
52 cfg.Title = "Add Pickup";
53 cfg.Size = svec2i(WIDTH, HEIGHT);
54 color_t bg = {41, 26, 26, 255};
55 cfg.BG = bg;
56 cfg.Handlers = handlers;
57 cfg.Draw = Draw;
58 cfg.WindowFlags = SDL_WINDOW_RESIZABLE;
59
60 NKWindowInit(&cfg);
61
62 AddPickupData data;
63 memset(&data, 0, sizeof data);
64 data.pm = pm;
65 data.pc = pc;
66 CArrayInitFill(
67 &data.texIdsPickupClasses, sizeof(GLuint),
68 PickupClassesCount(&gPickupClasses), NULL);
69 glGenTextures(
70 PickupClassesCount(&gPickupClasses),
71 (GLuint *)data.texIdsPickupClasses.data);
72 cfg.DrawData = &data;
73 data.ctx = cfg.ctx;
74
75 NKWindow(cfg);
76
77 glDeleteTextures(
78 (GLsizei)data.texIdsPickupClasses.size,
79 (const GLuint *)data.texIdsPickupClasses.data);
80 CArrayTerminate(&data.texIdsPickupClasses);
81 return EDITOR_RESULT_CHANGE_TOOL;
82 }
83
84 static void DrawOpsRow(struct nk_context *ctx, bool *result);
85 static void DrawPickup(
86 AddPickupData *aData, const PickupClass *pc, const int idx);
87 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
88 {
89 bool result = true;
90 AddPickupData *aData = data;
91 struct vec2i windowSize;
92 SDL_GetWindowSize(win, &windowSize.x, &windowSize.y);
93 const float mainWidth = (float)(windowSize.x - SIDE_WIDTH);
94
95 if (nk_begin(
96 ctx, "", nk_rect(0, 0, mainWidth, (float)windowSize.y),
97 NK_WINDOW_BORDER))
98 {
99 DrawOpsRow(ctx, &result);
100
101 nk_layout_row_dynamic(ctx, 30 * PIC_SCALE, (int)(mainWidth / 90));
102 for (int i = 0; i < PickupClassesCount(&gPickupClasses); i++)
103 {
104 const PickupClass *pc = PickupClassGetById(&gPickupClasses, i);
105 DrawPickup(aData, pc, i);
106 }
107 nk_end(ctx);
108 }
109
110 if (nk_begin(
111 ctx, "Properties",
112 nk_rect(mainWidth, 0, SIDE_WIDTH, (float)windowSize.y),
113 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
114 {
115 if (*aData->pc != NULL)
116 {
117 PickupClassDrawPropsSidebar(ctx, *aData->pc, ROW_HEIGHT);
118 }
119 nk_end(ctx);
120 }
121
122 return result;
123 }
124 static void DrawOpsRow(struct nk_context *ctx, bool *result)
125 {
126 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
127 if (nk_button_label(ctx, "Done"))
128 {
129 *result = false;
130 }
131 }
132
133 static void DrawPickup(
134 AddPickupData *aData, const PickupClass *pc, const int idx)
135 {
136 const bool selected = *aData->pc == pc;
137 if (nk_select_label(
138 aData->ctx, pc->Name,
139 NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED, selected))
140 {
141 *aData->pc = pc;
142 }
143 const GLuint *texid = CArrayGet(&aData->texIdsPickupClasses, idx);
144 const Pic *pic = CPicGetPic(&pc->Pic, 0);
145 DrawPic(aData->ctx, pic, *texid, svec2i(-40, 5), PIC_SCALE);
146 }