diff --git a/firmware/application/recent_entries.hpp b/firmware/application/recent_entries.hpp index 2af1e485..335cf0b7 100644 --- a/firmware/application/recent_entries.hpp +++ b/firmware/application/recent_entries.hpp @@ -155,7 +155,7 @@ public: const auto is_selected_key = (selected_key == entry.key()); const auto item_style = (has_focus() && is_selected_key) ? s.invert() : s; draw(entry, target_rect, painter, item_style); - target_rect.pos.y += target_rect.height(); + target_rect += { 0, target_rect.height() }; } painter.fill_rectangle( diff --git a/firmware/application/touch.hpp b/firmware/application/touch.hpp index 0c421f7d..549d3809 100644 --- a/firmware/application/touch.hpp +++ b/firmware/application/touch.hpp @@ -128,12 +128,12 @@ struct Calibration { const std::array& s, const std::array& d ) : k { (s[0].x - s[2].x) * (s[1].y - s[2].y) - (s[1].x - s[2].x) * (s[0].y - s[2].y) }, - a { (d[0].x - d[2].x) * (s[1].y - s[2].y) - (d[1].x - d[2].x) * (s[0].y - s[2].y) }, - b { (s[0].x - s[2].x) * (d[1].x - d[2].x) - (d[0].x - d[2].x) * (s[1].x - s[2].x) }, - c { s[0].y * (s[2].x * d[1].x - s[1].x * d[2].x) + s[1].y * (s[0].x * d[2].x - s[2].x * d[0].x) + s[2].y * (s[1].x * d[0].x - s[0].x * d[1].x) }, - d { (d[0].y - d[2].y) * (s[1].y - s[2].y) - (d[1].y - d[2].y) * (s[0].y - s[2].y) }, - e { (s[0].x - s[2].x) * (d[1].y - d[2].y) - (d[0].y - d[2].y) * (s[1].x - s[2].x) }, - f { s[0].y * (s[2].x * d[1].y - s[1].x * d[2].y) + s[1].y * (s[0].x * d[2].y - s[2].x * d[0].y) + s[2].y * (s[1].x * d[0].y - s[0].x * d[1].y) } + a { (d[0].x() - d[2].x()) * (s[1].y - s[2].y) - (d[1].x() - d[2].x()) * (s[0].y - s[2].y) }, + b { (s[0].x - s[2].x) * (d[1].x() - d[2].x()) - (d[0].x() - d[2].x()) * (s[1].x - s[2].x) }, + c { s[0].y * (s[2].x * d[1].x() - s[1].x * d[2].x()) + s[1].y * (s[0].x * d[2].x() - s[2].x * d[0].x()) + s[2].y * (s[1].x * d[0].x() - s[0].x * d[1].x()) }, + d { (d[0].y() - d[2].y()) * (s[1].y - s[2].y) - (d[1].y() - d[2].y()) * (s[0].y - s[2].y) }, + e { (s[0].x - s[2].x) * (d[1].y() - d[2].y()) - (d[0].y() - d[2].y()) * (s[1].x - s[2].x) }, + f { s[0].y * (s[2].x * d[1].y() - s[1].x * d[2].y()) + s[1].y * (s[0].x * d[2].y() - s[2].x * d[0].y()) + s[2].y * (s[1].x * d[0].y() - s[0].x * d[1].y()) } { } diff --git a/firmware/application/ui_console.cpp b/firmware/application/ui_console.cpp index 28b9f298..af71ee14 100644 --- a/firmware/application/ui_console.cpp +++ b/firmware/application/ui_console.cpp @@ -45,15 +45,15 @@ void Console::write(const std::string& message) { } else { const auto glyph = font.glyph(c); const auto advance = glyph.advance(); - if( (pos.x + advance.x) > rect.width() ) { + if( (pos.x() + advance.x()) > rect.width() ) { crlf(); } const Point pos_glyph { - rect.pos.x + pos.x, - display.scroll_area_y(pos.y) + rect.pos.x() + pos.x(), + display.scroll_area_y(pos.y()) }; display.draw_glyph(pos_glyph, glyph, s.foreground, s.background); - pos.x += advance.x; + pos += { advance.x(), 0 }; } } } @@ -86,14 +86,13 @@ void Console::crlf() { const Style& s = style(); const auto sr = screen_rect(); const auto line_height = s.font.line_height(); - pos.x = 0; - pos.y += line_height; - const int32_t y_excess = pos.y + line_height - sr.height(); + pos = { 0, pos.y() + line_height }; + const int32_t y_excess = pos.y() + line_height - sr.height(); if( y_excess > 0 ) { display.scroll(-y_excess); - pos.y -= y_excess; + pos = { pos.x(), pos.y() - y_excess }; - const Rect dirty { sr.left(), display.scroll_area_y(pos.y), sr.width(), line_height }; + const Rect dirty { sr.left(), display.scroll_area_y(pos.y()), sr.width(), line_height }; display.fill_rectangle(dirty, s.background); } } diff --git a/firmware/application/ui_menu.cpp b/firmware/application/ui_menu.cpp index 4a04e817..1406875b 100644 --- a/firmware/application/ui_menu.cpp +++ b/firmware/application/ui_menu.cpp @@ -54,7 +54,7 @@ void MenuItemView::paint(Painter& painter) { ); painter.draw_string( - { r.pos.x + 8, r.pos.y + (r.size.h - font_height) / 2 }, + { r.pos.x() + 8, r.pos.y() + (r.size.h - font_height) / 2 }, paint_style, item.text ); diff --git a/firmware/application/ui_touch_calibration.cpp b/firmware/application/ui_touch_calibration.cpp index 37cd0df8..a5a7c348 100644 --- a/firmware/application/ui_touch_calibration.cpp +++ b/firmware/application/ui_touch_calibration.cpp @@ -92,8 +92,8 @@ void TouchCalibrationView::set_phase(const Phase value) { uint32_t TouchCalibrationView::distance_squared(const Point& touch_point, const Image& target) { const auto target_point = target.screen_rect().center(); - const int32_t dx = target_point.x - touch_point.x; - const int32_t dy = target_point.y - touch_point.y; + const int32_t dx = target_point.x() - touch_point.x(); + const int32_t dy = target_point.y() - touch_point.y(); const uint32_t dx2 = dx * dx; const uint32_t dy2 = dy * dy; return dx2 + dy2; diff --git a/firmware/common/lcd_ili9341.cpp b/firmware/common/lcd_ili9341.cpp index ddda0f8c..68a04ef9 100644 --- a/firmware/common/lcd_ili9341.cpp +++ b/firmware/common/lcd_ili9341.cpp @@ -196,8 +196,8 @@ void lcd_start_ram_write( const ui::Point p, const ui::Size s ) { - lcd_caset(p.x, p.x + s.w - 1); - lcd_paset(p.y, p.y + s.h - 1); + lcd_caset(p.x(), p.x() + s.w - 1); + lcd_paset(p.y(), p.y() + s.h - 1); lcd_ramwr_start(); } @@ -205,8 +205,8 @@ void lcd_start_ram_read( const ui::Point p, const ui::Size s ) { - lcd_caset(p.x, p.x + s.w - 1); - lcd_paset(p.y, p.y + s.h - 1); + lcd_caset(p.x(), p.x() + s.w - 1); + lcd_paset(p.y(), p.y() + s.h - 1); lcd_ramrd_start(); } @@ -292,8 +292,8 @@ void ILI9341::fill_circle( const bool inside = d2 < radius2; const auto color = inside ? foreground : background; draw_pixel({ - static_cast(x + center.x), - static_cast(y + center.y) + static_cast(x + center.x()), + static_cast(y + center.y()) }, color); } } diff --git a/firmware/common/ui.cpp b/firmware/common/ui.cpp index 5b0f55b2..a9a3eae9 100644 --- a/firmware/common/ui.cpp +++ b/firmware/common/ui.cpp @@ -26,8 +26,8 @@ namespace ui { bool Rect::contains(const Point p) const { - return (p.x >= left()) && (p.y >= top()) && - (p.x < right()) && (p.y < bottom()); + return (p.x() >= left()) && (p.y() >= top()) && + (p.x() < right()) && (p.y() < bottom()); } Rect Rect::intersect(const Rect& o) const { @@ -64,4 +64,9 @@ Rect& Rect::operator+=(const Point& p) { return *this; } +Rect& Rect::operator-=(const Point& p) { + pos -= p; + return *this; +} + } /* namespace ui */ diff --git a/firmware/common/ui.hpp b/firmware/common/ui.hpp index f363a841..fb331f12 100644 --- a/firmware/common/ui.hpp +++ b/firmware/common/ui.hpp @@ -82,38 +82,54 @@ struct ColorRGB888 { }; struct Point { - Coord x; - Coord y; +private: + Coord _x; + Coord _y; +public: constexpr Point( - ) : x { 0 }, - y { 0 } + ) : _x { 0 }, + _y { 0 } { } constexpr Point( int x, int y - ) : x { static_cast(x) }, - y { static_cast(y) } + ) : _x { static_cast(x) }, + _y { static_cast(y) } { } - Point operator-() const { - return { -x, -y }; + constexpr int x() const { + return _x; } - Point operator+(const Point& p) const { - return { x + p.x, y + p.y }; + constexpr int y() const { + return _y; } - Point operator-(const Point& p) const { - return { x - p.x, y - p.y }; + 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 }; } Point& operator+=(const Point& p) { - x += p.x; - y += p.y; + _x += p._x; + _y += p._y; + return *this; + } + + Point& operator-=(const Point& p) { + _x -= p._x; + _y -= p._y; return *this; } }; @@ -168,19 +184,19 @@ struct Rect { } int top() const { - return pos.y; + return pos.y(); } int bottom() const { - return pos.y + size.h; + return pos.y() + size.h; } int left() const { - return pos.x; + return pos.x(); } int right() const { - return pos.x + size.w; + return pos.x() + size.w; } int width() const { @@ -192,7 +208,7 @@ struct Rect { } Point center() const { - return { pos.x + size.w / 2, pos.y + size.h / 2 }; + return { pos.x() + size.w / 2, pos.y() + size.h / 2 }; } bool is_empty() const { @@ -209,6 +225,7 @@ struct Rect { Rect& operator+=(const Rect& p); Rect& operator+=(const Point& p); + Rect& operator-=(const Point& p); operator bool() const { return !size.is_empty(); diff --git a/firmware/common/ui_focus.cpp b/firmware/common/ui_focus.cpp index 92547d35..48d30b02 100644 --- a/firmware/common/ui_focus.cpp +++ b/firmware/common/ui_focus.cpp @@ -127,14 +127,14 @@ static int32_t rect_distances( switch(direction) { case KeyEvent::Right: case KeyEvent::Left: - perpendicular_axis_start = rect_start.center().y; - perpendicular_axis_end = rect_end.center().y; + perpendicular_axis_start = rect_start.center().y(); + perpendicular_axis_end = rect_end.center().y(); break; case KeyEvent::Up: case KeyEvent::Down: - perpendicular_axis_start = rect_start.center().x; - perpendicular_axis_end = rect_end.center().x; + perpendicular_axis_start = rect_start.center().x(); + perpendicular_axis_end = rect_end.center().x(); break; default: diff --git a/firmware/common/ui_painter.cpp b/firmware/common/ui_painter.cpp index 2b54e194..6ad11a0b 100644 --- a/firmware/common/ui_painter.cpp +++ b/firmware/common/ui_painter.cpp @@ -39,7 +39,7 @@ Style Style::invert() const { int Painter::draw_char(const Point p, const Style& style, const char c) { const auto glyph = style.font.glyph(c); display.draw_glyph(p, glyph, style.foreground, style.background); - return glyph.advance().x; + return glyph.advance().x(); } int Painter::draw_string(Point p, const Style& style, const std::string text) { @@ -49,7 +49,7 @@ int Painter::draw_string(Point p, const Style& style, const std::string text) { display.draw_glyph(p, glyph, style.foreground, style.background); const auto advance = glyph.advance(); p += advance; - width += advance.x; + width += advance.x(); } return width; } @@ -68,9 +68,9 @@ void Painter::draw_vline(Point p, int height, const Color c) { void Painter::draw_rectangle(const Rect r, const Color c) { draw_hline(r.pos, r.size.w, c); - draw_vline({ r.pos.x, r.pos.y + 1 }, r.size.h - 2, c); - draw_vline({ r.pos.x + r.size.w - 1, r.pos.y + 1 }, r.size.h - 2, c); - draw_hline({ r.pos.x, r.pos.y + r.size.h - 1 }, r.size.w, c); + draw_vline({ r.pos.x(), r.pos.y() + 1 }, r.size.h - 2, c); + draw_vline({ r.pos.x() + r.size.w - 1, r.pos.y() + 1 }, r.size.h - 2, c); + draw_hline({ r.pos.x(), r.pos.y() + r.size.h - 1 }, r.size.w, c); } void Painter::fill_rectangle(const Rect r, const Color c) { diff --git a/firmware/common/ui_widget.cpp b/firmware/common/ui_widget.cpp index 94716753..a84b731a 100644 --- a/firmware/common/ui_widget.cpp +++ b/firmware/common/ui_widget.cpp @@ -351,13 +351,13 @@ void Button::paint(Painter& painter) { painter.draw_rectangle(r, style().foreground); painter.fill_rectangle( - { r.pos.x + 1, r.pos.y + 1, r.size.w - 2, r.size.h - 2 }, + { r.pos.x() + 1, r.pos.y() + 1, r.size.w - 2, r.size.h - 2 }, paint_style.background ); const auto label_r = paint_style.font.size_of(text_); painter.draw_string( - { r.pos.x + (r.size.w - label_r.w) / 2, r.pos.y + (r.size.h - label_r.h) / 2 }, + { r.pos.x() + (r.size.w - label_r.w) / 2, r.pos.y() + (r.size.h - label_r.h) / 2 }, paint_style, text_ );