Skip to content

Commit

Permalink
Merge pull request FreezingMoon#1526 from TheSeally/fixHoverOnTouchDe…
Browse files Browse the repository at this point in the history
…vices

UI tooltips touch events, fixes FreezingMoon#1506
  • Loading branch information
DreadKnight committed May 15, 2019
2 parents 0d7141d + 3e982ab commit 8331b92
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/style/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ div.section.info {
overflow: hidden;
}

.button:hover + .desc {
.button.hover + .desc {
width: 270px;
border: 2px black solid;
overflow: visible;
Expand Down Expand Up @@ -1021,7 +1021,7 @@ div.section.info {
border-left: 6px solid black;
}

#rightpanel .button:hover + .desc {
#rightpanel .button.hover + .desc {
width: 250px;
left: -259px;
text-align: right;
Expand Down
60 changes: 60 additions & 0 deletions src/ui/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export class Button {
click: function() {},
mouseover: function() {},
mouseleave: function() {},
touchstart: function() {},
touchend: function() {},
touchX: 0,
touchY: 0,
hasShortcut: false,
clickable: true,
state: 'normal', // disabled, normal, glowing, selected, active
$button: undefined,
Expand All @@ -40,6 +45,8 @@ export class Button {
this.$button
.unbind('click')
.unbind('mouseover')
.unbind('touchstart')
.unbind('touchend')
.unbind('mouseleave');

if (state != 'disabled') {
Expand All @@ -57,6 +64,10 @@ export class Button {
return;
}

if (this.hasShortcut) {
this.$button.addClass('hover');
}

this.mouseover();
});

Expand All @@ -65,9 +76,46 @@ export class Button {
return;
}

if (this.hasShortcut) {
this.$button.removeClass('hover');
}

this.mouseleave();
});

this.$button.bind('touchstart', event => {
event.preventDefault();
event.stopPropagation();

if (game.freezedInput || !this.clickable) {
return;
}

if (this.hasShortcut) {
this.$button.addClass('hover');
}

this.touchX = event.changedTouches[0].pageX;
this.touchY = event.changedTouches[0].pageY;
});

this.$button.bind('touchend', event => {
event.preventDefault();
event.stopPropagation();

if (game.freezedInput || !this.clickable) {
return;
}

if (this.hasShortcut) {
this.$button.removeClass('hover');
}

if (this.shouldTriggerClick(event.changedTouches[0]) && this.state != 'disabled') {
this.click();
}
});

this.$button.removeClass('disabled glowing selected active noclick');
this.$button.css(this.css.normal);

Expand Down Expand Up @@ -100,4 +148,16 @@ export class Button {

this.mouseleave();
}

shouldTriggerClick(changedTouches) {
const endTouchX = changedTouches.pageX;
const endTouchY = changedTouches.pageY;
let result = false;

if (Math.abs(this.touchX - endTouchX) < 50 && Math.abs(this.touchY - endTouchY) < 50) {
result = true;
}

return result;
}
}
5 changes: 5 additions & 0 deletions src/ui/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class UI {
this.btnAudio = new Button(
{
$button: $j('#audio.button'),
hasShortcut: true,
click: () => {
// if audio element was already active, close dash
if ($j('#musicplayerwrapper').is(':visible')) {
Expand All @@ -98,6 +99,7 @@ export class UI {
this.btnSkipTurn = new Button(
{
$button: $j('#skip.button'),
hasShortcut: true,
click: () => {
if (!this.dashopen) {
if (game.turnThrottle) {
Expand All @@ -119,6 +121,7 @@ export class UI {
this.btnDelay = new Button(
{
$button: $j('#delay.button'),
hasShortcut: true,
click: () => {
if (!this.dashopen) {
let creature = game.activeCreature;
Expand Down Expand Up @@ -147,6 +150,7 @@ export class UI {
this.btnFlee = new Button(
{
$button: $j('#flee.button'),
hasShortcut: true,
click: () => {
if (!this.dashopen) {
if (game.turn < game.minimumTurnBeforeFleeing) {
Expand Down Expand Up @@ -465,6 +469,7 @@ export class UI {
let b = new Button(
{
$button: $j('#abilities > div:nth-child(' + (i + 1) + ') > .ability'),
hasShortcut: true,
abilityId: i,
css: {
disabled: {
Expand Down

0 comments on commit 8331b92

Please sign in to comment.