Fleshed out some of DialogOverlay -- we can set a title and a message, and have the overlay automatically resize itself based on content. Supports line breaking for the message.

This commit is contained in:
Casey Langen 2016-09-01 23:02:21 -07:00
parent 7c972bf011
commit 4240a10963
6 changed files with 198 additions and 23 deletions

View File

@ -31,6 +31,7 @@ set (BOX_SRCS
./cursespp/App.cpp
./cursespp/Checkbox.cpp
./cursespp/Colors.cpp
./cursespp/DialogOverlay.cpp
./cursespp/LayoutBase.cpp
./cursespp/LayoutStack.cpp
./cursespp/ListWindow.cpp

View File

@ -259,12 +259,9 @@ void App::CheckShowOverlay() {
if (top) {
top->Layout();
top->Show();
top->BringToTop();
}
}
if (top) {
top->BringToTop();
}
}
void App::ChangeLayout(ILayoutPtr newLayout) {

View File

@ -0,0 +1,119 @@
//////////////////////////////////////////////////////////////////////////////
//
// 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.
//
//////////////////////////////////////////////////////////////////////////////
#include "DialogOverlay.h"
#include "Colors.h"
#include "Screen.h"
#include "Text.h"
using namespace cursespp;
#define HORIZONTAL_PADDING 4
#define VERTICAL_PADDING 2
DialogOverlay::DialogOverlay() {
this->SetContentColor(CURSESPP_OVERLAY_BACKGROUND);
this->width = this->height = 0;
}
void DialogOverlay::Layout() {
this->RecalculateSize();
this->MoveAndResize(
HORIZONTAL_PADDING,
VERTICAL_PADDING,
this->width,
this->height);
this->Redraw();
}
DialogOverlay& DialogOverlay::SetTitle(const std::string& title) {
this->title = title;
this->RecalculateSize();
this->Repaint();
return *this;
}
DialogOverlay& DialogOverlay::SetMessage(const std::string& message) {
this->message = message;
this->width = 0; /* implicitly triggers a new BreakLines() */
this->RecalculateSize();
this->Repaint();
return *this;
}
void DialogOverlay::OnVisibilityChanged(bool visible) {
if (visible) {
this->Redraw();
}
}
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 += (this->title.size()) ? 2 : 0;
this->height += (this->messageLines.size()) ? messageLines.size() + 1 : 0;
this->height += 1; /* shortcuts */
}
void DialogOverlay::Redraw() {
WINDOW* c = this->GetContent();
const int currentX = 1;
int currentY = 1;
if (this->title.size()) {
wmove(c, currentY, currentX);
wattron(c, A_BOLD);
wprintw(c, text::Ellipsize(this->title, this->width - 2).c_str());
wattroff(c, A_BOLD);
currentY += 2;
}
if (this->message.size()) {
for (size_t i = 0; i < messageLines.size(); i++) {
wmove(c, currentY, currentX);
wprintw(c, this->messageLines.at(i).c_str());
++currentY;
}
}
}

View File

@ -0,0 +1,65 @@
//////////////////////////////////////////////////////////////////////////////
//
// 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 "LayoutBase.h"
#include "TextLabel.h"
#include <vector>
namespace cursespp {
class DialogOverlay :
public LayoutBase,
public std::enable_shared_from_this<DialogOverlay>
{
public:
DialogOverlay();
virtual void Layout();
DialogOverlay& SetTitle(const std::string& title);
DialogOverlay& SetMessage(const std::string& message);
protected:
virtual void OnVisibilityChanged(bool visible);
private:
void Redraw();
void RecalculateSize();
std::string title;
std::string message;
std::vector<std::string> messageLines;
int width, height;
};
}

View File

@ -33,30 +33,20 @@
//////////////////////////////////////////////////////////////////////////////
#include "Overlays.h"
#include "LayoutBase.h"
#include "DialogOverlay.h"
#include "Colors.h"
#include "Screen.h"
#include <iostream>
using namespace cursespp;
static ILayoutPtr none;
class OverlayLayout :
public LayoutBase,
public std::enable_shared_from_this<OverlayLayout>
{
virtual void Layout() {
this->MoveAndResize(2, 0, Screen::GetWidth() - 4, 8);
}
};
std::shared_ptr<LayoutBase> temp;
std::shared_ptr<DialogOverlay> temp;
Overlays::Overlays() {
temp.reset(new OverlayLayout());
temp->SetContentColor(CURSESPP_OVERLAY_BACKGROUND);
temp.reset(new DialogOverlay());
temp->SetTitle("musikbox")
.SetMessage("welcome to musikbox! welcome to musikbox! welcome to musikbox! welcome to musikbox!\n\ntesting line breaks");
}
ILayoutPtr Overlays::Top() {

View File

@ -232,11 +232,14 @@ namespace cursespp {
std::vector<std::string> BreakLines(const std::string& line, size_t width) {
std::vector<std::string> result;
std::vector<std::string> split;
boost::algorithm::split(split, line, boost::is_any_of("\n"));
for (size_t i = 0; i < split.size(); i++) {
privateBreakLines(split.at(i), width, result);
if (width > 0) {
std::vector<std::string> split;
boost::algorithm::split(split, line, boost::is_any_of("\n"));
for (size_t i = 0; i < split.size(); i++) {
privateBreakLines(split.at(i), width, result);
}
}
return result;