Added support for enabling and disabling scripts

OnEnable and OnDisable are not called on GameObject active toggling for now but will be added in future
This commit is contained in:
Kah Wei 2022-11-21 17:16:16 +08:00
parent 819607925f
commit e3c573baa7
5 changed files with 188 additions and 64 deletions

View File

@ -155,16 +155,23 @@ namespace SHADE
}
bool SHEditorUI::InputCheckbox(const std::string& label, bool& value, bool* isHovered)
{
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
return ImGui::Checkbox("##", &value);
}
bool SHEditorUI::InputInt(const std::string& label, int& value, bool* isHovered)
{
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -177,7 +184,11 @@ namespace SHADE
bool SHEditorUI::InputUnsignedInt(const std::string& label, unsigned int& value, bool* isHovered)
{
int signedVal = static_cast<int>(value);
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -190,8 +201,12 @@ namespace SHADE
return CHANGED;
}
bool SHEditorUI::InputFloat(const std::string& label, float& value, bool* isHovered)
{
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -212,8 +227,12 @@ namespace SHADE
return CHANGED;
}
bool SHEditorUI::InputSlider(const std::string& label, int min, int max, int& value, bool* isHovered /*= nullptr*/)
{
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -235,8 +254,12 @@ namespace SHADE
}
bool SHEditorUI::InputSlider(const std::string& label, float min, float max, float& value, bool* isHovered)
{
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -264,7 +287,7 @@ namespace SHADE
}
bool SHEditorUI::InputVec3(const std::string& label, SHVec3& value, bool* isHovered)
{
static const std::vector<std::string> COMPONENT_LABELS = { "X", "Y", "Z"};
static const std::vector<std::string> COMPONENT_LABELS = { "X", "Y", "Z" };
return SHEditorWidgets::DragN<float, 3>(label, COMPONENT_LABELS, { &value.x, &value.y, &value.z }, 0.1f, "%.3f", float{}, float{}, 0, isHovered);
}
@ -272,7 +295,11 @@ namespace SHADE
{
std::array<char, TEXT_FIELD_MAX_LENGTH> buffer = { '\0' };
strcpy_s(buffer.data(), TEXT_FIELD_MAX_LENGTH, value.c_str());
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -285,8 +312,12 @@ namespace SHADE
}
bool SHEditorUI::InputGameObjectField(const std::string& label, uint32_t& value, bool* isHovered, bool alwaysNull)
{
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();
@ -326,7 +357,11 @@ namespace SHADE
const std::string& INITIAL_NAME = v >= static_cast<int>(enumNames.size()) ? "Unknown" : enumNames[v];
bool b = false;
if (!label.empty())
{
ImGui::Text(label.c_str());
ImGui::SameLine();
}
if (isHovered)
*isHovered = ImGui::IsItemHovered();
ImGui::SameLine();

View File

@ -117,6 +117,12 @@ namespace SHADE
// Header
SHEditorUI::PushID(index);
bool enabled = script->Enabled;
if (SHEditorUI::InputCheckbox("", enabled))
{
script->Enabled = enabled;
}
SHEditorUI::SameLine();
if (SHEditorUI::CollapsingHeader(LABEL))
{
SHEditorUI::PushID(LABEL);

View File

@ -22,6 +22,36 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
GameObject Script::Owner::get()
{
return owner;
}
GameObject Script::GameObject::get()
{
return owner;
}
bool Script::Enabled::get()
{
return enabled;
}
void Script::Enabled::set(bool value)
{
// Same, don't set
if (value == enabled)
return;
enabled = value;
// There's a change, so call the appropriate function
if (enabled)
OnEnable();
else
OnDisable();
}
/*---------------------------------------------------------------------------------*/
/* Component Access Functions */
/*---------------------------------------------------------------------------------*/
@ -104,11 +134,10 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* "All-time" Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::Initialize(GameObject newOwner)
void Script::Initialize(SHADE::GameObject newOwner)
{
owner = newOwner;
}
void Script::OnAttached()
{
SAFE_NATIVE_CALL_BEGIN
@ -131,6 +160,12 @@ namespace SHADE
awake();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnEnable()
{
SAFE_NATIVE_CALL_BEGIN
onEnable();
SAFE_NATIVE_CALL_END(this)
}
void Script::Start()
{
SAFE_NATIVE_CALL_BEGIN
@ -162,6 +197,12 @@ namespace SHADE
onDrawGizmos();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnDisable()
{
SAFE_NATIVE_CALL_BEGIN
onDisable();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnDestroy()
{
SAFE_NATIVE_CALL_BEGIN
@ -228,6 +269,7 @@ namespace SHADE
/* Virtual Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::awake() {}
void Script::onEnable() {}
void Script::start() {}
void Script::fixedUpdate() {}
void Script::update() {}
@ -236,6 +278,7 @@ namespace SHADE
{
OnGizmosDrawOverriden = false;
}
void Script::onDisable() {}
void Script::onDestroy() {}
/*---------------------------------------------------------------------------------*/

View File

@ -38,11 +38,28 @@ namespace SHADE
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// GameObject that this Script belongs to. This is a legacy interface, use
/// GameObject instead.
/// </summary>
[System::ObsoleteAttribute("Use GameObject instead.", false)]
property SHADE::GameObject Owner
{
SHADE::GameObject get();
}
/// <summary>
/// GameObject that this Script belongs to.
/// </summary>
property GameObject Owner
property SHADE::GameObject GameObject
{
GameObject get() { return owner; }
SHADE::GameObject get();
}
/// <summary>
/// Whether or not this Script should have it's update functions be executed.
/// </summary>
property bool Enabled
{
bool get();
void set(bool value);
}
/*-----------------------------------------------------------------------------*/
@ -127,7 +144,7 @@ namespace SHADE
/// </summary>
/// <typeparam name="T">
/// Type of script to get.
/// This needs to be a default constructable Script.
/// This needs to be a default constructible Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
@ -206,7 +223,7 @@ namespace SHADE
/// <summary>
/// Used to initialize a Script with a GameObject.
/// </summary>
void Initialize(GameObject newOwner);
void Initialize(SHADE::GameObject newOwner);
/// <summary>
/// Used to call onAttached(). This is called immediately when this script is
/// attached to a GameObject.
@ -232,6 +249,11 @@ namespace SHADE
/// </summary>
void Start();
/// <summary>
/// Used to call onEnable. This should be called right when a script is enabled
/// directly.
/// </summary>
void OnEnable();
/// <summary>
/// Used to call fixedUpdate(). This should be called in sync with Physics
/// update steps and thus in most cases will execute more than Update() will.
/// This will be called immediately before a Physics update step.
@ -253,6 +275,11 @@ namespace SHADE
/// </summary>
void OnDrawGizmos();
/// <summary>
/// Used to call onDisable. This should be called right when a script is disabled
/// directly.
/// </summary>
void OnDisable();
/// <summary>
/// Used to call onDestroy(). This should be called at the end of the frame
/// where the attached GameObject or this script is destroyed directly or
/// indirectly due to destruction of the owner.
@ -329,6 +356,10 @@ namespace SHADE
/// </summary>
virtual void awake();
/// <summary>
/// Called when this script is enabled.
/// </summary>
virtual void onEnable();
/// <summary>
/// Called on the first frame that the attached GameObject is active but always
/// after Awake().
/// </summary>
@ -353,6 +384,10 @@ namespace SHADE
/// </summary>
virtual void onDrawGizmos();
/// <summary>
/// Called when this script is disabled.
/// </summary>
virtual void onDisable();
/// <summary>
/// Called just before the end of the frame where the attached GameObject or
/// this script is destroyed directly or indirectly due to destruction of the
/// owner.
@ -403,7 +438,8 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
GameObject owner;
SHADE::GameObject owner;
bool enabled = true;
};
}

View File

@ -528,6 +528,7 @@ namespace SHADE
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
if (scripts[i]->Enabled)
scripts[i]->FixedUpdate();
}
}
@ -546,6 +547,7 @@ namespace SHADE
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
if (scripts[i]->Enabled)
scripts[i]->Update();
}
}
@ -564,6 +566,7 @@ namespace SHADE
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
if (scripts[i]->Enabled)
scripts[i]->LateUpdate();
}
}
@ -583,6 +586,7 @@ namespace SHADE
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
if (scripts[i]->Enabled)
scripts[i]->OnDrawGizmos();
}
}