Keep FrameTags in order

This commit is contained in:
David Capello 2015-03-09 15:56:04 -03:00
parent 58d302749c
commit 1f4400b943
3 changed files with 32 additions and 1 deletions

View File

@ -10,10 +10,13 @@
#include "doc/frame_tag.h"
#include "doc/frame_tags.h"
namespace doc {
FrameTag::FrameTag(frame_t from, frame_t to)
: Object(ObjectType::FrameTag)
, m_owner(nullptr)
, m_from(from)
, m_to(to)
, m_color(rgba(0, 0, 0, 255))
@ -22,10 +25,23 @@ FrameTag::FrameTag(frame_t from, frame_t to)
{
}
void FrameTag::setOwner(FrameTags* owner)
{
ASSERT(!m_owner);
m_owner = owner;
}
void FrameTag::setFrameRange(frame_t from, frame_t to)
{
FrameTags* owner = m_owner;
if (owner)
owner->remove(this);
m_from = from;
m_to = to;
if (owner)
owner->add(this); // Re-add the tag, so it's added in the correct place
}
void FrameTag::setName(const std::string& name)

View File

@ -17,11 +17,13 @@
#include <string>
namespace doc {
class FrameTags;
class FrameTag : public Object {
public:
FrameTag(frame_t from, frame_t to);
FrameTags* owner() const { return m_owner; }
frame_t fromFrame() const { return m_from; }
frame_t toFrame() const { return m_to; }
const std::string& name() const { return m_name; }
@ -33,7 +35,10 @@ namespace doc {
void setColor(color_t color);
void setAniDir(AniDir aniDir);
void setOwner(FrameTags* owner);
public:
FrameTags* m_owner;
frame_t m_from, m_to;
color_t m_color;
std::string m_name;

View File

@ -10,6 +10,8 @@
#include "doc/frame_tags.h"
#include "doc/frame_tag.h"
#include <algorithm>
namespace doc {
@ -21,7 +23,13 @@ FrameTags::FrameTags(Sprite* sprite)
void FrameTags::add(FrameTag* tag)
{
m_tags.push_back(tag);
auto it = begin(), end = this->end();
for (; it != end; ++it) {
if ((*it)->fromFrame() > tag->fromFrame())
break;
}
m_tags.insert(it, tag);
tag->setOwner(this);
}
void FrameTags::remove(FrameTag* tag)
@ -30,6 +38,8 @@ void FrameTags::remove(FrameTag* tag)
ASSERT(it != m_tags.end());
if (it != m_tags.end())
m_tags.erase(it);
tag->setOwner(nullptr);
}
} // namespace doc