LVGL drawing

This commit is contained in:
Raphael Coeffic
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent ac5a7bef0d
commit fe7d425ced
3 changed files with 49 additions and 55 deletions
+3 -38
View File
@@ -20,6 +20,8 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "keyboard_base.h" #include "keyboard_base.h"
#include "lvgl/lvgl.h"
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
#include "touch.h" #include "touch.h"
#endif #endif
@@ -28,12 +30,10 @@
TouchState touchState; TouchState touchState;
#endif #endif
static LvglWidgetFactory mainWindowFactory = { lv_obj_create, nullptr };
MainWindow * MainWindow::_instance = nullptr; MainWindow * MainWindow::_instance = nullptr;
MainWindow::MainWindow() : MainWindow::MainWindow() :
Window(nullptr, {0, 0, LCD_W, LCD_H}, 0, 0, &mainWindowFactory), Window(nullptr, {0, 0, LCD_W, LCD_H}),
invalidatedRect(rect) invalidatedRect(rect)
{ {
Layer::push(this); Layer::push(this);
@@ -93,37 +93,6 @@ void MainWindow::invalidate(const rect_t & rect)
invalidatedRect = rect; invalidatedRect = rect;
} }
} }
#include "lvgl/lvgl.h"
extern lv_obj_t * canvas;
extern BitmapBuffer canBuf;
bool MainWindow::refresh()
{
if (invalidatedRect.w) {
canBuf.setOffset(0, 0);
canBuf.setClippingRect(0,LCD_W,0, LCD_H);
fullPaint(&canBuf);
lv_obj_invalidate(canvas);
}
return false;
if (invalidatedRect.w) {
if (invalidatedRect.x > 0 || invalidatedRect.y > 0 || invalidatedRect.w < LCD_W || invalidatedRect.h < LCD_H) {
TRACE_WINDOWS("Refresh rect: left=%d top=%d width=%d height=%d", invalidatedRect.left(), invalidatedRect.top(), invalidatedRect.w, invalidatedRect.h);
lcdCopy(lcd->getData(), lcdFront->getData());
}
else {
TRACE_WINDOWS("Refresh full screen");
}
lcd->setOffset(0, 0);
lcd->setClippingRect(invalidatedRect.left(), invalidatedRect.right(), invalidatedRect.top(), invalidatedRect.bottom());
fullPaint(lcd);
invalidatedRect.w = 0;
return true;
}
else {
return false;
}
}
void MainWindow::run(bool trash) void MainWindow::run(bool trash)
{ {
@@ -138,10 +107,6 @@ void MainWindow::run(bool trash)
emptyTrash(); emptyTrash();
} }
if (refresh()) {
lcdRefresh();
}
auto delta = ticksNow() - start; auto delta = ticksNow() - start;
if (delta > 10 * SYSTEM_TICKS_1MS) { if (delta > 10 * SYSTEM_TICKS_1MS) {
TRACE_WINDOWS("MainWindow::run took %dms", (ticksNow() - start) / SYSTEM_TICKS_1MS); TRACE_WINDOWS("MainWindow::run took %dms", (ticksNow() - start) / SYSTEM_TICKS_1MS);
+42 -13
View File
@@ -37,14 +37,39 @@ static void window_event_cb(lv_event_t * e)
Window* window = (Window *)lv_obj_get_user_data(target); Window* window = (Window *)lv_obj_get_user_data(target);
if (!window) return; if (!window) return;
if(window->deleted()) return;
if(code == LV_EVENT_DELETE ||
window->deleted()) return;
if (code == LV_EVENT_GET_SELF_SIZE) { if (code == LV_EVENT_GET_SELF_SIZE) {
lv_point_t *p = (lv_point_t *)lv_event_get_param(e); lv_point_t *p = (lv_point_t *)lv_event_get_param(e);
if (p->x >= 0) { p->x = window->getInnerWidth(); } if (p->x >= 0) { p->x = window->getInnerWidth(); }
if (p->y >= 0) { p->y = window->getInnerHeight(); } if (p->y >= 0) { p->y = window->getInnerHeight(); }
} else if (code == LV_EVENT_DRAW_MAIN) { } else if (code == LV_EVENT_DRAW_MAIN) {
lv_draw_ctx_t* draw_ctx = lv_event_get_draw_ctx(e);
lv_area_t a, clipping, obj_coords;
lv_area_copy(&a, draw_ctx->buf_area);
lv_area_copy(&clipping, draw_ctx->clip_area);
lv_obj_get_coords(target, &obj_coords);
auto w = a.x2 - a.x1 + 1;
auto h = a.y2 - a.y1 + 1;
TRACE_WINDOWS("DRAW_MAIN %s", window->getWindowDebugString().c_str()); TRACE_WINDOWS("DRAW_MAIN %s", window->getWindowDebugString().c_str());
BitmapBuffer buf = {BMP_RGB565, (uint16_t)w, (uint16_t)h,
(uint16_t *)draw_ctx->buf};
buf.setOffset(obj_coords.x1 - a.x1, obj_coords.y1 - a.y1);
buf.setClippingRect(clipping.x1 - a.x1, clipping.x2 + 1 - a.x1,
clipping.y1 - a.y1, clipping.y2 + 1 - a.y1);
window->paint(&buf);
} else if (code == LV_EVENT_PRESSED || code == LV_EVENT_RELEASED) { } else if (code == LV_EVENT_PRESSED || code == LV_EVENT_RELEASED) {
TRACE_WINDOWS("PRESSED: %s", window->getWindowDebugString().c_str()); TRACE_WINDOWS("PRESSED: %s", window->getWindowDebugString().c_str());
@@ -86,12 +111,16 @@ static void window_event_cb(lv_event_t * e)
window->onTouchEnd(rel_pos.x, rel_pos.y); window->onTouchEnd(rel_pos.x, rel_pos.y);
} }
} else if (code == LV_EVENT_SCROLL) { } else if (code == LV_EVENT_SCROLL) {
lv_coord_t scroll_y = lv_obj_get_scroll_y(target); lv_coord_t scroll_y = lv_obj_get_scroll_y(target);
lv_coord_t scroll_x = lv_obj_get_scroll_x(target); lv_coord_t scroll_x = lv_obj_get_scroll_x(target);
TRACE_WINDOWS("SCROLL[x=%d;y=%d] %s", scroll_x, scroll_y, TRACE_WINDOWS("SCROLL[x=%d;y=%d] %s", scroll_x, scroll_y,
window->getWindowDebugString().c_str()); window->getWindowDebugString().c_str());
window->setScrollPositionY(scroll_y); window->setScrollPositionY(scroll_y);
window->setScrollPositionX(scroll_x); window->setScrollPositionX(scroll_x);
} else if (code == LV_EVENT_SCROLL_BEGIN) { } else if (code == LV_EVENT_SCROLL_BEGIN) {
TRACE("SCROLL_BEGIN"); TRACE("SCROLL_BEGIN");
is_scrolling = true; is_scrolling = true;
@@ -138,6 +167,10 @@ Window::Window(Window *parent, const rect_t &rect, WindowFlags windowFlags,
lv_obj_set_size(lvobj, rect.w, rect.h); lv_obj_set_size(lvobj, rect.w, rect.h);
lv_obj_set_user_data(lvobj, this); lv_obj_set_user_data(lvobj, this);
if (windowFlags & OPAQUE) {
lv_obj_set_style_bg_opa(lvobj, LV_OPA_MAX, LV_PART_MAIN);
}
lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF); lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF);
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLL_ELASTIC); lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_SCROLL_ELASTIC);
lv_obj_add_event_cb(lvobj, window_event_cb, LV_EVENT_ALL, this); lv_obj_add_event_cb(lvobj, window_event_cb, LV_EVENT_ALL, this);
@@ -154,7 +187,7 @@ Window::Window(Window *parent, const rect_t &rect, WindowFlags windowFlags,
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE); lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
} }
} }
extern lv_obj_t * canvas;
Window::~Window() Window::~Window()
{ {
TRACE_WINDOWS("Destroy %p %s", this, getWindowDebugString().c_str()); TRACE_WINDOWS("Destroy %p %s", this, getWindowDebugString().c_str());
@@ -162,22 +195,19 @@ Window::~Window()
focusWindow = nullptr; focusWindow = nullptr;
} }
if(children.size() > 0) if (children.size() > 0) deleteChildren();
deleteChildren();
if (lvobj != nullptr && lvobj != canvas) if (lvobj != nullptr) {
{
lv_obj_del(lvobj); lv_obj_del(lvobj);
lvobj = nullptr; lvobj = nullptr;
} }
} }
void Window::attach(Window * newParent) void Window::attach(Window *newParent)
{ {
if (parent) detach(); if (parent) detach();
parent = newParent; parent = newParent;
if (newParent) if (newParent) {
{
newParent->addChild(this); newParent->addChild(this);
} }
} }
@@ -187,6 +217,7 @@ void Window::detach()
if (parent) { if (parent) {
parent->removeChild(this); parent->removeChild(this);
parent = nullptr; parent = nullptr;
// if (lvobj != nullptr) lv_obj_set_parent(lvobj, nullptr);
} }
} }
@@ -613,9 +644,7 @@ void Window::moveWindowsTop(coord_t y, coord_t delta)
void Window::invalidate(const rect_t & rect) void Window::invalidate(const rect_t & rect)
{ {
if (isVisible()) { if (lvobj) lv_obj_invalidate(lvobj);
parent->invalidate({this->rect.x + rect.x - parent->scrollPositionX, this->rect.y + rect.y - parent->scrollPositionY, rect.w, rect.h});
}
} }
void Window::drawVerticalScrollbar(BitmapBuffer * dc) void Window::drawVerticalScrollbar(BitmapBuffer * dc)
+4 -4
View File
@@ -405,6 +405,10 @@ class Window
return _deleted; return _deleted;
} }
virtual void paint(BitmapBuffer *)
{
}
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
virtual bool onTouchStart(coord_t x, coord_t y); virtual bool onTouchStart(coord_t x, coord_t y);
virtual bool onTouchEnd(coord_t x, coord_t y); virtual bool onTouchEnd(coord_t x, coord_t y);
@@ -463,10 +467,6 @@ class Window
void fullPaint(BitmapBuffer * dc); void fullPaint(BitmapBuffer * dc);
virtual void paint(BitmapBuffer *)
{
}
virtual void onFocusLost() virtual void onFocusLost()
{ {
TRACE_WINDOWS("%s onFocusLost()", getWindowDebugString().c_str()); TRACE_WINDOWS("%s onFocusLost()", getWindowDebugString().c_str());