summaryrefslogtreecommitdiff
path: root/editor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor.cpp')
-rw-r--r--editor.cpp131
1 files changed, 129 insertions, 2 deletions
diff --git a/editor.cpp b/editor.cpp
index 9cc4539..32e4498 100644
--- a/editor.cpp
+++ b/editor.cpp
@@ -2,6 +2,7 @@
#include "debugdraw.hpp"
#include "editor.hpp"
#include "model.hpp"
+#include "physics.hpp"
#include "scene.hpp"
#include "ui.hpp"
#include "world.hpp"
@@ -11,13 +12,32 @@ extern "C" {
#include "plat.h"
}
+#include <vector>
+
+struct Editor_PD : Physics_Debughook {
+ void shrinkwrap(
+ const Int_Collider* col,
+ int axis,
+ const v2f* points,
+ int count
+ ) override;
+ void projection(
+ const Int_Collider* col,
+ const v3f& n,
+ int axis,
+ const v2f* points,
+ int count
+ ) override;
+};
+
static struct {
Editor_Settings settings;
+ Editor_PD physics_debughook;
UI* ui;
UI::Toolbar* toolbar;
UI::Button* mat_btn, * ent_btn, * phy_btn;
UI::Modal* mat_win, * ent_win, * phy_win;
- UI::Label* mat_name;
+ UI::Label* mat_name, * spl;
UI::Slider* metalness_input;
UI::Slider* roughness_input;
UI::Slider* ao_input;
@@ -28,8 +48,12 @@ static struct {
int selected_mesh;
int hovered_axis;
int dragging_gizmo;
+ int debug_sat_axis;
v3f drag_axis, drag_off;
v3f gf, gu, gr;
+ std::vector<v2f> shrinkwrap_points;
+ std::vector<v2f> projection_points;
+ v3f projection_start, projection_end;
} editor;
static int mat_win_handler(UI::Element* e, const UI::Message& m) {
@@ -215,6 +239,29 @@ static int phy_btn_handler(UI::Element* e, const UI::Message& m) {
}
return 0;
};
+ auto csat = ui->create_element<UI::Checkbox>(cont, "Debug SAT");
+ csat->set_val(editor.settings.debug_sat);
+ csat->handler = [](UI::Element* e, const UI::Message& m) {
+ if (m.type == UI::Message::Type::checkbox_changed) {
+ UI::Checkbox* ch = (UI::Checkbox*)e;
+ editor.settings.debug_sat = ch->val;
+ }
+ return 0;
+ };
+ int rows[] = { 150, 50, 0 };
+ auto tab = ui->create_element<UI::Table>(cont, rows);
+ ui->create_element<UI::Label>(tab, "Debug Axis");
+ auto iaxis = ui->create_element<UI::Int_Input>(tab);
+ iaxis->set_val(editor.debug_sat_axis);
+ iaxis->handler = [](UI::Element* e, const UI::Message& m) {
+ if (m.type == UI::Message::Type::input_finalised) {
+ UI::Int_Input* i = (UI::Int_Input*)e;
+ editor.debug_sat_axis = i->val;
+ }
+ return 0;
+ };
+ ui->create_element<UI::Label>(tab, "Shrinkwrap");
+ editor.spl = ui->create_element<UI::Label>(tab, "0");
}
return 0;
}
@@ -240,6 +287,7 @@ void init_editor(UI* ui, World* world) {
editor.mat_win = 0;
editor.ent_win = 0;
editor.phy_win = 0;
+ editor.debug_sat_axis = 0;
editor.world = world;
editor.dragging_gizmo = 0;
zero(&editor.settings, sizeof editor.settings);
@@ -249,7 +297,7 @@ void deinit_editor() {
}
void editor_on_select(Entity_Id e, int m) {
- if (!editor.ui->hovered) {
+ if (!editor.ui->hovered && !editor.dragging_gizmo) {
editor.selected = e;
if (e && editor.world->has<C_Model>(e)) {
auto& cm = editor.world->get<C_Model>(e);
@@ -300,6 +348,25 @@ void editor_draw(Line_Renderer& lr) {
lr.colour(v3f(0.0f, 0.0f, 1.0f));
lr.add_arrow(p, p + r);
}
+ if (w.has<Rigidbody>(e)) {
+ if (editor.settings.debug_sat) {
+ lr.colour(v3f(1.0f, 1.0f, 1.0f));
+ int i, c = editor.shrinkwrap_points.size();
+ for (i = 0; i < c; i++) {
+ v2f point = editor.shrinkwrap_points[i];
+ v2f prev = editor.shrinkwrap_points[(i + 1) % c];
+ lr.add_line(v3f(prev, 0.0f), v3f(point, 0.0f));
+ }
+ lr.colour(v3f(1.0f, 0.0f, 0.0f));
+ c = editor.projection_points.size();
+ for (i = 0; i < c; i++) {
+ v2f point = editor.projection_points[i];
+ lr.add_point(v3f(point, 0.0f));
+ }
+ lr.colour(v3f(1.0f, 0.5f, 1.0f));
+ lr.add_arrow(editor.projection_start, editor.projection_end);
+ }
+ }
}
}
@@ -332,6 +399,10 @@ void editor_update(const App& app, const Camera& cam) {
m.m[3][0] += (gp.x - m.m[3][0]) * editor.drag_axis.x;
m.m[3][1] += (gp.y - m.m[3][1]) * editor.drag_axis.y;
m.m[3][2] += (gp.z - m.m[3][2]) * editor.drag_axis.z;
+ if (w.has<Rigidbody>(e)) {
+ Rigidbody& rb = w.get<Rigidbody>(e);
+ rb.set_pos(v3f(m.m[3][0], m.m[3][1], m.m[3][2]));
+ }
if (app.mjr(mbtn_left))
editor.dragging_gizmo = 0;
return;
@@ -383,3 +454,59 @@ void editor_update(const App& app, const Camera& cam) {
Editor_Settings& editor_settings() {
return editor.settings;
}
+
+Physics_Debughook* editor_physics_debughook() {
+ return &editor.physics_debughook;
+}
+
+void Editor_PD::shrinkwrap(
+ const Int_Collider* col,
+ int axis,
+ const v2f* points,
+ int count
+) {
+ World& world = *editor.world;
+ Entity_Id s = editor.selected;
+ Rigidbody* rb;
+ if (!editor.settings.debug_sat) return;
+ if (!s) return;
+ if (!world.has<Rigidbody>(s)) return;
+ rb = &world.get<Rigidbody>(s);
+ if (axis == editor.debug_sat_axis && col->rb == rb) {
+ int i;
+ auto& v = editor.shrinkwrap_points;
+ v.clear();
+ editor.spl->set_int(count);
+ for (i = 0; i < count; i++)
+ v.push_back(points[i]);
+ }
+}
+
+void Editor_PD::projection(
+ const Int_Collider* col,
+ const v3f& n,
+ int axis,
+ const v2f* points,
+ int count
+) {
+ World& world = *editor.world;
+ Entity_Id s = editor.selected;
+ Rigidbody* rb;
+ if (!editor.settings.debug_sat) return;
+ if (!s) return;
+ if (!world.has<Rigidbody>(s)) return;
+ rb = &world.get<Rigidbody>(s);
+ if (axis == editor.debug_sat_axis && col->rb == rb) {
+ int i;
+ float w = 1.0f / (float)count;
+ auto& v = editor.projection_points;
+ v.clear();
+ editor.projection_start = v3f(0.0f);
+ for (i = 0; i < count; i++) {
+ const v2f& point = points[i];
+ v.push_back(point);
+ editor.projection_start += v3f(point, 0.0f) * w;
+ }
+ editor.projection_end = editor.projection_start + n;
+ }
+}