362 lines
5.7 KiB
C++
Raw Permalink Normal View History

2015-07-08 08:39:24 -07: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.
*/
#ifndef __UI_H__
#define __UI_H__
#include <cstdint>
namespace ui {
#define DEG_TO_RAD(d) (d * (2 * pi) / 360.0)
2015-07-08 08:39:24 -07:00
using Coord = int16_t;
using Dim = int16_t;
struct Color {
2016-05-09 20:42:20 +02:00
uint16_t v; // rrrrrGGGGGGbbbbb
2015-07-08 08:39:24 -07:00
constexpr Color(
) : v { 0 }
{
}
2016-05-09 20:42:20 +02:00
constexpr Color(
uint16_t v
) : v { v }
{
}
2016-02-07 21:52:36 -08:00
2015-07-08 08:39:24 -07:00
constexpr Color(
uint8_t r,
uint8_t g,
uint8_t b
) : v {
static_cast<uint16_t>(
((r & 0xf8) << 8)
| ((g & 0xfc) << 3)
| ((b & 0xf8) >> 3)
)}
{
}
uint8_t to_greyscale() {
uint32_t r = ((v >> 11) & 31U) << 3;
uint32_t g = ((v >> 5) & 63U) << 2;
uint32_t b = (v & 31U) << 3;
uint32_t grey = (r * 299 + g * 587 + b * 114) / 1000;
return (uint8_t)grey;
}
2016-05-09 20:42:20 +02:00
Color operator-() const {
return (v ^ 0xffff);
}
2015-07-08 08:39:24 -07:00
static constexpr Color black() {
return { 0, 0, 0 };
}
static constexpr Color red() {
return { 255, 0, 0 };
}
static constexpr Color dark_red() {
return { 159, 0, 0 };
}
static constexpr Color orange() {
return { 255, 175, 0 };
}
static constexpr Color dark_orange() {
return { 191, 95, 0 };
}
2015-07-08 08:39:24 -07:00
static constexpr Color yellow() {
return { 255, 255, 0 };
}
static constexpr Color dark_yellow() {
return { 191, 191, 0 };
}
2015-07-08 08:39:24 -07:00
static constexpr Color green() {
return { 0, 255, 0 };
}
static constexpr Color dark_green() {
return { 0, 159, 0 };
}
2015-07-08 08:39:24 -07:00
static constexpr Color blue() {
return { 0, 0, 255 };
}
2017-05-18 21:56:55 +01:00
static constexpr Color dark_blue() {
return { 0, 0, 191 };
2017-05-18 21:56:55 +01:00
}
static constexpr Color cyan() {
return { 0, 255, 255 };
}
static constexpr Color dark_cyan() {
return { 0, 191, 191 };
}
static constexpr Color magenta() {
return { 255, 0, 255 };
}
static constexpr Color dark_magenta() {
return { 191, 0, 191 };
}
2015-07-08 08:39:24 -07:00
static constexpr Color white() {
return { 255, 255, 255 };
}
2016-12-09 02:35:50 +01:00
static constexpr Color light_grey() {
return { 191, 191, 191 };
}
2016-12-09 02:35:50 +01:00
static constexpr Color grey() {
return { 127, 127, 127 };
2016-12-09 02:35:50 +01:00
}
static constexpr Color dark_grey() {
return { 63, 63, 63 };
2016-12-09 02:35:50 +01:00
}
static constexpr Color purple() {
return { 204, 0, 102 };
}
2015-07-08 08:39:24 -07:00
};
2016-02-07 21:52:36 -08:00
extern Color term_colors[16];
struct ColorRGB888 {
uint8_t r;
uint8_t g;
uint8_t b;
2015-07-08 08:39:24 -07:00
};
2015-07-08 08:39:24 -07:00
struct Point {
2016-11-28 10:39:10 -08:00
private:
Coord _x;
Coord _y;
2015-07-08 08:39:24 -07:00
2016-11-28 10:39:10 -08:00
public:
2015-07-08 08:39:24 -07:00
constexpr Point(
2016-11-28 10:39:10 -08:00
) : _x { 0 },
_y { 0 }
2015-07-08 08:39:24 -07:00
{
}
constexpr Point(
2016-01-31 09:34:24 +01:00
int x,
int y
2016-11-28 10:39:10 -08:00
) : _x { static_cast<Coord>(x) },
_y { static_cast<Coord>(y) }
2015-07-08 08:39:24 -07:00
{
}
2016-11-28 10:39:10 -08:00
constexpr int x() const {
return _x;
2015-07-08 08:39:24 -07:00
}
2016-11-28 10:39:10 -08:00
constexpr int y() const {
return _y;
2015-07-08 08:39:24 -07:00
}
2016-11-28 10:39:10 -08:00
constexpr Point operator-() const {
return { -_x, -_y };
}
constexpr Point operator+(const Point& p) const {
return { _x + p._x, _y + p._y };
}
constexpr Point operator-(const Point& p) const {
return { _x - p._x, _y - p._y };
2015-07-08 08:39:24 -07:00
}
Point& operator+=(const Point& p) {
2016-11-28 10:39:10 -08:00
_x += p._x;
_y += p._y;
return *this;
}
Point& operator-=(const Point& p) {
_x -= p._x;
_y -= p._y;
2015-07-08 08:39:24 -07:00
return *this;
}
};
struct Size {
2016-11-28 10:55:45 -08:00
private:
Dim _w;
Dim _h;
2015-07-08 08:39:24 -07:00
2016-11-28 10:55:45 -08:00
public:
2015-07-08 08:39:24 -07:00
constexpr Size(
2016-11-28 10:55:45 -08:00
) : _w { 0 },
_h { 0 }
2015-07-08 08:39:24 -07:00
{
}
constexpr Size(
2016-01-31 09:34:24 +01:00
int w,
int h
2016-11-28 10:55:45 -08:00
) : _w { static_cast<Dim>(w) },
_h { static_cast<Dim>(h) }
2015-07-08 08:39:24 -07:00
{
}
2016-11-28 10:55:45 -08:00
int width() const {
return _w;
}
int height() const {
return _h;
}
2015-07-08 08:39:24 -07:00
bool is_empty() const {
2016-11-28 10:55:45 -08:00
return (_w < 1) || (_h < 1);
2015-07-08 08:39:24 -07:00
}
};
struct Rect {
2016-11-28 11:25:27 -08:00
private:
Point _pos;
Size _size;
2015-07-08 08:39:24 -07:00
2016-11-28 11:25:27 -08:00
public:
2015-07-08 08:39:24 -07:00
constexpr Rect(
2016-11-28 11:25:27 -08:00
) : _pos { },
_size { }
2015-07-08 08:39:24 -07:00
{
}
constexpr Rect(
2016-01-31 09:34:24 +01:00
int x, int y,
int w, int h
2016-11-28 11:25:27 -08:00
) : _pos { x, y },
_size { w, h }
2015-07-08 08:39:24 -07:00
{
}
constexpr Rect(
Point pos,
Size size
2016-11-28 11:25:27 -08:00
) : _pos(pos),
_size(size)
2015-07-08 08:39:24 -07:00
{
}
2016-01-31 09:34:24 +01:00
2016-11-28 11:25:27 -08:00
Point location() const {
return _pos;
}
Size size() const {
return _size;
}
2016-01-31 09:34:24 +01:00
int top() const {
2016-11-28 11:25:27 -08:00
return _pos.y();
2015-07-08 08:39:24 -07:00
}
2016-01-31 09:34:24 +01:00
int bottom() const {
2016-11-28 11:25:27 -08:00
return _pos.y() + _size.height();
2015-07-08 08:39:24 -07:00
}
2016-01-31 09:34:24 +01:00
int left() const {
2016-11-28 11:25:27 -08:00
return _pos.x();
2015-07-08 08:39:24 -07:00
}
2016-01-31 09:34:24 +01:00
int right() const {
2016-11-28 11:25:27 -08:00
return _pos.x() + _size.width();
2015-07-08 08:39:24 -07:00
}
2016-01-31 09:34:24 +01:00
int width() const {
2016-11-28 11:25:27 -08:00
return _size.width();
2015-07-08 08:39:24 -07:00
}
2016-01-31 09:34:24 +01:00
int height() const {
2016-11-28 11:25:27 -08:00
return _size.height();
2015-07-08 08:39:24 -07:00
}
Point center() const {
2016-11-28 11:25:27 -08:00
return { _pos.x() + _size.width() / 2, _pos.y() + _size.height() / 2 };
2015-07-08 08:39:24 -07:00
}
bool is_empty() const {
2016-11-28 11:25:27 -08:00
return _size.is_empty();
2015-07-08 08:39:24 -07:00
}
bool contains(const Point p) const;
Rect intersect(const Rect& o) const;
Rect operator+(const Point& p) const {
2016-11-28 11:25:27 -08:00
return { _pos + p, _size };
2015-07-08 08:39:24 -07:00
}
Rect& operator+=(const Rect& p);
Rect& operator+=(const Point& p);
2016-11-28 10:39:10 -08:00
Rect& operator-=(const Point& p);
2015-07-08 08:39:24 -07:00
operator bool() const {
2016-11-28 11:25:27 -08:00
return !_size.is_empty();
2015-07-08 08:39:24 -07:00
}
};
2016-02-03 13:23:23 -08:00
struct Bitmap {
const Size size;
const uint8_t* const data;
};
2015-07-08 08:39:24 -07:00
enum class KeyEvent {
/* Ordinals map to bit positions reported by CPLD */
Right = 0,
Left = 1,
Down = 2,
Up = 3,
Select = 4,
2023-04-16 18:23:54 +02:00
Dfu = 5,
Back = 6, /* Left and Up together */
2015-07-08 08:39:24 -07:00
};
using EncoderEvent = int32_t;
struct TouchEvent {
enum class Type : uint32_t {
Start = 0,
Move = 1,
End = 2,
};
Point point;
Type type;
};
Point polar_to_point(float angle, uint32_t distance);
Point fast_polar_to_point(int32_t angle, uint32_t distance);
2015-07-08 08:39:24 -07:00
} /* namespace ui */
#endif/*__UI_H__*/