le2d v0.4.7
2D game engine framework
 
Loading...
Searching...
No Matches
tweakable.hpp
Go to the documentation of this file.
1#pragma once
3#include <klib/base_types.hpp>
4#include <functional>
5
6namespace le {
7namespace tweak {
9template <typename Type>
10using Callback = std::move_only_function<bool(Type const& new_value)>;
11} // namespace tweak
12
14class ITweakable : public klib::Polymorphic {
15 public:
16 [[nodiscard]] virtual auto type_name() const -> std::string_view = 0;
17 [[nodiscard]] virtual auto as_string() const -> std::string_view = 0;
18 virtual auto assign(std::string_view input) -> bool = 0;
19};
20
22template <typename Type>
23class Tweakable : public ITweakable {
24 public:
26
29
30 explicit(false) Tweakable() { set_value({}); }
31
32 template <std::convertible_to<Type> T>
33 explicit(false) Tweakable(T t) {
34 set_value(std::move(t));
35 }
36
37 auto operator=(Type t) -> Tweakable& {
38 set_value(std::move(t));
39 return *this;
40 }
41
42 [[nodiscard]] auto type_name() const -> std::string_view final { return Parser::type_name(); }
43
44 [[nodiscard]] auto as_string() const -> std::string_view final { return m_as_string; }
45
49 auto assign(std::string_view const input) -> bool final {
50 auto value = Type{};
51 if (!Parser::parse(input, value)) { return false; }
52 return set_value(std::move(value));
53 }
54
55 [[nodiscard]] auto get_value() const -> Type const& { return m_value; }
56 [[nodiscard]] auto get_value() -> Type& { return m_value; }
57
60 auto set_value(Type value) -> bool {
61 if (m_on_set && !m_on_set(value)) { return false; }
62 m_value = std::move(value);
63 m_as_string = Parser::to_string(m_value);
64 return true;
65 }
66
68 void on_set(Callback callback) { m_on_set = std::move(callback); }
69
70 explicit(std::same_as<bool, Type>) operator Type const&() const { return m_value; }
71
72 private:
73 Type m_value{};
74 std::string m_as_string{};
75 Callback m_on_set{};
76};
77} // namespace le
Interface for concrete Tweakables.
Definition tweakable.hpp:14
virtual auto assign(std::string_view input) -> bool=0
virtual auto type_name() const -> std::string_view=0
virtual auto as_string() const -> std::string_view=0
Tweakable wrapper over a Type value.
Definition tweakable.hpp:23
auto as_string() const -> std::string_view final
Definition tweakable.hpp:44
auto get_value() const -> Type const &
Definition tweakable.hpp:55
auto set_value(Type value) -> bool
Set value and invoke OnChange (if not null).
Definition tweakable.hpp:60
auto operator=(Type t) -> Tweakable &
Definition tweakable.hpp:37
tweak::Callback< Type > Callback
Setter hook callback.
Definition tweakable.hpp:28
auto get_value() -> Type &
Definition tweakable.hpp:56
auto assign(std::string_view const input) -> bool final
Parse input into value and assign if successful.
Definition tweakable.hpp:49
auto type_name() const -> std::string_view final
Definition tweakable.hpp:42
void on_set(Callback callback)
Install setter hook callback.
Definition tweakable.hpp:68
std::move_only_function< bool(Type const &new_value)> Callback
Definition tweakable.hpp:10
Definition animation.hpp:8
Definition parser.hpp:39
Customization point. Specialize this type for custom Tweakable template types.
Definition parser.hpp:22