mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-16 10:48:00 +00:00
Improve gesture support in and around toggle switches
This commit is contained in:
@@ -279,7 +279,7 @@ public class ConstructedScreen extends LaunchScreen {
|
||||
nameRandomiser = createNameRandomizer();
|
||||
add(nameRandomiser);
|
||||
|
||||
humanAiSwitch.setToggled(index == 0);
|
||||
humanAiSwitch.setToggled(index != 0);
|
||||
humanAiSwitch.setChangedHandler(humanAiSwitched);
|
||||
add(humanAiSwitch);
|
||||
|
||||
@@ -445,7 +445,7 @@ public class ConstructedScreen extends LaunchScreen {
|
||||
}
|
||||
|
||||
public PlayerType getPlayerType() {
|
||||
return humanAiSwitch.isToggled() ? PlayerType.HUMAN : PlayerType.COMPUTER;
|
||||
return humanAiSwitch.isToggled() ? PlayerType.COMPUTER : PlayerType.HUMAN;
|
||||
}
|
||||
|
||||
public void setVanguardButtonText(String text) {
|
||||
|
||||
@@ -69,7 +69,7 @@ public abstract class FContainer extends FDisplayObject {
|
||||
|
||||
@Override
|
||||
public void buildTouchListeners(float screenX, float screenY, ArrayList<FDisplayObject> listeners) {
|
||||
if (isEnabled() && contains(screenToLocalX(screenX), screenToLocalY(screenY))) {
|
||||
if (isEnabled() && contains(getLeft() + screenToLocalX(screenX), getTop() + screenToLocalY(screenY))) {
|
||||
for (int i = children.size() - 1; i >= 0; i--) {
|
||||
children.get(i).buildTouchListeners(screenX, screenY, listeners);
|
||||
}
|
||||
|
||||
@@ -64,16 +64,16 @@ public abstract class FDisplayObject {
|
||||
}
|
||||
|
||||
public float screenToLocalX(float x) {
|
||||
return x - screenPosition.x + bounds.x;
|
||||
return x - screenPosition.x;
|
||||
}
|
||||
public float screenToLocalY(float y) {
|
||||
return y - screenPosition.y + bounds.y;
|
||||
return y - screenPosition.y;
|
||||
}
|
||||
public float localToScreenX(float x) {
|
||||
return x - bounds.x + screenPosition.x;
|
||||
return x + screenPosition.x;
|
||||
}
|
||||
public float localToScreenY(float y) {
|
||||
return y - bounds.y + screenPosition.y;
|
||||
return y + screenPosition.y;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
@@ -91,9 +91,8 @@ public abstract class FDisplayObject {
|
||||
}
|
||||
|
||||
public abstract void draw(Graphics g);
|
||||
|
||||
public void buildTouchListeners(float screenX, float screenY, ArrayList<FDisplayObject> listeners) {
|
||||
if (enabled && contains(screenToLocalX(screenX), screenToLocalY(screenY))) {
|
||||
if (enabled && contains(getLeft() + screenToLocalX(screenX), getTop() + screenToLocalY(screenY))) {
|
||||
listeners.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,20 +229,18 @@ public abstract class FGestureAdapter extends InputAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean handled = false;
|
||||
if (wasPanning) { // handle no longer panning
|
||||
handled = panStop(x, y);
|
||||
|
||||
gestureStartTime = 0;
|
||||
long time = Gdx.input.getCurrentEventTime();
|
||||
if (time - tracker.lastTime < flingDelay) { // handle fling if needed
|
||||
tracker.update(x, y, time);
|
||||
if (fling(tracker.getVelocityX(), tracker.getVelocityY())) {
|
||||
return true;
|
||||
}
|
||||
handled = fling(tracker.getVelocityX(), tracker.getVelocityY()) || handled;
|
||||
}
|
||||
|
||||
return panStop(x, y);
|
||||
}
|
||||
|
||||
return false;
|
||||
return handled;
|
||||
}
|
||||
|
||||
private void startPress() {
|
||||
|
||||
@@ -196,10 +196,10 @@ public abstract class FScrollPane extends FContainer {
|
||||
if (setScrollPositions(pos.x, pos.y) && physicsObj.isMoving()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//end fling animation if can't scroll anymore or physics object is no longer moving
|
||||
lastFlingStopTime = TimeUtils.nanoTime();
|
||||
}
|
||||
|
||||
//end fling animation if can't scroll anymore or physics object is no longer moving
|
||||
lastFlingStopTime = TimeUtils.nanoTime();
|
||||
activeFlingAnimation = null;
|
||||
return false;
|
||||
}
|
||||
@@ -231,7 +231,7 @@ public abstract class FScrollPane extends FContainer {
|
||||
}
|
||||
|
||||
//if fling ended just shortly before, still prevent touch events on child controls
|
||||
//in case user tapped just to late to stop scrolling before scroll bounds reached
|
||||
//in case user tapped just too late to stop scrolling before scroll bounds reached
|
||||
if (lastFlingStopTime > 0) {
|
||||
if (TimeUtils.nanoTime() - lastFlingStopTime < FLING_STOP_DELAY) {
|
||||
listeners.add(this);
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
package forge.toolbox;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
|
||||
|
||||
import forge.Forge.Graphics;
|
||||
import forge.assets.FSkinColor;
|
||||
import forge.assets.FSkinColor.Colors;
|
||||
import forge.assets.FSkinFont;
|
||||
import forge.toolbox.FEvent.FEventHandler;
|
||||
import forge.toolbox.FEvent.FEventType;
|
||||
import forge.utils.Utils;
|
||||
|
||||
public class FToggleSwitch extends FDisplayObject {
|
||||
private static final FSkinColor ACTIVE_COLOR = FSkinColor.get(Colors.CLR_ACTIVE);
|
||||
private static final FSkinColor PRESSED_COLOR = ACTIVE_COLOR.stepColor(-30);
|
||||
private static final FSkinColor INACTIVE_COLOR = FSkinColor.get(Colors.CLR_INACTIVE);
|
||||
private static final FSkinColor FORE_COLOR = FSkinColor.get(Colors.CLR_TEXT);
|
||||
private static float MIN_PAN_DELTA = Utils.AVG_FINGER_WIDTH / 2f;
|
||||
private static final float INSETS = 2;
|
||||
private static final float PADDING = 3;
|
||||
|
||||
private FSkinFont font;
|
||||
private final String onText, offText;
|
||||
private boolean toggled = false;
|
||||
private final String offText, onText;
|
||||
private boolean toggled, pressed;
|
||||
private FEventHandler changedHandler;
|
||||
|
||||
public FToggleSwitch() {
|
||||
this("On", "Off");
|
||||
this("Off", "On");
|
||||
}
|
||||
|
||||
public FToggleSwitch(final String onText0, final String offText0) {
|
||||
onText = onText0;
|
||||
public FToggleSwitch(final String offText0, final String onText0) {
|
||||
offText = offText0;
|
||||
onText = onText0;
|
||||
font = FSkinFont.get(14);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,18 @@ public class FToggleSwitch extends FDisplayObject {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean press(float x, float y) {
|
||||
pressed = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean release(float x, float y) {
|
||||
pressed = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean tap(float x, float y, int count) {
|
||||
setToggled(!toggled, true);
|
||||
@@ -82,17 +94,37 @@ public class FToggleSwitch extends FDisplayObject {
|
||||
//support dragging finger left or right to toggle on/off
|
||||
@Override
|
||||
public final boolean pan(float x, float y, float deltaX, float deltaY) {
|
||||
if (deltaX < -MIN_PAN_DELTA && x < getWidth() / 2) {
|
||||
setToggled(true, true);
|
||||
return true;
|
||||
if (contains(getLeft() + x, getTop() + y)) {
|
||||
if (x < getHeight()) {
|
||||
setToggled(false, true);
|
||||
return true;
|
||||
}
|
||||
if (x > getWidth() - getHeight()) {
|
||||
setToggled(true, true);
|
||||
return true;
|
||||
}
|
||||
pressed = true;
|
||||
}
|
||||
if (deltaX > MIN_PAN_DELTA && x > getWidth() / 2) {
|
||||
setToggled(false, true);
|
||||
else {
|
||||
pressed = false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean panStop(float x, float y) {
|
||||
if (pressed) {
|
||||
pressed = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fling(float velocityX, float velocityY) {
|
||||
return Math.abs(velocityX) > Math.abs(velocityY); //handle fling if more horizontal than vertical
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics g) {
|
||||
float x = 1; //leave a pixel so border displays in full
|
||||
@@ -106,17 +138,17 @@ public class FToggleSwitch extends FDisplayObject {
|
||||
final String text;
|
||||
float switchWidth = w - h + PADDING;
|
||||
if (toggled) {
|
||||
x = w - switchWidth + 1;
|
||||
text = onText;
|
||||
}
|
||||
else {
|
||||
x = w - switchWidth + 1;
|
||||
text = offText;
|
||||
}
|
||||
x += INSETS;
|
||||
y += INSETS;
|
||||
h -= 2 * INSETS;
|
||||
w = switchWidth - 2 * INSETS;
|
||||
g.fillRect(ACTIVE_COLOR, x, y, w, h);
|
||||
g.fillRect(pressed ? PRESSED_COLOR : ACTIVE_COLOR, x, y, w, h);
|
||||
|
||||
x += PADDING;
|
||||
w -= 2 * PADDING;
|
||||
|
||||
Reference in New Issue
Block a user