lua: Add support for selection tools in app.useTool()

Fix https://github.com/aseprite/api/issues/37
This commit is contained in:
David Capello 2021-04-22 13:00:13 -03:00
parent 32b5d20a8d
commit d7a1c71df0
6 changed files with 52 additions and 7 deletions

View File

@ -73,6 +73,7 @@
</enum>
<enum id="SelectionMode">
<value id="DEFAULT" value="0" />
<value id="REPLACE" value="0" />
<value id="ADD" value="1" />
<value id="SUBTRACT" value="2" />
<value id="INTERSECT" value="3" />

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
@ -10,6 +10,6 @@
// Increment this value if the scripting API is modified between two
// released Aseprite versions.
#define API_VERSION 13
#define API_VERSION 14
#endif

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2015-2018 David Capello
//
// This program is distributed under the terms of
@ -26,6 +26,7 @@
#include "app/script/security.h"
#include "app/site.h"
#include "app/tools/active_tool.h"
#include "app/tools/ink.h"
#include "app/tools/tool_box.h"
#include "app/tools/tool_loop.h"
#include "app/tools/tool_loop_manager.h"
@ -388,6 +389,30 @@ int App_useTool(lua_State* L)
params.freehandAlgorithm = get_value_from_lua<tools::FreehandAlgorithm>(L, -1);
lua_pop(L, 1);
if (params.ink->isSelection()) {
gen::SelectionMode selectionMode = Preferences::instance().selection.mode();
type = lua_getfield(L, 1, "selection");
if (type != LUA_TNIL)
selectionMode = get_value_from_lua<gen::SelectionMode>(L, -1);
lua_pop(L, 1);
switch (selectionMode) {
case gen::SelectionMode::REPLACE:
params.modifiers = tools::ToolLoopModifiers::kReplaceSelection;
break;
case gen::SelectionMode::ADD:
params.modifiers = tools::ToolLoopModifiers::kAddSelection;
break;
case gen::SelectionMode::SUBTRACT:
params.modifiers = tools::ToolLoopModifiers::kSubtractSelection;
break;
case gen::SelectionMode::INTERSECT:
params.modifiers = tools::ToolLoopModifiers::kIntersectSelection;
break;
}
}
// Do the tool loop
type = lua_getfield(L, 1, "points");
if (type == LUA_TTABLE) {

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -15,6 +15,7 @@
#include "app/console.h"
#include "app/doc_exporter.h"
#include "app/doc_range.h"
#include "app/pref/preferences.h"
#include "app/script/luacpp.h"
#include "app/script/security.h"
#include "app/sprite_sheet_type.h"
@ -367,6 +368,15 @@ Engine::Engine()
setfield_integer(L, "X2", (int)ui::kButtonX2);
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "SelectionMode");
setfield_integer(L, "REPLACE", (int)gen::SelectionMode::REPLACE);
setfield_integer(L, "ADD", (int)gen::SelectionMode::ADD);
setfield_integer(L, "SUBTRACT", (int)gen::SelectionMode::SUBTRACT);
setfield_integer(L, "INTERSECT", (int)gen::SelectionMode::INTERSECT);
lua_pop(L, 1);
// Register classes/prototypes
register_brush_class(L);
register_cel_class(L);

View File

@ -121,6 +121,9 @@ protected:
doc::color_t m_secondaryColor;
tools::DynamicsOptions m_dynamics;
// Modifiers that can be used with scripts
tools::ToolLoopModifiers m_staticToolModifiers;
public:
ToolLoopBase(Editor* editor, Site site,
ToolLoopParams& params)
@ -157,6 +160,7 @@ public:
, m_bgColor(color_utils::color_for_target_mask(params.bg, m_colorTarget))
, m_primaryColor(m_button == tools::ToolLoop::Left ? m_fgColor: m_bgColor)
, m_secondaryColor(m_button == tools::ToolLoop::Left ? m_bgColor: m_fgColor)
, m_staticToolModifiers(params.modifiers)
{
ASSERT(m_tool);
ASSERT(m_ink);
@ -266,8 +270,10 @@ public:
int getTolerance() override { return m_tolerance; }
bool getContiguous() override { return m_contiguous; }
tools::ToolLoopModifiers getModifiers() override {
return m_editor ? m_editor->getToolLoopModifiers():
tools::ToolLoopModifiers::kNone;
return
(m_staticToolModifiers == tools::ToolLoopModifiers::kNone &&
m_editor ? m_editor->getToolLoopModifiers():
m_staticToolModifiers);
}
filters::TiledMode getTiledMode() override { return m_docPref.tiled.mode(); }
bool getGridVisible() override { return m_docPref.show.grid(); }

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
@ -46,6 +46,9 @@ namespace app {
int tolerance = 0;
bool contiguous = true;
tools::FreehandAlgorithm freehandAlgorithm = tools::FreehandAlgorithm::DEFAULT;
// For selection tools executed from scripts
tools::ToolLoopModifiers modifiers = tools::ToolLoopModifiers::kNone;
};
//////////////////////////////////////////////////////////////////////