Skip to content

Commit

Permalink
Fixes and refactoring for drag and drop (#308)
Browse files Browse the repository at this point in the history
* Simplifying and unifying drag and drop logic

* Simplifying EquipLoc hover logic

* Fix for gamepad being able to drag and drop multiple items

* More gamepad drag and drop fixes

* Making the gamepad dragged item bleed through the cursor

* Hiding the cursor when dragging an inventory item

* Unifying dragging and dropping an item from the world

* Fixes for gamepad drag and drop from world

* Moving more drag and drop code to the base Hud
  • Loading branch information
Interrupt authored Jan 13, 2023
1 parent 5fb5136 commit 55d9432
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 418 deletions.
6 changes: 5 additions & 1 deletion Dungeoneer/src/com/interrupt/dungeoneer/GameInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class GameInput implements InputProcessor {
public Vector2 gamepadCursorPosition = new Vector2();
public final int gamepadPointerNum = 0;

public boolean isNewlyTouched = false;

public void setGamepadManager(GamepadManager gamepadManager) {
this.gamepadManager = gamepadManager;
gamepadCursorPosition = new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
Expand All @@ -76,6 +78,8 @@ public void tick() {
newlyMouseScrollUp=false;
newlyMouseScrollDown=false;

isNewlyTouched = false;

if(!Gdx.input.isCursorCatched()) {
ignoreLastMouseLocation = true;
}
Expand Down Expand Up @@ -119,8 +123,8 @@ public boolean scrolled(float amountX, float amountY) {

@Override
public boolean touchDown(int x, int y, int pointer, int button) {

lastTouchedPointer = pointer;
isNewlyTouched = true;

if(menuUi != null) menuUi.touchDown(x, y, pointer, button);

Expand Down
56 changes: 41 additions & 15 deletions Dungeoneer/src/com/interrupt/dungeoneer/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,19 @@ public void editorTick(Level level, float delta) {
tick(level, delta);
}

private boolean canShowUseMessages(GameInput input) {
if(isDead)
return false;
if(Game.isMobile)
return false;
if(OverlayManager.instance.shouldPauseGame())
return false;
if(input.showingGamepadCursor)
return false;

return true;
}

public void tick(Level level, float delta, GameInput input) {

boolean isInOverlay = OverlayManager.instance.current() != null && OverlayManager.instance.current().catchInput;
Expand Down Expand Up @@ -918,35 +931,45 @@ else if(input.showingGamepadCursor) {
}

input.gamepadCursorPosition.x = Math.min(input.gamepadCursorPosition.x, Gdx.graphics.getWidth());
input.gamepadCursorPosition.y = Math.min(input.gamepadCursorPosition.y, Gdx.graphics.getHeight());
input.gamepadCursorPosition.y = Math.min(input.gamepadCursorPosition.y, Gdx.graphics.getHeight() - 1);
input.gamepadCursorPosition.x = Math.max(input.gamepadCursorPosition.x, 0);
input.gamepadCursorPosition.y = Math.max(input.gamepadCursorPosition.y, 0);

Game.ui.mouseMoved((int)input.gamepadCursorPosition.x, Gdx.graphics.getHeight() - (int)input.gamepadCursorPosition.y);

if(controllerState.use) {
wasGamepadDragging = true;
input.touchDown((int)input.gamepadCursorPosition.x, Gdx.graphics.getHeight() - (int)input.gamepadCursorPosition.y, input.gamepadPointerNum, 0);
// Kick off a gamepad drag
if(!wasGamepadDragging) {
wasGamepadDragging = true;
input.touchDown((int) input.gamepadCursorPosition.x, Gdx.graphics.getHeight() - (int) input.gamepadCursorPosition.y, input.gamepadPointerNum, 0);
}
}
else if(wasGamepadDragging) {
// End a gamepad drag
wasGamepadDragging = false;
input.touchUp((int)input.gamepadCursorPosition.x, Gdx.graphics.getHeight() - (int)input.gamepadCursorPosition.y, input.gamepadPointerNum, 0);
}
}
}

// If the cursor is over an item in the game world, pick it up if the cursor is pressed
touchingItem = false;
boolean inOverlay = OverlayManager.instance.current() != null && OverlayManager.instance.current().catchInput;

if(!inOverlay && !isDead && ((Game.isMobile || !input.isCursorCatched()) && Gdx.input.isButtonPressed(Input.Buttons.LEFT))) {
boolean shouldPickupItem = (!input.isCursorCatched() || input.showingGamepadCursor) &&
Game.instance.input.isPointerTouched(0);

if(!inOverlay && !isDead && shouldPickupItem) {
boolean wasJustTouched = Gdx.input.justTouched() || input.isNewlyTouched;
if(Game.hud.dragging != null) {
touchingItem = true;
}
else if(Gdx.input.justTouched() && !attack && input.uiTouchPointer == null && input.lastTouchedPointer != null) {
Entity touching = pickEntity(level, Gdx.input.getX(input.lastTouchedPointer), Gdx.input.getY(input.lastTouchedPointer), 0.9f);
if(touching != null) input.uiTouchPointer = input.lastTouchedPointer;
else if(wasJustTouched && !attack && input.uiTouchPointer == null && input.lastTouchedPointer != null) {
Entity touching = pickEntity(level, input.getPointerX(), input.getPointerY(), 0.9f);
if(touching != null)
input.uiTouchPointer = input.lastTouchedPointer;

if(touching != null && touching instanceof Item) {
if(touching instanceof Item) {
touchingItem = true;

// drag item
Expand All @@ -955,10 +978,7 @@ else if(Gdx.input.justTouched() && !attack && input.uiTouchPointer == null && in
touching.use(this, 0, 0);
}
else {
touching.isActive = false;
Game.hud.dragging = (Item)touching;
Game.dragging = Game.hud.dragging;
Game.hud.refresh();
Game.hud.startDragFromWorld((Item)touching, input.uiTouchPointer);
}
}
}
Expand All @@ -969,11 +989,12 @@ else if (touching != null && !(touching instanceof Stairs) && !(touching instanc
}

hovering = null;
if(!touchingItem && !Game.isMobile && !input.isCursorCatched()) {
if(!touchingItem && !Game.isMobile && (!input.isCursorCatched() || input.showingGamepadCursor)) {
hovering = pickItem(level, input.getPointerX(), input.getPointerY(), 0.9f);
}

if(!isDead && (!Game.isMobile || input.isCursorCatched()) && !OverlayManager.instance.shouldPauseGame()) {
boolean showUseMessages = canShowUseMessages(input);
if(showUseMessages) {
String useText = ReadableKeys.keyNames.get(Actions.keyBindings.get(Action.USE));
if(Game.isMobile) useText = StringManager.get("entities.Player.mobileUseText");

Expand Down Expand Up @@ -1507,6 +1528,11 @@ else if(e instanceof Actor) {
}
}

// Don't try to use items if we are not showing use messages
boolean canShowUseMessages = canShowUseMessages(Game.instance.input);
if(!canShowUseMessages)
return;

// use the entity / wall hit by the camera ray
float usedist = 0.95f;
Tile hit = null;
Expand Down Expand Up @@ -1665,7 +1691,7 @@ public void throwItem(Item itm, Level level, float throwPower, float xOffset) {

itm.isActive = true;
itm.isDynamic = true;
itm.z = z + 0.5f;
itm.z = z + 0.35f;
itm.xa = projx * (throwPower * 0.3f);
itm.ya = projy * (throwPower * 0.3f);
itm.za = throwPower * 0.05f;
Expand Down
9 changes: 5 additions & 4 deletions Dungeoneer/src/com/interrupt/dungeoneer/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,11 @@ public static DragAndDropResult DragAndDropInventoryItem( Item dragging, Integer
}

String equipOverSlot = null;
for(EquipLoc loc : hud.equipLocations.values())
{
equipOverSlot = loc.getMouseOverSlot();
if(equipOverSlot != null) break;
for(EquipLoc loc : hud.equipLocations.values()) {
if(loc.isHovered()) {
equipOverSlot = loc.equipLoc;
break;
}
}

if(equipOverSlot != null) {
Expand Down
95 changes: 60 additions & 35 deletions Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@
import com.interrupt.dungeoneer.partitioning.TriangleSpatialHash;
import com.interrupt.dungeoneer.statuseffects.StatusEffect;
import com.interrupt.dungeoneer.tiles.Tile;
import com.interrupt.dungeoneer.ui.EquipLoc;
import com.interrupt.dungeoneer.ui.FontBounds;
import com.interrupt.dungeoneer.ui.Hotbar;
import com.interrupt.dungeoneer.ui.UiSkin;
import com.interrupt.dungeoneer.ui.*;
import com.interrupt.managers.ShaderManager;
import com.interrupt.managers.TileManager;
import com.noise.PerlinNoise;
Expand Down Expand Up @@ -719,17 +716,30 @@ private void drawUI() {

if(!game.gameOver)
{
if(!Options.instance.hideUI || Game.instance.getShowingMenu()) Game.ui.draw();
// Draw the UI
if(!Options.instance.hideUI || Game.instance.getShowingMenu())
Game.ui.draw();

float uiSize = Game.GetUiSize();

Item hoverItm = Game.hud.getMouseOverItem();
if(hoverItm == null) hoverItm = game.player.hovering;
// Draw any tooltips
Item hoverItm = null;

if(Game.isMobile) {
hoverItm = Game.hudManager.quickSlots.dragging;
if(hoverItm == null) hoverItm = Game.hudManager.backpack.dragging;
if(hoverItm == null) hoverItm = Game.hud.dragging;
// If nothing is being dragged, see what is under the cursor
if(Hud.getItemBeingDragged() == null) {
hoverItm = Game.hud.getHoveredInventoryItem();
if(hoverItm == null)
hoverItm = game.player.hovering;
}

boolean usingGamepadCursor = game.input.getGamepadCursorPosition() != null;
if(Game.isMobile || usingGamepadCursor) {
InventoryItemButton draggingButton = Hud.getItemBeingDragged();
if(draggingButton != null) {
hoverItm = draggingButton.getItem();
}
if(hoverItm == null)
hoverItm = Game.hud.dragging;
}

if(hoverItm != null && (OverlayManager.instance.current() == null || !OverlayManager.instance.shouldPauseGame()))
Expand All @@ -742,7 +752,10 @@ private void drawUI() {
if(Game.isMobile) {
this.drawTextOnScreen(hoverItm.GetInfoText(), Gdx.input.getX(uiTouchPointer) - Gdx.graphics.getWidth() / 2 + uiSize * 1.25f, -Gdx.input.getY(uiTouchPointer) + Gdx.graphics.getHeight() / 2, uiSize / 5, Color.WHITE, Color.BLACK);
} else {
Game.tooltip.show(game.input.getPointerX(uiTouchPointer), -game.input.getPointerY(uiTouchPointer) + Gdx.graphics.getHeight(), hoverItm);
int tooltipX = game.input.getPointerX(uiTouchPointer);
int tooltipY = game.input.getPointerY(uiTouchPointer);

Game.tooltip.show(tooltipX, -tooltipY + Gdx.graphics.getHeight(), hoverItm);
}

uiBatch.end();
Expand Down Expand Up @@ -1713,7 +1726,7 @@ protected void drawInventory(ArrayMap<String,EquipLoc> equipLocations) {
// pick a color for this box
uiBatch.enableBlending();

if((Game.isMobile || !game.input.caughtCursor) && equipLoc.getMouseOverSlot() != null && equipLoc.getMouseOverSlot() == equipLoc.equipLoc)
if((Game.isMobile || !game.input.caughtCursor) && equipLoc.isHovered())
if(Game.dragging != null && Game.dragging.GetEquipLoc().equals(equipLoc.equipLoc))
uiBatch.setColor(INVBOX_CAN_EQUIP_HOVER);
else
Expand All @@ -1731,16 +1744,22 @@ else if(Game.dragging != null) {

uiBatch.draw(itemTextures.getSprite(equipLoc.bgTex), xPos + xOffset, yPos - yOffset, uiSize, uiSize);

if(at instanceof ItemStack && !( equipLoc.isDragging.containsKey(equipLoc)) ) {
ItemStack stack = (ItemStack)at;
drawText(CachePools.toString(stack.count), xPos + xOffset + uiSize * 0.95f - (CachePools.toString(stack.count).length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}
// Draw number of charges, unless being dragged
InventoryItemButton itemButton = Hud.getItemBeingDragged();
Item beingDragged = itemButton != null ? itemButton.getItem() : null;

if(at instanceof Wand && !( equipLoc.isDragging.containsKey(equipLoc))) {
Wand wand = (Wand)at;
String chargeText = wand.getChargeText();
drawText(chargeText, xPos + xOffset + uiSize * 0.95f - (chargeText.length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}
if(at != beingDragged) {
if (at instanceof ItemStack) {
ItemStack stack = (ItemStack) at;
drawText(CachePools.toString(stack.count), xPos + xOffset + uiSize * 0.95f - (CachePools.toString(stack.count).length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}

if (at instanceof Wand) {
Wand wand = (Wand) at;
String chargeText = wand.getChargeText();
drawText(chargeText, xPos + xOffset + uiSize * 0.95f - (chargeText.length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}
}

uiBatch.end();
}
Expand Down Expand Up @@ -1787,16 +1806,22 @@ else if((Game.isMobile || !game.input.caughtCursor) && hotbar.getMouseOverSlot()

uiBatch.draw(itemTextures.getSprite(127), xPos, yPos - yOffset, uiSize, uiSize);

if(at instanceof ItemStack && !( hotbar.isDragging.containsKey(i + hotbar.invOffset)) ) {
ItemStack stack = (ItemStack)at;
drawText(CachePools.toString(stack.count), xPos + uiSize * 0.95f - (CachePools.toString(stack.count).length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}

if(at instanceof Wand && !( hotbar.isDragging.containsKey(i + hotbar.invOffset))) {
Wand wand = (Wand)at;
String chargeText = wand.getChargeText();
drawText(chargeText, xPos + uiSize * 0.95f - (chargeText.length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}
// Draw the number of charges, unless we are being dragged
InventoryItemButton itemButton = Hud.getItemBeingDragged();

Item beingDragged = itemButton != null ? itemButton.getItem() : null;
if(at != beingDragged) {
if (at instanceof ItemStack) {
ItemStack stack = (ItemStack) at;
drawText(CachePools.toString(stack.count), xPos + uiSize * 0.95f - (CachePools.toString(stack.count).length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}

if (at instanceof Wand) {
Wand wand = (Wand) at;
String chargeText = wand.getChargeText();
drawText(chargeText, xPos + uiSize * 0.95f - (chargeText.length() * uiSize * 0.2f), yPos - yOffset + uiSize * 0.65f, uiSize * 0.2f, Color.WHITE);
}
}
}
}

Expand All @@ -1815,14 +1840,14 @@ else if((Game.isMobile || !game.input.caughtCursor) && hotbar.getMouseOverSlot()
}

protected void drawGamepadCursor() {
if(game.input.getGamepadCursorPosition() != null) {
if(game.input.getGamepadCursorPosition() != null && Hud.getItemBeingDragged() == null) {
float uiSize = Game.GetUiSize();
Vector2 gamepadCursorPos = game.input.getGamepadCursorPosition();

Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
uiBatch.begin();
uiBatch.disableBlending();
uiBatch.setColor(Color.WHITE);
uiBatch.enableBlending();
uiBatch.setColor(Color.WHITE);
uiBatch.draw(itemTextures.getSprite(126), -Gdx.graphics.getWidth() / 2f + gamepadCursorPos.x - uiSize / 2.6f, -Gdx.graphics.getHeight() / 2f + gamepadCursorPos.y - uiSize / 1.25f, uiSize, uiSize);
uiBatch.end();
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Expand Down
Loading

0 comments on commit 55d9432

Please sign in to comment.