1
0
Fork 0

Fixed issue with mouse hovering/clicking

This commit is contained in:
Michaël Lemaire 2017-03-15 00:08:07 +01:00
parent b916230002
commit 55a11e521a
3 changed files with 49 additions and 17 deletions

1
TODO
View file

@ -7,7 +7,6 @@
* Ensure that tweens and particle emitters get destroyed once animation is done (or view changes)
* Highlight ships that will be included as target of current action
* Controls: Do not focus on ship while targetting for area effects (dissociate hover and target)
* Controls: fix hover not working when mouse goes rapidly over and out of a ship
* Active effects are not enough visible in ship list (maybe better in arena ?)
* All things displayed in battle should be updated from LogProcess forwarding, not from current game state
* Drones: add tooltip

View file

@ -11,7 +11,7 @@ module TS.SpaceTac.UI.Specs {
expect(Tools.normalizeAngle(-Math.PI - 0.5)).toBeCloseTo(Math.PI - 0.5, 0.000001);
});
it("handles hover and click on desktops and mobile targets", function () {
it("handles hover and click on desktops and mobile targets", function (done) {
let newButton: () => [Phaser.Button, any] = () => {
var button = new Phaser.Button(testgame.ui);
var funcs = {
@ -42,6 +42,20 @@ module TS.SpaceTac.UI.Specs {
expect(funcs.enter).toHaveBeenCalledTimes(1);
expect(funcs.leave).toHaveBeenCalledTimes(1);
expect(funcs.click).toHaveBeenCalledTimes(1);
// Hold to hover on mobile
[button, funcs] = newButton();
button.onInputDown.dispatch();
Timer.global.schedule(150, () => {
expect(funcs.enter).toHaveBeenCalledTimes(1);
expect(funcs.leave).toHaveBeenCalledTimes(0);
expect(funcs.click).toHaveBeenCalledTimes(0);
button.onInputUp.dispatch();
expect(funcs.enter).toHaveBeenCalledTimes(1);
expect(funcs.leave).toHaveBeenCalledTimes(1);
expect(funcs.click).toHaveBeenCalledTimes(0);
done();
});
});
});
}

View file

@ -10,7 +10,8 @@ module TS.SpaceTac.UI {
static setHoverClick(obj: Phaser.Button, enter: Function, leave: Function, click: Function, hovertime = 300, holdtime = 600) {
let holdstart = new Date();
let enternext = null;
let hovered = false;
let entercalled = false;
let cursorinside = false;
obj.input.useHandCursor = true;
@ -18,38 +19,56 @@ module TS.SpaceTac.UI {
if (enternext != null) {
Timer.global.cancel(enternext);
enternext = null;
return true;
} else {
return false;
}
};
let effectiveenter = () => {
enternext = null;
entercalled = true;
enter();
}
let effectiveleave = () => {
prevententer();
if (entercalled) {
entercalled = false;
leave();
}
}
obj.onInputOver.add(() => {
enternext = Timer.global.schedule(hovertime, enter);
hovered = true;
cursorinside = true;
enternext = Timer.global.schedule(hovertime, effectiveenter);
});
obj.onInputOut.add(() => {
prevententer();
leave();
hovered = false;
cursorinside = false;
effectiveleave();
});
obj.onInputDown.add(() => {
prevententer();
holdstart = new Date();
enternext = Timer.global.schedule(holdtime, enter);
if (!cursorinside && !enternext) {
enternext = Timer.global.schedule(holdtime, effectiveenter);
}
});
obj.onInputUp.add(() => {
prevententer();
if (!cursorinside) {
effectiveleave();
}
if (new Date().getTime() - holdstart.getTime() < holdtime) {
if (!hovered) {
enter();
if (!cursorinside) {
effectiveenter();
}
click();
if (!hovered) {
leave();
if (!cursorinside) {
effectiveleave();
}
} else if (!hovered) {
leave();
}
});
}