script: add Sprite.colorMode property and ColorMode constants

This commit is contained in:
David Capello 2016-05-07 19:44:11 -03:00
parent e03f34688e
commit 31116ff424
4 changed files with 40 additions and 0 deletions

View File

@ -22,6 +22,17 @@
namespace app {
namespace {
const script::ConstantEntry ColorMode_constants[] = {
{ "RGB", double(IMAGE_RGB) },
{ "GRAYSCALE", double(IMAGE_GRAYSCALE) },
{ "INDEXED", double(IMAGE_INDEXED) },
{ nullptr, 0.0 }
};
}
AppScripting::AppScripting(script::EngineDelegate* delegate)
: script::Engine(delegate)
{
@ -30,6 +41,13 @@ AppScripting::AppScripting(script::EngineDelegate* delegate)
register_console_object(ctx);
ctx.pushGlobalObject();
{
script::index_t obj = ctx.pushObject();
ctx.registerConstants(obj, ColorMode_constants);
ctx.setProp(-2, "ColorMode");
}
register_image_class(-1, ctx);
register_sprite_class(-1, ctx);
register_selection_class(-1, ctx);

View File

@ -208,6 +208,14 @@ script::result_t Sprite_get_height(script::ContextHandle handle)
return 1;
}
script::result_t Sprite_get_colorMode(script::ContextHandle handle)
{
script::Context ctx(handle);
auto wrap = (SpriteWrap*)ctx.getThis();
ctx.pushInt(wrap->sprite()->pixelFormat());
return 1;
}
script::result_t Sprite_set_height(script::ContextHandle handle)
{
script::Context ctx(handle);
@ -243,6 +251,7 @@ const script::PropertyEntry Sprite_props[] = {
{ "filename", Sprite_get_filename, nullptr },
{ "width", Sprite_get_width, Sprite_set_width },
{ "height", Sprite_get_height, Sprite_set_height },
{ "colorMode", Sprite_get_colorMode, nullptr },
{ "selection", Sprite_get_selection, nullptr },
{ nullptr, nullptr, 0 }
};

View File

@ -327,6 +327,12 @@ void Context::pushGlobalObject()
duk_push_global_object(m_handle);
}
void Context::registerConstants(index_t idx,
const ConstantEntry* consts)
{
duk_put_number_list(m_handle, idx, (const duk_number_list_entry*)consts);
}
void Context::registerProp(index_t idx,
const char* id,
Function getter,

View File

@ -35,6 +35,11 @@ namespace script {
Function setter;
};
struct ConstantEntry {
const char* id;
double value;
};
class Module {
public:
virtual ~Module() { }
@ -100,6 +105,8 @@ namespace script {
index_t pushObject(void* ptr, const char* className);
void pushGlobalObject();
void registerConstants(index_t idx,
const ConstantEntry* consts);
void registerProp(index_t idx,
const char* id,
Function getter,