mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-04 19:14:05 +00:00
Add activeSprite global
This commit is contained in:
parent
6dfbb05d58
commit
ffdf8ad568
@ -34,6 +34,7 @@ AppScripting::AppScripting(scripting::EngineDelegate* delegate)
|
||||
registerFunction("rgbaB", rgbaB, 1);
|
||||
registerFunction("rgbaA", rgbaA, 1);
|
||||
registerClass("Sprite", Sprite_ctor, 3, Sprite_methods, Sprite_props);
|
||||
registerGlobal("activeSprite", activeSprite_getter, activeSprite_setter);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -107,6 +107,26 @@ scripting::result_t Sprite_get_height(scripting::ContextHandle handle)
|
||||
return 1;
|
||||
}
|
||||
|
||||
scripting::result_t activeSprite_getter(scripting::ContextHandle handle)
|
||||
{
|
||||
scripting::Context ctx(handle);
|
||||
app::Document* doc = UIContext::instance()->activeDocument();
|
||||
if (doc)
|
||||
ctx.pushObject(doc, "Sprite");
|
||||
else
|
||||
ctx.pushNull();
|
||||
return 1;
|
||||
}
|
||||
|
||||
scripting::result_t activeSprite_setter(scripting::ContextHandle handle)
|
||||
{
|
||||
scripting::Context ctx(handle);
|
||||
Document* doc = (Document*)ctx.requireObject(0, "Sprite");
|
||||
if (doc)
|
||||
UIContext::instance()->setActiveDocument(doc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const scripting::FunctionEntry Sprite_methods[] = {
|
||||
{ "putPixel", Sprite_putPixel, 3 },
|
||||
{ "getPixel", Sprite_getPixel, 2 },
|
||||
|
@ -127,6 +127,15 @@ const char* Context::requireString(index_t i)
|
||||
return duk_require_string(m_handle, i);
|
||||
}
|
||||
|
||||
void* Context::requireObject(index_t i, const char* className)
|
||||
{
|
||||
duk_get_prop_string(m_handle, i, "\xFF" "\xFF" "ptr");
|
||||
void* result = (void*)duk_to_pointer(m_handle, -1);
|
||||
// TODO check pointer type
|
||||
duk_pop(m_handle);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Context::pushUndefined()
|
||||
{
|
||||
duk_push_undefined(m_handle);
|
||||
@ -174,6 +183,18 @@ void Context::pushThis(void* ptr)
|
||||
duk_put_prop_string(m_handle, -2, "\xFF" "\xFF" "ptr");
|
||||
}
|
||||
|
||||
void Context::pushObject(void* ptr, const char* className)
|
||||
{
|
||||
duk_push_object(m_handle);
|
||||
duk_push_pointer(m_handle, ptr);
|
||||
duk_put_prop_string(m_handle, -2, "\xFF" "\xFF" "ptr");
|
||||
|
||||
duk_get_global_string(m_handle, className);
|
||||
duk_get_prop_string(m_handle, -1, "prototype");
|
||||
duk_set_prototype(m_handle, -3);
|
||||
duk_pop(m_handle);
|
||||
}
|
||||
|
||||
void* Context::getThis()
|
||||
{
|
||||
duk_push_this(m_handle);
|
||||
@ -301,4 +322,30 @@ void Engine::registerClass(const char* id,
|
||||
duk_put_global_string(handle, id);
|
||||
}
|
||||
|
||||
void Engine::registerGlobal(const char* id,
|
||||
Function getter, Function setter)
|
||||
{
|
||||
ContextHandle handle = m_ctx.handle();
|
||||
|
||||
duk_push_global_object(handle);
|
||||
duk_push_string(handle, id);
|
||||
|
||||
duk_idx_t objidx = -2;
|
||||
duk_uint_t flags = 0;
|
||||
|
||||
if (getter) {
|
||||
flags |= DUK_DEFPROP_HAVE_GETTER;
|
||||
duk_push_c_function(handle, getter, 0);
|
||||
--objidx;
|
||||
}
|
||||
|
||||
if (setter) {
|
||||
flags |= DUK_DEFPROP_HAVE_SETTER;
|
||||
duk_push_c_function(handle, setter, 1);
|
||||
--objidx;
|
||||
}
|
||||
|
||||
duk_def_prop(handle, objidx, flags);
|
||||
}
|
||||
|
||||
} // namespace scripting
|
||||
|
@ -46,6 +46,7 @@ namespace scripting {
|
||||
int requireInt(index_t i);
|
||||
unsigned int requireUInt(index_t i);
|
||||
const char* requireString(index_t i);
|
||||
void* requireObject(index_t i, const char* className);
|
||||
|
||||
void pushUndefined();
|
||||
void pushNull();
|
||||
@ -56,6 +57,7 @@ namespace scripting {
|
||||
void pushUInt(unsigned int val);
|
||||
void pushString(const char* str);
|
||||
void pushThis(void* ptr);
|
||||
void pushObject(void* ptr, const char* className);
|
||||
|
||||
private:
|
||||
ContextHandle m_handle;
|
||||
@ -88,6 +90,8 @@ namespace scripting {
|
||||
Function ctorFunc, int ctorNargs,
|
||||
const FunctionEntry* methods,
|
||||
const PropertyEntry* props);
|
||||
void registerGlobal(const char* id,
|
||||
Function getter, Function setter);
|
||||
|
||||
private:
|
||||
Context m_ctx;
|
||||
|
Loading…
x
Reference in New Issue
Block a user