| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "file_dialog.h" |
| 29 |
|
| 30 |
#include <tinydir/tinydir.h> |
| 31 |
|
| 32 |
#include <cdogs/log.h> |
| 33 |
|
| 34 |
#include "nk_window.h" |
| 35 |
|
| 36 |
#define WIDTH 400 |
| 37 |
#define HEIGHT 400 |
| 38 |
#define ROW_HEIGHT_SMALL 18 |
| 39 |
#define ROW_HEIGHT 35 |
| 40 |
#define FILE_LIST_HEIGHT (HEIGHT - ROW_HEIGHT_SMALL - 2 * ROW_HEIGHT) |
| 41 |
|
| 42 |
typedef struct |
| 43 |
{ |
| 44 |
struct nk_context *ctx; |
| 45 |
CArray files; |
| 46 |
int selected; |
| 47 |
char pathOrig[CDOGS_PATH_MAX]; |
| 48 |
char filename[CDOGS_FILENAME_MAX]; |
| 49 |
char dir[CDOGS_PATH_MAX]; |
| 50 |
bool mustExist; |
| 51 |
bool result; |
| 52 |
} FileData; |
| 53 |
|
| 54 |
static void OpenDir(SDL_Window *win, FileData *data, const char *path); |
| 55 |
static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data); |
| 56 |
|
| 57 |
bool TryFileDialog( |
| 58 |
char *out, EventHandlers *handlers, const char *dir, const char *filename, |
| 59 |
const char *title, const bool mustExist) |
| 60 |
{ |
| 61 |
NKWindowConfig cfg; |
| 62 |
memset(&cfg, 0, sizeof cfg); |
| 63 |
cfg.Title = title; |
| 64 |
cfg.Size = svec2i(WIDTH, HEIGHT); |
| 65 |
color_t bg = {41, 26, 26, 255}; |
| 66 |
cfg.BG = bg; |
| 67 |
cfg.Handlers = handlers; |
| 68 |
cfg.Draw = DrawDialog; |
| 69 |
|
| 70 |
NKWindowInit(&cfg); |
| 71 |
|
| 72 |
FileData data; |
| 73 |
memset(&data, 0, sizeof data); |
| 74 |
data.ctx = cfg.ctx; |
| 75 |
strcpy(data.filename, filename); |
| 76 |
data.mustExist = mustExist; |
| 77 |
OpenDir(NULL, &data, dir); |
| 78 |
cfg.DrawData = &data; |
| 79 |
char buf[CDOGS_PATH_MAX]; |
| 80 |
sprintf(buf, "%s/%s", dir, filename); |
| 81 |
RealPath(buf, data.pathOrig); |
| 82 |
|
| 83 |
NKWindow(cfg); |
| 84 |
|
| 85 |
if (data.result) |
| 86 |
{ |
| 87 |
sprintf(out, "%s/%s", data.dir, data.filename); |
| 88 |
} |
| 89 |
return data.result; |
| 90 |
} |
| 91 |
|
| 92 |
static void OpenDir(SDL_Window *win, FileData *data, const char *path) |
| 93 |
{ |
| 94 |
|
| 95 |
tinydir_dir dir; |
| 96 |
|
| 97 |
if (tinydir_open_sorted(&dir, path) == -1) |
| 98 |
{ |
| 99 |
LOG(LM_EDIT, LL_ERROR, "Cannot load dir %s", path); |
| 100 |
if (win) |
| 101 |
{ |
| 102 |
char msgbuf[CDOGS_PATH_MAX]; |
| 103 |
sprintf(msgbuf, "Cannot open %s", path); |
| 104 |
SDL_ShowSimpleMessageBox( |
| 105 |
SDL_MESSAGEBOX_ERROR, "Open Dir Error", msgbuf, win); |
| 106 |
} |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
RealPath(path, data->dir); |
| 111 |
|
| 112 |
CArrayTerminate(&data->files); |
| 113 |
CArrayInit(&data->files, CDOGS_FILENAME_MAX); |
| 114 |
char buf[CDOGS_FILENAME_MAX]; |
| 115 |
if (strcmp(path, "/") != 0) |
| 116 |
{ |
| 117 |
CArrayPushBack(&data->files, "../"); |
| 118 |
} |
| 119 |
for (int i = 0; i < (int)dir.n_files; i++) |
| 120 |
{ |
| 121 |
tinydir_file file; |
| 122 |
tinydir_readfile_n(&dir, &file, i); |
| 123 |
|
| 124 |
if (file.name[0] == '~' || file.name[0] == '.') |
| 125 |
{ |
| 126 |
continue; |
| 127 |
} |
| 128 |
if ((file.is_dir && Stricmp(file.extension, "cdogscpn") == 0) || |
| 129 |
(file.is_reg && Stricmp(file.extension, "cpn") == 0)) |
| 130 |
{ |
| 131 |
strcpy(buf, file.name); |
| 132 |
CArrayPushBack(&data->files, buf); |
| 133 |
} |
| 134 |
else if (file.is_dir) |
| 135 |
{ |
| 136 |
sprintf(buf, "%s/", file.name); |
| 137 |
CArrayPushBack(&data->files, buf); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
tinydir_close(&dir); |
| 142 |
} |
| 143 |
|
| 144 |
bool TryOpenDir( |
| 145 |
char *out, EventHandlers *handlers, const char *dir, const char *filename) |
| 146 |
{ |
| 147 |
return TryFileDialog(out, handlers, dir, filename, "Open File", true); |
| 148 |
} |
| 149 |
bool TrySaveFile( |
| 150 |
char *out, EventHandlers *handlers, const char *dir, const char *filename) |
| 151 |
{ |
| 152 |
return TryFileDialog(out, handlers, dir, filename, "Save File", false); |
| 153 |
} |
| 154 |
|
| 155 |
static void OnSelectChange(FileData *fData, const int selected); |
| 156 |
static bool OnSelect(SDL_Window *win, FileData *fData); |
| 157 |
static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data) |
| 158 |
{ |
| 159 |
FileData *fData = data; |
| 160 |
char buf[CDOGS_PATH_MAX]; |
| 161 |
|
| 162 |
bool done = false; |
| 163 |
float y = 0; |
| 164 |
if (nk_begin( |
| 165 |
ctx, "Dir", nk_rect(0, y, WIDTH, ROW_HEIGHT_SMALL), |
| 166 |
NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR)) |
| 167 |
{ |
| 168 |
const float colRatios[] = {0.1f, 0.9f}; |
| 169 |
nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios); |
| 170 |
nk_label(ctx, "Dir:", NK_TEXT_ALIGN_LEFT); |
| 171 |
if (strlen(fData->dir) > 44) |
| 172 |
{ |
| 173 |
sprintf(buf, "... %s", strrchr(fData->dir, '/')); |
| 174 |
nk_label(ctx, buf, NK_TEXT_ALIGN_LEFT); |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
nk_label(ctx, fData->dir, NK_TEXT_ALIGN_LEFT); |
| 179 |
} |
| 180 |
nk_end(ctx); |
| 181 |
} |
| 182 |
y += ROW_HEIGHT_SMALL; |
| 183 |
|
| 184 |
if (nk_begin( |
| 185 |
ctx, "Picker", nk_rect(0, y, WIDTH, FILE_LIST_HEIGHT), |
| 186 |
NK_WINDOW_BORDER)) |
| 187 |
{ |
| 188 |
nk_layout_row_dynamic(ctx, ROW_HEIGHT_SMALL, 1); |
| 189 |
|
| 190 |
CA_FOREACH(const char, filename, fData->files) |
| 191 |
const bool selected = fData->selected == _ca_index; |
| 192 |
nk_select_label( |
| 193 |
fData->ctx, filename, NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_LEFT, |
| 194 |
selected); |
| 195 |
if (nk_widget_is_mouse_clicked(ctx, NK_BUTTON_LEFT)) |
| 196 |
{ |
| 197 |
OnSelectChange(fData, _ca_index); |
| 198 |
done = OnSelect(win, fData); |
| 199 |
} |
| 200 |
else if (nk_widget_is_hovered(ctx)) |
| 201 |
{ |
| 202 |
OnSelectChange(fData, _ca_index); |
| 203 |
} |
| 204 |
CA_FOREACH_END() |
| 205 |
|
| 206 |
if (nk_input_is_key_pressed(&ctx->input, NK_KEY_DOWN)) |
| 207 |
{ |
| 208 |
OnSelectChange( |
| 209 |
fData, |
| 210 |
CLAMP_OPPOSITE( |
| 211 |
fData->selected + 1, 0, (int)fData->files.size - 1)); |
| 212 |
} |
| 213 |
else if (nk_input_is_key_pressed(&ctx->input, NK_KEY_UP)) |
| 214 |
{ |
| 215 |
OnSelectChange( |
| 216 |
fData, |
| 217 |
CLAMP_OPPOSITE( |
| 218 |
fData->selected - 1, 0, (int)fData->files.size - 1)); |
| 219 |
} |
| 220 |
else if ( |
| 221 |
nk_input_is_key_pressed(&ctx->input, NK_KEY_ENTER) && |
| 222 |
fData->selected >= 0) |
| 223 |
{ |
| 224 |
done = OnSelect(win, fData); |
| 225 |
} |
| 226 |
|
| 227 |
nk_end(ctx); |
| 228 |
} |
| 229 |
y += FILE_LIST_HEIGHT; |
| 230 |
|
| 231 |
if (nk_begin( |
| 232 |
ctx, "Filename", nk_rect(0, y, WIDTH, ROW_HEIGHT), |
| 233 |
NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR)) |
| 234 |
{ |
| 235 |
nk_layout_row_dynamic(ctx, ROW_HEIGHT - 10, 1); |
| 236 |
DrawTextbox( |
| 237 |
ctx, fData->filename, CDOGS_FILENAME_MAX, NULL, NK_EDIT_FIELD); |
| 238 |
nk_end(ctx); |
| 239 |
} |
| 240 |
y += ROW_HEIGHT; |
| 241 |
|
| 242 |
if (nk_begin( |
| 243 |
ctx, "Controls", nk_rect(0, y, WIDTH, ROW_HEIGHT), |
| 244 |
NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR)) |
| 245 |
{ |
| 246 |
nk_layout_row_dynamic(ctx, ROW_HEIGHT - 10, 2); |
| 247 |
if (nk_button_label(ctx, "OK") && fData->selected >= 0) |
| 248 |
{ |
| 249 |
done = OnSelect(win, fData); |
| 250 |
} |
| 251 |
if (nk_button_label(ctx, "Cancel")) |
| 252 |
{ |
| 253 |
fData->result = false; |
| 254 |
done = true; |
| 255 |
} |
| 256 |
nk_end(ctx); |
| 257 |
} |
| 258 |
|
| 259 |
return !done; |
| 260 |
} |
| 261 |
static void OnSelectChange(FileData *fData, const int selected) |
| 262 |
{ |
| 263 |
if (fData->selected == selected || selected < 0 || |
| 264 |
selected >= (int)fData->files.size) |
| 265 |
return; |
| 266 |
|
| 267 |
const char *filename = CArrayGet(&fData->files, selected); |
| 268 |
strcpy(fData->filename, filename); |
| 269 |
fData->selected = selected; |
| 270 |
} |
| 271 |
static bool OnSelect(SDL_Window *win, FileData *fData) |
| 272 |
{ |
| 273 |
char buf[CDOGS_PATH_MAX]; |
| 274 |
sprintf(buf, "%s/%s", fData->dir, fData->filename); |
| 275 |
char buf2[CDOGS_PATH_MAX]; |
| 276 |
RealPath(buf, buf2); |
| 277 |
if (fData->filename[strlen(fData->filename) - 1] == '/') |
| 278 |
{ |
| 279 |
fData->selected = 0; |
| 280 |
OpenDir(win, fData, buf2); |
| 281 |
fData->filename[0] = '\0'; |
| 282 |
return false; |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
if (strcmp(buf2, fData->pathOrig) != 0 && !fData->mustExist) |
| 287 |
{ |
| 288 |
|
| 289 |
const SDL_MessageBoxButtonData buttons[] = { |
| 290 |
{SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 1, "Cancel"}, |
| 291 |
{SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK"}, |
| 292 |
}; |
| 293 |
const SDL_MessageBoxData messageboxdata = { |
| 294 |
SDL_MESSAGEBOX_INFORMATION, |
| 295 |
win, |
| 296 |
"Overwrite File", |
| 297 |
"File already exists. Do you want to overwrite?", |
| 298 |
SDL_arraysize(buttons), |
| 299 |
buttons, |
| 300 |
NULL, |
| 301 |
}; |
| 302 |
int buttonid; |
| 303 |
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) |
| 304 |
{ |
| 305 |
LOG(LM_EDIT, LL_ERROR, "error displaying message box"); |
| 306 |
} |
| 307 |
if (buttonid != 0) |
| 308 |
{ |
| 309 |
return false; |
| 310 |
} |
| 311 |
} |
| 312 |
fData->result = true; |
| 313 |
return true; |
| 314 |
} |
| 315 |
} |