Lagrange
Loading...
Searching...
No Matches
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_factory<T>().template custom<TypeData>(
30 TypeData().set_display_name(display_name).set_icon(icon).set_keybind(keybind));
31
32 return entt::type_id<T>().hash();
33}
34
35
36/*
37 Container for Tool systems
38*/
39class LA_UI_API Tools
40{
41public:
42 template <typename ToolType, typename ElementType>
43 void register_tool(const System& tool_system)
44 {
45 const auto k = key<ToolType, ElementType>();
46 m_tool_systems[k] = tool_system;
47
48 if (std::find(m_tool_types.begin(), m_tool_types.end(), k.first) == m_tool_types.end()) {
49 m_tool_types.push_back(k.first);
50 }
51
52 if (std::find(m_element_types.begin(), m_element_types.end(), k.second) ==
53 m_element_types.end()) {
54 m_element_types.push_back(k.second);
55 }
56 }
57
58 template <typename ToolType, typename ElementType>
59 void run(Registry& registry)
60 {
61 m_tool_systems[key<ToolType, ElementType>()](registry);
62 }
63
64 void run(entt::id_type tool_type, entt::id_type element_type, Registry& registry);
65
66
67 bool run_current(Registry& registry);
68
69
70 const std::vector<entt::id_type>& get_element_types() const;
71 const std::vector<entt::id_type>& get_tool_types() const;
72
73 std::vector<entt::id_type>& get_element_types();
74 std::vector<entt::id_type>& get_tool_types();
75
76 entt::id_type get_current_tool_type() const;
77 entt::id_type get_current_element_type() const;
78
79 bool has_backface_selection_tool() const;
80 void enable_backface_selection_tool(bool has_backfaces_tool);
81
82 void set_current_element_type(entt::id_type element_type);
83
84 void set_current_tool_type(entt::id_type tool_type);
85
86 template <typename T>
87 void set_current_element_type()
88 {
89 set_current_element_type(entt::type_id<T>().hash());
90 }
91
92 template <typename T>
93 void set_current_tool_type()
94 {
95 set_current_tool_type(entt::type_id<T>().hash());
96 }
97
98private:
99 using KeyType = std::pair<entt::id_type, entt::id_type>;
100
101 template <typename ToolType, typename ElementType>
102 static constexpr KeyType key()
103 {
104 return {entt::type_id<ToolType>().hash(), entt::type_id<ElementType>().hash()};
105 }
106 std::unordered_map<KeyType, System, utils::pair_hash> m_tool_systems;
107 std::vector<entt::id_type> m_tool_types;
108 std::vector<entt::id_type> m_element_types;
109
110 KeyType m_current_key = {0, 0};
111
112 bool m_has_backface_selection_tool = true;
113};
114
115
116} // namespace ui
117} // namespace lagrange
Definition Tools.h:40
Lagrange UI Viewer and mini 3D engine.
Definition AcceleratedPicking.h:23
Main namespace for Lagrange.
Definition Entity.h:34