Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlVector2.h
Go to the documentation of this file.
1#pragma once
2
5#include "Math/TVector2.h"
6#include "SDL/sdl_helpers.h"
7
8#include <Common/assertion.h>
9#include <Common/primitive_type.h>
10
11#include <type_traits>
12#include <string>
13#include <cstddef>
14
15namespace ph
16{
17
18template<typename Owner, typename Element, typename SdlValueType = TSdlValue<math::TVector2<Element>, Owner>>
19class TSdlVector2 : public SdlValueType
20{
21 static_assert(std::is_base_of_v<TSdlAbstractValue<math::TVector2<Element>, Owner>, SdlValueType>,
22 "SdlValueType should be a subclass of TSdlAbstractValue.");
23
24public:
25 template<typename ValueType>
26 TSdlVector2(std::string valueName, ValueType Owner::* const valuePtr) :
27 SdlValueType("vector2", std::move(valueName), valuePtr)
28 {
29 if constexpr(std::is_same_v<SdlValueType, TSdlValue<math::TVector2<Element>, Owner>>)
30 {
32 }
33 }
34
35 std::string valueAsString(const math::TVector2<Element>& vec2) const override
36 {
37 return vec2.toString();
38 }
39
40 SdlNativeData ownedNativeData(Owner& owner) const override
41 {
42 math::TVector2<Element>* const vec2 = this->getValue(owner);
43 if constexpr(std::is_base_of_v<TSdlOptionalValue<math::TVector2<Element>, Owner>, SdlValueType>)
44 {
45 auto data = SdlNativeData(
46 [&optVec2 = this->valueRef(owner)](std::size_t elementIdx) -> SdlGetterVariant
47 {
48 return optVec2
49 ? SdlNativeData::permissiveElementGetter(&((*optVec2)[elementIdx]))
50 : std::monostate{};
51 },
52 [&optVec2 = this->valueRef(owner)](std::size_t elementIdx, SdlSetterVariant input) -> bool
53 {
54 if(input.isEmpty())
55 {
56 optVec2 = std::nullopt;
57 return true;
58 }
59 else
60 {
61 if(!optVec2)
62 {
63 optVec2 = math::TVector2<Element>{};
64 }
65
66 return SdlNativeData::permissiveElementSetter(input, &((*optVec2)[elementIdx]));
67 }
68 },
69 AnyNonConstPtr(vec2));
70
72 data.elementType = sdl::number_type_of<Element>();
73 data.numElements = vec2 ? 2 : 0;
74 data.tupleSize = 2;
75 data.isNullClearable = true;
76 return data;
77 }
78 else
79 {
80 SdlNativeData data;
81 if(vec2)
82 {
83 data = SdlNativeData(
84 [vec2](std::size_t elementIdx) -> SdlGetterVariant
85 {
86 return SdlNativeData::permissiveElementGetter(&((*vec2)[elementIdx]));
87 },
88 [vec2](std::size_t elementIdx, SdlSetterVariant input) -> bool
89 {
90 return SdlNativeData::permissiveElementSetter(input, &((*vec2)[elementIdx]));
91 },
92 AnyNonConstPtr(vec2));
93 }
96 data.numElements = vec2 ? 2 : 0;
97 data.tupleSize = 2;
98 return data;
99 }
100 }
101
102protected:
104 Owner& owner,
105 const SdlInputClause& clause,
106 const SdlInputContext& ctx) const override
107 {
108 this->setValue(owner, sdl::load_vector2<Element>(clause.value));
109 }
110
112 const Owner& owner,
113 SdlOutputClause& out_clause,
114 const SdlOutputContext& ctx) const override
115 {
116 if(const math::TVector2<Element>* vec2 = this->getConstValue(owner); vec2)
117 {
118 sdl::save_vector2(*vec2, out_clause.value);
119 }
120 else
121 {
122 out_clause.isEmpty = true;
123 }
124 }
125};
126
127template<typename Owner, typename Element>
129
130template<typename Owner>
132
133template<typename Owner>
135
136template<typename Owner>
138
139template<typename Owner>
141
142}// end namespace ph
Carries SDL representation of various data during the input process. Helps to read input data such as...
Definition SdlInputClause.h:15
std::string value
Loaded stringified data of a clause. All potential SDL value prefixes or suffixes (e....
Definition SdlInputClause.h:25
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
Definition SdlNativeData.h:88
static auto permissiveElementSetter(SdlSetterVariant input, ElementType *out_elementPtr) -> bool
Given a valid target element, set its value in a permissive way (with auto conversions).
Definition SdlNativeData.ipp:349
std::size_t numElements
Hint for number of elements in this block of native data. For example, numElements would be 12 for an...
Definition SdlNativeData.h:99
std::size_t tupleSize
Hint for number of elements that form a natural group. For an array of 10 vec3s, tupleSize may have a...
Definition SdlNativeData.h:105
static auto permissiveElementGetter(ElementType *elementPtr) -> SdlGetterVariant
Given a valid target element, get its value in a permissive way (with auto conversions).
Definition SdlNativeData.ipp:312
ESdlDataType elementType
Hint for the type of elements.
Definition SdlNativeData.h:113
ESdlDataFormat elementContainer
Hint for the type that encapsulates elements.
Definition SdlNativeData.h:109
Carries SDL representation of various data during the output process. Helps to write output data such...
Definition SdlOutputClause.h:14
std::string value
Stores stringified data of a clause. As the output clause generator knows best how its data look like...
Definition SdlOutputClause.h:29
bool isEmpty
If the clause carries no data and does not need to be written.
Definition SdlOutputClause.h:41
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Definition SdlNativeData.h:24
void setValue(Owner &owner, T value) const override
Store a value.
Definition TSdlValue.ipp:38
const T * getConstValue(const Owner &owner) const override
Get a pointer to the stored value.
Definition TSdlValue.ipp:50
T * getValue(Owner &owner) const override
Get a pointer to the stored value.
Definition TSdlValue.ipp:44
TSdlValue & defaultTo(T defaultValue)
Definition TSdlValue.ipp:71
T & valueRef(Owner &owner) const
Definition TSdlValue.ipp:132
Definition TSdlVector2.h:20
void saveToSdl(const Owner &owner, SdlOutputClause &out_clause, const SdlOutputContext &ctx) const override
Convert actual value back to SDL value. Saving a loaded value as SDL value should rarely fail–as load...
Definition TSdlVector2.h:111
std::string valueAsString(const math::TVector2< Element > &vec2) const override
Definition TSdlVector2.h:35
SdlNativeData ownedNativeData(Owner &owner) const override
Direct access to the field memory of an owner. Short-lived owner objects such as function parameter s...
Definition TSdlVector2.h:40
void loadFromSdl(Owner &owner, const SdlInputClause &clause, const SdlInputContext &ctx) const override
Load SDL value to actual value and store it in the owner's field. Implementations are highly encourag...
Definition TSdlVector2.h:103
TSdlVector2(std::string valueName, ValueType Owner::*const valuePtr)
Definition TSdlVector2.h:26
Represents a 2-D vector.
Definition TVector2.h:19
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
math::TVector2< Element > load_vector2(std::string_view sdlVec2Str)
Definition sdl_helpers.ipp:115
constexpr ESdlDataType number_type_of()
Definition sdl_helpers.ipp:478
void save_vector2(const math::TVector2< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:308
The root for all renderer implementations.
Definition EEngineProject.h:6
TAnyPtr< false > AnyNonConstPtr
A type-safe, lightweight wrapper for any non-const raw pointer type.
Definition TAnyPtr.h:47
Definition TAABB2D.h:96
Low-level helpers for SDL. Helpers are in an additional sdl namespace.