1
0
Fork 0

Improved arena camera dragging

This commit is contained in:
Michaël Lemaire 2019-05-20 23:38:55 +02:00
parent 61cab14005
commit 77e5e659cc
1 changed files with 23 additions and 15 deletions

View File

@ -25,7 +25,7 @@ module TK.SpaceTac.UI {
// Currently selected ship
private selected: ArenaShip | null
// Layer for particles
// Layers
container: UIContainer
layer_garbage: UIContainer
layer_hints: UIContainer
@ -39,20 +39,16 @@ module TK.SpaceTac.UI {
callbacks_click: (() => void)[] = []
// Create a graphical arena for ship sprites to fight in a 2D space
constructor(view: BattleView, container?: UIContainer) {
constructor(view: BattleView, parent: UIContainer) {
this.view = view;
this.selected = null;
this.hovered = null;
this.range_hint = new RangeHint(this);
let builder = new UIBuilder(view, container);
if (!container) {
container = builder.container("arena");
builder = builder.in(container);
}
this.container = container;
this.setupMouseCapture(parent);
this.setupMouseCapture();
this.container = parent.getBuilder().container("arena");
const builder = new UIBuilder(view, this.container);
this.layer_garbage = builder.container("garbage");
this.layer_hints = builder.container("hints");
@ -81,34 +77,46 @@ module TK.SpaceTac.UI {
/**
* Setup the mouse capture for targetting events
*/
setupMouseCapture() {
setupMouseCapture(parent: UIContainer) {
let view = this.view;
let button_down = false;
let dragged = false;
let zone = new UIBuilder(view, this.container).capture("arena-events");
let zone = new UIBuilder(view, parent).capture("arena-events");
// Capture clicks
zone.on("pointerdown", (pointer: Phaser.Input.Pointer) => {
button_down = (pointer.buttons == 1);
dragged = false;
});
zone.on("pointerup", () => {
if (button_down) {
// TODO Only if no significant drag happened
button_down = false;
this.callbacks_click.forEach(callback => callback());
if (!dragged) {
this.callbacks_click.forEach(callback => callback());
}
}
});
zone.on("pointerout", () => {
button_down = false;
this.callbacks_hover.forEach(callback => callback(null, null));
});
// Watch mouse move to capture hovering
zone.on("pointermove", (pointer: Phaser.Input.Pointer) => {
if (button_down) {
if (pointer.buttons == 1 && !button_down) {
button_down = true;
}
if (button_down && pointer.buttons == 1) {
let dx = pointer.x - pointer.prevPosition.x;
let dy = pointer.y - pointer.prevPosition.y;
this.scrollCamera(dx, dy);
if (dragged || Math.abs(dx) + Math.abs(dy) > 5) {
dragged = true;
this.scrollCamera(dx, dy);
}
} else {
button_down = false;
let location = new ArenaLocation(pointer.x - this.container.x, pointer.y - this.container.y);
let ship = this.getShip(location);
this.callbacks_hover.forEach(callback => callback(location, ship));