Renamed all instances of "Property" to "Field" in ComponentWrapper to maintain consistent naming with ComponentInfo

This commit is contained in:
2016-01-16 23:24:45 +01:00
parent 9ec32464bc
commit d8a24bbfde
3 changed files with 20 additions and 21 deletions
+10 -11
View File
@@ -5,8 +5,7 @@
#include "Entity.h" #include "Entity.h"
#include "ComponentInfo.h" #include "ComponentInfo.h"
#include "Util/Any.h" #include "Util/Any.h"
Minecraft hard drilling
// TODO: Change all instances "Property" to "Field" to remain consistent with ComponentInfo
struct ComponentWrapper struct ComponentWrapper
{ {
ComponentWrapper(const ComponentInfo& componentInfo, char* data) ComponentWrapper(const ComponentInfo& componentInfo, char* data)
@@ -25,20 +24,20 @@ struct ComponentWrapper
} }
template <typename T> template <typename T>
T& Property(std::string name) T& Field(std::string name)
{ {
unsigned int offset = Info.Fields.at(name).Offset; unsigned int offset = Info.Fields.at(name).Offset;
return *reinterpret_cast<T*>(&Data[offset]); return *reinterpret_cast<T*>(&Data[offset]);
} }
template <typename T> template <typename T>
void SetProperty(std::string name, const T value) { Property<T>(name) = value; } void SetField(std::string name, const T value) { Field<T>(name) = value; }
//template <typename T> //template <typename T>
//void SetProperty(std::string name, T& value) { Property<T>(name) = value; } //void SetField(std::string name, T& value) { Field<T>(name) = value; }
// Specialization for string literals // Specialization for string literals
template <std::size_t N> template <std::size_t N>
void SetProperty(std::string name, const char(&value)[N]) { Property<std::string>(name) = std::string(value); } void SetField(std::string name, const char(&value)[N]) { Field<std::string>(name) = std::string(value); }
struct SubscriptProxy struct SubscriptProxy
{ {
@@ -57,17 +56,17 @@ struct ComponentWrapper
int Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); } int Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); }
template <typename T> template <typename T>
operator T&() { return m_Component->Property<T>(m_PropertyName); } operator T&() { return m_Component->Field<T>(m_PropertyName); }
template <typename T> template <typename T>
void operator=(const T val) { m_Component->SetProperty<T>(m_PropertyName, val); } void operator=(const T val) { m_Component->SetField<T>(m_PropertyName, val); }
// TODO: Pass by reference and rvalue (universal reference?) // TODO: Pass by reference and rvalue (universal reference?)
//template <typename T> //template <typename T>
//void operator=(T& val) { m_Component->SetProperty<T>(m_PropertyName, val); } //void operator=(T& val) { m_Component->SetField<T>(m_PropertyName, val); }
// Specialization for string literals // Specialization for string literals
template<std::size_t N> template <std::size_t N>
void operator=(const char(&val)[N]) { m_Component->SetProperty<N>(m_PropertyName, val); } void operator=(const char(&val)[N]) { m_Component->SetField<N>(m_PropertyName, val); }
}; };
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); } SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
}; };
+9 -9
View File
@@ -496,7 +496,7 @@ void EditorSystem::drawUI(World* world, double dt)
std::string uniqueID = componentType + fieldName; std::string uniqueID = componentType + fieldName;
ImGui::PushID(uniqueID.c_str()); ImGui::PushID(uniqueID.c_str());
if (field.Type == "Vector") { if (field.Type == "Vector") {
auto& val = component.Property<glm::vec3>(fieldName); auto& val = component.Field<glm::vec3>(fieldName);
if (fieldName == "Scale") { if (fieldName == "Scale") {
ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, 0.f, std::numeric_limits<float>::max()); ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, 0.f, std::numeric_limits<float>::max());
} else if (fieldName == "Orientation") { } else if (fieldName == "Orientation") {
@@ -508,10 +508,10 @@ void EditorSystem::drawUI(World* world, double dt)
ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max()); ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max());
} }
} else if (field.Type == "Color") { } else if (field.Type == "Color") {
auto& val = component.Property<glm::vec4>(fieldName); auto& val = component.Field<glm::vec4>(fieldName);
ImGui::ColorEdit4("", glm::value_ptr(val), true); ImGui::ColorEdit4("", glm::value_ptr(val), true);
} else if (field.Type == "string") { } else if (field.Type == "string") {
std::string& val = component.Property<std::string>(fieldName); std::string& val = component.Field<std::string>(fieldName);
char tempString[1024]; char tempString[1024];
memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString))); memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString)));
if (ImGui::InputText("", tempString, sizeof(tempString))) { if (ImGui::InputText("", tempString, sizeof(tempString))) {
@@ -525,15 +525,15 @@ void EditorSystem::drawUI(World* world, double dt)
} }
} else if (field.Type == "double") { } else if (field.Type == "double") {
float tempVal = static_cast<float>(component.Property<double>(fieldName)); float tempVal = static_cast<float>(component.Field<double>(fieldName));
if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) { if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) {
component.SetProperty(fieldName, static_cast<double>(tempVal)); component.SetField(fieldName, static_cast<double>(tempVal));
} }
} else if (field.Type == "int") { } else if (field.Type == "int") {
int val = component.Property<int>(fieldName); int val = component.Field<int>(fieldName);
ImGui::InputInt("", &val); ImGui::InputInt("", &val);
} else if (field.Type == "enum") { } else if (field.Type == "enum") {
int currentValue = component.Property<int>(fieldName); int currentValue = component.Field<int>(fieldName);
int item = -1; int item = -1;
std::stringstream enumKeys; std::stringstream enumKeys;
std::vector<int> enumValues; std::vector<int> enumValues;
@@ -547,10 +547,10 @@ void EditorSystem::drawUI(World* world, double dt)
i++; i++;
} }
if (ImGui::Combo("", &item, enumKeys.str().c_str())) { if (ImGui::Combo("", &item, enumKeys.str().c_str())) {
component.SetProperty(fieldName, enumValues.at(item)); component.SetField(fieldName, enumValues.at(item));
} }
} else if (field.Type == "bool") { } else if (field.Type == "bool") {
auto& val = component.Property<bool>(fieldName); auto& val = component.Field<bool>(fieldName);
ImGui::Checkbox("", &val); ImGui::Checkbox("", &val);
} else { } else {
ImGui::TextDisabled(field.Type.c_str()); ImGui::TextDisabled(field.Type.c_str());
+1 -1
View File
@@ -20,7 +20,7 @@ BOOST_AUTO_TEST_CASE(WorldTestSingleAllocation, * boost::unit_test::tolerance(0.
ComponentWrapper c = w.AttachComponent(e, "Test"); ComponentWrapper c = w.AttachComponent(e, "Test");
// Check default values // Check default values
BOOST_TEST((int)c["TestInteger"] == c.Property<int>("TestInteger")); BOOST_TEST((int)c["TestInteger"] == c.Field<int>("TestInteger"));
BOOST_TEST((int)c["TestInteger"] == 1337); BOOST_TEST((int)c["TestInteger"] == 1337);
BOOST_TEST((double)c["TestDouble"] == 13.37); BOOST_TEST((double)c["TestDouble"] == 13.37);
BOOST_TEST((std::string)c["TestString"] == "Carlito"); BOOST_TEST((std::string)c["TestString"] == "Carlito");