载入中...
搜索中...
未找到
EditorToolbar.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5namespace eve::editor {
6
8 tools_.clear();
9 active_.clear();
10}
11
12int EditorToolbar::findIndex(const std::string &id) const {
13 for (int i = 0; i < static_cast<int>(tools_.size()); ++i) {
14 if (tools_[i].id == id) return i;
15 }
16 return -1;
17}
18
19void EditorToolbar::addTool(const std::string &id, const std::string &label) {
20 if (id.empty()) throw Exception("EditorToolbar::addTool: empty id");
21 if (findIndex(id) >= 0) throw Exception("EditorToolbar::addTool: duplicate id");
22 tools_.push_back(Tool{id, label, ""});
23 if (active_.empty()) active_ = id;
24}
25
26void EditorToolbar::setShortcut(const std::string &id, const std::string &key) {
27 int i = findIndex(id);
28 if (i < 0) throw Exception("EditorToolbar::setShortcut: unknown id");
29 tools_[i].shortcut = key;
30}
31
32bool EditorToolbar::setActive(const std::string &id) {
33 if (findIndex(id) < 0) return false;
34 active_ = id;
35 return true;
36}
37
38bool EditorToolbar::matchShortcut(const std::string &key) {
39 for (const auto &t : tools_) {
40 if (!t.shortcut.empty() && t.shortcut == key) {
41 active_ = t.id;
42 return true;
43 }
44 }
45 return false;
46}
47
48std::string EditorToolbar::getToolId(int index) const {
49 if (index < 0 || index >= static_cast<int>(tools_.size()))
50 throw Exception("EditorToolbar::getToolId: bad index");
51 return tools_[index].id;
52}
53
54std::string EditorToolbar::getToolLabel(int index) const {
55 if (index < 0 || index >= static_cast<int>(tools_.size()))
56 throw Exception("EditorToolbar::getToolLabel: bad index");
57 return tools_[index].label;
58}
59
60std::string EditorToolbar::getToolShortcut(int index) const {
61 if (index < 0 || index >= static_cast<int>(tools_.size()))
62 throw Exception("EditorToolbar::getToolShortcut: bad index");
63 return tools_[index].shortcut;
64}
65
66} // namespace eve::editor
std::string id
std::string getToolLabel(int index) const
std::string getToolId(int index) const
void addTool(const std::string &id, const std::string &label)
bool setActive(const std::string &id)
void setShortcut(const std::string &id, const std::string &key)
std::string getToolShortcut(int index) const
bool matchShortcut(const std::string &key)