lua: Add a way to set/get the default app palette (and well-known palettes from resources)

This commit is contained in:
David Capello 2019-04-24 19:08:25 -03:00
parent 5cf762c9cf
commit ad1a0af752
3 changed files with 68 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include "app/doc_access.h"
#include "app/i18n/strings.h"
#include "app/loop_tag.h"
#include "app/modules/palettes.h"
#include "app/pref/preferences.h"
#include "app/script/api_version.h"
#include "app/script/docobj.h"
@ -515,6 +516,16 @@ int App_get_activeBrush(lua_State* L)
return 1;
}
int App_get_defaultPalette(lua_State* L)
{
const Palette* pal = get_default_palette();
if (pal)
push_palette(L, new Palette(*pal));
else
lua_pushnil(L);
return 1;
}
int App_set_activeSprite(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 2);
@ -580,6 +591,13 @@ int App_set_activeBrush(lua_State* L)
return 0;
}
int App_set_defaultPalette(lua_State* L)
{
if (const doc::Palette* pal = get_palette_from_arg(L, 2))
set_default_palette(pal);
return 0;
}
const luaL_Reg App_methods[] = {
{ "open", App_open },
{ "exit", App_exit },
@ -609,6 +627,7 @@ const Property App_properties[] = {
{ "site", App_get_site, nullptr },
{ "range", App_get_range, nullptr },
{ "isUIAvailable", App_get_isUIAvailable, nullptr },
{ "defaultPalette", App_get_defaultPalette, App_set_defaultPalette },
{ nullptr, nullptr, nullptr }
};

View File

@ -123,6 +123,7 @@ namespace app {
void push_image(lua_State* L, doc::Image* image);
void push_images(lua_State* L, const doc::ObjectIds& images);
void push_layers(lua_State* L, const doc::ObjectIds& layers);
void push_palette(lua_State* L, doc::Palette* palette);
void push_sprite_cel(lua_State* L, doc::Cel* cel);
void push_sprite_frame(lua_State* L, doc::Sprite* sprite, doc::frame_t frame);
void push_sprite_frames(lua_State* L, doc::Sprite* sprite);

View File

@ -12,6 +12,7 @@
#include "app/cmd/set_palette.h"
#include "app/color.h"
#include "app/file/palette_file.h"
#include "app/res/palettes_loader_delegate.h"
#include "app/script/docobj.h"
#include "app/script/engine.h"
#include "app/script/luacpp.h"
@ -88,6 +89,39 @@ int Palette_new(lua_State* L)
}
}
lua_pop(L, 1);
// Palette{ fromResource }
type = lua_getfield(L, 1, "fromResource");
if (type != LUA_TNIL) {
if (const char* _id = lua_tostring(L, -1)) {
std::string id = _id;
lua_pop(L, 1);
// TODO Improve this, maybe using an existent set of palettes
// TODO Centralize the resources management/loading process on-demand (#2059)
std::map<std::string, std::string> idAndPaths;
PalettesLoaderDelegate().getResourcesPaths(idAndPaths);
if (!idAndPaths[id].empty()) {
std::string absFn = base::get_absolute_path(idAndPaths[id]);
if (!ask_access(L, absFn.c_str(), FileAccessMode::Read, true))
return luaL_error(L, "script doesn't have access to open file %s",
absFn.c_str());
Palette* pal = load_palette(absFn.c_str());
if (pal)
push_new<PaletteObj>(L, nullptr, pal);
else
lua_pushnil(L);
return 1;
}
else {
return luaL_error(L, "palette resource with ID %s not found", _id);
}
}
}
lua_pop(L, 1);
}
else {
int ncolors = lua_tointeger(L, 1);
@ -113,6 +147,14 @@ int Palette_len(lua_State* L)
return 1;
}
int Palette_eq(lua_State* L)
{
const auto a = get_obj<PaletteObj>(L, 1);
const auto b = get_obj<PaletteObj>(L, 2);
lua_pushboolean(L, *a->palette(L) == *b->palette(L));
return 1;
}
int Palette_resize(lua_State* L)
{
auto obj = get_obj<PaletteObj>(L, 1);
@ -216,6 +258,7 @@ int Palette_get_frameNumber(lua_State* L)
const luaL_Reg Palette_methods[] = {
{ "__gc", Palette_gc },
{ "__len", Palette_len },
{ "__eq", Palette_eq },
{ "resize", Palette_resize },
{ "getColor", Palette_getColor },
{ "setColor", Palette_setColor },
@ -248,6 +291,11 @@ void push_sprite_palette(lua_State* L, doc::Sprite* sprite, doc::Palette* palett
push_new<PaletteObj>(L, sprite, palette);
}
void push_palette(lua_State* L, doc::Palette* palette)
{
push_new<PaletteObj>(L, nullptr, palette);
}
doc::Palette* get_palette_from_arg(lua_State* L, int index)
{
auto obj = get_obj<PaletteObj>(L, index);