2019-01-08 03:42:55 +08:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2019 Igara Studio S.A.
|
|
|
|
//
|
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/commands/new_params.h"
|
|
|
|
|
2019-01-09 03:42:01 +08:00
|
|
|
#include "app/doc_exporter.h"
|
|
|
|
#include "app/sprite_sheet_type.h"
|
|
|
|
#include "base/convert_to.h"
|
|
|
|
#include "base/string.h"
|
2019-06-29 05:50:13 +08:00
|
|
|
#include "doc/color_mode.h"
|
2019-01-08 03:42:55 +08:00
|
|
|
|
2019-05-09 02:53:55 +08:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
|
|
|
#include "app/script/luacpp.h"
|
|
|
|
#endif
|
|
|
|
|
2019-01-08 03:42:55 +08:00
|
|
|
namespace app {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<bool>::fromString(const std::string& value)
|
|
|
|
{
|
2019-01-09 03:42:01 +08:00
|
|
|
setValue(value == "1" || value == "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<int>::fromString(const std::string& value)
|
|
|
|
{
|
|
|
|
setValue(base::convert_to<int>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<std::string>::fromString(const std::string& value)
|
|
|
|
{
|
|
|
|
setValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<app::SpriteSheetType>::fromString(const std::string& value)
|
|
|
|
{
|
|
|
|
if (value == "horizontal")
|
|
|
|
setValue(app::SpriteSheetType::Horizontal);
|
|
|
|
else if (value == "vertical")
|
|
|
|
setValue(app::SpriteSheetType::Vertical);
|
|
|
|
else if (value == "rows")
|
|
|
|
setValue(app::SpriteSheetType::Rows);
|
|
|
|
else if (value == "columns")
|
|
|
|
setValue(app::SpriteSheetType::Columns);
|
|
|
|
else if (value == "packed")
|
|
|
|
setValue(app::SpriteSheetType::Packed);
|
|
|
|
else
|
|
|
|
setValue(app::SpriteSheetType::None);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<app::DocExporter::DataFormat>::fromString(const std::string& value)
|
|
|
|
{
|
|
|
|
// JsonArray, json-array, json_array, etc.
|
|
|
|
if (base::utf8_icmp(value, "JsonArray") == 0 ||
|
|
|
|
base::utf8_icmp(value, "json-array") == 0 ||
|
|
|
|
base::utf8_icmp(value, "json_array") == 0)
|
|
|
|
setValue(app::DocExporter::JsonArrayDataFormat);
|
|
|
|
else
|
|
|
|
setValue(app::DocExporter::JsonHashDataFormat);
|
2019-01-08 03:42:55 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 05:50:13 +08:00
|
|
|
template<>
|
|
|
|
void Param<doc::ColorMode>::fromString(const std::string& value)
|
|
|
|
{
|
|
|
|
if (base::utf8_icmp(value, "rgb") == 0)
|
|
|
|
setValue(doc::ColorMode::RGB);
|
|
|
|
else if (base::utf8_icmp(value, "gray") == 0 ||
|
|
|
|
base::utf8_icmp(value, "grayscale") == 0)
|
|
|
|
setValue(doc::ColorMode::GRAYSCALE);
|
|
|
|
else if (base::utf8_icmp(value, "indexed") == 0)
|
|
|
|
setValue(doc::ColorMode::INDEXED);
|
|
|
|
else
|
|
|
|
setValue(doc::ColorMode::RGB);
|
|
|
|
}
|
|
|
|
|
2019-01-08 03:42:55 +08:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<bool>::fromLua(lua_State* L, int index)
|
|
|
|
{
|
2019-01-09 03:42:01 +08:00
|
|
|
setValue(lua_toboolean(L, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<int>::fromLua(lua_State* L, int index)
|
|
|
|
{
|
|
|
|
setValue(lua_tointeger(L, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<std::string>::fromLua(lua_State* L, int index)
|
|
|
|
{
|
|
|
|
if (const char* s = lua_tostring(L, index))
|
|
|
|
setValue(s);
|
|
|
|
else
|
|
|
|
setValue(std::string());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<app::SpriteSheetType>::fromLua(lua_State* L, int index)
|
|
|
|
{
|
2019-01-10 07:41:41 +08:00
|
|
|
if (lua_type(L, index) == LUA_TSTRING)
|
2019-01-09 03:42:01 +08:00
|
|
|
fromString(lua_tostring(L, index));
|
|
|
|
else
|
|
|
|
setValue((app::SpriteSheetType)lua_tointeger(L, index));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void Param<app::DocExporter::DataFormat>::fromLua(lua_State* L, int index)
|
|
|
|
{
|
2019-01-10 07:41:41 +08:00
|
|
|
if (lua_type(L, index) == LUA_TSTRING)
|
2019-01-09 03:42:01 +08:00
|
|
|
fromString(lua_tostring(L, index));
|
|
|
|
else
|
|
|
|
setValue((app::DocExporter::DataFormat)lua_tointeger(L, index));
|
2019-01-08 03:42:55 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 05:50:13 +08:00
|
|
|
template<>
|
|
|
|
void Param<doc::ColorMode>::fromLua(lua_State* L, int index)
|
|
|
|
{
|
|
|
|
if (lua_type(L, index) == LUA_TSTRING)
|
|
|
|
fromString(lua_tostring(L, index));
|
|
|
|
else
|
|
|
|
setValue((doc::ColorMode)lua_tointeger(L, index));
|
|
|
|
}
|
|
|
|
|
2019-01-08 03:42:55 +08:00
|
|
|
void CommandWithNewParamsBase::loadParamsFromLuaTable(lua_State* L, int index)
|
|
|
|
{
|
|
|
|
onResetValues();
|
|
|
|
if (lua_istable(L, index)) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, index) != 0) {
|
|
|
|
if (const char* k = lua_tostring(L, -2)) {
|
|
|
|
if (ParamBase* p = onGetParam(k))
|
|
|
|
p->fromLua(L, -1);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1); // Pop the value, leave the key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_skipLoadParams = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-05-09 02:53:55 +08:00
|
|
|
void CommandWithNewParamsBase::onLoadParams(const Params& params)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_SCRIPTING
|
|
|
|
if (m_skipLoadParams) {
|
|
|
|
m_skipLoadParams = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
onResetValues();
|
|
|
|
for (const auto& pair : params) {
|
|
|
|
if (ParamBase* p = onGetParam(pair.first))
|
|
|
|
p->fromString(pair.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 03:42:55 +08:00
|
|
|
} // namespace app
|