Skip to content

Commit

Permalink
Keyboard control file dialog #751
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Nov 8, 2022
1 parent d2c8c80 commit 7e9603f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/cdogs/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2017, 2019-2021 Cong Xu
Copyright (c) 2013-2017, 2019-2022 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -130,6 +130,7 @@ void PathGetBasenameWithoutExtension(char *buf, const char *path)
PathGetWithoutExtension(buf, basename);
}

static void TrimSlashes(char *s);
#ifdef _WIN32
#include "sys_config.h"
#define realpath(src, dst) _fullpath(dst, src, CDOGS_PATH_MAX)
Expand Down Expand Up @@ -240,11 +241,11 @@ void RealPath(const char *src, char *dest)
}
#endif
}
TrimSlashes(dest);
}

// Convert an absolute path to a relative path
// e.g. /a/path/from/here, /a/path/to -> ../to
static void TrimSlashes(char *s);
void RelPath(char *buf, const char *to, const char *from)
{
#ifdef _WIN32
Expand Down
85 changes: 58 additions & 27 deletions src/cdogsed/file_dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
typedef struct
{
struct nk_context *ctx;
CArray files; // of char[CDOGS_FILENAME_MAX]
CArray files; // of char[CDOGS_FILENAME_MAX]
int selected;
char filename[CDOGS_FILENAME_MAX];
char dir[CDOGS_PATH_MAX];
Expand All @@ -52,7 +52,9 @@ typedef struct
static void OpenDir(SDL_Window *win, FileData *data, const char *path);
static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data);

bool TryFileDialog(char *out, EventHandlers *handlers, const char *dir, const char *filename, const char *title, const bool mustExist)
bool TryFileDialog(
char *out, EventHandlers *handlers, const char *dir, const char *filename,
const char *title, const bool mustExist)
{
NKWindowConfig cfg;
memset(&cfg, 0, sizeof cfg);
Expand Down Expand Up @@ -86,20 +88,21 @@ static void OpenDir(SDL_Window *win, FileData *data, const char *path)
{
// Fill FileData with the file listings of a dir
tinydir_dir dir;

if (tinydir_open_sorted(&dir, path) == -1)
{
LOG(LM_EDIT, LL_ERROR, "Cannot load dir %s", path);
if (win)
{
char msgbuf[CDOGS_PATH_MAX];
sprintf(msgbuf, "Cannot open %s", path);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Open Dir Error", msgbuf, win);
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, "Open Dir Error", msgbuf, win);
}
return;
}

strcpy(data->dir, path);
RealPath(path, data->dir);

CArrayTerminate(&data->files);
CArrayInit(&data->files, CDOGS_FILENAME_MAX);
Expand Down Expand Up @@ -133,16 +136,18 @@ static void OpenDir(SDL_Window *win, FileData *data, const char *path)
tinydir_close(&dir);
}

bool TryOpenDir(char *out, EventHandlers *handlers, const char *dir, const char *filename)
bool TryOpenDir(
char *out, EventHandlers *handlers, const char *dir, const char *filename)
{
return TryFileDialog(out, handlers, dir, filename, "Open File", true);
}
bool TrySaveFile(char *out, EventHandlers *handlers, const char *dir, const char *filename)
bool TrySaveFile(
char *out, EventHandlers *handlers, const char *dir, const char *filename)
{
return TryFileDialog(out, handlers, dir, filename, "Save File", false);
}


static bool OnSelect(SDL_Window *win, FileData *fData, const char *filename);
static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
{
FileData *fData = data;
Expand All @@ -167,41 +172,47 @@ static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
}
nk_end(ctx);
}

if (nk_begin(
ctx, "Picker", nk_rect(0, ROW_HEIGHT, WIDTH, HEIGHT - ROW_HEIGHT),
NK_WINDOW_BORDER))
{
nk_layout_row_dynamic(ctx, ROW_HEIGHT_SMALL, 1);

CA_FOREACH(const char, filename, fData->files)
const bool selected = fData->selected == _ca_index;
struct nk_rect bounds = nk_widget_bounds(ctx);
nk_select_label(
fData->ctx, filename, NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_LEFT,
selected);
if (nk_input_is_mouse_click_in_rect(&ctx->input, NK_BUTTON_LEFT, bounds))
fData->ctx, filename, NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_LEFT,
selected);
if (nk_input_is_mouse_click_in_rect(
&ctx->input, NK_BUTTON_LEFT, bounds))
{
if (filename[strlen(filename)-1] == '/')
{
fData->selected = 0;
sprintf(buf, "%s/%s", fData->dir, filename);
char buf2[CDOGS_PATH_MAX];
RealPath(buf, buf2);
OpenDir(win, fData, buf2);
fData->filename[0] = '\0';
}
else
{
fData->result = true;
done = true;
strcpy(fData->filename, filename);
}
done = OnSelect(win, fData, filename);
}
else if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
{
fData->selected = _ca_index;
}
CA_FOREACH_END()

if (nk_input_is_key_pressed(&ctx->input, NK_KEY_DOWN))
{
fData->selected = CLAMP_OPPOSITE(
fData->selected + 1, 0, (int)fData->files.size - 1);
}
else if (nk_input_is_key_pressed(&ctx->input, NK_KEY_UP))
{
fData->selected = CLAMP_OPPOSITE(
fData->selected - 1, 0, (int)fData->files.size - 1);
}
else if (
nk_input_is_key_pressed(&ctx->input, NK_KEY_ENTER) &&
fData->selected >= 0)
{
const char *filename = CArrayGet(&fData->files, fData->selected);
done = OnSelect(win, fData, filename);
}
/*
nk_layout_row_dynamic(ctx, ROW_HEIGHT, 2);
if (nk_button_label(ctx, "OK"))
Expand All @@ -219,3 +230,23 @@ static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
}
return !done;
}
static bool OnSelect(SDL_Window *win, FileData *fData, const char *filename)
{
if (filename[strlen(filename) - 1] == '/')
{
char buf[CDOGS_PATH_MAX];
fData->selected = 0;
sprintf(buf, "%s/%s", fData->dir, filename);
char buf2[CDOGS_PATH_MAX];
RealPath(buf, buf2);
OpenDir(win, fData, buf2);
fData->filename[0] = '\0';
return false;
}
else
{
fData->result = true;
strcpy(fData->filename, filename);
return true;
}
}
6 changes: 4 additions & 2 deletions src/cdogsed/file_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@

#include <cdogs/events.h>

bool TryOpenDir(char *out, EventHandlers *handlers, const char *dir, const char *filename);
bool TrySaveFile(char *out, EventHandlers *handlers, const char *dir, const char *filename);
bool TryOpenDir(
char *out, EventHandlers *handlers, const char *dir, const char *filename);
bool TrySaveFile(
char *out, EventHandlers *handlers, const char *dir, const char *filename);

0 comments on commit 7e9603f

Please sign in to comment.