From 8870fa6dce6b0581876fb4066121ff88f61c4edf Mon Sep 17 00:00:00 2001 From: Cong Date: Sat, 22 Feb 2020 20:45:09 +1100 Subject: [PATCH] Close button for tile brush window #475 Also fix adding tile type doesn't show in editor --- src/cdogsed/char_editor.c | 56 ++++++++++++++++++---------------- src/cdogsed/editor_ui_static.c | 2 ++ src/cdogsed/nk_window.c | 18 ++++++++--- src/cdogsed/nk_window.h | 46 ++++++++++++++-------------- src/cdogsed/tile_brush.c | 10 ++++-- 5 files changed, 75 insertions(+), 57 deletions(-) diff --git a/src/cdogsed/char_editor.c b/src/cdogsed/char_editor.c index 64576cb4b..d0c5088cb 100644 --- a/src/cdogsed/char_editor.c +++ b/src/cdogsed/char_editor.c @@ -1,29 +1,29 @@ /* - C-Dogs SDL - A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2017-2019 Cong Xu - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. + C-Dogs SDL + A port of the legendary (and fun) action/arcade cdogs. + Copyright (c) 2017-2019 Cong Xu + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ #include "char_editor.h" @@ -60,7 +60,7 @@ static const char *IndexGunName(const int i); static int NumGuns(void); static int GunIndex(const WeaponClass *wc); static void AddCharacterTextures(EditorContext *ec); -static void Draw(SDL_Window *win, struct nk_context *ctx, void *data); +static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data); void CharEditor( GraphicsDevice *g, CampaignSetting *setting, EventHandlers *handlers, bool *fileChanged) @@ -242,7 +242,7 @@ static void DrawFlag( static void DrawCharacter( struct nk_context *ctx, Character *c, GLuint *texids, const struct vec2i pos, const Animation *anim, const direction_e d); -static void Draw(SDL_Window *win, struct nk_context *ctx, void *data) +static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data) { EditorContext *ec = data; // Stretch char store with window @@ -458,6 +458,8 @@ static void Draw(SDL_Window *win, struct nk_context *ctx, void *data) AnimationUpdate(&ec->anim, 1); AnimationUpdate(&ec->animSelection, 1); + + return true; } static void AddCharacter(EditorContext *ec, const int cloneIdx) diff --git a/src/cdogsed/editor_ui_static.c b/src/cdogsed/editor_ui_static.c index 7cc60787d..3a667f8e1 100644 --- a/src/cdogsed/editor_ui_static.c +++ b/src/cdogsed/editor_ui_static.c @@ -382,6 +382,7 @@ UIObject *CreateStaticMapObjs( o2->ChangeFunc = BrushChangeMainType; o2->OnFocusFunc = ActivateBrush; o2->OnUnfocusFunc = DeactivateBrush; + o2->ReloadData = true; // may add new tile type CSTRDUP(o2->Tooltip, "Left click to paint the map with this tile type"); o2->Pos = pos; UIObjectAddChild(c, o2); @@ -392,6 +393,7 @@ UIObject *CreateStaticMapObjs( o2->ChangeFunc = BrushChangeSecondaryType; o2->OnFocusFunc = ActivateBrush; o2->OnUnfocusFunc = DeactivateBrush; + o2->ReloadData = true; // may add new tile type CSTRDUP(o2->Tooltip, "Right click to paint the map with this tile type"); o2->Pos = pos; UIObjectAddChild(c, o2); diff --git a/src/cdogsed/nk_window.c b/src/cdogsed/nk_window.c index 6e37f9a19..43793d091 100644 --- a/src/cdogsed/nk_window.c +++ b/src/cdogsed/nk_window.c @@ -1,7 +1,7 @@ /* C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2019 Cong Xu + Copyright (c) 2019-2020 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -92,7 +92,7 @@ void NKWindowInit(NKWindowConfig *cfg) static bool HandleEvents(EventHandlers *handler, const Uint32 ticks); -static void Draw(const NKWindowConfig cfg); +static bool Draw(const NKWindowConfig cfg); void NKWindow(NKWindowConfig cfg) { CASSERT(cfg.win, "Error: did not initialise window"); @@ -116,7 +116,10 @@ void NKWindow(NKWindowConfig cfg) { goto bail; } - Draw(cfg); + if (!Draw(cfg)) + { + goto bail; + } nk_input_end(cfg.ctx); ticksElapsed = 0; @@ -140,9 +143,12 @@ static bool HandleEvents(EventHandlers *handler, const Uint32 ticks) handler->HasQuit = false; return run; } -static void Draw(const NKWindowConfig cfg) +static bool Draw(const NKWindowConfig cfg) { - cfg.Draw(cfg.win, cfg.ctx, cfg.DrawData); + if (!cfg.Draw(cfg.win, cfg.ctx, cfg.DrawData)) + { + return false; + } int winWidth, winHeight; SDL_GetWindowSize(cfg.win, &winWidth, &winHeight); @@ -154,6 +160,8 @@ static void Draw(const NKWindowConfig cfg) nk_sdl_render(NK_ANTI_ALIASING_ON); SDL_GL_SwapWindow(cfg.win); + + return true; } static void BeforeDrawTex(const GLuint texid); diff --git a/src/cdogsed/nk_window.h b/src/cdogsed/nk_window.h index c22961955..f354f2770 100644 --- a/src/cdogsed/nk_window.h +++ b/src/cdogsed/nk_window.h @@ -1,29 +1,29 @@ /* - C-Dogs SDL - A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2019, Cong Xu - All rights reserved. + C-Dogs SDL + A port of the legendary (and fun) action/arcade cdogs. + Copyright (c) 2019-2020 Cong Xu + All rights reserved. - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. + Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ #pragma once @@ -68,7 +68,7 @@ typedef struct color_t BG; SDL_Surface *Icon; EventHandlers *Handlers; - void (*Draw)(SDL_Window *, struct nk_context *, void *); + bool (*Draw)(SDL_Window *, struct nk_context *, void *); void *DrawData; } NKWindowConfig; // Note: need to init before initialising textures diff --git a/src/cdogsed/tile_brush.c b/src/cdogsed/tile_brush.c index a6a75d9fb..e1bdc4595 100644 --- a/src/cdogsed/tile_brush.c +++ b/src/cdogsed/tile_brush.c @@ -48,7 +48,7 @@ typedef struct static void ResetTexIds(TileBrushData *data); -static void Draw(SDL_Window *win, struct nk_context *ctx, void *data); +static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data); void TileBrush( const PicManager *pm, EventHandlers *handlers, CampaignOptions *co, int *brushIdx) @@ -82,13 +82,18 @@ void TileBrush( CArrayTerminate(&data.texIdsTileClasses); } static int DrawTileType(any_t data, any_t key); -static void Draw(SDL_Window *win, struct nk_context *ctx, void *data) +static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data) { UNUSED(win); + bool result = true; TileBrushData *tbData = data; if (nk_begin(ctx, "", nk_rect(0, 0, WIDTH, HEIGHT), 0)) { nk_layout_row_dynamic(ctx, ROW_HEIGHT, 5); + if (nk_button_label(ctx, "Close")) + { + result = false; + } if (nk_button_label(ctx, "Add")) { TileClass tc; @@ -135,6 +140,7 @@ static void Draw(SDL_Window *win, struct nk_context *ctx, void *data) } } nk_end(ctx); + return result; } static void DrawTileClass( struct nk_context *ctx, const PicManager *pm, const TileClass *tc,