简单添加ScriptComponent序列化功能
This commit is contained in:
@ -8,7 +8,7 @@ namespace Sandbox {
|
||||
// private TransformComponent m_Transform;
|
||||
// private RigidBody2DComponent m_Rigidbody;
|
||||
|
||||
|
||||
public float MoveSpeed;
|
||||
/*
|
||||
void OnCreate()
|
||||
{
|
||||
@ -23,7 +23,7 @@ namespace Sandbox {
|
||||
{
|
||||
// Console.WriteLine($"Player.OnUpdate: {ts}");
|
||||
|
||||
float speed = 1.0f;
|
||||
float speed = MoveSpeed;
|
||||
Vector3 velocity = Vector3.Zero;
|
||||
|
||||
if(Input.IsKeyDown(KeyCode.UP))
|
||||
|
||||
@ -8,7 +8,8 @@ namespace Sandbox {
|
||||
private TransformComponent m_Transform;
|
||||
private RigidBody2DComponent m_Rigidbody;
|
||||
|
||||
public float Speed = 1.0f;
|
||||
public float Force;
|
||||
public float Time;
|
||||
|
||||
void OnCreate()
|
||||
{
|
||||
@ -20,9 +21,9 @@ namespace Sandbox {
|
||||
|
||||
void OnUpdate(float ts)
|
||||
{
|
||||
// Console.WriteLine($"Player.OnUpdate: {ts}");
|
||||
Time += ts;
|
||||
|
||||
float speed = Speed;
|
||||
float speed = Force;
|
||||
Vector3 velocity = Vector3.Zero;
|
||||
|
||||
if(Input.IsKeyDown(KeyCode.W))
|
||||
@ -36,14 +37,6 @@ namespace Sandbox {
|
||||
velocity.X = 1.0f;
|
||||
|
||||
m_Rigidbody.ApplyLinearImpulseToCenter(velocity.XY * speed, true);
|
||||
|
||||
/*
|
||||
velocity *= speed;
|
||||
|
||||
Vector3 translation = m_Transform.Translation;
|
||||
translation += velocity * ts;
|
||||
m_Transform.Translation = translation;
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -48,6 +48,7 @@ namespace Hazel
|
||||
if (ImGui::MenuItem("Create Empty Entity"))
|
||||
{
|
||||
m_SelectionContext = m_Context->CreateEntity("Empty Entity");
|
||||
m_LastSelectionContext = m_SelectionContext;
|
||||
}
|
||||
else if (ImGui::MenuItem("Create Camera"))
|
||||
{
|
||||
@ -61,9 +62,9 @@ namespace Hazel
|
||||
}
|
||||
|
||||
ImGui::Begin("Properties");
|
||||
if (m_SelectionContext)
|
||||
if (m_LastSelectionContext)
|
||||
{
|
||||
DrawComponents(m_SelectionContext);
|
||||
DrawComponents(m_LastSelectionContext);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
@ -80,6 +81,7 @@ namespace Hazel
|
||||
if (ImGui::IsItemClicked())
|
||||
{
|
||||
m_SelectionContext = entity;
|
||||
m_LastSelectionContext = m_SelectionContext;
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopupContextItem())
|
||||
@ -100,6 +102,7 @@ namespace Hazel
|
||||
if (m_SelectionContext == entity)
|
||||
{
|
||||
m_SelectionContext = {};
|
||||
m_LastSelectionContext = m_SelectionContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -358,40 +361,93 @@ namespace Hazel
|
||||
}
|
||||
});
|
||||
|
||||
DrawComponent<ScriptComponent>("Script", entity, [entity](auto& component) mutable
|
||||
DrawComponent<ScriptComponent>("Script", entity, [entity, &scene = m_Context](auto& component) mutable
|
||||
{
|
||||
const bool scriptClassExists = ScriptEngine::ClassExists(component.ClassName);
|
||||
bool scriptClassExists = ScriptEngine::ClassExists(component.ClassName);
|
||||
bool setStyleFlag = false;
|
||||
|
||||
static char buffer[64] = {};
|
||||
strcpy(buffer, component.ClassName.c_str());
|
||||
|
||||
if (!scriptClassExists)
|
||||
{
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.9f, 0.2f, 0.3f, 1.0f));
|
||||
setStyleFlag = true;
|
||||
}
|
||||
|
||||
if (ImGui::InputText("Script", buffer, sizeof(buffer)))
|
||||
if (ImGui::InputText("Class", buffer, sizeof(buffer)))
|
||||
{
|
||||
component.ClassName = buffer;
|
||||
scriptClassExists = ScriptEngine::ClassExists(component.ClassName);
|
||||
}
|
||||
|
||||
|
||||
// Fields
|
||||
|
||||
Ref<ScriptInstance> scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID());
|
||||
|
||||
if (scriptInstance)
|
||||
const bool sceneRunning = scene->IsRunning();
|
||||
if (sceneRunning)
|
||||
{
|
||||
const auto& fields = scriptInstance->GetScriptClass()->GetFields();
|
||||
for (const auto&[name, field] : fields)
|
||||
if (scriptInstance)
|
||||
{
|
||||
if (field.Type == ScriptFieldType::Float)
|
||||
const auto& fields = scriptInstance->GetScriptClass()->GetFields();
|
||||
for (const auto& [name, field] : fields)
|
||||
{
|
||||
float data = scriptInstance->GetFieldValue<float>(name);
|
||||
if (ImGui::DragFloat(name.c_str(), &data))
|
||||
if (field.Type == ScriptFieldType::Float)
|
||||
{
|
||||
scriptInstance->SetFieldValue(name, data);
|
||||
float data = scriptInstance->GetFieldValue<float>(name);
|
||||
if (ImGui::DragFloat(name.c_str(), &data))
|
||||
{
|
||||
scriptInstance->SetFieldValue(name, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (scriptClassExists)
|
||||
{
|
||||
Ref<ScriptClass> entityClass = ScriptEngine::GetEntityClass(component.ClassName);
|
||||
const auto& fields = entityClass->GetFields();
|
||||
auto& entityFields = ScriptEngine::GetScriptFieldMap(entity);
|
||||
|
||||
if (!scriptClassExists)
|
||||
|
||||
for (const auto& [name, field] : fields)
|
||||
{
|
||||
// field has been set in Editor
|
||||
if (entityFields.contains(name))
|
||||
{
|
||||
ScriptFieldInstance& scriptField = entityFields.at(name);
|
||||
|
||||
// display control to set
|
||||
if (field.Type == ScriptFieldType::Float)
|
||||
{
|
||||
float data = scriptField.GetValue<float>();
|
||||
if (ImGui::DragFloat(name.c_str(), &data))
|
||||
scriptField.SetValue(data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// display control to set
|
||||
if (field.Type == ScriptFieldType::Float)
|
||||
{
|
||||
float data = 0.0f;
|
||||
if (ImGui::DragFloat(name.c_str(), &data))
|
||||
{
|
||||
ScriptFieldInstance& fieldInstance = entityFields[name];
|
||||
fieldInstance.Field = field;
|
||||
fieldInstance.SetValue(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (setStyleFlag)
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
});
|
||||
@ -478,5 +534,6 @@ namespace Hazel
|
||||
void SceneHierachyPanel::SetSelectedEntity(const Entity entity)
|
||||
{
|
||||
m_SelectionContext = entity;
|
||||
m_LastSelectionContext = m_SelectionContext;
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,7 @@ namespace Hazel
|
||||
|
||||
Ref<Scene> m_Context;
|
||||
Entity m_SelectionContext;
|
||||
Entity m_LastSelectionContext;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user