From 3fe8f59f57f0dfb46dff683ea595e291b7ff802d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20L=C3=B6tscher?= <40485433+rolandlo@users.noreply.github.com> Date: Mon, 21 Sep 2020 19:18:03 +0200 Subject: [PATCH] Plugin for cycling through a color list Plugin for cycling through a color list --- plugins/ColorCycle/main.lua | 38 +++++++++++++++++ plugins/ColorCycle/plugin.ini | 15 +++++++ src/plugin/luapi_application.h | 77 +++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 plugins/ColorCycle/main.lua create mode 100644 plugins/ColorCycle/plugin.ini diff --git a/plugins/ColorCycle/main.lua b/plugins/ColorCycle/main.lua new file mode 100644 index 00000000..82cbb7ff --- /dev/null +++ b/plugins/ColorCycle/main.lua @@ -0,0 +1,38 @@ +-- Register all Toolbar actions and intialize all UI stuff +function initUi() + app.registerUi({["menu"] = "Cycle through color list", ["callback"] = "cycle", ["accelerator"] = "c"}); + -- if you want to have multiple color lists you must use the app.registerUi function multiple times + -- with different callback functions and accelerators +end + +-- Predefined colors copied from LoadHandlerHelper.cpp +-- modify to your needs +local colorList = { + {"black", 0x000000}, + {"green", 0x008000}, + {"lightblue", 0x00c0ff}, + {"lightgreen", 0x00ff00}, + {"blue", 0x3333cc}, + {"gray", 0x808080}, + {"red", 0xff0000}, + {"magenta", 0xff00ff}, + {"orange", 0xff8000}, + {"yellow", 0xffff00}, + {"white", 0xffffff} +} + +-- start with first color +local currentColor = 0 + +function cycle() + if (currentColor < #colorList) then + currentColor = currentColor + 1 + else + currentColor = 1 + end + -- apply color to currently used tool and allow coloring of elements from selections + app.changeToolColor({["color"] = colorList[currentColor][2], ["selection"] = true}) + -- use app.changeColor({["color"] = colorList[currentColor][2], ["tool"] = "pen""}) + -- instead if you only want to change pen color + -- similarly if you want to change hilighter color or the color from another tool with color capabilities. +end diff --git a/plugins/ColorCycle/plugin.ini b/plugins/ColorCycle/plugin.ini new file mode 100644 index 00000000..340c3867 --- /dev/null +++ b/plugins/ColorCycle/plugin.ini @@ -0,0 +1,15 @@ +[about] +## Author / Copyright notice +author=Roland Lötscher + +description=Cycling through color list. + +## If the plugin is packed with Xournal++, use +## then it gets the same version number +version= + +[default] +enabled=false + +[plugin] +mainfile=main.lua diff --git a/src/plugin/luapi_application.h b/src/plugin/luapi_application.h index 19367a71..ffb386bf 100644 --- a/src/plugin/luapi_application.h +++ b/src/plugin/luapi_application.h @@ -14,6 +14,7 @@ #include "control/Control.h" #include "control/PageBackgroundChangeController.h" +#include "control/Tool.h" #include "control/pagetype/PageTypeHandler.h" #include "StringUtils.h" @@ -218,7 +219,7 @@ static int applib_uiActionSelected(lua_State* L) { } /** - * Select UI action + * Change page background */ static int applib_changeCurrentPageBackground(lua_State* L) { PageType pt; @@ -233,6 +234,79 @@ static int applib_changeCurrentPageBackground(lua_State* L) { return 1; } +/** + * Change color of a specified tool or of the current tool + */ +static int applib_changeToolColor(lua_State* L) { + + // discard any extra arguments passed in + lua_settop(L, 1); + luaL_checktype(L, 1, LUA_TTABLE); + + lua_getfield(L, 1, "selection"); /* either true or false, for changing selection color + defaults to false*/ + lua_getfield(L, 1, "tool"); /* "pen", "hilighter", "text" + "select_rect", "select_object", "select_region" + if omitted, current Tool is used */ + lua_getfield(L, 1, "color"); // an RGB hex code defining the color + // stack now has following: + // 1 = {["color"] = 0xff00ff, ["tool"] = "PEN", ["selection"] = false} + // -3 = false + // -2 = "pen" + // -1 = 0xff0077 + + Plugin* plugin = Plugin::getPluginFromLua(L); + Control* ctrl = plugin->getControl(); + ToolHandler* toolHandler = ctrl->getToolHandler(); + + bool selection = false; + if (lua_isboolean(L, -3)) { + selection = lua_toboolean(L, -3); + } else if (!lua_isnil(L, -3)) { + g_warning("" + "selection" + " key should be a boolean value (or nil)"); + } + + ToolType toolType = toolHandler->getToolType(); + const char* toolStr = luaL_optstring(L, -2, nullptr); + if (toolStr != nullptr) { + toolType = toolTypeFromString(StringUtils::toLowerCase(toolStr)); + } + + if (toolType == TOOL_NONE) { + g_warning("tool \"%s\" is not valid or no tool has been selected", toolTypeToString(toolType).c_str()); + lua_pop(L, 3); + return 0; + } + + int color = 0x000000; + if (lua_isinteger(L, -1)) { + color = lua_tointeger(L, -1); + if (color < 0x000000 || color > 0xffffff) { + g_warning("Color 0x%x is no valid RGB color. ", color); + return 0; + } + } else if (!lua_isnil(L, -1)) { + g_warning(" " + "color" + " key should be an RGB hex code in the form 0xRRGGBB (or nil)"); + } + + Tool& tool = toolHandler->getTool(toolType); + + if (tool.hasCapability(TOOL_CAP_COLOR)) { + tool.setColor(color); + ctrl->toolColorChanged(selection); + } else { + g_warning("tool \"%s\" has no color capability", toolTypeToString(toolType).c_str()); + } + + // Make sure to remove all vars which are put to the stack before! + lua_pop(L, 3); + + return 1; +} static const luaL_Reg applib[] = {{"msgbox", applib_msgbox}, {"registerUi", applib_registerUi}, @@ -240,6 +314,7 @@ static const luaL_Reg applib[] = {{"msgbox", applib_msgbox}, {"uiActionSelected", applib_uiActionSelected}, {"changeCurrentPageBackground", applib_changeCurrentPageBackground}, {"saveAs", applib_saveAs}, + {"changeToolColor", applib_changeToolColor}, // Placeholder // {"MSG_BT_OK", nullptr},