From 165cd3bdf00a555ecb9652e6fe6fba6eb95a4409 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 23 Nov 2018 16:08:49 -0300 Subject: [PATCH] lua: Use active cel info in Range when there is no timeline range enabled With this change we can always use app.range collections (e.g. app.range.editableImages) for our filters in the scripts. --- src/app/script/app_object.cpp | 2 +- src/app/script/engine.h | 2 +- src/app/script/range_class.cpp | 40 +++++++++++++++++++++------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/app/script/app_object.cpp b/src/app/script/app_object.cpp index cd13fe231..532b45e19 100644 --- a/src/app/script/app_object.cpp +++ b/src/app/script/app_object.cpp @@ -301,7 +301,7 @@ int App_get_range(lua_State* L) app::Context* ctx = App::instance()->context(); Site site = ctx->activeSite(); if (site.sprite() && App::instance()->timeline()) { - push_doc_range(L, site.sprite(), App::instance()->timeline()->range()); + push_doc_range(L, site, App::instance()->timeline()->range()); } else { lua_pushnil(L); diff --git a/src/app/script/engine.h b/src/app/script/engine.h index 1c77af9e4..0c9ea106b 100644 --- a/src/app/script/engine.h +++ b/src/app/script/engine.h @@ -101,7 +101,7 @@ namespace app { void push_cels(lua_State* L, const doc::ObjectIds& cels); void push_cels(lua_State* L, doc::Layer* layer); void push_cels(lua_State* L, doc::Sprite* sprite); - void push_doc_range(lua_State* L, doc::Sprite* sprite, const DocRange& docRange); + void push_doc_range(lua_State* L, Site& site, const DocRange& docRange); void push_images(lua_State* L, const doc::ObjectIds& images); void push_layers(lua_State* L, const doc::ObjectIds& layers); void push_sprite_cel(lua_State* L, doc::Cel* cel); diff --git a/src/app/script/range_class.cpp b/src/app/script/range_class.cpp index 587c5a312..e1405b40f 100644 --- a/src/app/script/range_class.cpp +++ b/src/app/script/range_class.cpp @@ -12,6 +12,7 @@ #include "app/script/docobj.h" #include "app/script/engine.h" #include "app/script/luacpp.h" +#include "app/site.h" #include "app/util/range_utils.h" #include "doc/cel.h" #include "doc/layer.h" @@ -34,21 +35,30 @@ struct RangeObj { // This is like DocRange but referencing objects with IDs std::vector frames; std::set cels; - RangeObj(Sprite* sprite, const DocRange& docRange) { - spriteId = sprite->id(); + RangeObj(Site& site, const DocRange& docRange) { + spriteId = site.sprite()->id(); type = docRange.type(); - for (const Layer* layer : docRange.selectedLayers()) - layers.insert(layer->id()); - for (const frame_t frame : docRange.selectedFrames()) - frames.push_back(frame); - // TODO improve this, in the best case we should defer layers, - // frames, and cels vectors when the properties are accessed, but - // it might not be possible because we have to save the IDs of the - // objects (and we cannot store the DocRange because it contains - // pointers instead of IDs). - for (Cel* cel : get_cels(sprite, docRange)) - cels.insert(cel->id()); + if (docRange.enabled()) { + for (const Layer* layer : docRange.selectedLayers()) + layers.insert(layer->id()); + for (const frame_t frame : docRange.selectedFrames()) + frames.push_back(frame); + + // TODO improve this, in the best case we should defer layers, + // frames, and cels vectors when the properties are accessed, but + // it might not be possible because we have to save the IDs of the + // objects (and we cannot store the DocRange because it contains + // pointers instead of IDs). + for (Cel* cel : get_cels(site.sprite(), docRange)) + cels.insert(cel->id()); + } + else { + // Put the active frame/layer/cel information in the range + frames.push_back(site.frame()); + if (site.layer()) layers.insert(site.layer()->id()); + if (site.cel()) cels.insert(site.cel()->id()); + } } RangeObj(const RangeObj&) = delete; RangeObj& operator=(const RangeObj&) = delete; @@ -200,9 +210,9 @@ void register_range_class(lua_State* L) REG_CLASS_PROPERTIES(L, Range); } -void push_doc_range(lua_State* L, Sprite* sprite, const DocRange& docRange) +void push_doc_range(lua_State* L, Site& site, const DocRange& docRange) { - push_new(L, sprite, docRange); + push_new(L, site, docRange); } } // namespace script