Added auto-dismiss behavior to DialogOverlay, and some additional cleanup.

This commit is contained in:
Casey Langen 2016-09-02 06:31:06 -07:00
parent 55b2f262bf
commit e0f232fcad
6 changed files with 166 additions and 12 deletions

View File

@ -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;
}

View File

@ -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<DialogOverlay>
public OverlayBase
#if (__clang_major__ == 7 && __clang_minor__ == 3)
, public std::enable_shared_from_this<DialogOverlay>
#endif
{
public:
using ButtonCallback = std::function<void(std::string key)>;
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<std::string> messageLines;
std::shared_ptr<ShortcutsWindow> shortcuts;
int width, height;
bool autoDismiss;
std::map<std::string, ButtonCallback> buttons;
};

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -41,9 +41,9 @@ using namespace cursespp;
static ILayoutPtr none;
std::shared_ptr<DialogOverlay> temp;
Overlays::Overlays() {
std::shared_ptr<DialogOverlay> 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<IOverlay*>(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);
}
}

View File

@ -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<ILayoutPtr> stack;