From 55b2f262bfe44d82181098c60721c57ab3c8f22f Mon Sep 17 00:00:00 2001 From: Casey Langen Date: Fri, 2 Sep 2016 00:34:04 -0700 Subject: [PATCH] - Added Overlays::Push, Overlays::Remove - Fixed dismissing of overlay and proper redrawing in App - New overlay frame style in Colors --- src/musikbox/cursespp/App.cpp | 9 ++-- src/musikbox/cursespp/Colors.cpp | 1 + src/musikbox/cursespp/Colors.h | 3 +- src/musikbox/cursespp/DialogOverlay.cpp | 65 +++++++++++++++++++++---- src/musikbox/cursespp/DialogOverlay.h | 18 ++++++- src/musikbox/cursespp/Overlays.cpp | 35 ++++++++++++- src/musikbox/cursespp/Overlays.h | 5 ++ 7 files changed, 118 insertions(+), 18 deletions(-) diff --git a/src/musikbox/cursespp/App.cpp b/src/musikbox/cursespp/App.cpp index 21ea85052..ab6d9762c 100755 --- a/src/musikbox/cursespp/App.cpp +++ b/src/musikbox/cursespp/App.cpp @@ -256,10 +256,11 @@ void App::CheckShowOverlay() { this->state.overlay = top; - if (top) { - top->Layout(); - top->Show(); - top->BringToTop(); + ILayoutPtr newTopLayout = this->state.ActiveLayout(); + if (newTopLayout) { + newTopLayout->Layout(); + newTopLayout->Show(); + newTopLayout->BringToTop(); } } } diff --git a/src/musikbox/cursespp/Colors.cpp b/src/musikbox/cursespp/Colors.cpp index bd7023b6b..bcd7977c7 100755 --- a/src/musikbox/cursespp/Colors.cpp +++ b/src/musikbox/cursespp/Colors.cpp @@ -138,5 +138,6 @@ void Colors::Init(bool disableCustomColors) { init_pair(CURSESPP_SHORTCUT_ROW_NORMAL, yellow, selected); init_pair(CURSESPP_SHORTCUT_ROW_FOCUSED, white, darkRed); + init_pair(CURSESPP_OVERLAY_FRAME, blue, selected); init_pair(CURSESPP_OVERLAY_BACKGROUND, white, selected); } diff --git a/src/musikbox/cursespp/Colors.h b/src/musikbox/cursespp/Colors.h index 32f343acf..370c4e007 100755 --- a/src/musikbox/cursespp/Colors.h +++ b/src/musikbox/cursespp/Colors.h @@ -61,7 +61,8 @@ #define CURSESPP_SHORTCUT_ROW_NORMAL 19 #define CURSESPP_SHORTCUT_ROW_FOCUSED 20 -#define CURSESPP_OVERLAY_BACKGROUND 21 +#define CURSESPP_OVERLAY_FRAME 21 +#define CURSESPP_OVERLAY_BACKGROUND 22 namespace cursespp { class Colors { diff --git a/src/musikbox/cursespp/DialogOverlay.cpp b/src/musikbox/cursespp/DialogOverlay.cpp index b629a97dd..c6bbb62a7 100644 --- a/src/musikbox/cursespp/DialogOverlay.cpp +++ b/src/musikbox/cursespp/DialogOverlay.cpp @@ -43,25 +43,40 @@ using namespace cursespp; #define VERTICAL_PADDING 2 DialogOverlay::DialogOverlay() { + this->SetFrameVisible(true); + this->SetFrameColor(CURSESPP_OVERLAY_FRAME); this->SetContentColor(CURSESPP_OVERLAY_BACKGROUND); + this->width = this->height = 0; + + this->shortcuts.reset(new ShortcutsWindow()); + this->AddWindow(this->shortcuts); } -void DialogOverlay::Layout() { +void DialogOverlay::Layout() { this->RecalculateSize(); - this->MoveAndResize( - HORIZONTAL_PADDING, - VERTICAL_PADDING, - this->width, - this->height); + if (this->width > 0 && this->height > 0) { + this->MoveAndResize( + HORIZONTAL_PADDING, + VERTICAL_PADDING, + this->width, + this->height + 2); - this->Redraw(); + this->shortcuts->MoveAndResize( + HORIZONTAL_PADDING + 1, + VERTICAL_PADDING + this->height, + this->GetContentWidth(), + 1); + + this->Redraw(); + } } DialogOverlay& DialogOverlay::SetTitle(const std::string& title) { this->title = title; this->RecalculateSize(); + this->Layout(); this->Repaint(); return *this; } @@ -70,10 +85,35 @@ DialogOverlay& DialogOverlay::SetMessage(const std::string& message) { this->message = message; this->width = 0; /* implicitly triggers a new BreakLines() */ this->RecalculateSize(); + this->Layout(); this->Repaint(); return *this; } +DialogOverlay& DialogOverlay::AddButton( + const std::string& rawKey, + const std::string& key, + const std::string& caption, + ButtonCallback callback) +{ + this->shortcuts->AddShortcut(key, caption); + this->buttons[rawKey] = callback; + this->Layout(); + this->Repaint(); + return *this; +} + +bool DialogOverlay::KeyPress(const std::string& key) { + ButtonCallback cb = this->buttons[key]; + + if (cb) { + cb(key); + return true; + } + + return LayoutBase::KeyPress(key); +} + void DialogOverlay::OnVisibilityChanged(bool visible) { if (visible) { this->Redraw(); @@ -82,24 +122,29 @@ void DialogOverlay::OnVisibilityChanged(bool visible) { void DialogOverlay::RecalculateSize() { int lastWidth = this->width; + this->width = std::max(0, Screen::GetWidth() - (HORIZONTAL_PADDING * 2)); - + if (lastWidth != this->width) { /* the "-2" is for left and right padding */ messageLines = text::BreakLines(this->message, this->width - 2); } - this->height = 1; /* top padding */ + this->height = 0; /* top padding */ this->height += (this->title.size()) ? 2 : 0; this->height += (this->messageLines.size()) ? messageLines.size() + 1 : 0; this->height += 1; /* shortcuts */ } void DialogOverlay::Redraw() { + if (this->width <= 0 || this->height <= 0) { + return; + } + WINDOW* c = this->GetContent(); const int currentX = 1; - int currentY = 1; + int currentY = 0; if (this->title.size()) { wmove(c, currentY, currentX); diff --git a/src/musikbox/cursespp/DialogOverlay.h b/src/musikbox/cursespp/DialogOverlay.h index f40d1ce1e..ad7f9610f 100644 --- a/src/musikbox/cursespp/DialogOverlay.h +++ b/src/musikbox/cursespp/DialogOverlay.h @@ -36,7 +36,10 @@ #include "LayoutBase.h" #include "TextLabel.h" +#include "ShortcutsWindow.h" + #include +#include namespace cursespp { class DialogOverlay : @@ -44,12 +47,22 @@ namespace cursespp { public std::enable_shared_from_this { public: + using ButtonCallback = std::function; + DialogOverlay(); - virtual void Layout(); DialogOverlay& SetTitle(const std::string& title); DialogOverlay& SetMessage(const std::string& message); + DialogOverlay& AddButton( + const std::string& rawKey, + const std::string& key, + const std::string& caption, + ButtonCallback callback); + + virtual void Layout(); + virtual bool KeyPress(const std::string& key); + protected: virtual void OnVisibilityChanged(bool visible); @@ -60,6 +73,9 @@ namespace cursespp { std::string title; std::string message; std::vector messageLines; + std::shared_ptr shortcuts; int width, height; + + std::map buttons; }; } \ No newline at end of file diff --git a/src/musikbox/cursespp/Overlays.cpp b/src/musikbox/cursespp/Overlays.cpp index 89f13edd1..1a142c5d2 100644 --- a/src/musikbox/cursespp/Overlays.cpp +++ b/src/musikbox/cursespp/Overlays.cpp @@ -45,11 +45,42 @@ std::shared_ptr temp; Overlays::Overlays() { temp.reset(new DialogOverlay()); + temp->SetTitle("musikbox") .SetMessage("welcome to musikbox! welcome to musikbox! welcome to musikbox! welcome to musikbox!\n\ntesting line breaks"); + + temp->AddButton( + "KEY_ENTER", + "ENTER", + "ok", + [this](std::string kn) { + this->Remove(temp); + }); + + temp->AddButton( + "^[", + "ESC", + "cancel", + [this](std::string kn) { + + }); + + this->Push(temp); } ILayoutPtr Overlays::Top() { - // return temp; - return none; + return this->stack.size() ? this->stack[0] : none; +} + +void Overlays::Push(ILayoutPtr overlay) { + this->stack.insert(this->stack.begin(), overlay); +} + +void Overlays::Remove(ILayoutPtr overlay) { + auto it = std::find( + this->stack.begin(), this->stack.end(), overlay); + + if (it != this->stack.end()) { + this->stack.erase(it); + } } \ No newline at end of file diff --git a/src/musikbox/cursespp/Overlays.h b/src/musikbox/cursespp/Overlays.h index 976de77f8..ca3866101 100644 --- a/src/musikbox/cursespp/Overlays.h +++ b/src/musikbox/cursespp/Overlays.h @@ -41,5 +41,10 @@ namespace cursespp { public: Overlays(); ILayoutPtr Top(); + void Push(ILayoutPtr overlay); + void Remove(ILayoutPtr overlay); + + private: + std::vector stack; }; } \ No newline at end of file