[lua] Add Dialog:repaint() function

This commit is contained in:
David Capello 2022-12-14 21:17:51 -03:00
parent 2cb526a19b
commit 25682a44d8
3 changed files with 40 additions and 7 deletions

View File

@ -32,6 +32,20 @@ Canvas::Canvas() : ui::Widget(Type())
{
}
void Canvas::callPaint()
{
if (!m_surface)
return;
os::Paint p;
p.color(bgColor());
m_surface->drawRect(m_surface->bounds(), p);
// Draw only on resize (onPaint we draw the cached m_surface)
GraphicsContext gc(m_surface);
Paint(gc);
}
void Canvas::onInitTheme(ui::InitThemeEvent& ev)
{
Widget::onInitTheme(ev);
@ -56,13 +70,7 @@ void Canvas::onResize(ui::ResizeEvent& ev)
m_surface->height() != h) {
m_surface = os::instance()->makeSurface(w, h);
os::Paint p;
p.color(bgColor());
m_surface->drawRect(m_surface->bounds(), p);
// Draw only on resize (onPaint we draw the cached m_surface)
GraphicsContext gc(m_surface);
Paint(gc);
callPaint();
}
}
else

View File

@ -24,6 +24,8 @@ public:
Canvas();
void callPaint();
obs::signal<void(GraphicsContext&)> Paint;
private:

View File

@ -37,6 +37,7 @@
#include "ui/window.h"
#include <map>
#include <stack>
#include <string>
#include <vector>
@ -1115,6 +1116,27 @@ int Dialog_modify(lua_State* L)
return 1;
}
int Dialog_repaint(lua_State* L)
{
auto dlg = get_obj<Dialog>(L, 1);
std::stack<ui::Widget*> widgets;
widgets.push(&dlg->grid);
while (!widgets.empty()) {
auto child = widgets.top();
widgets.pop();
if (child->type() == Canvas::Type()) {
static_cast<Canvas*>(child)->callPaint();
child->invalidate();
}
for (auto subchild : child->children())
widgets.push(subchild);
}
return 0;
}
int Dialog_get_data(lua_State* L)
{
auto dlg = get_obj<Dialog>(L, 1);
@ -1333,6 +1355,7 @@ const luaL_Reg Dialog_methods[] = {
{ "file", Dialog_file },
{ "canvas", Dialog_canvas },
{ "modify", Dialog_modify },
{ "repaint", Dialog_repaint },
{ nullptr, nullptr }
};