feat: add imguizmo + trigger buxfix

This commit is contained in:
2026-10-18 11:45:37 +02:00
parent 70c940c476
commit 4bd99b25d1
37 changed files with 700 additions and 58 deletions
@@ -34,9 +34,12 @@ FirstPersonController::FirstPersonController(GameObject& parent)
{
}
void FirstPersonController::SetMouseCaptured(bool captured)
void FirstPersonController::SetPossessed(bool possessed)
{
InputManager::GetInstance().SetMouseCaptured(captured);
m_Possessed = possessed;
m_JumpHeld = false;
m_JumpBufferTimer = 0.0f;
InputManager::GetInstance().SetMouseCaptured(possessed);
}
void FirstPersonController::Start()
@@ -65,26 +68,16 @@ void FirstPersonController::Update(float dt)
{
const auto& input = InputManager::GetInstance();
if (input.WasKeyPressed(SDL_SCANCODE_F5))
{
SetMouseCaptured(!IsMouseCaptured());
}
m_FocusedInteractable = nullptr;
if (!IsMouseCaptured())
if (!m_Possessed)
{
m_JumpHeld = false;
if (input.WasMousePressed(SDL_BUTTON_LEFT) && !ImGui::GetIO().WantCaptureMouse)
{
SetMouseCaptured(true);
}
return;
}
if (input.WasKeyPressed(SDL_SCANCODE_ESCAPE))
{
SetMouseCaptured(false);
SetPossessed(false);
return;
}
@@ -143,7 +136,7 @@ void FirstPersonController::FixedUpdate(float fixedDt)
m_CoyoteTimer = m_Grounded ? m_CoyoteTime : std::max(m_CoyoteTimer - fixedDt, 0.0f);
const auto& input = InputManager::GetInstance();
const bool sprinting = IsMouseCaptured() && input.IsKeyDown(SDL_SCANCODE_LSHIFT);
const bool sprinting = m_Possessed && input.IsKeyDown(SDL_SCANCODE_LSHIFT);
const glm::vec3 wishVelocity = GetWishDirection() * (sprinting ? m_SprintSpeed : m_WalkSpeed);
const float acceleration = m_Grounded ? m_GroundAcceleration : m_AirAcceleration;
@@ -211,7 +204,7 @@ bool FirstPersonController::CheckGrounded() const
glm::vec3 FirstPersonController::GetWishDirection() const
{
if (!IsMouseCaptured())
if (!m_Possessed)
return glm::vec3{0.0f};
const auto& input = InputManager::GetInstance();
@@ -255,10 +248,8 @@ void FirstPersonController::ImGuiInspector()
ImGui::Text("Grounded: %s", m_Grounded ? "yes" : "no");
ImGui::Text("Yaw: %.1f deg", glm::degrees(m_Yaw));
ImGui::Text("Pitch: %.1f deg", glm::degrees(m_Pitch));
bool captured = IsMouseCaptured();
ImGui::Checkbox("Mouse Captured", &captured);
if (captured != IsMouseCaptured()) {
SetMouseCaptured(captured);
if (bool possessed = m_Possessed; ImGui::Checkbox("Possessed", &possessed)) {
SetPossessed(possessed);
}
}
@@ -33,14 +33,18 @@ InteractableButton::InteractableButton(GameObject& parent)
void InteractableButton::Start()
{
ApplyColor();
if (m_CycleColors) {
ApplyColor();
}
}
void InteractableButton::Interact(GameObject& interactor)
{
m_ColorIndex = (m_ColorIndex + 1) % ButtonColors.size();
++m_PressCount;
ApplyColor();
if (m_CycleColors) {
m_ColorIndex = (m_ColorIndex + 1) % ButtonColors.size();
ApplyColor();
}
spdlog::info("{} pressed {} by {}", GetGameObject()->GetName(), m_PressCount, interactor.GetName());
ActivateTargets(interactor);
@@ -107,6 +111,7 @@ nlohmann::json InteractableButton::Serialize() const
{
return {
{"colorIndex", m_ColorIndex},
{"cycleColors", m_CycleColors},
{"pressCount", m_PressCount},
{"targets", m_Targets},
};
@@ -115,6 +120,7 @@ nlohmann::json InteractableButton::Serialize() const
void InteractableButton::Deserialize(const nlohmann::json& data)
{
m_ColorIndex = data.value("colorIndex", m_ColorIndex) % ButtonColors.size();
m_CycleColors = data.value("cycleColors", m_CycleColors);
m_PressCount = data.value("pressCount", m_PressCount);
m_Targets = data.value("targets", std::vector<ObjectId>{});
}
@@ -125,6 +131,9 @@ void InteractableButton::ImGuiInspector()
if (ImGui::Button("Press")) {
Interact(*GetGameObject());
}
if (ImGui::Checkbox("Cycle Colors", &m_CycleColors) && m_CycleColors) {
ApplyColor();
}
DrawTargetsInspector();
}
@@ -0,0 +1,68 @@
#include <components/LightToggle.h>
#include <destrum/Components/LightComponent.h>
#include <destrum/Components/MeshRendererComponent.h>
#include <destrum/Graphics/Renderer.h>
#include <destrum/ObjectModel/GameObject.h>
#include <destrum/Util/GameState.h>
#include <imgui.h>
LightToggle::LightToggle(GameObject& parent)
: Activatable(parent, "LightToggle")
{
}
void LightToggle::Start()
{
Apply();
}
void LightToggle::SetOn(bool on)
{
m_On = on;
Apply();
}
void LightToggle::OnActivate(GameObject&)
{
SetOn(!m_On);
}
void LightToggle::Apply()
{
GameObject* object = GetGameObject();
auto* light = object->GetComponent<LightComponent>();
if (light != nullptr) {
light->SetEnabled(m_On);
}
// The bulb glows in the light's colour while it is on
const auto* meshRenderer = object->GetComponent<MeshRendererComponent>();
if (meshRenderer == nullptr || meshRenderer->GetMaterialID() == NULL_MATERIAL_ID ||
!GameState::GetInstance().HasRenderer()) {
return;
}
GameRenderer& renderer = GameState::GetInstance().Renderer();
Material& material = renderer.getMaterialMutable(meshRenderer->GetMaterialID());
material.emissiveColor = m_On && light != nullptr ? light->GetColor() : glm::vec3{0.0f};
renderer.updateMaterialGPU(meshRenderer->GetMaterialID());
}
nlohmann::json LightToggle::Serialize() const
{
return {{"on", m_On}};
}
void LightToggle::Deserialize(const nlohmann::json& data)
{
m_On = data.value("on", m_On);
}
void LightToggle::ImGuiInspector()
{
if (bool on = m_On; ImGui::Checkbox("On", &on)) {
SetOn(on);
}
}