Skip to content

Commit

Permalink
improve assistant tool selection
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwarthog committed Aug 29, 2023
1 parent ef40cf1 commit 534c7ce
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 72 deletions.
14 changes: 11 additions & 3 deletions toonz/sources/include/tools/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class DVAPI TTool {

EmptyTarget = 0x80, //!< Will work on empty cells/columns

MetaImage = 0x100, //!< Will work on mets images
MetaImage = 0x100, //!< Will work on meta images

CommonImages = VectorImage | ToonzImage | RasterImage,
AllImages = CommonImages | MeshImage | MetaImage,
Expand Down Expand Up @@ -572,8 +572,16 @@ transformation.

static std::set<TFrameId> m_selectedFrames;

protected:
void bind(int targetType);
private:
void bind(const std::string &name, int targetType);

public:
inline void bind(int targetType)
{ bind(getName(), targetType); }
void bind( int targetType,
const std::string &alias1,
const std::string &alias2 = std::string(),
const std::string &alias3 = std::string() );

virtual void onSelectedFramesChanged() {}

Expand Down
5 changes: 3 additions & 2 deletions toonz/sources/include/tools/toolhandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ class DVAPI ToolHandle final : public QObject {
~ToolHandle();

TTool *getTool() const;
void setTool(TTool *tool);

void setTool(QString name);
void setTargetType(int targetType);

const QString& getRequestedToolName() const
{ return m_toolName; }

// used to change tool for a short while (e.g. while keeping pressed a
// short-key)
void storeTool();
Expand Down
61 changes: 53 additions & 8 deletions toonz/sources/tnztools/editassistantstool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ class EditAssistantsTool final : public TTool {
TAssistant *m_writeAssistant;

Selection *selection;

// don't try to follow the pointer from history, it may be invalidated
typedef std::pair<const void*, int> HistoryItem;
typedef std::vector<HistoryItem> History;
History m_history;

public:
EditAssistantsTool():
Expand All @@ -188,7 +193,8 @@ class EditAssistantsTool final : public TTool {
m_writeAssistant()
{
selection = new Selection(*this);
bind(MetaImage | EmptyTarget);
// also assign assistants to the "brush" button in toolbar
bind(MetaImage | EmptyTarget, "T_Brush");
m_toolProperties.bind(m_assistantType);
updateTranslation();
}
Expand All @@ -203,15 +209,15 @@ class EditAssistantsTool final : public TTool {
int getCursorId() const override
{ return ToolCursor::StrokeSelectCursor; }
void onImageChanged() override {
if (m_currentImage != getImage(false)) resetCurrentPoint();
if (m_currentImage != getImage(false)) loadHistory();
getViewer()->GLInvalidateAll();
}

void updateAssistantTypes() {
std::wstring value = m_assistantType.getValue();

m_assistantType.deleteAllValues();
m_assistantType.addValueWithUIName(L"", tr("--"));
m_assistantType.addValueWithUIName(L"", tr("<choose to create>"));

const TMetaObject::Registry &registry = TMetaObject::getRegistry();
for(TMetaObject::Registry::const_iterator i = registry.begin(); i != registry.end(); ++i)
Expand Down Expand Up @@ -240,11 +246,11 @@ class EditAssistantsTool final : public TTool {

void onActivate() override {
updateAssistantTypes();
resetCurrentPoint();
loadHistory();
}

void onDeactivate() override
{ resetCurrentPoint(); }
{ resetCurrentPoint(true, false); }

void updateTranslation() override {
m_assistantType.setQStringName( tr("Assistant Type") );
Expand All @@ -270,6 +276,22 @@ class EditAssistantsTool final : public TTool {
{ return isSelected() ? selection : 0; }

protected:
void putHistory(const void *img, int assistantIndex) {
if (!img) return;
for(History::iterator i = m_history.begin(); i != m_history.end(); )
if (i->first == img) i = m_history.erase(i); else ++i;
if (m_history.size() >= 10) m_history.pop_back();
m_history.push_back(HistoryItem(img, assistantIndex));
}

void loadHistory() {
int index = -1;
if (Closer closer = read(ModeImage))
for(History::iterator i = m_history.begin(); i != m_history.end(); ++i)
if (i->first == m_readImage) index = i->second;
if (index < 0) resetCurrentPoint(true, false); else chooseAssistant(index);
}

void close() {
m_readAssistant = 0;
m_readObject.reset();
Expand Down Expand Up @@ -377,7 +399,7 @@ class EditAssistantsTool final : public TTool {
void updateOptionsBox()
{ getApplication()->getCurrentTool()->notifyToolOptionsBoxChanged(); }

void resetCurrentPoint(bool updateOptionsBox = true) {
void resetCurrentPoint(bool updateOptionsBox = true, bool updateHistory = true) {
close();
m_currentImage.reset();
m_currentAssistant.reset();
Expand All @@ -389,15 +411,37 @@ class EditAssistantsTool final : public TTool {
m_currentAssistantBackup.reset();

// deselect points
if (Closer closer = read(ModeImage))
if (Closer closer = read(ModeImage)) {
for(TMetaObjectListCW::iterator i = (*m_reader)->begin(); i != (*m_reader)->end(); ++i)
if (*i)
if (const TAssistant *assistant = (*i)->getHandler<TAssistant>())
assistant->deselectAll();
if (updateHistory) putHistory(m_readImage, -1);
}

if (updateOptionsBox) this->updateOptionsBox();
}

bool chooseAssistant(int index) {
resetCurrentPoint(false);
if (index >= 0)
if (Closer closer = read(ModeImage)) {
m_currentImage.set(m_readImage);
if (index < (*m_reader)->size())
if (const TMetaObjectPC &obj = (**m_reader)[index])
if (const TAssistant *assistant = obj->getHandler<TAssistant>()) {
assistant->deselectAll();
m_currentAssistant.set(obj);
m_currentAssistantIndex = index;
m_currentPointName = assistant->getBasePoint().name;
assistant->selectAll();
}
putHistory(m_readImage, m_currentAssistantIndex);
}
this->updateOptionsBox();
return m_currentAssistantIndex >= 0;
}

bool findCurrentPoint(const TPointD &position, double pixelSize = 1, bool updateOptionsBox = true) {
resetCurrentPoint(false);
if (Closer closer = read(ModeImage)) {
Expand Down Expand Up @@ -428,6 +472,7 @@ class EditAssistantsTool final : public TTool {
}
}
}
putHistory(m_readImage, m_currentAssistantIndex);
}

if (updateOptionsBox) this->updateOptionsBox();
Expand Down Expand Up @@ -469,7 +514,7 @@ class EditAssistantsTool final : public TTool {

public:
void deselect()
{ resetCurrentPoint(); }
{ resetCurrentPoint(true, false); }

bool isSelected()
{ return read(ModeAssistant); }
Expand Down
Loading

0 comments on commit 534c7ce

Please sign in to comment.