[lua] Add GraphicsContext.palette property to set the palette used to draw indexed images

This commit is contained in:
Martín Capello 2023-09-19 14:50:15 -03:00 committed by David Capello
parent 54116a9550
commit d777a9a325
2 changed files with 34 additions and 5 deletions

View File

@ -46,7 +46,7 @@ void GraphicsContext::drawImage(const doc::Image* img, int x, int y)
if (m_paint.blendMode() == os::BlendMode::Src) {
convert_image_to_surface(
img,
get_current_palette(),
m_palette ? m_palette : get_current_palette(),
m_surface.get(),
0, 0,
x, y,
@ -75,7 +75,7 @@ void GraphicsContext::drawImage(const doc::Image* img,
if (tmpSurface) {
convert_image_to_surface(
img,
get_current_palette(),
m_palette ? m_palette : get_current_palette(),
tmpSurface.get(),
srcRc.x, srcRc.y,
0, 0,
@ -458,6 +458,21 @@ int GraphicsContext_set_opacity(lua_State* L)
return 0;
}
int GraphicsContext_get_palette(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
push_palette(L, gc->palette());
return 1;
}
int GraphicsContext_set_palette(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
doc::Palette* palette = get_palette_from_arg(L, 2);
gc->palette(palette);
return 1;
}
const luaL_Reg GraphicsContext_methods[] = {
{ "__gc", GraphicsContext_gc },
{ "save", GraphicsContext_save },
@ -492,6 +507,7 @@ const Property GraphicsContext_properties[] = {
{ "strokeWidth", GraphicsContext_get_strokeWidth, GraphicsContext_set_strokeWidth },
{ "blendMode", GraphicsContext_get_blendMode, GraphicsContext_set_blendMode },
{ "opacity", GraphicsContext_get_opacity, GraphicsContext_set_opacity },
{ "palette", GraphicsContext_get_palette, GraphicsContext_set_palette },
{ nullptr, nullptr, nullptr }
};

View File

@ -10,6 +10,7 @@
#ifdef ENABLE_UI
#include "doc/palette.h"
#include "gfx/path.h"
#include "os/font.h"
#include "os/paint.h"
@ -25,6 +26,12 @@ namespace app {
namespace script {
class GraphicsContext {
private:
struct State {
os::Paint paint;
doc::Palette* palette = nullptr;
};
public:
GraphicsContext(const os::SurfaceRef& surface, int uiscale) : m_surface(surface), m_uiscale(uiscale) { }
GraphicsContext(GraphicsContext&& gc) {
@ -38,17 +45,22 @@ public:
os::FontRef font() const { return m_font; }
void font(const os::FontRef& font) { m_font = font; }
doc::Palette* palette() const { return m_palette; }
void palette(doc::Palette* palette) { m_palette = palette; }
int width() const { return m_surface->width(); }
int height() const { return m_surface->height(); }
void save() {
m_saved.push(m_paint);
m_saved.push(State{m_paint, m_palette});
m_surface->save();
}
void restore() {
if (!m_saved.empty()) {
m_paint = m_saved.top();
auto state = m_saved.top();
m_paint = state.paint;
m_palette = state.palette;
m_saved.pop();
m_surface->restore();
}
@ -130,7 +142,8 @@ private:
os::Paint m_paint;
os::FontRef m_font;
gfx::Path m_path;
std::stack<os::Paint> m_saved;
std::stack<State> m_saved;
doc::Palette* m_palette = nullptr;
};
} // namespace script