From 7da4493a0fa875abd1a28131342ccf4257a2132d Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Tue, 12 Jul 2022 15:59:18 +0200 Subject: [PATCH] Use command metadata for vector clip toolbar This cuts down on the data duplication for the command names and help texts, at the expense of copying a bit of code from the general toolbar logic. Modifying the toolbar Populate() function to also add additional buttons appears very hard or impossible. --- src/command/command.h | 2 ++ src/visual_tool_vector_clip.cpp | 40 +++++++++++++++++++++++---------- src/visual_tool_vector_clip.h | 3 +++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/command/command.h b/src/command/command.h index 08752adbe..4d7b00225 100644 --- a/src/command/command.h +++ b/src/command/command.h @@ -16,6 +16,8 @@ /// @brief Command base class and main header. /// @ingroup command +#pragma once + #include #include #include diff --git a/src/visual_tool_vector_clip.cpp b/src/visual_tool_vector_clip.cpp index 8cae6b513..b14aa81b5 100644 --- a/src/visual_tool_vector_clip.cpp +++ b/src/visual_tool_vector_clip.cpp @@ -38,24 +38,40 @@ VisualToolVectorClip::VisualToolVectorClip(VideoDisplay *parent, agi::Context *c { } +// Having the mode as an extra argument here isn't the cleanest, but using a counter instead +// 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. +void VisualToolVectorClip::AddTool(std::string command_name, VisualToolVectorClipMode mode) { + cmd::Command *command; + 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(); + toolBar->AddTool(BUTTON_ID_BASE + mode, command->StrDisplay(c), command->Icon(icon_size), command->StrHelp(), wxITEM_CHECK); +} + + void VisualToolVectorClip::SetToolbar(wxToolBar *toolBar) { this->toolBar = toolBar; toolBar->AddSeparator(); - int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt(); + AddTool("video/tool/vclip/drag", VCLIP_DRAG); + AddTool("video/tool/vclip/line", VCLIP_LINE); + AddTool("video/tool/vclip/bicubic", VCLIP_BICUBIC); + toolBar->AddSeparator(); + AddTool("video/tool/vclip/convert", VCLIP_CONVERT); + AddTool("video/tool/vclip/insert", VCLIP_INSERT); + AddTool("video/tool/vclip/remove", VCLIP_REMOVE); + toolBar->AddSeparator(); + AddTool("video/tool/vclip/freehand", VCLIP_FREEHAND); + AddTool("video/tool/vclip/freehand_smooth", VCLIP_FREEHAND_SMOOTH); -#define ICON(name) icon_size == 16 ? GETIMAGE(name ## _16) : GETIMAGE(name ## _24) - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_DRAG, _("Drag"), ICON(visual_vector_clip_drag), _("Drag control points"), wxITEM_CHECK); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_LINE, _("Line"), ICON(visual_vector_clip_line), _("Appends a line"), wxITEM_CHECK); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_BICUBIC, _("Bicubic"), ICON(visual_vector_clip_bicubic), _("Appends a bezier bicubic curve"), wxITEM_CHECK); - toolBar->AddSeparator(); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_CONVERT, _("Convert"), ICON(visual_vector_clip_convert), _("Converts a segment between line and bicubic"), wxITEM_CHECK); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_INSERT, _("Insert"), ICON(visual_vector_clip_insert), _("Inserts a control point"), wxITEM_CHECK); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_REMOVE, _("Remove"), ICON(visual_vector_clip_remove), _("Removes a control point"), wxITEM_CHECK); - toolBar->AddSeparator(); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_FREEHAND, _("Freehand"), ICON(visual_vector_clip_freehand), _("Draws a freehand shape"), wxITEM_CHECK); - toolBar->AddTool(BUTTON_ID_BASE + VCLIP_FREEHAND_SMOOTH, _("Freehand smooth"), ICON(visual_vector_clip_freehand_smooth), _("Draws a smoothed freehand shape"), wxITEM_CHECK); toolBar->ToggleTool(BUTTON_ID_BASE + VCLIP_DRAG, true); toolBar->Realize(); toolBar->Show(true); diff --git a/src/visual_tool_vector_clip.h b/src/visual_tool_vector_clip.h index 4f0c8d0be..c1ce99ed6 100644 --- a/src/visual_tool_vector_clip.h +++ b/src/visual_tool_vector_clip.h @@ -19,6 +19,7 @@ #include "visual_feature.h" #include "visual_tool.h" #include "spline.h" +#include "command/command.h" class wxToolBar; @@ -56,6 +57,8 @@ class VisualToolVectorClip final : public VisualTool