Files
armorpaint/Sources/arm/trait/OrbitCamera.hx
T

88 lines
2.0 KiB
Haxe
Raw Normal View History

2018-10-11 17:55:05 +02:00
package arm.trait;
2019-02-02 18:49:19 +01:00
import iron.system.Input;
2019-03-02 23:41:26 +01:00
import arm.ui.*;
2017-12-14 11:12:51 +01:00
2018-03-06 23:33:34 +01:00
class OrbitCamera extends iron.Trait {
2017-12-14 11:12:51 +01:00
2018-03-06 23:33:34 +01:00
public static var inst:OrbitCamera;
2018-05-02 02:08:19 +02:00
var redraws = 0;
2019-03-14 11:53:21 +01:00
var first = true;
var dist = 0.0;
2018-05-02 02:08:19 +02:00
2017-12-14 11:12:51 +01:00
public function new() {
super();
2018-03-06 23:33:34 +01:00
inst = this;
2019-02-02 18:49:19 +01:00
var mouse = Input.getMouse();
var kb = Input.getKeyboard();
2017-12-14 11:12:51 +01:00
notifyOnUpdate(function() {
2019-02-02 18:49:19 +01:00
if (Input.occupied ||
!arm.App.uienabled ||
arm.App.isDragging ||
UITrait.inst.isScrolling ||
UITrait.inst.cameraControls != 1 ||
mouse.x < 0 ||
mouse.x > iron.App.w()) return;
2017-12-14 11:12:51 +01:00
2018-03-06 23:33:34 +01:00
var camera = iron.Scene.active.camera;
2017-12-14 11:12:51 +01:00
2019-03-14 11:53:21 +01:00
if (first) {
first = false;
reset();
}
2017-12-14 11:12:51 +01:00
if (mouse.wheelDelta != 0) {
2018-05-02 02:08:19 +02:00
redraws = 2;
2019-03-14 11:53:21 +01:00
var f = mouse.wheelDelta * (-0.1);
camera.transform.move(camera.look(), f);
dist -= f;
2017-12-14 11:12:51 +01:00
}
2018-11-01 13:39:27 +01:00
if (mouse.down("middle") || (mouse.down("right") && kb.down("space"))) {
2018-05-02 02:08:19 +02:00
redraws = 2;
2019-03-14 11:22:11 +01:00
if (kb.down("control")) {
2019-03-14 11:53:21 +01:00
var f = mouse.movementX / 75;
camera.transform.move(camera.look(), f);
dist -= f;
2019-03-14 11:22:11 +01:00
}
else {
var look = camera.transform.look().normalize().mult(mouse.movementY / 150);
var right = camera.transform.right().normalize().mult(-mouse.movementX / 150);
camera.transform.loc.add(look);
camera.transform.loc.add(right);
camera.buildMatrix();
}
2017-12-14 11:12:51 +01:00
}
2018-05-02 02:08:19 +02:00
2018-11-01 13:39:27 +01:00
if (mouse.down("right") || (mouse.down("left") && kb.down("control"))) {
redraws = 2;
2019-03-14 11:22:11 +01:00
camera.transform.move(camera.lookWorld(), dist);
2019-02-02 18:49:19 +01:00
camera.transform.rotate(new iron.math.Vec4(0, 0, 1), -mouse.movementX / 100);
2018-11-01 13:39:27 +01:00
// Rotate Y
if (!kb.down("shift")) {
camera.transform.rotate(camera.rightWorld(), -mouse.movementY / 100);
if (camera.upWorld().z < 0) {
camera.transform.rotate(camera.rightWorld(), mouse.movementY / 100);
}
}
2019-03-14 11:53:21 +01:00
camera.transform.move(camera.lookWorld(), -dist);
2018-11-01 13:39:27 +01:00
}
2018-05-02 02:08:19 +02:00
if (redraws > 0) {
redraws--;
2018-10-15 14:36:24 +02:00
UITrait.inst.ddirty = 2;
2018-05-02 02:08:19 +02:00
}
2017-12-14 11:12:51 +01:00
});
}
2019-03-14 11:53:21 +01:00
public function reset() {
var camera = iron.Scene.active.camera;
dist = camera.transform.loc.length();
}
2017-12-14 11:12:51 +01:00
}