List of usage examples for com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener ActorGestureListener
public ActorGestureListener(float halfTapSquareSize, float tapCountInterval, float longPressDuration, float maxFlingDelay)
From source file:com.vlaaad.dice.game.world.controllers.SpawnController.java
License:Open Source License
private EventListener createMoveSpawnedListener(final Creature creature, final WorldObjectView view, final WorldObjectView spawnView) { return new ActorGestureListener(8, 0.4f, 1.1f, 0.15f) { private boolean isDragging; @Override//from w ww. ja v a 2 s . c o m public void touchDown(InputEvent event, float x, float y, int pointer, int button) { view.toFront(); SoundManager.instance.playSound("ui-button-down"); } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (!isDragging) { return; } isDragging = false; viewController.scroller.enable(); Vector2 coordinate = world.getController(ViewController.class) .stageToWorldCoordinates(event.getStageX(), event.getStageY()); final int cellX = (int) coordinate.x; final int cellY = (int) coordinate.y; boolean isAllowedCell = world.level.getElement(LevelElementType.spawn, cellX, cellY) == world.viewer.fraction; if (!isAllowedCell) { removeFromPlaced(creature); SoundManager.instance.playSound("ui-button-up"); return; } if (!world.inBounds(cellX, cellY) || world.get(cellX, cellY) != null) { WorldObject prev = world.get(cellX, cellY); boolean shouldSwap = false; int swapX = 0; int swapY = 0; Creature other = null; if (prev instanceof Creature) { other = (Creature) prev; if (other.player == world.viewer && other != creature) { shouldSwap = true; for (int i = 0; i < world.width; i++) { for (int j = 0; j < world.height; j++) { if (world.get(i, j) == null) { swapX = i; swapY = j; shouldSwap = true; break; } } } } } if (shouldSwap) { int prevX = prev.getX(); int prevY = prev.getY(); int creatureX = creature.getX(); int creatureY = creature.getY(); world.move(creature, swapX, swapY); world.move(prev, creatureX, creatureY); world.move(creature, prevX, prevY); } else { view.setPosition(creature.getX() * ViewController.CELL_SIZE, creature.getY() * ViewController.CELL_SIZE); } } else { world.move(creature, cellX, cellY); SoundManager.instance.playSound("ui-button-up"); } world.getController(ViewController.class).resort(); } @Override public void tap(InputEvent event, float x, float y, int count, int button) { if (event.isCancelled() || WindowManager.instance.isShown(CreatureInfoWindow.class)) { world.getController(ViewController.class).resort(); return; } removeFromPlaced(creature); SoundManager.instance.playSound("ui-button-up"); } private Vector2 tmp = new Vector2(); @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) { Vector2 local = view.getParent() .stageToLocalCoordinates(tmp.set(event.getStageX(), event.getStageY())); view.setPosition(local.x - ViewController.CELL_SIZE / 2, local.y - ViewController.CELL_SIZE / 2); isDragging = true; viewController.scroller.disable(); } }; }
From source file:com.vlaaad.dice.ui.components.DieInventory.java
License:Open Source License
private void addCounter(final Ability ability, int value) { final AbilityIconCounter counter = new AbilityIconCounter(ability, value); SoundHelper.init(counter);/*from w w w . j a va 2s.c o m*/ table.add(counter).size(23); icons.put(ability, counter); if (!ability.requirement.isSatisfied(die)) { counter.getColor().a = 0.5f; } counter.addListener(new ActorGestureListener(1.5f, 0.4f, 1.1f, 0.15f) { public Vector2 tmp = new Vector2(); public AbilityIcon icon = new AbilityIcon(ability); public Image selection = new Image(Config.skin, "ui/dice-window/inventory-selection-selected"); @Override public void touchDown(InputEvent event, float x, float y, int pointer, int button) { if (!ability.requirement.isSatisfied(die)) return; event.stop(); selectionGroup.addActor(selection); Vector2 pos = selectionGroup .stageToLocalCoordinates(counter.localToStageCoordinates(tmp.set(0, 0))); selection.setPosition(pos.x, pos.y + 1); } @Override public void tap(InputEvent event, float x, float y, int count, int button) { new InventoryAbilityWindow() .show(new InventoryAbilityWindow.Params(ability, new InventoryAbilityWindow.Callback() { @Override public void equip() { if (!ability.requirement.isSatisfied(die)) return; if (net == null) return; selectionGroup.addActor(selection); net.startHighlighting(ability); final ClickListener listener = new ClickListener() { @Override public boolean handle(Event e) { if (!(e instanceof InputEvent)) return false; InputEvent event = (InputEvent) e; switch (event.getType()) { case keyDown: case keyUp: case keyTyped: return super.handle(e); } e.stop(); return super.handle(e); } @Override public void clicked(InputEvent event, float x, float y) { cancelEquip.run(); checkAddToNet(ability, event); } }; cancelEquip = new Runnable() { @Override public void run() { cancelEquip = null; net.stopHighlighting(); selection.remove(); getStage().removeCaptureListener(listener); } }; getStage().addCaptureListener(listener); } @Override public void sell() { int count = die.inventory.get(ability, 0); if (count <= 0) return; die.inventory.getAndIncrement(ability, 0, -1); Item coin = Config.items.get("coin"); int coinCount = userData.getItemCount(coin); userData.setItemCount(coin, coinCount + ability.sellCost); SoundManager.instance.playSound("coins"); refresh(); fire(RefreshEvent.INSTANCE); } }, die)); } @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) { if (!ability.requirement.isSatisfied(die)) return; if (icon.getStage() == null) { net.startHighlighting(ability); getStage().addActor(icon); icon.setPosition(event.getStageX() - icon.getWidth() / 2, event.getStageY() - icon.getHeight() / 2); } else { icon.moveBy(deltaX, deltaY); } event.stop(); } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { super.touchUp(event, x, y, pointer, button); if (!ability.requirement.isSatisfied(die)) return; icon.remove(); selection.remove(); net.stopHighlighting(); if (!event.isCancelled()) checkAddToNet(ability, event); } private void checkAddToNet(Ability ability, InputEvent event) { if (net != null) { tmp.set(event.getStageX(), event.getStageY()); net.stageToLocalCoordinates(tmp); int abilityIndex = net.getAbilityIndex(tmp.x, tmp.y); if (abilityIndex != -1 && die.getAvailableIndices(ability).contains(abilityIndex)) { Ability prev = null; if (die.abilities.size > abilityIndex) prev = die.abilities.get(abilityIndex); if (prev != null) { die.inventory.getAndIncrement(prev, 0, 1); } while (die.abilities.size <= abilityIndex) die.abilities.add(null); die.abilities.set(abilityIndex, ability); die.inventory.getAndIncrement(ability, 0, -1); if (die.inventory.get(ability, 0) == 0) { die.inventory.remove(ability, 0); } net.refresh(); refresh(); } } } }); }
From source file:com.vlaaad.dice.ui.components.DieNet.java
License:Open Source License
private void addAbilityIcon(final int index, final Ability ability) { final AbilityDarkIcon abilityIcon = new AbilityDarkIcon(ability); SoundHelper.init(abilityIcon);//from ww w.jav a 2s . co m Vector2 iconPosition = iconPositions[index]; abilityIcon.setPosition(iconPosition.x, iconPosition.y); addActor(abilityIcon); abilityIcon.addListener(new ActorGestureListener(1.5f, 0.4f, 1.1f, 0.15f) { public Vector2 tmp = new Vector2(); public AbilityIcon draggedIcon; @Override public void touchDown(InputEvent event, float x, float y, int pointer, int button) { abilityIcon.setVisible(false); draggedIcon = new AbilityIcon(ability); getStage().addActor(draggedIcon); draggedIcon.setPosition(event.getStageX() - draggedIcon.getWidth() / 2, event.getStageY() - draggedIcon.getHeight() / 2); event.cancel(); highlightMove(ability); } @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) { super.pan(event, x, y, deltaX, deltaY); draggedIcon.moveBy(deltaX, deltaY); event.cancel(); } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { super.touchUp(event, x, y, pointer, button); abilityIcon.setVisible(true); stopHighlighting(); draggedIcon.remove(); if (inventory != null) { tmp.set(event.getStageX(), event.getStageY()); inventory.stageToLocalCoordinates(tmp); if (tmp.x > 0 && tmp.x < inventory.getWidth() && tmp.y > 0 && tmp.y < inventory.getHeight() && inventory.getStage() != null) { Ability v = die.abilities.get(index); if (v == null) return; die.abilities.set(index, null); die.inventory.getAndIncrement(v, 0, 1); refresh(); inventory.refresh(); return; } } tmp.set(event.getStageX(), event.getStageY()); stageToLocalCoordinates(tmp); int newIndex = getAbilityIndex(tmp.x, tmp.y); while (die.abilities.size < 6) die.abilities.add(null); if (newIndex != -1) { Ability prev = die.abilities.get(newIndex); die.abilities.set(newIndex, ability); die.abilities.set(index, prev); refresh(); } } }); }
From source file:com.vlaaad.dice.ui.components.Hint.java
License:Open Source License
public static Hint make(final Actor actor, String locKey, ObjectMap<String, String> params) { final Hint result = new Hint(locKey, params); actor.addCaptureListener(new ActorGestureListener(20, 0.4f, 0.3f, 0.15f) { @Override// w w w. j a v a 2 s.com public boolean longPress(Actor actor, float x, float y) { if (actor.getStage() == null) return false; actor.localToStageCoordinates(tmp.set(x, y)); result.show(actor.getStage(), tmp.x, tmp.y); return false; } @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) { if (result.hide()) event.cancel(); } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (result.hide()) { event.cancel(); if (actor.getStage() != null) actor.getStage().cancelTouchFocus(); } } }); return result; }
From source file:com.vlaaad.dice.ui.components.IngredientsPane.java
License:Open Source License
public IngredientsPane(final UserData userData, final CraftingPane craftingPane, final PotionsWindow window) { super(Config.skin); this.userData = userData; this.craftingPane = craftingPane; craftingPane.clean();/*w ww. j a v a 2s.c o m*/ craftingPane.addListener(new CraftingPane.Listener() { @Override protected void onIngredientReplaced(Item item) { ItemCountIcon icon = icons.get(item); icon.setCount(icon.getCount() + 1); } }); craftingPane.addListener(new ActorGestureListener(8, 0.4f, 1.1f, 0.15f) { private boolean alreadyPanning; public ItemIcon pannedIcon; public float stageX; public float stageY; @Override public void touchDown(InputEvent event, float x, float y, int pointer, int button) { stageX = event.getStageX(); stageY = event.getStageY(); event.stop(); SoundManager.instance.playSound("ui-button-down"); } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (pannedIcon != null) { if (!craftingPane.swapIngredients(pannedIcon, stageX, stageY)) { ItemCountIcon icon = icons.get(pannedIcon.item); icon.setCount(icon.getCount() + 1); } pannedIcon.remove(); SoundManager.instance.playSound("ui-button-up"); } alreadyPanning = false; pannedIcon = null; } @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) { if (!alreadyPanning) { alreadyPanning = true; pannedIcon = craftingPane.removeIngredient(stageX, stageY); if (pannedIcon != null) { getStage().addActor(pannedIcon); pannedIcon.setPosition(event.getStageX() - pannedIcon.getWidth() / 2, event.getStageY() - pannedIcon.getWidth() / 2); } } else if (pannedIcon != null) { pannedIcon.moveBy(deltaX, deltaY); } } @Override public void tap(InputEvent event, float x, float y, int count, int button) { final Actor actor = event.getTarget(); if (!(actor instanceof AbilityIcon)) return; window.scrollTo(window.potionsList, new Runnable() { @Override public void run() { Ability ability = ((AbilityIcon) actor).ability; if (!userData.canWithdraw(craftingPane.res)) return; userData.withdrawItems(craftingPane.res); userData.addPotion(ability); craftingPane.clean(); Config.achievements.fire(EventType.brewPotion, Pools.obtain(BrewEvent.class).potion(ability)); } }); } }); setBackground("ui/dice-window/inventory-background"); defaults().size(24).padLeft(1).padRight(-2).padBottom(-1); top().left(); int i = 0; for (Item item : Config.items) { if (item.type != Item.Type.ingredient) continue; ItemCountIcon icon = new ItemCountIcon(item, userData.getItemCount(item)); icons.put(item, icon); icon.addListener(createListener(icon)); add(icon); i++; if (i % 5 == 0) row(); } }
From source file:com.vlaaad.dice.ui.components.IngredientsPane.java
License:Open Source License
private EventListener createListener(final ItemCountIcon icon) { return new ActorGestureListener(1.5f, 0.4f, 1.1f, 0.15f) { private final ItemIcon pannedIcon = new ItemIcon(icon.item); public boolean alreadyPanning; @Override//w ww. j a v a 2 s. c o m public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (alreadyPanning && !craftingPane.putIngredient(pannedIcon)) { icon.setCount(icon.getCount() + 1); } alreadyPanning = false; pannedIcon.remove(); SoundManager.instance.playSound("ui-button-up"); } @Override public void touchDown(InputEvent event, float x, float y, int pointer, int button) { event.stop(); SoundManager.instance.playSound("ui-button-down"); } @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) { if (!alreadyPanning) { if (icon.getCount() == 0) return; icon.setCount(icon.getCount() - 1); alreadyPanning = true; getStage().addActor(pannedIcon); pannedIcon.setPosition(event.getStageX() - pannedIcon.getWidth() / 2, event.getStageY() - pannedIcon.getWidth() / 2); } else { pannedIcon.moveBy(deltaX, deltaY); } } @Override public void tap(InputEvent event, float x, float y, int count, int button) { IngredientWindow window = new IngredientWindow(); window.show(new IngredientWindow.Params(icon.item, userData)); window.addListener(new IngredientWindow.Listener() { @Override protected void bought() { icon.setCount(icon.getCount() + 1); } }); } }; }
From source file:com.vlaaad.dice.ui.windows.CreatureQueueWindow.java
License:Open Source License
private EventListener createListener(final Creature creature) { return new ActorGestureListener(20, 0.4f, 0.5f, 0.15f) { @Override/*from ww w.j a va2 s . com*/ public boolean longPress(Actor actor, float x, float y) { creatureInfoWindow.show(new CreatureInfoWindow.Params(creature, array.first().world)); return true; } }; }