sandbox
Loading...
Searching...
No Matches
viewport_registry.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_GRAPHICS_VIEWPORT_REGISTRY_HPP_
3#define LIBSBX_GRAPHICS_VIEWPORT_REGISTRY_HPP_
4
5#include <string>
6#include <unordered_map>
7#include <unordered_set>
8#include <vector>
9
10#include <libsbx/math/vector2.hpp>
11
12#include <libsbx/signals/signal.hpp>
13
14namespace sbx::graphics {
15
17
18public:
19
21
22 auto declare(std::string name, const math::vector2u& initial_size) -> void {
23 _sizes.insert_or_assign(name, initial_size);
24 _dirty.insert(std::move(name));
25 }
26
27 auto resize(const std::string& name, const math::vector2u& size) -> bool {
28 auto entry = _sizes.find(name);
29
30 if (entry == _sizes.end()) {
31 _sizes.emplace(name, size);
32 _dirty.insert(name);
33 _on_changed.emit(name, size);
34
35 return true;
36 }
37
38 if (entry->second == size) {
39 return false;
40 }
41
42 entry->second = size;
43 _dirty.insert(name);
44 _on_changed.emit(name, size);
45
46 return true;
47 }
48
49 auto size(const std::string& name) const -> math::vector2u {
50 if (auto entry = _sizes.find(name); entry != _sizes.end()) {
51 return entry->second;
52 }
53
54 return math::vector2u{0u, 0u};
55 }
56
57 auto contains(const std::string& name) const -> bool {
58 return _sizes.find(name) != _sizes.end();
59 }
60
61 auto take_dirty() -> std::vector<std::string> {
62 auto result = std::vector<std::string>{_dirty.begin(), _dirty.end()};
63 _dirty.clear();
64
65 return result;
66 }
67
68 auto on_changed() -> change_signal& {
69 return _on_changed;
70 }
71
72private:
73
74 std::unordered_map<std::string, math::vector2u> _sizes;
75 std::unordered_set<std::string> _dirty;
76 change_signal _on_changed;
77
78}; // class viewport_registry
79
80} // namespace sbx::graphics
81
82#endif // LIBSBX_GRAPHICS_VIEWPORT_REGISTRY_HPP_
Definition: viewport_registry.hpp:16
A vector in two-dimensional space.
Definition: vector2.hpp:28
Definition: signal.hpp:18