Skip to content

Commit

Permalink
Add basic debug rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelOtter committed Oct 3, 2024
1 parent 8e6a3be commit 86929f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/scene/include/growl/scene/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

enum class DebugRendering { OFF, ON, MOUSEOVER };
5 changes: 5 additions & 0 deletions src/scene/include/growl/scene/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "growl/core/error.h"
#include "growl/core/input/processor.h"
#include "growl/core/scripting/script.h"
#include "growl/scene/debug.h"
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -41,6 +42,8 @@ class Node : public InputProcessor, public Entity {

Error bindScript(API& api, Script& script);

void setDebugRendering(DebugRendering debug);

protected:
Node* getParent() {
return parent;
Expand Down Expand Up @@ -77,6 +80,8 @@ class Node : public InputProcessor, public Entity {
std::vector<std::unique_ptr<Node>> children;
glm::mat4x4 local_transform;
std::unique_ptr<ScriptingRef> bound_script_obj = nullptr;
DebugRendering debug_rendering = DebugRendering::OFF;
bool debug_mouseover = false;

void computeLocalTransform();
void drawChildren(Batch& batch, float parent_alpha);
Expand Down
27 changes: 27 additions & 0 deletions src/scene/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ using Growl::InputMouseEvent;
using Growl::Node;
using Growl::Script;

#ifdef GROWL_IMGUI
static const char* debug_rendering_options[]{"Off", "On", "Mouseover"};
#endif

std::string& Node::getLabel() {
return label;
}
Expand Down Expand Up @@ -49,6 +53,12 @@ void Node::populateDebugUI(Batch& batch) {
parent ? parent->getHeight() : batch.getTargetHeight(), "%.2f");
ImGui::SliderFloat(
"Rotation", &rotation, 0.0f, 2 * glm::pi<float>(), "%.2f");
int selected_item = (int)debug_rendering;
if (ImGui::Combo(
"Debug rendering", &selected_item, debug_rendering_options,
IM_ARRAYSIZE(debug_rendering_options))) {
setDebugRendering((DebugRendering)selected_item);
}

onPopulateDebugUI(batch);
ImGui::TreePop();
Expand Down Expand Up @@ -82,6 +92,13 @@ void Node::populateDebugUI(Batch& batch) {
#endif
}

void Node::setDebugRendering(DebugRendering debug_rendering) {
this->debug_rendering = debug_rendering;
for (auto& child : children) {
child->setDebugRendering(debug_rendering);
}
}

void Node::tick(double delta_time) {
if (!bound_script_obj) {
return onTick(delta_time);
Expand Down Expand Up @@ -110,6 +127,13 @@ void Node::draw(Batch& batch, float parent_alpha) {
populateDebugUI(batch);
}
onDraw(batch, parent_alpha, local_transform);
if (debug_rendering == DebugRendering::ON ||
(debug_rendering == DebugRendering::MOUSEOVER && debug_mouseover)) {
Color c = batch.getColor();
batch.setColor(1, 1, 1, 0.25f);
batch.drawRect(0, 0, getWidth(), getHeight(), local_transform);
batch.setColor(c);
}
}

void Node::onDraw(Batch& batch, float parent_alpha, glm::mat4x4 transform) {
Expand Down Expand Up @@ -204,6 +228,9 @@ bool Node::onMouseEvent(const InputMouseEvent& event) {
}

bool Node::onMouseEventRaw(const InputMouseEvent& event) {
if (debug_rendering == DebugRendering::MOUSEOVER) {
debug_mouseover = hit(event.mouseX, event.mouseY);
}
return InputProcessor::onMouseEvent(event);
}

Expand Down

0 comments on commit 86929f8

Please sign in to comment.