28typedef std::variant<int, float, bvec, ivec, vec, std::string>
PropertyValue;
82 void changed(std::any argument)
const;
86 name(_name), type(_type), min(_min), def(_def), max(_max), on_change(_on_change) {}
89 name(_name), type(_type), def(_def), on_change(_on_change) {}
101template<
typename PropertyMetaT = PropertyMeta>
106 const PropertyMetaT& meta;
123 std::string to_string()
const;
137 virtual void pack(std::vector<uint8_t>& buf)
const;
138 virtual size_t unpack(
const uint8_t* buf,
size_t len);
140 Property(
const PropertyMetaT& _meta);
160template<
typename PropertyMetaT>
163 return std::get<int>(value);
174template<
typename PropertyMetaT>
177 return std::get<float>(value);
188template<
typename PropertyMetaT>
191 return std::get<bvec>(value);
202template<
typename PropertyMetaT>
205 return std::get<ivec>(value);
216template<
typename PropertyMetaT>
219 return std::get<vec>(value);
230template<
typename PropertyMetaT>
233 return std::get<std::string>(value);
244template<
typename PropertyMetaT>
249 case PropertyType::Int:
251 case PropertyType::Float:
252 return sizeof(float);
253 case PropertyType::Color:
255 case PropertyType::IntVec:
257 case PropertyType::FloatVec:
259 case PropertyType::String:
260 return get_string().size();
266template<
typename PropertyMetaT>
271 case PropertyType::Int:
272 return std::to_string(get_int());
274 case PropertyType::Float:
275 return std::to_string(get_float());
277 case PropertyType::Color:
278 return std::to_string(get_color().r) +
" " + std::to_string(get_color().g) +
" " + std::to_string(get_color().b);
280 case PropertyType::IntVec:
281 return std::to_string(get_ivec().x) +
" " + std::to_string(get_ivec().y) +
" " + std::to_string(get_ivec().z);
283 case PropertyType::FloatVec:
284 return std::to_string(get_fvec().x) +
" " + std::to_string(get_fvec().y) +
" " + std::to_string(get_fvec().z);
286 case PropertyType::String:
294template<
typename PropertyMetaT>
295bool Property<PropertyMetaT>::set_check_type(PropertyType type)
const
303 if(get_type() != type)
323template<
typename PropertyMetaT>
337template<
typename PropertyMetaT>
340 return meta.get_type();
353template<
typename PropertyMetaT>
356 set_no_cb(prop.value);
359template<
typename PropertyMetaT>
364 case PropertyType::Int:
366 int v = std::get<int>(value);
367 this->value = std::clamp(v, std::get<int>(meta.get_min()), std::get<int>(meta.get_max()));
371 case PropertyType::Float:
373 float v = std::get<float>(value);
374 this->value = std::clamp(v, std::get<float>(meta.get_min()), std::get<float>(meta.get_max()));
378 case PropertyType::IntVec:
380 ivec v = std::get<ivec>(value);
381 this->value =
ivec(v).min(std::get<ivec>(meta.get_max()))
382 .max(std::get<ivec>(meta.get_min()));
386 case PropertyType::FloatVec:
388 vec v = std::get<vec>(value);
389 this->value =
vec(v).min(std::get<vec>(meta.get_max()))
390 .max(std::get<vec>(meta.get_min()));
410template<
typename PropertyMetaT>
413 if(set_check_type(
static_cast<PropertyType>(value.index())))
416 meta.changed(on_change_arg);
429template<
typename PropertyMetaT>
432 if(set_check_type(
static_cast<PropertyType>(value.index())))
443template<
typename PropertyMetaT>
446 this->value = meta.get_def();
453 case PropertyType::Int:
454 intret(std::get<int>(value));
457 case PropertyType::Float:
461 case PropertyType::Color:
462 intret(std::get<bvec>(value).tohexcolor());
465 case PropertyType::IntVec:
467 const ivec& v = std::get<ivec>(value);
468 result(tempformatstring(
"%d %d %d", v.x, v.y, v.z));
472 case PropertyType::FloatVec:
474 const vec& v = std::get<vec>(value);
475 result(tempformatstring(
"%f %f %f", v.x, v.y, v.z));
479 case PropertyType::String:
480 result(std::get<std::string>(value).c_str());
492template<
typename PropertyMetaT>
495 _prop_cmd_result_value(value, get_type());
503template<
typename PropertyMetaT>
506 _prop_cmd_result_value(meta.get_min(), get_type());
514template<
typename PropertyMetaT>
517 _prop_cmd_result_value(meta.get_def(), get_type());
525template<
typename PropertyMetaT>
528 _prop_cmd_result_value(meta.get_max(), get_type());
537template<
typename PropertyMetaT>
540 size_t data_size = get_size();
542 vectorput(buf, data_size);
546 case PropertyType::Int:
548 vectorput(buf, get_int());
552 case PropertyType::Float:
554 vectorput(buf, get_float());
558 case PropertyType::Color:
560 vectorput(buf, get_color());
564 case PropertyType::IntVec:
566 vectorput(buf, get_ivec());
570 case PropertyType::FloatVec:
572 vectorput(buf, get_fvec());
576 case PropertyType::String:
578 std::string data = get_string();
580 vectorput(buf,
reinterpret_cast<const uint8_t*
>(data.data()), data.size());
597template<
typename PropertyMetaT>
601 const size_t* data_size_packed =
nullptr;
603 if(buf_size <=
sizeof(*data_size_packed))
609 data_size_packed =
reinterpret_cast<const size_t*
>(buf);
611 buf_read +=
sizeof(*data_size_packed);
613 static auto size_check = [&](
size_t needed_size)
615 if(buf_size - buf_read < needed_size)
626 case PropertyType::Int:
627 case PropertyType::Color:
628 case PropertyType::Float:
629 if(*data_size_packed != get_size())
643 case PropertyType::Int:
645 if(!size_check(get_size()))
651 memcpy(&ival, buf + buf_read, get_size());
652 buf_read += get_size();
657 case PropertyType::Float:
659 if(!size_check(get_size()))
665 memcpy(&fval, buf + buf_read, get_size());
666 buf_read += get_size();
671 case PropertyType::Color:
673 if(!size_check(get_size()))
679 memcpy(&cval, buf + buf_read, get_size());
680 buf_read += get_size();
685 case PropertyType::IntVec:
687 if(!size_check(get_size()))
693 memcpy(&ivecval, buf + buf_read, get_size());
694 buf_read += get_size();
699 case PropertyType::FloatVec:
701 if(!size_check(get_size()))
707 memcpy(&vecval, buf + buf_read, get_size());
708 buf_read += get_size();
713 case PropertyType::String:
715 if(!size_check(*data_size_packed))
720 std::string sval(
reinterpret_cast<const char*
>(buf + buf_read), *data_size_packed);
721 buf_read += *data_size_packed;
738template<
typename PropertyMetaT>
794void PropertyMeta::changed(std::any argument)
const
819template<
typename PropertyT, std::
size_t N>
820PropertyT*
find_prop(std::string name, std::array<PropertyT, N>& props)
822 for(PropertyT& prop : props)
824 if(prop.get_name() == name)
842template<
typename PropertyT, std::
size_t N>
843const PropertyT*
find_prop(std::string name,
const std::array<PropertyT, N>& props)
845 for(
const PropertyT& prop : props)
847 if(prop.get_name() == name)
867template<
typename PropertyT, std::
size_t N>
869 std::any on_change_arg = std::any())
871 PropertyT* prop =
find_prop(name, props);
878 prop->set(value, on_change_arg);
891template<
typename PropertyMetaT, std::
size_t N>
892const PropertyMetaT*
find_prop_meta(std::string name,
const std::array<PropertyMetaT, N>& prop_metas)
894 for(
const PropertyMetaT& prop_meta : prop_metas)
896 if(prop_meta.get_name() == name)
913template<
typename PropertyT, std::
size_t N>
914void pack_props(
const std::array<PropertyT, N>& props, std::vector<uint8_t>& buf)
916 for(
const PropertyT& prop : props)
931template<
typename PropertyT, std::
size_t N>
932size_t unpack_props(
const std::vector<uint8_t>& buf, std::array<PropertyT, N>& props)
937 while(read_pos < buf.size())
944 PropertyT &prop = props[prop_idx];
946 int unpacked_size = prop.unpack(buf.data() + read_pos, buf.size() - read_pos);
952 read_pos += unpacked_size;
959template<std::size_t N,
typename PropertyT, std::size_t... Is,
typename PropertyMetaT>
960std::array<PropertyT, N> _make_props_array_impl(std::index_sequence<Is...>,
961 const PropertyMetaT (&prop_meta)[N])
963 return { PropertyT(prop_meta[Is])... };
976template<std::size_t N,
typename PropertyT, std::size_t... Is,
typename PropertyMetaT>
979 return _make_props_array_impl<N, PropertyT>(std::make_index_sequence<N>{}, prop_meta);
A property.
Definition prop.h:103
void reset()
Reset the value of the property to the default value.
Definition prop.h:444
size_t get_size() const
Get the size of the property.
Definition prop.h:245
void cmd_result_def() const
Results the default value of the property.
Definition prop.h:515
const vec & get_fvec() const
Get the float vector value of the property.
Definition prop.h:217
void cmd_result_max() const
Results the maximum value of the property.
Definition prop.h:526
void set(PropertyValue value, std::any on_change_argument=std::any())
Set the value of the property.
Definition prop.h:411
const ivec & get_ivec() const
Get the integer vector value of the property.
Definition prop.h:203
PropertyType get_type() const
Get the type of the property.
Definition prop.h:338
void cmd_result() const
Results the value of the property.
Definition prop.h:493
const bvec & get_color() const
Get the color value of the property.
Definition prop.h:189
void copy(const Property &prop)
Copy the value of another property, will not invoke callback.
Definition prop.h:354
void set_no_cb(PropertyValue value)
Set the value of the property without invoking callback.
Definition prop.h:430
void cmd_result_min() const
Results the minimum value of the property.
Definition prop.h:504
float get_float() const
Get the float value of the property.
Definition prop.h:175
int get_int() const
Get the integer value of the property.
Definition prop.h:161
Property(const PropertyMetaT &_meta)
Creates a new property with the given meta class.
Definition prop.h:739
virtual void pack(std::vector< uint8_t > &buf) const
Packs the property into a buffer.
Definition prop.h:538
const std::string & get_string() const
Get the string value of the property.
Definition prop.h:231
virtual size_t unpack(const uint8_t *buf, size_t len)
Unpacks the property from a buffer.
Definition prop.h:598
std::string get_name() const
Get the name of the property.
Definition prop.h:324
void result(const char *s)
Returns an alias' name from a Cubescript command.
void intret(int v)
Returns an integer value from a Cubescript command.
void floatret(float v)
Returns a float value from a Cubescript command.
Needed for making games, along with iengine.h.
std::function< void(std::any argument)> OnChangeCallback
Callback type for property changes.
Definition prop.h:35
bool set_prop(std::string name, PropertyValue value, std::array< PropertyT, N > &props, std::any on_change_arg=std::any())
Sets the value of a property with the given name in the given array.
Definition prop.h:868
void pack_props(const std::array< PropertyT, N > &props, std::vector< uint8_t > &buf)
Packs the given properties into the given buffer.
Definition prop.h:914
std::variant< int, float, bvec, ivec, vec, std::string > PropertyValue
Storage for a property value.
Definition prop.h:28
PropertyType
Type of a property.
Definition prop.h:44
std::array< PropertyT, N > make_props_array(const PropertyMetaT(&prop_meta)[N])
Creates an array of properties from the given property definitions.
Definition prop.h:977
PropertyT * find_prop(std::string name, std::array< PropertyT, N > &props)
Finds a property with the given name in the given array.
Definition prop.h:820
size_t unpack_props(const std::vector< uint8_t > &buf, std::array< PropertyT, N > &props)
Unpacks the given buffer into the given properties.
Definition prop.h:932
const PropertyMetaT * find_prop_meta(std::string name, const std::array< PropertyMetaT, N > &prop_metas)
Finds a property definition with the given name in the given array.
Definition prop.h:892
three dimensional Cartesian byte vector
Definition geom.h:637
three dimensional Cartesian vector object
Definition geom.h:185