2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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, 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_painter.hpp"
|
|
|
|
|
2015-08-15 00:27:46 -04:00
|
|
|
#include "ui_widget.hpp"
|
|
|
|
|
2015-08-01 17:31:51 -04:00
|
|
|
#include "portapack.hpp"
|
|
|
|
using namespace portapack;
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
Style Style::invert() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {
|
|
|
|
.font = font,
|
|
|
|
.background = foreground,
|
|
|
|
.foreground = background};
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
int Painter::draw_char(Point p, const Style& style, char c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto glyph = style.font.glyph(c);
|
|
|
|
display.draw_glyph(p, glyph, style.foreground, style.background);
|
|
|
|
return glyph.advance().x();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
int Painter::draw_string(Point p, const Style& style, std::string_view text) {
|
|
|
|
return draw_string(p, style.font, style.foreground, style.background, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Painter::draw_string(
|
|
|
|
Point p,
|
|
|
|
const Font& font,
|
|
|
|
Color foreground,
|
|
|
|
Color background,
|
|
|
|
std::string_view text) {
|
2023-05-18 16:16:05 -04:00
|
|
|
bool escape = false;
|
|
|
|
size_t width = 0;
|
|
|
|
Color pen = foreground;
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
for (auto c : text) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (escape) {
|
|
|
|
if (c <= 15)
|
|
|
|
pen = term_colors[c & 15];
|
|
|
|
else
|
|
|
|
pen = foreground;
|
|
|
|
escape = false;
|
|
|
|
} else {
|
|
|
|
if (c == '\x1B') {
|
|
|
|
escape = true;
|
|
|
|
} else {
|
|
|
|
const auto glyph = font.glyph(c);
|
|
|
|
display.draw_glyph(p, glyph, pen, background);
|
|
|
|
const auto advance = glyph.advance();
|
|
|
|
p += advance;
|
|
|
|
width += advance.x();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
return width;
|
2017-02-12 02:23:31 -05:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::draw_bitmap(Point p, const Bitmap& bitmap, Color foreground, Color background) {
|
2023-06-25 13:48:51 -04:00
|
|
|
// If bright foreground colors on white background, darken the foreground color to improve visibility
|
2023-06-27 04:19:34 -04:00
|
|
|
if ((background.v == ui::Color::white().v) && (foreground.to_greyscale() > 146))
|
2023-06-25 13:48:51 -04:00
|
|
|
foreground = foreground.dark();
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
display.draw_bitmap(p, bitmap.size, bitmap.data, foreground, background);
|
2016-02-03 16:23:46 -05:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::draw_hline(Point p, int width, Color c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
display.fill_rectangle({p, {width, 1}}, c);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::draw_vline(Point p, int height, Color c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
display.fill_rectangle({p, {1, height}}, c);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::draw_rectangle(Rect r, Color c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
draw_hline(r.location(), r.width(), c);
|
|
|
|
draw_vline({r.left(), r.top() + 1}, r.height() - 2, c);
|
|
|
|
draw_vline({r.left() + r.width() - 1, r.top() + 1}, r.height() - 2, c);
|
|
|
|
draw_hline({r.left(), r.top() + r.height() - 1}, r.width(), c);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::fill_rectangle(Rect r, Color c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
display.fill_rectangle(r, c);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::fill_rectangle_unrolled8(Rect r, Color c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
display.fill_rectangle_unrolled8(r, c);
|
2018-05-21 13:46:48 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::paint_widget_tree(Widget* w) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (ui::is_dirty()) {
|
|
|
|
paint_widget(w);
|
|
|
|
ui::dirty_clear();
|
|
|
|
}
|
2015-08-15 00:36:51 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
void Painter::paint_widget(Widget* w) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (w->hidden()) {
|
|
|
|
// Mark widget (and all children) as invisible.
|
|
|
|
w->visible(false);
|
|
|
|
} else {
|
|
|
|
// Mark this widget as visible and recurse.
|
|
|
|
w->visible(true);
|
|
|
|
|
|
|
|
if (w->dirty()) {
|
|
|
|
w->paint(*this);
|
|
|
|
// Force-paint all children.
|
|
|
|
for (const auto child : w->children()) {
|
|
|
|
child->set_dirty();
|
|
|
|
paint_widget(child);
|
|
|
|
}
|
|
|
|
w->set_clean();
|
|
|
|
} else {
|
|
|
|
// Selectively paint all children.
|
|
|
|
for (const auto child : w->children()) {
|
|
|
|
paint_widget(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-15 00:27:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
} /* namespace ui */
|