Skip to content

Commit

Permalink
Dialog buttons #751
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Nov 8, 2022
1 parent 7e9603f commit 5cfe17c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 49 deletions.
3 changes: 1 addition & 2 deletions src/cdogsed/char_editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,8 @@ static void DrawFlag(
struct nk_context *ctx, EditorContext *ec, const char *label,
const int flag, const char *tooltip)
{
struct nk_rect bounds = nk_widget_bounds(ctx);
nk_checkbox_flags_label(ctx, label, &ec->Char->flags, flag);
if (tooltip && nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (tooltip && nk_widget_is_hovered(ctx))
{
nk_tooltip(ctx, tooltip);
}
Expand Down
6 changes: 2 additions & 4 deletions src/cdogsed/exit_brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,10 @@ static void DrawPropsSidebar(
char buf[256];

// Location
struct nk_rect bounds = nk_widget_bounds(ctx);
sprintf(
buf, "Location: %d, %d", selectedExit->R.Pos.x, selectedExit->R.Pos.y);
nk_label(ctx, buf, NK_TEXT_LEFT);
if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (nk_widget_is_hovered(ctx))
{
nk_tooltip(ctx, "Click and drag location in main editor");
}
Expand All @@ -164,15 +163,14 @@ static void DrawPropsSidebar(
nk_label(ctx, buf, NK_TEXT_LEFT);

// Mission
bounds = nk_widget_bounds(ctx);
// Note: show 1-indexed mission
int mission = selectedExit->Mission;
mission++;
nk_property_int(
ctx, "Mission", 1, &mission, (int)eData->co->Setting.Missions.size + 1,
1, 1.f);
selectedExit->Mission = mission - 1;
if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (nk_widget_is_hovered(ctx))
{
sprintf(buf, "Current mission: %d", eData->co->MissionIndex + 1);
nk_tooltip(ctx, buf);
Expand Down
31 changes: 20 additions & 11 deletions src/cdogsed/file_dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
char buf[CDOGS_PATH_MAX];

bool done = false;
float y = 0;
if (nk_begin(
ctx, "Dir", nk_rect(0, 0, WIDTH, ROW_HEIGHT),
ctx, "Dir", nk_rect(0, y, WIDTH, ROW_HEIGHT),
NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
{
const float colRatios[] = {0.1f, 0.9f};
Expand All @@ -172,25 +173,25 @@ static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
}
nk_end(ctx);
}
y += ROW_HEIGHT;

if (nk_begin(
ctx, "Picker", nk_rect(0, ROW_HEIGHT, WIDTH, HEIGHT - ROW_HEIGHT),
ctx, "Picker", nk_rect(0, y, WIDTH, HEIGHT - y - 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))
if (nk_widget_is_mouse_clicked(
ctx, NK_BUTTON_LEFT))
{
done = OnSelect(win, fData, filename);
}
else if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
else if (nk_widget_is_hovered(ctx))
{
fData->selected = _ca_index;
}
Expand All @@ -213,21 +214,29 @@ static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
const char *filename = CArrayGet(&fData->files, fData->selected);
done = OnSelect(win, fData, filename);
}
/*

nk_end(ctx);
}
y = HEIGHT - ROW_HEIGHT;

if (nk_begin(
ctx, "Controls", nk_rect(0, y, WIDTH, ROW_HEIGHT),
NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
{
nk_layout_row_dynamic(ctx, ROW_HEIGHT, 2);
if (nk_button_label(ctx, "OK"))
if (nk_button_label(ctx, "OK") && fData->selected >= 0)
{
fData->result = true;
done = true;
const char *filename = CArrayGet(&fData->files, fData->selected);
done = OnSelect(win, fData, filename);
}
if (nk_button_label(ctx, "Cancel"))
{
fData->result = false;
done = true;
}
*/
nk_end(ctx);
}

return !done;
}
static bool OnSelect(SDL_Window *win, FileData *fData, const char *filename)
Expand Down
12 changes: 5 additions & 7 deletions src/cdogsed/nk_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ void NKWindow(NKWindowConfig cfg)
MouseSetCursor(&cfg.Handlers->mouse, SDL_SYSTEM_CURSOR_ARROW);
if (SDL_SetWindowModalFor(cfg.win, gGraphicsDevice.gameWindow.window) != 0)
{
LOG(LM_EDIT, LL_ERROR, "Failed to set modal parent: %s", SDL_GetError());
LOG(LM_EDIT, LL_ERROR, "Failed to set modal parent: %s",
SDL_GetError());
}
Uint32 ticksNow = SDL_GetTicks();
Uint32 ticksElapsed = 0;
Expand Down Expand Up @@ -286,11 +287,10 @@ bool DrawCheckbox(
struct nk_context *ctx, const char *label, const char *tooltip,
bool *value)
{
struct nk_rect bounds = nk_widget_bounds(ctx);
int iValue = (int)*value;
const bool changed = nk_checkbox_label(ctx, label, &iValue);
*value = (bool)iValue;
if (tooltip && nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (tooltip && nk_widget_is_hovered(ctx))
{
nk_tooltip(ctx, tooltip);
}
Expand All @@ -301,10 +301,9 @@ bool DrawNumberSlider(
struct nk_context *ctx, const char *label, const char *tooltip,
const int min, const int max, const int step, int *value)
{
struct nk_rect bounds = nk_widget_bounds(ctx);
const int origValue = *value;
nk_property_int(ctx, label, min, value, max, step, 1.0f);
if (tooltip && nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (tooltip && nk_widget_is_hovered(ctx))
{
nk_tooltip(ctx, tooltip);
}
Expand Down Expand Up @@ -342,9 +341,8 @@ void DrawTextbox(
struct nk_context *ctx, char *value, const int len, const char *tooltip,
const nk_flags flags)
{
struct nk_rect bounds = nk_widget_bounds(ctx);
nk_edit_string_zero_terminated(ctx, flags, value, len, nk_filter_default);
if (tooltip && nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (tooltip && nk_widget_is_hovered(ctx))
{
nk_tooltip(ctx, tooltip);
}
Expand Down
46 changes: 25 additions & 21 deletions src/cdogsed/tile_brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ static const char *IndexBulletClassName(const int i)
}
else
{
b = CArrayGet(&gBulletClasses.CustomClasses, i - gBulletClasses.Classes.size);
b = CArrayGet(
&gBulletClasses.CustomClasses, i - gBulletClasses.Classes.size);
}
return b->Name;
}
Expand Down Expand Up @@ -110,8 +111,7 @@ static const TileClass *GetOrAddTileClass(
const color_t mask, const color_t maskAlt);
static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
EditorResult TileBrush(
PicManager *pm, EventHandlers *handlers, Campaign *co,
int *brushIdx)
PicManager *pm, EventHandlers *handlers, Campaign *co, int *brushIdx)
{
NKWindowConfig cfg;
memset(&cfg, 0, sizeof cfg);
Expand Down Expand Up @@ -145,26 +145,29 @@ EditorResult TileBrush(
// TODO: tile previews show wrong icons; possibly due to clashes between
// SDL2 textures and OGL textures. Load SDL2 textures first and in a second
// run, load OGL textures
data.bullets = GetClassNames(BulletClassesCount(&gBulletClasses), IndexBulletClassName);
data.bullets = GetClassNames(
BulletClassesCount(&gBulletClasses), IndexBulletClassName);
TexArrayInit(&data.texIdsFloorStyles, pm->tileStyleNames.size);
CA_FOREACH(const GLuint, texid, data.texIdsFloorStyles)
const char *style = *(char **)CArrayGet(&pm->tileStyleNames, _ca_index);
const TileClass *styleClass = GetOrAddTileClass(
gMap.TileClasses, &gTileFloor, pm, style, colorBattleshipGrey, colorOfficeGreen);
gMap.TileClasses, &gTileFloor, pm, style, colorBattleshipGrey,
colorOfficeGreen);
LoadTexFromPic(*texid, styleClass->Pic);
CA_FOREACH_END()
TexArrayInit(&data.texIdsWallStyles, pm->wallStyleNames.size);
CA_FOREACH(const GLuint, texid, data.texIdsWallStyles)
const char *style = *(char **)CArrayGet(&pm->wallStyleNames, _ca_index);
const TileClass *styleClass = GetOrAddTileClass(
gMap.TileClasses, &gTileWall, pm, style, colorGravel, colorOfficeGreen);
gMap.TileClasses, &gTileWall, pm, style, colorGravel,
colorOfficeGreen);
LoadTexFromPic(*texid, styleClass->Pic);
CA_FOREACH_END()
TexArrayInit(&data.texIdsDoorStyles, pm->doorStyleNames.size);
CA_FOREACH(const GLuint, texid, data.texIdsDoorStyles)
const char *style = *(char **)CArrayGet(&pm->doorStyleNames, _ca_index);
const TileClass *styleClass =
GetOrAddTileClass(gMap.TileClasses, &gTileDoor, pm, style, colorWhite, colorOfficeGreen);
const TileClass *styleClass = GetOrAddTileClass(
gMap.TileClasses, &gTileDoor, pm, style, colorWhite, colorOfficeGreen);
LoadTexFromPic(*texid, styleClass->Pic);
CA_FOREACH_END()

Expand Down Expand Up @@ -221,8 +224,9 @@ static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
}

if (nk_begin(
ctx, "Tiles", nk_rect(SIDE_WIDTH, OPS_HEIGHT, MAIN_WIDTH, HEIGHT - OPS_HEIGHT),
NK_WINDOW_BORDER))
ctx, "Tiles",
nk_rect(SIDE_WIDTH, OPS_HEIGHT, MAIN_WIDTH, HEIGHT - OPS_HEIGHT),
NK_WINDOW_BORDER))
{
nk_layout_row_dynamic(ctx, 40 * PIC_SCALE, MAIN_WIDTH / 120);
int tilesDrawn = 0;
Expand Down Expand Up @@ -312,8 +316,8 @@ static void DrawTilePropsSidebar(
{
bool hasDamage = StrBulletClass(selectedTC->DamageBullet) != NULL;
if (DrawCheckbox(
ctx, "Damaging", "Whether actors take damage on this tile",
&hasDamage))
ctx, "Damaging", "Whether actors take damage on this tile",
&hasDamage))
{
CFREE(selectedTC->DamageBullet);
if (hasDamage)
Expand Down Expand Up @@ -449,20 +453,20 @@ static void DrawTileStyleSelect(
static void DrawTileBulletSelect(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
{
struct nk_rect bounds = nk_widget_bounds(ctx);
nk_label(ctx, "Damage Bullet:", NK_TEXT_LEFT);
const int selectedIndex = BulletClassIndex(StrBulletClass(selectedTC->DamageBullet));
const int selectedIndex =
BulletClassIndex(StrBulletClass(selectedTC->DamageBullet));
const int newBullet = nk_combo_separator(
ctx, tbData->bullets, '\0', selectedIndex, BulletClassesCount(&gBulletClasses),
ROW_HEIGHT, nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT));
ctx, tbData->bullets, '\0', selectedIndex,
BulletClassesCount(&gBulletClasses), ROW_HEIGHT,
nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT));
if (newBullet != selectedIndex)
{
CFREE(selectedTC->DamageBullet);
CSTRDUP(
selectedTC->DamageBullet, IndexBulletClassName(newBullet));
CSTRDUP(selectedTC->DamageBullet, IndexBulletClassName(newBullet));
tbData->result |= EDITOR_RESULT_CHANGED_AND_RELOAD;
}
if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
if (nk_widget_is_hovered(ctx))
{
nk_tooltip(ctx, "Bullet type to hit player with when stepped on");
}
Expand Down Expand Up @@ -576,8 +580,8 @@ static const TileClass *GetOrAddTileClass(
if (styleClass == &gTileNothing)
{
styleClass = TileClassesAdd(
c, pm, base, style, TileClassBaseStyleType(base->Type),
mask, maskAlt);
c, pm, base, style, TileClassBaseStyleType(base->Type), mask,
maskAlt);
}
return styleClass;
}
11 changes: 7 additions & 4 deletions src/nuklear/nuklear_sdl_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ NK_API int
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;
const Uint32 windowId = SDL_GetWindowID(sdl.win);

/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
Expand Down Expand Up @@ -313,10 +314,12 @@ nk_sdl_handle_event(SDL_Event *evt)
return 1;
} else if (evt->type == SDL_MOUSEMOTION) {
/* mouse motion */
if (ctx->input.mouse.grabbed) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
} else nk_input_motion(ctx, evt->motion.x, evt->motion.y);
if (windowId == evt->motion.windowID) {
if (ctx->input.mouse.grabbed) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
} else nk_input_motion(ctx, evt->motion.x, evt->motion.y);
}
return 1;
} else if (evt->type == SDL_TEXTINPUT) {
/* text input */
Expand Down

0 comments on commit 5cfe17c

Please sign in to comment.