Lagrange
Tools.h
1/*
2 * Copyright 2021 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/Entity.h>
15#include <lagrange/ui/api.h>
16#include <lagrange/ui/utils/pair_hash.h>
17
18namespace lagrange {
19namespace ui {
20
21template <typename T>
22entt::id_type register_tool_type(
23 const std::string& display_name,
24 const std::string& icon,
25 const std::string& keybind)
26{
27 using namespace entt::literals;
28
29 entt::meta<T>()
30 .prop("display_name"_hs, display_name)
31 .prop("icon"_hs, icon)
32 .prop("keybind"_hs, keybind);
33
34 return entt::type_id<T>().hash();
35}
36
37
38/*
39 Container for Tool systems
40*/
41class LA_UI_API Tools
42{
43public:
44 template <typename ToolType, typename ElementType>
45 void register_tool(const System& tool_system)
46 {
47 const auto k = key<ToolType, ElementType>();
48 m_tool_systems[k] = tool_system;
49
50 if (std::find(m_tool_types.begin(), m_tool_types.end(), k.first) == m_tool_types.end()) {
51 m_tool_types.push_back(k.first);
52 }
53
54 if (std::find(m_element_types.begin(), m_element_types.end(), k.second) ==
55 m_element_types.end()) {
56 m_element_types.push_back(k.second);
57 }
58 }
59
60 template <typename ToolType, typename ElementType>
61 void run(Registry& registry)
62 {
63 m_tool_systems[key<ToolType, ElementType>()](registry);
64 }
65
66 void run(entt::id_type tool_type, entt::id_type element_type, Registry& registry);
67
68
69 bool run_current(Registry& registry);
70
71
72 const std::vector<entt::id_type>& get_element_types() const;
73 const std::vector<entt::id_type>& get_tool_types() const;
74
75 std::vector<entt::id_type>& get_element_types();
76 std::vector<entt::id_type>& get_tool_types();
77
78 entt::id_type get_current_tool_type() const;
79 entt::id_type get_current_element_type() const;
80
81 bool has_backface_selection_tool() const;
82 void enable_backface_selection_tool(bool has_backfaces_tool);
83
84 void set_current_element_type(entt::id_type element_type);
85
86 void set_current_tool_type(entt::id_type tool_type);
87
88 template <typename T>
89 void set_current_element_type()
90 {
91 set_current_element_type(entt::type_id<T>().hash());
92 }
93
94 template <typename T>
95 void set_current_tool_type()
96 {
97 set_current_tool_type(entt::type_id<T>().hash());
98 }
99
100private:
101 using KeyType = std::pair<entt::id_type, entt::id_type>;
102
103 template <typename ToolType, typename ElementType>
104 static constexpr KeyType key()
105 {
106 return {entt::type_id<ToolType>().hash(), entt::type_id<ElementType>().hash()};
107 }
108 std::unordered_map<KeyType, System, utils::pair_hash> m_tool_systems;
109 std::vector<entt::id_type> m_tool_types;
110 std::vector<entt::id_type> m_element_types;
111
112 KeyType m_current_key = {0, 0};
113
114 bool m_has_backface_selection_tool = true;
115};
116
117
118} // namespace ui
119} // namespace lagrange
Definition: Tools.h:42
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30