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.
This commit is contained in:
arch1t3cht 2022-07-12 15:59:18 +02:00
parent 10d233d3e2
commit 7da4493a0f
3 changed files with 33 additions and 12 deletions

View File

@ -16,6 +16,8 @@
/// @brief Command base class and main header.
/// @ingroup command
#pragma once
#include <map>
#include <string>
#include <vector>

View File

@ -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);

View File

@ -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<VisualToolVectorClipDraggab
void Save();
void Commit(wxString message="") override;
void AddTool(std::string command_name, VisualToolVectorClipMode mode);
void MakeFeature(size_t idx);
void MakeFeatures();