Files
armorpaint/Sources/arm/plugin/Gizmo.hx
T

154 lines
5.5 KiB
Haxe
Raw Normal View History

2019-06-19 22:42:17 +02:00
package arm.plugin;
2019-05-10 19:28:59 +02:00
import iron.object.MeshObject;
2019-06-20 11:23:01 +02:00
import iron.object.LightObject;
import iron.system.Input;
import iron.math.RayCaster;
import iron.math.Vec4;
import iron.Scene;
2020-03-18 20:26:28 +01:00
import arm.ui.UISidebar;
2019-05-10 19:28:59 +02:00
class Gizmo {
public static function update() {
2020-03-18 20:26:28 +01:00
var gizmo = UISidebar.inst.gizmo;
2019-05-10 19:28:59 +02:00
if (!gizmo.visible) return;
2019-06-23 14:50:41 +02:00
if (Context.object != null) {
2019-08-25 21:22:19 +02:00
var cam = Scene.active.camera;
2019-06-23 14:50:41 +02:00
gizmo.transform.loc.setFrom(Context.object.transform.loc);
2019-08-25 21:22:19 +02:00
var dist = Vec4.distance(cam.transform.loc, gizmo.transform.loc) / 10;
gizmo.transform.scale.set(dist, dist, dist);
2020-03-18 20:26:28 +01:00
UISidebar.inst.gizmoX.transform.scale.set(dist, dist, dist);
UISidebar.inst.gizmoY.transform.scale.set(dist, dist, dist);
UISidebar.inst.gizmoZ.transform.scale.set(dist, dist, dist);
2019-05-10 19:28:59 +02:00
gizmo.transform.buildMatrix();
}
2019-06-20 11:23:01 +02:00
var mouse = Input.getMouse();
var kb = Input.getKeyboard();
2019-05-10 19:28:59 +02:00
2019-10-11 20:12:31 +02:00
if (mouse.viewX < App.w()) {
2019-05-10 19:28:59 +02:00
if (kb.started("delete") || kb.started("backspace")) {
2019-06-23 14:50:41 +02:00
if (Context.object != null) {
Context.object.remove();
Context.selectObject(Scene.active.getChild("Scene"));
2019-05-10 19:28:59 +02:00
}
}
2019-06-23 14:50:41 +02:00
if (kb.started("c") && Context.object != null) {
if (Std.is(Context.object, MeshObject)) {
var mo = cast(Context.object, MeshObject);
2019-06-20 11:23:01 +02:00
var object = Scene.active.addMeshObject(mo.data, mo.materials, Scene.active.getChild("Scene"));
2019-12-09 00:28:10 +01:00
object.name = mo.name + ".1";
2019-05-10 19:28:59 +02:00
object.transform.loc.setFrom(mo.transform.loc);
object.transform.rot.setFrom(mo.transform.rot);
object.transform.scale.setFrom(mo.transform.scale);
2019-10-11 20:12:31 +02:00
var hit = RayCaster.planeIntersect(Vec4.zAxis(), new Vec4(), mouse.viewX, mouse.viewY, Scene.active.camera);
2019-05-10 19:28:59 +02:00
if (hit != null) {
object.transform.loc.x = hit.x;
object.transform.loc.y = hit.y;
object.transform.setRotation(0, 0, Math.random() * 3.1516 * 2);
}
object.transform.buildMatrix();
2020-02-03 18:11:20 +01:00
for (t in mo.traits) { // Clone traits
var trait = Type.createInstance(Type.getClass(t), []);
object.addTrait(trait);
}
2019-05-10 19:28:59 +02:00
2019-06-23 14:50:41 +02:00
Context.selectObject(object);
2019-05-10 19:28:59 +02:00
}
2019-06-23 14:50:41 +02:00
else if (Std.is(Context.object, LightObject)) {
var lo = cast(Context.object, LightObject);
2019-06-20 11:23:01 +02:00
var object = Scene.active.addLightObject(lo.data, Scene.active.getChild("Scene"));
2019-12-09 00:28:10 +01:00
object.name = lo.name + ".1";
2019-05-10 19:28:59 +02:00
object.transform.loc.setFrom(lo.transform.loc);
object.transform.rot.setFrom(lo.transform.rot);
object.transform.scale.setFrom(lo.transform.scale);
object.transform.buildMatrix();
2019-06-23 14:50:41 +02:00
Context.selectObject(object);
2019-05-10 19:28:59 +02:00
}
2019-08-18 15:35:18 +02:00
Context.rdirty = 3;
Context.ddirty = 3;
2019-05-10 19:28:59 +02:00
}
if (kb.started("m")) { // skip voxel
2019-10-29 12:50:23 +01:00
var raw = Context.materialScene.data.raw;
2019-12-09 00:28:10 +01:00
raw.skip_context = raw.skip_context == "" ? "voxel" : "";
2019-05-10 19:28:59 +02:00
}
}
if (mouse.started("middle")) {
#if arm_physics
2019-12-03 00:25:38 +01:00
var physics = arm.plugin.PhysicsWorld.active;
var pb = physics.pickClosest(mouse.viewX, mouse.viewY);
if (pb != null) Context.selectObject(pb.object);
2019-05-10 19:28:59 +02:00
#end
}
2019-06-23 14:50:41 +02:00
if (mouse.started("left") && Context.object.name != "Scene") {
2019-05-10 19:28:59 +02:00
gizmo.transform.buildMatrix();
2020-03-18 20:26:28 +01:00
var trs = [UISidebar.inst.gizmoX.transform, UISidebar.inst.gizmoY.transform, UISidebar.inst.gizmoZ.transform];
2019-10-11 20:12:31 +02:00
var hit = RayCaster.closestBoxIntersect(trs, mouse.viewX, mouse.viewY, Scene.active.camera);
2019-05-10 19:28:59 +02:00
if (hit != null) {
2020-03-18 20:26:28 +01:00
if (hit.object == UISidebar.inst.gizmoX) UISidebar.inst.axisX = true;
else if (hit.object == UISidebar.inst.gizmoY) UISidebar.inst.axisY = true;
else if (hit.object == UISidebar.inst.gizmoZ) UISidebar.inst.axisZ = true;
if (UISidebar.inst.axisX || UISidebar.inst.axisY || UISidebar.inst.axisZ) UISidebar.inst.axisStart = 0.0;
2019-05-10 19:28:59 +02:00
}
}
else if (mouse.released("left")) {
2020-03-18 20:26:28 +01:00
UISidebar.inst.axisX = UISidebar.inst.axisY = UISidebar.inst.axisZ = false;
2019-05-10 19:28:59 +02:00
}
2020-03-18 20:26:28 +01:00
if (UISidebar.inst.axisX || UISidebar.inst.axisY || UISidebar.inst.axisZ) {
2019-06-23 14:50:41 +02:00
var t = Context.object.transform;
2019-06-20 11:23:01 +02:00
var v = new Vec4();
2019-05-10 19:28:59 +02:00
v.set(t.worldx(), t.worldy(), t.worldz());
2019-10-06 19:03:59 +02:00
2020-03-18 20:26:28 +01:00
if (UISidebar.inst.axisX) {
2019-10-11 20:12:31 +02:00
var hit = RayCaster.planeIntersect(Vec4.yAxis(), v, mouse.viewX, mouse.viewY, Scene.active.camera);
2019-05-10 19:28:59 +02:00
if (hit != null) {
2020-03-18 20:26:28 +01:00
if (UISidebar.inst.axisStart == 0) UISidebar.inst.axisStart = hit.x - Context.object.transform.loc.x;
Context.object.transform.loc.x = hit.x - UISidebar.inst.axisStart;
2019-06-23 14:50:41 +02:00
Context.object.transform.buildMatrix();
2019-05-10 19:28:59 +02:00
#if arm_physics
2019-12-03 00:25:38 +01:00
var pb = Context.object.getTrait(arm.plugin.PhysicsBody);
if (pb != null) pb.syncTransform();
2019-05-10 19:28:59 +02:00
#end
}
}
2020-03-18 20:26:28 +01:00
else if (UISidebar.inst.axisY) {
2019-10-11 20:12:31 +02:00
var hit = RayCaster.planeIntersect(Vec4.xAxis(), v, mouse.viewX, mouse.viewY, Scene.active.camera);
2019-05-10 19:28:59 +02:00
if (hit != null) {
2020-03-18 20:26:28 +01:00
if (UISidebar.inst.axisStart == 0) UISidebar.inst.axisStart = hit.y - Context.object.transform.loc.y;
Context.object.transform.loc.y = hit.y - UISidebar.inst.axisStart;
2019-06-23 14:50:41 +02:00
Context.object.transform.buildMatrix();
2019-05-10 19:28:59 +02:00
#if arm_physics
2019-12-03 00:25:38 +01:00
var pb = Context.object.getTrait(arm.plugin.PhysicsBody);
if (pb != null) pb.syncTransform();
2019-05-10 19:28:59 +02:00
#end
}
}
2020-03-18 20:26:28 +01:00
else if (UISidebar.inst.axisZ) {
2019-10-11 20:12:31 +02:00
var hit = RayCaster.planeIntersect(Vec4.xAxis(), v, mouse.viewX, mouse.viewY, Scene.active.camera);
2019-05-10 19:28:59 +02:00
if (hit != null) {
2020-03-18 20:26:28 +01:00
if (UISidebar.inst.axisStart == 0) UISidebar.inst.axisStart = hit.z - Context.object.transform.loc.z;
Context.object.transform.loc.z = hit.z - UISidebar.inst.axisStart;
2019-06-23 14:50:41 +02:00
Context.object.transform.buildMatrix();
2019-05-10 19:28:59 +02:00
#if arm_physics
2019-12-03 00:25:38 +01:00
var pb = Context.object.getTrait(arm.plugin.PhysicsBody);
if (pb != null) pb.syncTransform();
2019-05-10 19:28:59 +02:00
#end
}
}
}
2020-03-18 20:26:28 +01:00
Input.occupied = (UISidebar.inst.axisX || UISidebar.inst.axisY || UISidebar.inst.axisZ) && mouse.viewX < App.w();
2019-05-10 19:28:59 +02:00
}
}