Add tag userdata

Some code from https://github.com/aseprite/aseprite/pull/2265

Co-authored-by: Clarence "Sparr" Risher <sparr0@gmail.com>
This commit is contained in:
Gaspar Capello 2020-08-20 08:58:04 -03:00 committed by David Capello
parent 5ee3ebdd71
commit 55a37d0e6b
11 changed files with 64 additions and 50 deletions

View File

@ -1,28 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Aseprite -->
<!-- Copyright (C) 2019 Igara Studio S.A. -->
<!-- Copyright (C) 2019-2020 Igara Studio S.A. -->
<!-- Copyright (C) 2015-2018 David Capello -->
<gui>
<window id="tag_properties" text="@.title">
<grid columns="2">
<grid columns="3">
<label text="@.name" />
<entry maxsize="256" id="name" magnet="true" />
<entry maxsize="256" id="name" magnet="true" cell_aling="horizontal" />
<button id="user_data" icon="icon_user_data" tooltip="@general.user_data" />
<label text="@.from" />
<expr id="from" />
<expr id="from" cell_hspan="2" />
<label text="@.to" />
<expr id="to" />
<expr id="to" cell_hspan="2" />
<label text="@.color" />
<colorpicker id="color" simple="true" />
<colorpicker id="color" simple="true" cell_hspan="2" />
<label text="@.ani_dir" />
<combobox id="anidir" />
<combobox id="anidir" cell_hspan="2" />
<separator horizontal="true" cell_hspan="2" />
<separator horizontal="true" cell_hspan="3" />
<box horizontal="true" homogeneous="true" cell_hspan="2" cell_align="right">
<box horizontal="true" homogeneous="true" cell_hspan="3" cell_align="right">
<button text="@general.ok" closewindow="true" id="ok" magnet="true" minwidth="60" />
<button text="@general.cancel" closewindow="true" />
</box>

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -13,6 +13,7 @@
#include "app/cmd/set_tag_color.h"
#include "app/cmd/set_tag_name.h"
#include "app/cmd/set_tag_range.h"
#include "app/cmd/set_user_data.h"
#include "app/color.h"
#include "app/commands/command.h"
#include "app/commands/params.h"
@ -23,6 +24,7 @@
#include "base/convert_to.h"
#include "doc/anidir.h"
#include "doc/sprite.h"
#include "doc/user_data.h"
#include "doc/tag.h"
namespace app {
@ -109,6 +111,11 @@ void FrameTagPropertiesCommand::onExecute(Context* context)
if (tag->aniDir() != anidir)
tx(new cmd::SetTagAniDir(tag, anidir));
// Change user data
doc::UserData userData = window.userDataValue();
if (tag->userData() != userData)
tx(new cmd::SetUserData(tag, userData));
tx.commit();
}

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -70,6 +70,7 @@ void NewFrameTagCommand::onExecute(Context* context)
tag->setName(window.nameValue());
tag->setColor(window.colorValue());
tag->setAniDir(window.aniDirValue());
tag->setUserData(window.userDataValue());
{
ContextWriter writer(reader);

View File

@ -1355,7 +1355,8 @@ void DocExporter::createDataFile(const Samples& samples,
os << "\n { \"name\": \"" << escape_for_json(tag->name()) << "\","
<< " \"from\": " << (tag->fromFrame()+delta.first) << ","
<< " \"to\": " << (tag->toFrame()+delta.second) << ","
<< " \"direction\": \"" << escape_for_json(convert_anidir_to_string(tag->aniDir())) << "\" }";
" \"direction\": \"" << escape_for_json(convert_anidir_to_string(tag->aniDir())) << "\"";
os << tag->userData() << " }";
}
}
os << "\n ]";

View File

@ -1106,6 +1106,8 @@ static void ase_file_write_tags_chunk(FILE* f,
fputc(0, f);
ase_file_write_string(f, tag->name());
if (!tag->userData().isEmpty())
ase_file_write_user_data_chunk(f, frame_header, &tag->userData());
}
}

View File

@ -16,6 +16,7 @@
#include "app/script/docobj.h"
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "app/script/userdata.h"
#include "app/tx.h"
#include "doc/sprite.h"
#include "doc/tag.h"
@ -83,20 +84,6 @@ int Tag_get_aniDir(lua_State* L)
return 1;
}
int Tag_get_color(lua_State* L)
{
auto tag = get_docobj<Tag>(L, 1);
doc::color_t docColor = tag->color();
app::Color appColor = app::Color::fromRgb(doc::rgba_getr(docColor),
doc::rgba_getg(docColor),
doc::rgba_getb(docColor),
doc::rgba_geta(docColor));
if (appColor.getAlpha() == 0)
appColor = app::Color::fromMask();
push_obj<app::Color>(L, appColor);
return 1;
}
int Tag_set_fromFrame(lua_State* L)
{
auto tag = get_docobj<Tag>(L, 1);
@ -142,16 +129,6 @@ int Tag_set_aniDir(lua_State* L)
return 0;
}
int Tag_set_color(lua_State* L)
{
auto tag = get_docobj<Tag>(L, 1);
doc::color_t docColor = convert_args_into_pixel_color(L, 2, doc::IMAGE_RGB);
Tx tx;
tx(new cmd::SetTagColor(tag, docColor));
tx.commit();
return 0;
}
const luaL_Reg Tag_methods[] = {
{ "__eq", Tag_eq },
{ nullptr, nullptr }
@ -164,7 +141,8 @@ const Property Tag_properties[] = {
{ "frames", Tag_get_frames, nullptr },
{ "name", Tag_get_name, Tag_set_name },
{ "aniDir", Tag_get_aniDir, Tag_set_aniDir },
{ "color", Tag_get_color, Tag_set_color },
{ "color", UserData_get_color<Tag>, UserData_set_color<Tag> },
{ "data", UserData_get_text<Tag>, UserData_set_text<Tag> },
{ nullptr, nullptr, nullptr }
};

View File

@ -14,6 +14,7 @@
#include "app/doc.h"
#include "app/pref/preferences.h"
#include "app/ui/layer_frame_comboboxes.h"
#include "app/ui/user_data_popup.h"
#include "base/clamp.h"
#include "doc/sprite.h"
#include "doc/tag.h"
@ -24,6 +25,7 @@ TagWindow::TagWindow(const doc::Sprite* sprite, const doc::Tag* tag)
: m_sprite(sprite)
, m_base(Preferences::instance().document(
static_cast<Doc*>(sprite->document())).timeline.firstFrame())
, m_userData(tag->userData())
{
name()->setText(tag->name());
from()->setTextf("%d", tag->fromFrame()+m_base);
@ -34,6 +36,7 @@ TagWindow::TagWindow(const doc::Sprite* sprite, const doc::Tag* tag)
doc::rgba_getb(tag->color())));
fill_anidir_combobox(anidir(), tag->aniDir());
userData()->Click.connect([this]{ onPopupUserData(); });
}
bool TagWindow::show()
@ -61,6 +64,9 @@ void TagWindow::rangeValue(doc::frame_t& from, doc::frame_t& to)
doc::color_t TagWindow::colorValue()
{
app::Color color = this->color()->getColor();
m_userData.setColor(doc::rgba(color.getRed(),
color.getGreen(),
color.getBlue(), 255));
return doc::rgba(color.getRed(), color.getGreen(), color.getBlue(), 255);
}
@ -69,4 +75,16 @@ doc::AniDir TagWindow::aniDirValue()
return (doc::AniDir)anidir()->getSelectedItemIndex();
}
void TagWindow::onPopupUserData()
{
if (m_userData.color() != colorValue())
m_userData.setColor(colorValue());
show_user_data_popup(userData()->bounds(), m_userData);
color_t color = m_userData.color();
app::Color c;
this->color()->setColor(c.fromRgb(int(rgba_getr(color)),
int(rgba_getg(color)),
int(rgba_getb(color)), 255));
}
} // namespace app

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
@ -12,6 +12,7 @@
#include "app/ui/color_button.h"
#include "doc/anidir.h"
#include "doc/frame.h"
#include "doc/user_data.h"
#include "tag_properties.xml.h"
@ -32,10 +33,14 @@ namespace app {
void rangeValue(doc::frame_t& from, doc::frame_t& to);
doc::color_t colorValue();
doc::AniDir aniDirValue();
const doc::UserData& userDataValue() { return m_userData; }
private:
void onPopupUserData();
const doc::Sprite* m_sprite;
int m_base;
doc::UserData m_userData;
};
}

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
@ -17,22 +17,20 @@
namespace doc {
Tag::Tag(frame_t from, frame_t to)
: Object(ObjectType::Tag)
: WithUserData(ObjectType::Tag)
, m_owner(nullptr)
, m_from(from)
, m_to(to)
, m_color(rgba(0, 0, 0, 255))
, m_name("Tag")
, m_aniDir(AniDir::FORWARD)
{
}
Tag::Tag(const Tag& other)
: Object(ObjectType::Tag)
: WithUserData(ObjectType::Tag)
, m_owner(nullptr)
, m_from(other.m_from)
, m_to(other.m_to)
, m_color(other.m_color)
, m_name(other.m_name)
, m_aniDir(other.m_aniDir)
{
@ -68,7 +66,7 @@ void Tag::setName(const std::string& name)
void Tag::setColor(color_t color)
{
m_color = color;
userData().setColor(color);
}
void Tag::setAniDir(AniDir aniDir)

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
@ -11,16 +11,16 @@
#include "base/disable_copying.h"
#include "doc/anidir.h"
#include "doc/color.h"
#include "doc/frame.h"
#include "doc/object.h"
#include "doc/with_user_data.h"
#include <string>
namespace doc {
class Tags;
class Tag : public Object {
class Tag : public WithUserData {
public:
Tag(frame_t from, frame_t to);
Tag(const Tag& other);
@ -31,7 +31,7 @@ namespace doc {
frame_t toFrame() const { return m_to; }
frame_t frames() const { return m_to - m_from + 1; }
const std::string& name() const { return m_name; }
color_t color() const { return m_color; }
color_t color() const { return userData().color(); }
AniDir aniDir() const { return m_aniDir; }
void setFrameRange(frame_t from, frame_t to);
@ -44,7 +44,6 @@ namespace doc {
public:
Tags* m_owner;
frame_t m_from, m_to;
color_t m_color;
std::string m_name;
AniDir m_aniDir;

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -14,6 +14,7 @@
#include "base/serialization.h"
#include "doc/string_io.h"
#include "doc/tag.h"
#include "doc/user_data_io.h"
#include <iostream>
#include <memory>
@ -33,6 +34,7 @@ void write_tag(std::ostream& os, const Tag* tag)
write32(os, tag->color());
write8(os, (int)tag->aniDir());
write_string(os, tag->name());
write_user_data(os, tag->userData());
}
Tag* read_tag(std::istream& is, bool setId)
@ -43,11 +45,13 @@ Tag* read_tag(std::istream& is, bool setId)
color_t color = read32(is);
AniDir aniDir = (AniDir)read8(is);
std::string name = read_string(is);
UserData userData = read_user_data(is);
std::unique_ptr<Tag> tag(new Tag(from, to));
tag->setColor(color);
tag->setAniDir(aniDir);
tag->setName(name);
tag->setUserData(userData);
if (setId)
tag->setId(id);
return tag.release();