From 4842d230bd6f0fc22c8409c808df71b88492b0c6 Mon Sep 17 00:00:00 2001 From: 3djc Date: Sat, 17 Oct 2020 09:38:24 +0200 Subject: [PATCH] Add drawLine function --- .../thirdparty/libopenui/src/bitmapbuffer.cpp | 53 +++++++++++++++++++ .../thirdparty/libopenui/src/bitmapbuffer.h | 2 + .../libopenui/src/libopenui_helpers.h | 6 +++ 3 files changed, 61 insertions(+) diff --git a/radio/src/thirdparty/libopenui/src/bitmapbuffer.cpp b/radio/src/thirdparty/libopenui/src/bitmapbuffer.cpp index 197c77208..7dceba807 100755 --- a/radio/src/thirdparty/libopenui/src/bitmapbuffer.cpp +++ b/radio/src/thirdparty/libopenui/src/bitmapbuffer.cpp @@ -126,6 +126,57 @@ void BitmapBuffer::drawVerticalLine(coord_t x, coord_t y, coord_t h, uint8_t pat } } +void BitmapBuffer::drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t pat, LcdFlags att) +{ + // Offsets + x1 += this->offsetX; + y1 += this->offsetY; + x2 += this->offsetX; + y2 += this->offsetY; + + pixel_t color = lcdColorTable[COLOR_IDX(att)]; + + int dx = x2 - x1; /* the horizontal distance of the line */ + int dy = y2 - y1; /* the vertical distance of the line */ + int dxabs = abs(dx); + int dyabs = abs(dy); + int sdx = sgn(dx); + int sdy = sgn(dy); + int x = dyabs >> 1; + int y = dxabs >> 1; + int px = x1; + int py = y1; + + if (dxabs >= dyabs) { + /* the line is more horizontal than vertical */ + for (int i = 0; i <= dxabs; i++) { + if ((1 << (px % 8)) & pat) { + drawPixel(px, py, color); + } + y += dyabs; + if (y >= dxabs) { + y -= dxabs; + py += sdy; + } + px += sdx; + } + } else { + /* the line is more vertical than horizontal */ + for (int i = 0; i <= dyabs; i++) { + if ((1 << (py % 8)) & pat) { + drawPixel(px, py, color); + } + x += dxabs; + if (x >= dyabs) { + x -= dyabs; + px += sdx; + } + py += sdy; + } + } +} + + void BitmapBuffer::drawRect(coord_t x, coord_t y, coord_t w, coord_t h, uint8_t thickness, uint8_t pat, LcdFlags att) { for (int i=0; idrawSolidFilledRect(x, y, thickness, h, att); diff --git a/radio/src/thirdparty/libopenui/src/bitmapbuffer.h b/radio/src/thirdparty/libopenui/src/bitmapbuffer.h index adfd435f4..e0243e56d 100755 --- a/radio/src/thirdparty/libopenui/src/bitmapbuffer.h +++ b/radio/src/thirdparty/libopenui/src/bitmapbuffer.h @@ -327,6 +327,8 @@ class BitmapBuffer: public BitmapBufferBase void drawVerticalLine(coord_t x, coord_t y, coord_t h, uint8_t pat = SOLID, LcdFlags flags = 0); + void drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t pat, LcdFlags att); + inline void drawSolidHorizontalLine(coord_t x, coord_t y, coord_t w, LcdFlags flags) { drawSolidFilledRect(x, y, w, 1, flags); diff --git a/radio/src/thirdparty/libopenui/src/libopenui_helpers.h b/radio/src/thirdparty/libopenui/src/libopenui_helpers.h index 640f05046..508dd1548 100644 --- a/radio/src/thirdparty/libopenui/src/libopenui_helpers.h +++ b/radio/src/thirdparty/libopenui/src/libopenui_helpers.h @@ -53,6 +53,12 @@ inline int divRoundClosest(const int n, const int d) return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d); } +template +inline T sgn(T a) +{ + return a > 0 ? 1 : (a < 0 ? -1 : 0); +} + // TODO duplicated code inline std::string TEXT_AT_INDEX(const char * val, uint8_t idx) {