载入中...
搜索中...
未找到
Icons.cpp
浏览该文件的文档.
1#include "ui/Icons.h"
2
3#include <algorithm>
4#include <array>
5#include <cctype>
6
7namespace eve::ui {
8namespace {
9
10struct IconEntry {
12 const char *name;
13 const char *glyph;
14};
15
16// UTF-8 codepoints match Font Awesome 4.7's canonical CSS mapping.
17constexpr std::array kIcons{
18 IconEntry{Icon::None, "none", ""},
19 IconEntry{Icon::Search, "search", "\xEF\x80\x82"},
20 IconEntry{Icon::Close, "close", "\xEF\x80\x8D"},
21 IconEntry{Icon::Check, "check", "\xEF\x80\x8C"},
22 IconEntry{Icon::Settings, "settings", "\xEF\x80\x93"},
23 IconEntry{Icon::Home, "home", "\xEF\x80\x95"},
24 IconEntry{Icon::File, "file", "\xEF\x85\x9B"},
25 IconEntry{Icon::Folder, "folder", "\xEF\x81\xBB"},
26 IconEntry{Icon::FolderOpen, "folder-open", "\xEF\x81\xBC"},
27 IconEntry{Icon::Save, "save", "\xEF\x83\x87"},
28 IconEntry{Icon::Undo, "undo", "\xEF\x83\xA2"},
29 IconEntry{Icon::Redo, "redo", "\xEF\x80\x9E"},
30 IconEntry{Icon::Refresh, "refresh", "\xEF\x80\xA1"},
31 IconEntry{Icon::Plus, "plus", "\xEF\x81\xA7"},
32 IconEntry{Icon::Minus, "minus", "\xEF\x81\xA8"},
33 IconEntry{Icon::ChevronLeft, "chevron-left", "\xEF\x81\x93"},
34 IconEntry{Icon::ChevronRight, "chevron-right", "\xEF\x81\x94"},
35 IconEntry{Icon::ChevronUp, "chevron-up", "\xEF\x81\xB7"},
36 IconEntry{Icon::ChevronDown, "chevron-down", "\xEF\x81\xB8"},
37 IconEntry{Icon::Menu, "menu", "\xEF\x83\x89"},
38 IconEntry{Icon::Grid, "grid", "\xEF\x80\x8A"},
39 IconEntry{Icon::List, "list", "\xEF\x80\xBA"},
40 IconEntry{Icon::Eye, "eye", "\xEF\x81\xAE"},
41 IconEntry{Icon::EyeSlash, "eye-slash", "\xEF\x81\xB0"},
42 IconEntry{Icon::Lock, "lock", "\xEF\x80\xA3"},
43 IconEntry{Icon::Unlock, "unlock", "\xEF\x82\x9C"},
44 IconEntry{Icon::Play, "play", "\xEF\x81\x8B"},
45 IconEntry{Icon::Pause, "pause", "\xEF\x81\x8C"},
46 IconEntry{Icon::Stop, "stop", "\xEF\x81\x8D"},
47 IconEntry{Icon::Move, "move", "\xEF\x81\x87"},
48 IconEntry{Icon::Pointer, "pointer", "\xEF\x89\x85"},
49 IconEntry{Icon::Pencil, "pencil", "\xEF\x81\x80"},
50 IconEntry{Icon::PaintBrush, "paint-brush", "\xEF\x87\xBC"},
51 IconEntry{Icon::Image, "image", "\xEF\x80\xBE"},
52 IconEntry{Icon::Camera, "camera", "\xEF\x80\xB0"},
53 IconEntry{Icon::Cube, "cube", "\xEF\x86\xB2"},
54 IconEntry{Icon::Cubes, "cubes", "\xEF\x86\xB3"},
55 IconEntry{Icon::Database, "database", "\xEF\x87\x80"},
56 IconEntry{Icon::Wrench, "wrench", "\xEF\x82\xAD"},
57 IconEntry{Icon::Sliders, "sliders", "\xEF\x87\x9E"},
58 IconEntry{Icon::Info, "info", "\xEF\x81\x9A"},
59 IconEntry{Icon::Question, "question", "\xEF\x81\x99"},
60 IconEntry{Icon::Warning, "warning", "\xEF\x81\xB1"},
61 IconEntry{Icon::Code, "code", "\xEF\x84\xA1"},
62 IconEntry{Icon::Bug, "bug", "\xEF\x86\x88"},
63 IconEntry{Icon::Terminal, "terminal", "\xEF\x84\xA0"},
64 IconEntry{Icon::Layers, "layers", "\xEF\x89\x8D"},
65 IconEntry{Icon::User, "user", "\xEF\x80\x87"},
66 IconEntry{Icon::Users, "users", "\xEF\x83\x80"},
67};
68
69std::string normalize(std::string_view value) {
70 std::string result(value);
71 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) {
72 if (c == '_' || c == ' ') return '-';
73 return static_cast<char>(std::tolower(c));
74 });
75 return result;
76}
77
78const IconEntry *findEntry(Icon icon) {
79 const auto it = std::find_if(kIcons.begin(), kIcons.end(),
80 [icon](const IconEntry &entry) { return entry.icon == icon; });
81 return it == kIcons.end() ? &kIcons.front() : &*it;
82}
83
84} // namespace
85
86const char *iconGlyph(Icon icon) { return findEntry(icon)->glyph; }
87
88const char *iconName(Icon icon) { return findEntry(icon)->name; }
89
90bool iconFromName(std::string_view name, Icon *out) {
91 if (!out) return false;
92 const std::string key = normalize(name);
93 const auto it = std::find_if(kIcons.begin(), kIcons.end(), [&](const IconEntry &entry) {
94 return key == entry.name;
95 });
96 if (it == kIcons.end()) return false;
97 *out = it->icon;
98 return true;
99}
100
101std::string iconText(Icon icon, std::string_view label) {
102 std::string result = iconGlyph(icon);
103 if (!label.empty()) {
104 if (!result.empty()) result += ' ';
105 result.append(label.data(), label.size());
106 }
107 return result;
108}
109
110} // namespace eve::ui
double value
std::string label
std::uint32_t key
std::int32_t c
const char * glyph
Definition Icons.cpp:13
Icon icon
Definition Icons.cpp:11
std::string name
Vec2 normalize(const Vec2 &a)
Normalize.
Definition UrbanTypes.h:44
const char * iconGlyph(Icon icon)
Returns the UTF-8 glyph for an icon, or an empty string for None.
Definition Icons.cpp:86
Icon
Semantic editor icons backed by the bundled Font Awesome 4.7 font.
Definition Icons.h:11
std::string iconText(Icon icon, std::string_view label)
Composes an icon with an optional visible label.
Definition Icons.cpp:101
const char * iconName(Icon icon)
Returns the stable kebab-case semantic name for an icon.
Definition Icons.cpp:88
bool iconFromName(std::string_view name, Icon *out)
Resolves a semantic icon name; accepts kebab-case names case-insensitively.
Definition Icons.cpp:90