From e0f232fcad71895725e20d06548dbc48616d5514 Mon Sep 17 00:00:00 2001 From: Casey Langen Date: Fri, 2 Sep 2016 06:31:06 -0700 Subject: [PATCH] Added auto-dismiss behavior to DialogOverlay, and some additional cleanup. --- src/musikbox/cursespp/DialogOverlay.cpp | 17 +++++++ src/musikbox/cursespp/DialogOverlay.h | 12 +++-- src/musikbox/cursespp/IOverlay.h | 45 +++++++++++++++++++ src/musikbox/cursespp/OverlayBase.h | 60 +++++++++++++++++++++++++ src/musikbox/cursespp/Overlays.cpp | 38 +++++++++++++--- src/musikbox/cursespp/Overlays.h | 6 ++- 6 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 src/musikbox/cursespp/IOverlay.h create mode 100644 src/musikbox/cursespp/OverlayBase.h diff --git a/src/musikbox/cursespp/DialogOverlay.cpp b/src/musikbox/cursespp/DialogOverlay.cpp index c6bbb62a7..f319aa21d 100644 --- a/src/musikbox/cursespp/DialogOverlay.cpp +++ b/src/musikbox/cursespp/DialogOverlay.cpp @@ -48,11 +48,15 @@ DialogOverlay::DialogOverlay() { this->SetContentColor(CURSESPP_OVERLAY_BACKGROUND); this->width = this->height = 0; + this->autoDismiss = true; this->shortcuts.reset(new ShortcutsWindow()); this->AddWindow(this->shortcuts); } +DialogOverlay::~DialogOverlay() { +} + void DialogOverlay::Layout() { this->RecalculateSize(); @@ -90,6 +94,11 @@ DialogOverlay& DialogOverlay::SetMessage(const std::string& message) { return *this; } +DialogOverlay& DialogOverlay::SetAutoDismiss(bool dismiss) { + this->autoDismiss = dismiss; + return *this; +} + DialogOverlay& DialogOverlay::AddButton( const std::string& rawKey, const std::string& key, @@ -108,6 +117,14 @@ bool DialogOverlay::KeyPress(const std::string& key) { if (cb) { cb(key); + + if (this->autoDismiss) { + Overlays* overlays = this->GetOverlays(); + if (overlays) { + overlays->Remove(this); + } + } + return true; } diff --git a/src/musikbox/cursespp/DialogOverlay.h b/src/musikbox/cursespp/DialogOverlay.h index ad7f9610f..0350f44a5 100644 --- a/src/musikbox/cursespp/DialogOverlay.h +++ b/src/musikbox/cursespp/DialogOverlay.h @@ -34,7 +34,7 @@ #pragma once -#include "LayoutBase.h" +#include "OverlayBase.h" #include "TextLabel.h" #include "ShortcutsWindow.h" @@ -43,13 +43,16 @@ namespace cursespp { class DialogOverlay : - public LayoutBase, - public std::enable_shared_from_this + public OverlayBase +#if (__clang_major__ == 7 && __clang_minor__ == 3) + , public std::enable_shared_from_this +#endif { public: using ButtonCallback = std::function; DialogOverlay(); + virtual ~DialogOverlay(); DialogOverlay& SetTitle(const std::string& title); DialogOverlay& SetMessage(const std::string& message); @@ -60,6 +63,8 @@ namespace cursespp { const std::string& caption, ButtonCallback callback); + DialogOverlay& SetAutoDismiss(bool dismiss = true); + virtual void Layout(); virtual bool KeyPress(const std::string& key); @@ -75,6 +80,7 @@ namespace cursespp { std::vector messageLines; std::shared_ptr shortcuts; int width, height; + bool autoDismiss; std::map buttons; }; diff --git a/src/musikbox/cursespp/IOverlay.h b/src/musikbox/cursespp/IOverlay.h new file mode 100644 index 000000000..df97c3747 --- /dev/null +++ b/src/musikbox/cursespp/IOverlay.h @@ -0,0 +1,45 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2007-2016 musikcube team +// +// 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. +// +// * Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// 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 OWNER 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 + +namespace cursespp { + class Overlays; + + class IOverlay { + public: + virtual ~IOverlay() { } + virtual void SetOverlays(Overlays* overlays) = 0; + }; +} diff --git a/src/musikbox/cursespp/OverlayBase.h b/src/musikbox/cursespp/OverlayBase.h new file mode 100644 index 000000000..bd84c0a09 --- /dev/null +++ b/src/musikbox/cursespp/OverlayBase.h @@ -0,0 +1,60 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2007-2016 musikcube team +// +// 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. +// +// * Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// 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 OWNER 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 + +#include "IOverlay.h" +#include "LayoutBase.h" +#include "Overlays.h" + +namespace cursespp { + class OverlayBase : public LayoutBase, public IOverlay { + public: + virtual ~OverlayBase() { + this->overlays = nullptr; + } + + virtual void SetOverlays(Overlays* overlays) { + this->overlays = overlays; + } + + protected: + Overlays* GetOverlays() { + return this->overlays; + } + + private: + Overlays* overlays; + }; +} diff --git a/src/musikbox/cursespp/Overlays.cpp b/src/musikbox/cursespp/Overlays.cpp index 1a142c5d2..ff22d843b 100644 --- a/src/musikbox/cursespp/Overlays.cpp +++ b/src/musikbox/cursespp/Overlays.cpp @@ -41,9 +41,9 @@ using namespace cursespp; static ILayoutPtr none; -std::shared_ptr temp; - Overlays::Overlays() { + std::shared_ptr temp; + temp.reset(new DialogOverlay()); temp->SetTitle("musikbox") @@ -54,7 +54,7 @@ Overlays::Overlays() { "ENTER", "ok", [this](std::string kn) { - this->Remove(temp); + }); temp->AddButton( @@ -72,15 +72,39 @@ ILayoutPtr Overlays::Top() { return this->stack.size() ? this->stack[0] : none; } -void Overlays::Push(ILayoutPtr overlay) { - this->stack.insert(this->stack.begin(), overlay); +inline void setOverlays(ILayoutPtr layout, Overlays* instance) { + IOverlay* overlay = dynamic_cast(layout.get()); + if (overlay) { + overlay->SetOverlays(instance); + } } -void Overlays::Remove(ILayoutPtr overlay) { +void Overlays::Push(ILayoutPtr layout) { + setOverlays(layout, this); + this->stack.insert(this->stack.begin(), layout); +} + +void Overlays::Remove(ILayoutPtr layout) { auto it = std::find( - this->stack.begin(), this->stack.end(), overlay); + this->stack.begin(), + this->stack.end(), layout); if (it != this->stack.end()) { this->stack.erase(it); + setOverlays(*it, nullptr); + } +} + +void Overlays::Remove(ILayout* layout) { + auto it = std::find_if( + this->stack.begin(), + this->stack.end(), + [layout] (ILayoutPtr layoutPtr) { + return layoutPtr.get() == layout; + }); + + if (it != this->stack.end()) { + this->stack.erase(it); + setOverlays(*it, nullptr); } } \ No newline at end of file diff --git a/src/musikbox/cursespp/Overlays.h b/src/musikbox/cursespp/Overlays.h index ca3866101..3d9e597f8 100644 --- a/src/musikbox/cursespp/Overlays.h +++ b/src/musikbox/cursespp/Overlays.h @@ -40,9 +40,11 @@ namespace cursespp { class Overlays { public: Overlays(); + ILayoutPtr Top(); - void Push(ILayoutPtr overlay); - void Remove(ILayoutPtr overlay); + void Push(ILayoutPtr layout); + void Remove(ILayoutPtr layout); + void Remove(ILayout* layout); private: std::vector stack;