visual tools: Add general interface for setting subtools

This commit is contained in:
arch1t3cht 2023-01-14 13:56:21 +01:00
parent af009c4ce7
commit bfdf01df9a
6 changed files with 17 additions and 42 deletions

View File

@ -59,12 +59,12 @@ namespace {
} }
bool IsActive(const agi::Context *c) override { bool IsActive(const agi::Context *c) override {
return c->videoDisplay->ToolIsVectorClipTool(M); return c->videoDisplay->ToolIsType(typeid(VisualToolVectorClip)) && c->videoDisplay->GetSubTool() == M;
} }
void operator()(agi::Context *c) override { void operator()(agi::Context *c) override {
c->videoDisplay->SetTool(agi::make_unique<VisualToolVectorClip>(c->videoDisplay, c)); c->videoDisplay->SetTool(agi::make_unique<VisualToolVectorClip>(c->videoDisplay, c));
c->videoDisplay->SetVectorClipTool(M); c->videoDisplay->SetSubTool(M);
} }
}; };

View File

@ -428,23 +428,10 @@ void VideoDisplay::SetTool(std::unique_ptr<VisualToolBase> new_tool) {
} }
} }
bool VideoDisplay::SetVectorClipTool(VisualToolVectorClipMode vcliptoolmode) const {
if (ToolIsType(typeid(VisualToolVectorClip))) {
static_cast<VisualToolVectorClip*>(tool.get())->SetMode(vcliptoolmode);
return true;
} else {
return false;
}
}
bool VideoDisplay::ToolIsType(std::type_info const& type) const { bool VideoDisplay::ToolIsType(std::type_info const& type) const {
return tool && typeid(*tool) == type; return tool && typeid(*tool) == type;
} }
bool VideoDisplay::ToolIsVectorClipTool(VisualToolVectorClipMode vcliptoolmode) const {
return ToolIsType(typeid(VisualToolVectorClip)) && static_cast<VisualToolVectorClip*>(tool.get());
}
Vector2D VideoDisplay::GetMousePosition() const { Vector2D VideoDisplay::GetMousePosition() const {
return last_mouse_pos ? tool->ToScriptCoords(last_mouse_pos) : last_mouse_pos; return last_mouse_pos ? tool->ToScriptCoords(last_mouse_pos) : last_mouse_pos;
} }

View File

@ -166,13 +166,11 @@ public:
void SetTool(std::unique_ptr<VisualToolBase> new_tool); void SetTool(std::unique_ptr<VisualToolBase> new_tool);
/// Will only set the vector clip mode if the vector clip tool is active already, void SetSubTool(int subtool) const { tool->SetSubTool(subtool); };
/// otherwise it just returns false.
bool SetVectorClipTool(VisualToolVectorClipMode vcliptoolmode) const;
bool ToolIsType(std::type_info const& type) const; bool ToolIsType(std::type_info const& type) const;
bool ToolIsVectorClipTool(VisualToolVectorClipMode vcliptoolmode) const; int GetSubTool() const { return tool->GetSubTool(); };
/// Discard all OpenGL state /// Discard all OpenGL state
void Unload(); void Unload();

View File

@ -146,6 +146,8 @@ public:
virtual void Draw()=0; virtual void Draw()=0;
virtual void SetDisplayArea(int x, int y, int w, int h); virtual void SetDisplayArea(int x, int y, int w, int h);
virtual void SetToolbar(wxToolBar *) { } virtual void SetToolbar(wxToolBar *) { }
virtual void SetSubTool(int subtool) { }
virtual int GetSubTool() { return 0; }
virtual ~VisualToolBase() = default; virtual ~VisualToolBase() = default;
}; };

View File

@ -42,15 +42,7 @@ VisualToolVectorClip::VisualToolVectorClip(VideoDisplay *parent, agi::Context *c
// as is done in toolbar.cpp feels like too big of a hack. At least this way the button's actions // as is done in toolbar.cpp feels like too big of a hack. At least this way the button's actions
// are not purely controlled by the order they're added in. // are not purely controlled by the order they're added in.
void VisualToolVectorClip::AddTool(std::string command_name, VisualToolVectorClipMode mode) { void VisualToolVectorClip::AddTool(std::string command_name, VisualToolVectorClipMode mode) {
cmd::Command *command; cmd::Command *command = cmd::get(command_name);
try {
command = cmd::get(command_name);
}
catch (cmd::CommandNotFound const&) {
// Toolbar names are all hardcoded so this should never happen
throw agi::InternalError("Toolbar named " + command_name + " not found.");
}
int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt(); int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt();
toolBar->AddTool(BUTTON_ID_BASE + mode, command->StrDisplay(c), command->Icon(icon_size), command->GetTooltip("Video"), wxITEM_CHECK); toolBar->AddTool(BUTTON_ID_BASE + mode, command->StrDisplay(c), command->Icon(icon_size), command->GetTooltip("Video"), wxITEM_CHECK);
} }
@ -75,24 +67,23 @@ void VisualToolVectorClip::SetToolbar(wxToolBar *toolBar) {
toolBar->ToggleTool(BUTTON_ID_BASE + VCLIP_DRAG, true); toolBar->ToggleTool(BUTTON_ID_BASE + VCLIP_DRAG, true);
toolBar->Realize(); toolBar->Realize();
toolBar->Show(true); toolBar->Show(true);
toolBar->Bind(wxEVT_TOOL, [=](wxCommandEvent& e) { SetMode((VisualToolVectorClipMode) (e.GetId() - BUTTON_ID_BASE)); }); toolBar->Bind(wxEVT_TOOL, [=](wxCommandEvent& e) { SetSubTool(e.GetId() - BUTTON_ID_BASE); });
SetMode(VCLIP_LINE); SetSubTool(VCLIP_LINE);
#undef ICON
} }
void VisualToolVectorClip::SetMode(VisualToolVectorClipMode new_mode) { void VisualToolVectorClip::SetSubTool(int subtool) {
if (toolBar == nullptr) { if (toolBar == nullptr) {
throw agi::InternalError("Vector clip toolbar hasn't been set yet!"); throw agi::InternalError("Vector clip toolbar hasn't been set yet!");
} }
// Manually enforce radio behavior as we want one selection in the bar // Manually enforce radio behavior as we want one selection in the bar
// rather than one per group // rather than one per group
for (int i = 0; i < VCLIP_LAST; i++) for (int i = 0; i < VCLIP_LAST; i++)
toolBar->ToggleTool(BUTTON_ID_BASE + i, i == new_mode); toolBar->ToggleTool(BUTTON_ID_BASE + i, i == subtool);
mode = new_mode; mode = subtool;
} }
VisualToolVectorClipMode VisualToolVectorClip::GetMode() { int VisualToolVectorClip::GetSubTool() {
return mode; return mode;
} }
@ -452,7 +443,7 @@ void VisualToolVectorClip::UpdateHold() {
// End freedraw // End freedraw
if (!holding && (mode == VCLIP_FREEHAND || mode == VCLIP_FREEHAND_SMOOTH)) { if (!holding && (mode == VCLIP_FREEHAND || mode == VCLIP_FREEHAND_SMOOTH)) {
SetMode(VCLIP_DRAG); SetSubTool(VCLIP_DRAG);
MakeFeatures(); MakeFeatures();
} }
} }

View File

@ -49,7 +49,7 @@ struct VisualToolVectorClipDraggableFeature final : public VisualDraggableFeatur
class VisualToolVectorClip final : public VisualTool<VisualToolVectorClipDraggableFeature> { class VisualToolVectorClip final : public VisualTool<VisualToolVectorClipDraggableFeature> {
Spline spline; /// The current spline Spline spline; /// The current spline
wxToolBar *toolBar = nullptr; /// The subtoolbar wxToolBar *toolBar = nullptr; /// The subtoolbar
VisualToolVectorClipMode mode = VCLIP_DRAG; /// 0-7 int mode = VCLIP_DRAG; /// 0-7
bool inverse = false; /// is iclip? bool inverse = false; /// is iclip?
std::set<Feature *> box_added; std::set<Feature *> box_added;
@ -75,9 +75,6 @@ public:
VisualToolVectorClip(VideoDisplay *parent, agi::Context *context); VisualToolVectorClip(VideoDisplay *parent, agi::Context *context);
void SetToolbar(wxToolBar *tb) override; void SetToolbar(wxToolBar *tb) override;
/// @brief Set the mode. Only valid if the tool is already active. void SetSubTool(int subtool) override;
/// @param mode int GetSubTool() override;
void SetMode(VisualToolVectorClipMode mode);
VisualToolVectorClipMode GetMode();
}; };