path is now a vector of localized strings

This commit is contained in:
SweetId 2023-10-21 10:22:44 -04:00
parent f2790e45ad
commit da21340331
3 changed files with 17 additions and 21 deletions

View File

@ -19,7 +19,7 @@ namespace Nz
{ {
std::string className; std::string className;
Nz::LocalizedText description; Nz::LocalizedText description;
Nz::LocalizedText path; std::vector<Nz::LocalizedText> path;
std::string category; std::string category;
Nz::Shortcut shortcut; Nz::Shortcut shortcut;

View File

@ -23,8 +23,8 @@ namespace Nz
virtual void OnRenderImgui() override; virtual void OnRenderImgui() override;
void AddMenuAction(const std::string& path, const std::string& shortcut, ActionCallback callback, const std::shared_ptr<Nz::Texture>& icon = {}); void AddMenuAction(const std::vector<Nz::LocalizedText>& path, const std::string& shortcut, ActionCallback callback, const std::shared_ptr<Nz::Texture>& icon = {});
void AddMenuSeparator(const std::string& path); void AddMenuSeparator(const std::vector<Nz::LocalizedText>& path);
EditorBaseApplication* GetApplication() { return m_application; } EditorBaseApplication* GetApplication() { return m_application; }
const EditorBaseApplication* GetApplication() const { return m_application; } const EditorBaseApplication* GetApplication() const { return m_application; }
@ -46,7 +46,7 @@ namespace Nz
struct MenuAction struct MenuAction
{ {
std::string label; Nz::LocalizedText label;
std::string shortcut; std::string shortcut;
std::shared_ptr<Nz::Texture> icon; std::shared_ptr<Nz::Texture> icon;
ActionCallback callback; ActionCallback callback;
@ -57,12 +57,12 @@ namespace Nz
struct MenuList struct MenuList
{ {
std::string label; Nz::LocalizedText label;
std::vector<std::variant<MenuAction, MenuSeparator, MenuList>> entries; std::vector<std::variant<MenuAction, MenuSeparator, MenuList>> entries;
}; };
MenuList m_root; MenuList m_root;
MenuList& GetOrCreateMenuHierarchy(const std::vector<std::string_view>& hierarchy); MenuList& GetOrCreateMenuHierarchy(const std::vector<Nz::LocalizedText>& hierarchy);
}; };
} }

View File

@ -18,7 +18,7 @@ namespace Nz
return; return;
auto name = prop.className; auto name = prop.className;
AddMenuAction(prop.path.ToString(), prop.shortcut.ToString(), [name]() { Nz::EditorBaseApplication::Instance()->GetActionStack().ExecuteAction(name); }, prop.icon); AddMenuAction(prop.path, prop.shortcut.ToString(), [name]() { Nz::EditorBaseApplication::Instance()->GetActionStack().ExecuteAction(name); }, prop.icon);
}); });
} }
@ -45,25 +45,21 @@ namespace Nz
} }
} }
void EditorWindow::AddMenuAction(const std::string& path, const std::string& shortcut, ActionCallback callback, const std::shared_ptr<Nz::Texture>& icon) void EditorWindow::AddMenuAction(const std::vector<Nz::LocalizedText>& path, const std::string& shortcut, ActionCallback callback, const std::shared_ptr<Nz::Texture>& icon)
{ {
std::vector<std::string_view> v; std::vector<Nz::LocalizedText> copy = path;
Nz::SplitString(path, "|", [&](std::string_view str) { v.push_back(str); return true; }); Nz::LocalizedText leaf = copy.back();
std::string leaf = std::string(v.back()); copy.pop_back(); // remove action name from hierarchy
v.pop_back(); // remove action name from hierarchy MenuList& parent = GetOrCreateMenuHierarchy(copy);
MenuList& parent = GetOrCreateMenuHierarchy(v);
parent.entries.push_back(MenuAction{ leaf, shortcut, icon, callback}); parent.entries.push_back(MenuAction{ leaf, shortcut, icon, callback});
} }
void EditorWindow::AddMenuSeparator(const std::string& path) void EditorWindow::AddMenuSeparator(const std::vector<Nz::LocalizedText>& path)
{ {
std::vector<std::string_view> v; MenuList& parent = GetOrCreateMenuHierarchy(path);
Nz::SplitString(path, "|", [&](std::string_view str) { v.push_back(str); return true; });
MenuList& parent = GetOrCreateMenuHierarchy(v);
parent.entries.push_back(MenuSeparator{}); parent.entries.push_back(MenuSeparator{});
} }
@ -109,9 +105,9 @@ namespace Nz
visitor(menu); visitor(menu);
} }
EditorWindow::MenuList& EditorWindow::GetOrCreateMenuHierarchy(const std::vector<std::string_view>& hierarchy) EditorWindow::MenuList& EditorWindow::GetOrCreateMenuHierarchy(const std::vector<Nz::LocalizedText>& hierarchy)
{ {
auto getOrCreate_submenu = [](MenuList* menu, std::string_view v) -> MenuList* auto getOrCreate_submenu = [](MenuList* menu, const Nz::LocalizedText& v) -> MenuList*
{ {
for (auto& e : menu->entries) for (auto& e : menu->entries)
{ {
@ -128,7 +124,7 @@ namespace Nz
return ptr; return ptr;
} }
menu->entries.push_back(MenuList{ std::string(v) }); menu->entries.push_back(MenuList{ v });
return &std::get<MenuList>(menu->entries.back()); return &std::get<MenuList>(menu->entries.back());
}; };