Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shell: Added text canvas classes for text-based drawing
- Loading branch information
Showing
7 changed files
with
345 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** @file cursestextcanvas.cpp Text-based drawing surface for curses. | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#include "cursestextcanvas.h" | ||
|
||
CursesTextCanvas::CursesTextCanvas(Size const &size, WINDOW *window, Coord const &originInWindow) | ||
: TextCanvas(size), _window(window), _origin(originInWindow) | ||
{} | ||
|
||
void CursesTextCanvas::show() | ||
{ | ||
Size const dims = size(); | ||
|
||
// All dirty characters are drawn. | ||
for(int row = 0; row < dims.y; ++row) | ||
{ | ||
bool needMove = true; | ||
|
||
for(int col = 0; col < dims.x; ++col) | ||
{ | ||
Coord const pos(col, row); | ||
Char const &ch = at(pos); | ||
|
||
if(!ch.attrib.dirty) | ||
{ | ||
needMove = true; | ||
continue; | ||
} | ||
|
||
if(needMove) | ||
{ | ||
// Advance cursor. | ||
wmove(_window, _origin.y + row, _origin.x + col); | ||
needMove = false; | ||
} | ||
|
||
// Set attributes. | ||
wattrset(_window, | ||
(ch.attrib.bold? A_BOLD : 0) | | ||
(ch.attrib.reverse? A_REVERSE : 0) | | ||
(ch.attrib.underline? A_UNDERLINE : 0)); | ||
|
||
waddch(_window, ch.ch.unicode()); // cursor advanced | ||
} | ||
} | ||
|
||
// Mark everything clean. | ||
TextCanvas::show(); | ||
|
||
wrefresh(_window); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** @file cursestextcanvas.h Text-based drawing surface for curses. | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#ifndef CURSESTEXTCANVAS_H | ||
#define CURSESTEXTCANVAS_H | ||
|
||
#include <curses.h> | ||
#include "textcanvas.h" | ||
|
||
class CursesTextCanvas : public TextCanvas | ||
{ | ||
public: | ||
CursesTextCanvas(Size const &size, WINDOW *window, Coord const &originInWindow = Coord(0, 0)); | ||
|
||
void show(); | ||
|
||
private: | ||
WINDOW *_window; | ||
Coord _origin; | ||
}; | ||
|
||
#endif // CURSESTEXTCANVAS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** @file textcanvas.cpp Text-based drawing surface. | ||
* | ||
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#include "textcanvas.h" | ||
#include <QList> | ||
|
||
struct TextCanvas::Instance | ||
{ | ||
Size size; | ||
|
||
typedef QList<Char> Line; | ||
QList<Line> lines; | ||
|
||
Instance(Size const &initialSize) : size(initialSize) | ||
{ | ||
// Allocate lines based on supplied initial size. | ||
for(int row = 0; row < size.y; ++row) | ||
{ | ||
lines.append(makeLine()); | ||
} | ||
} | ||
|
||
Line makeLine() | ||
{ | ||
Line line; | ||
for(int col = 0; col < size.x; ++col) | ||
{ | ||
line.append(Char()); | ||
} | ||
return line; | ||
} | ||
|
||
void resize(Size const &newSize) | ||
{ | ||
if(newSize == size) return; | ||
|
||
// Allocate or free lines. | ||
while(newSize.y < size.y) | ||
{ | ||
lines.removeLast(); | ||
size.y--; | ||
} | ||
while(newSize.y > size.y) | ||
{ | ||
lines.append(makeLine()); | ||
size.y++; | ||
} | ||
|
||
// Make sure all lines are the correct width. | ||
for(int row = 0; row < lines.size(); ++row) | ||
{ | ||
Line &line = lines[row]; | ||
if(line.size() == newSize.x) continue; | ||
|
||
while(newSize.x < line.size()) | ||
{ | ||
line.removeLast(); | ||
} | ||
while(newSize.x > line.size()) | ||
{ | ||
line.append(Char()); | ||
} | ||
} | ||
|
||
Q_ASSERT(lines.size() == size.y); | ||
} | ||
|
||
void markAllAsDirty(bool markDirty) | ||
{ | ||
for(int row = 0; row < lines.size(); ++row) | ||
{ | ||
Line &line = lines[row]; | ||
for(int col = 0; col < line.size(); ++col) | ||
{ | ||
line[col].attrib.dirty = markDirty; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
TextCanvas::TextCanvas(Size const &size) : d(new Instance(size)) | ||
{ | ||
d->size = size; | ||
} | ||
|
||
TextCanvas::~TextCanvas() | ||
{ | ||
delete d; | ||
} | ||
|
||
TextCanvas::Size TextCanvas::size() const | ||
{ | ||
return d->size; | ||
} | ||
|
||
void TextCanvas::resize(Size const &newSize) | ||
{ | ||
d->resize(newSize); | ||
} | ||
|
||
TextCanvas::Char &TextCanvas::at(Coord const &pos) | ||
{ | ||
Q_ASSERT(pos.x >= 0 && pos.x < d->size.x); | ||
Q_ASSERT(pos.y >= 0 && pos.y < d->size.y); | ||
Char &c = d->lines[pos.y][pos.x]; | ||
c.attrib.dirty = true; | ||
return c; | ||
} | ||
|
||
TextCanvas::Char const &TextCanvas::at(Coord const &pos) const | ||
{ | ||
Q_ASSERT(pos.x >= 0 && pos.x < d->size.x); | ||
Q_ASSERT(pos.y >= 0 && pos.y < d->size.y); | ||
return d->lines[pos.y].at(pos.x); | ||
} | ||
|
||
void TextCanvas::show() | ||
{ | ||
d->markAllAsDirty(false); | ||
} |
Oops, something went wrong.