From 55d9432bc1e94fef966c64cf255a309ac4f7ac28 Mon Sep 17 00:00:00 2001 From: Chad Cuddigan Date: Fri, 13 Jan 2023 13:31:22 -0800 Subject: [PATCH] Fixes and refactoring for drag and drop (#308) * 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 --- .../com/interrupt/dungeoneer/GameInput.java | 6 +- .../interrupt/dungeoneer/entities/Player.java | 56 +++-- .../com/interrupt/dungeoneer/game/Game.java | 9 +- .../interrupt/dungeoneer/gfx/GlRenderer.java | 95 +++++--- .../com/interrupt/dungeoneer/ui/EquipLoc.java | 173 ++++---------- .../com/interrupt/dungeoneer/ui/Hotbar.java | 161 ++++--------- .../src/com/interrupt/dungeoneer/ui/Hud.java | 179 +++++---------- .../dungeoneer/ui/InventoryItemButton.java | 211 ++++++++++++++++++ .../dungeoneer/ui/MultiTouchButton.java | 26 ++- .../com/interrupt/dungeoneer/ui/Tooltip.java | 3 +- 10 files changed, 501 insertions(+), 418 deletions(-) create mode 100644 Dungeoneer/src/com/interrupt/dungeoneer/ui/InventoryItemButton.java diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/GameInput.java b/Dungeoneer/src/com/interrupt/dungeoneer/GameInput.java index cf13d28d..34150b53 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/GameInput.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/GameInput.java @@ -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); @@ -76,6 +78,8 @@ public void tick() { newlyMouseScrollUp=false; newlyMouseScrollDown=false; + isNewlyTouched = false; + if(!Gdx.input.isCursorCatched()) { ignoreLastMouseLocation = true; } @@ -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); diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/entities/Player.java b/Dungeoneer/src/com/interrupt/dungeoneer/entities/Player.java index 2ec4ca09..d58f7782 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/entities/Player.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/entities/Player.java @@ -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; @@ -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 @@ -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); } } } @@ -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"); @@ -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; @@ -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; diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/game/Game.java b/Dungeoneer/src/com/interrupt/dungeoneer/game/Game.java index fcc4c83a..7994c963 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/game/Game.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/game/Game.java @@ -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) { diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java b/Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java index b7f46b22..f4e75da2 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java @@ -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; @@ -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())) @@ -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(); @@ -1713,7 +1726,7 @@ protected void drawInventory(ArrayMap 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 @@ -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(); } @@ -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); + } + } } } @@ -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); diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/ui/EquipLoc.java b/Dungeoneer/src/com/interrupt/dungeoneer/ui/EquipLoc.java index c064a81a..5b554c9d 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/ui/EquipLoc.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/ui/EquipLoc.java @@ -22,25 +22,21 @@ import com.interrupt.dungeoneer.ui.Hud.DragAndDropResult; public class EquipLoc { - + protected TextureRegion itemTextures[]; - public final HashMap itemButtons = new HashMap(); - public final HashMap wasPressedLast = new HashMap(); - public final HashMap isDragging = new HashMap(); - private String mouseOverSlot = null; - + public final HashMap itemButtons = new HashMap<>(); + private boolean isHovered = false; + public float yOffset = 0; public float xOffset = 0; public int bgTex = 127; - + public boolean visible = false; - - public Item dragging = null; - + public Integer lastUiTouchPointer = null; - + public String equipLoc = "ARMOR"; - + public EquipLoc(String equipLoc, float xOffset, float yOffset, int tex) { this.equipLoc = equipLoc; @@ -48,186 +44,111 @@ public EquipLoc(String equipLoc, float xOffset, float yOffset, int tex) this.yOffset = yOffset; this.bgTex = tex; } - + public void init(TextureRegion itemTextures[]) { this.itemTextures = itemTextures; } - - public void refresh() { - for(Entry entry : itemButtons.entrySet()) { + + public void refresh() { + for(Entry entry : itemButtons.entrySet()) { Game.ui.getActors().removeValue(entry.getValue(), true); } - + lastUiTouchPointer = null; - itemButtons.clear(); - wasPressedLast.clear(); - isDragging.clear(); - + if(Game.ui == null || !visible) return; - + if(Game.instance.player.equippedItems.containsKey(equipLoc)) { Item itm = Game.instance.player.equippedItems.get(equipLoc); - + if(itm != null) { TextureAtlas atlas = TextureAtlas.cachedAtlases.get(itm.spriteAtlas); if(atlas == null) atlas = TextureAtlas.cachedAtlases.get(itm.artType.toString()); if(atlas != null) { - TextureRegion region = atlas.getSprite(itm.getInventoryTex()); - - MultiTouchButton itmButton = new MultiTouchButton(new TextureRegionDrawable(itm.getInventoryTextureRegion()), new TextureRegionDrawable(itm.getInventoryTextureRegion())); + InventoryItemButton itmButton = new InventoryItemButton(this, null, new TextureRegionDrawable(itm.getInventoryTextureRegion())); itemButtons.put(equipLoc, itmButton ); - Game.ui.addActor(itmButton); - - itmButton.addListener( - new ClickListener() { - @Override - public void clicked(InputEvent event, float x, float y) { - if(Game.isMobile || !Game.instance.input.caughtCursor) - { - if(!isDragging.containsKey(equipLoc) || !isDragging.get(equipLoc)) - { - // TODO: equip, unequip here - //Game.instance.player.DoHotbarAction(btnLoc + 1); - } - } - } - } - ); } } } - + initButtons(); } - + public void initButtons() { final float uiSize = Game.GetUiSize(); - + int x = 0; int y = 0; - + if(itemButtons.containsKey(equipLoc)) { Button hb = itemButtons.get(equipLoc); - + int yPos = (int) (((Gdx.graphics.getHeight() - uiSize) - (int)(y * uiSize)) - (yOffset * uiSize)); int xPos = (int) ((uiSize * x + Gdx.graphics.getWidth() / 2.0 - uiSize * ( 1 / 2.0 )) + (xOffset * uiSize)); - + if(!hb.isPressed()) { hb.setY(yPos); hb.setX(xPos); } - + hb.setWidth(uiSize); hb.setHeight(uiSize); } } - + public void tickUI(GameInput input) - { + { Integer uiTouchPointer = input.uiTouchPointer; if(uiTouchPointer != null) lastUiTouchPointer = uiTouchPointer; else if (lastUiTouchPointer != null) uiTouchPointer = lastUiTouchPointer; else uiTouchPointer = 0; - + final float uiSize = Game.GetUiSize(); final float xCursorPos = input.getPointerX(uiTouchPointer) - Gdx.graphics.getWidth() / 2.0f; final float yCursorPos = input.getPointerY(uiTouchPointer); - mouseOverSlot = null; - dragging = null; - + isHovered = false; + if(!visible) return; - + if(Game.isMobile || !Game.instance.input.caughtCursor) { int x = 0; int y = 0; - + float xPos1 = -((uiSize * 1) / 2.0f) + uiSize * x; float xPosD = xPos1 + (xOffset * uiSize); - + if((Game.isMobile || !input.caughtCursor) && xCursorPos > xPosD && xCursorPos <= xPosD + uiSize && yCursorPos <= ((y + 1) * uiSize) + (yOffset * uiSize) && yCursorPos > (y * uiSize) + (yOffset * uiSize)) { - mouseOverSlot = equipLoc; + isHovered = true; } } - + if(itemButtons.containsKey(equipLoc)) { - MultiTouchButton hb = itemButtons.get(equipLoc); - + InventoryItemButton hb = itemButtons.get(equipLoc); + int x = 0; int y = 0; - + int yPos = (int) (((Gdx.graphics.getHeight() - uiSize) - (int)(y * uiSize)) - (yOffset * uiSize)); int xPos = (int) ((uiSize * x + Gdx.graphics.getWidth() / 2.0 - uiSize * ( 1 / 2.0 )) + (xOffset * uiSize)); - + hb.setWidth(uiSize); hb.setHeight(uiSize); - - if(hb.isDragging) - { - - if(Game.isMobile || !Game.instance.input.caughtCursor) - { - if( (isDragging.containsKey(equipLoc) && isDragging.get(equipLoc) ) || Math.abs(input.getUiTouchPosition().x - input.getPointerX(uiTouchPointer)) > uiSize / 8 || Math.abs(input.getUiTouchPosition().y - input.getPointerY(uiTouchPointer)) > uiSize / 8) { - hb.setY(-input.getPointerY(uiTouchPointer) + Gdx.graphics.getHeight() - uiSize / 2); - hb.setX(input.getPointerX(uiTouchPointer) - uiSize / 2); - isDragging.put(equipLoc, true); - - dragging = Game.instance.player.equippedItems.get(equipLoc); - Game.dragging = dragging; - } - } - - if(!isDragging.containsKey(equipLoc) || !isDragging.get(equipLoc)) { - hb.setX(xPos); - hb.setY(yPos); - } - } - else - { - // item was dragged out of the equip slot, switch inv slot or drop item - if(wasPressedLast.containsKey(equipLoc) && wasPressedLast.get(equipLoc)) { - - DragAndDropResult movedItem = Game.DragAndDropInventoryItem( Game.instance.player.equippedItems.get(equipLoc), null, equipLoc ); - - if(movedItem == DragAndDropResult.drop) - { - Item dragging = Game.instance.player.equippedItems.get(equipLoc); - Game.instance.player.equippedItems.put(equipLoc, null); - - Vector3 levelIntersection = new Vector3(); - Ray ray = Game.camera.getPickRay(input.getPointerX(uiTouchPointer), input.getPointerY(uiTouchPointer)); - float distance = 0; - if(Collidor.intersectRayTriangles(ray, GameManager.renderer.GetCollisionTrianglesAlong(ray, 20f), levelIntersection, null)) { - distance = ray.origin.sub(levelIntersection).len(); - } - - Game.instance.player.throwItem(dragging, Game.instance.level, 0, 0); - - dragging.xa = ray.direction.x * 0.28f * Math.min(1, distance / 6.0f); - dragging.za = ray.direction.y * 0.5f * Math.min(1, distance / 6.0f) + 0.04f; - dragging.ya = ray.direction.z * 0.28f * Math.min(1, distance / 6.0f); - } - - Game.RefreshUI(); - } - - hb.setY(yPos); - hb.setX(xPos); - } - - if(Game.isMobile || !Game.instance.input.caughtCursor) { - wasPressedLast.put(equipLoc, hb.isDragging); - } + + if(!hb.isBeingDragged) { + hb.setY(yPos); + hb.setX(xPos); + } } } - - public String getMouseOverSlot() { - return mouseOverSlot; - } + + public boolean isHovered() { + return isHovered; + } } diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hotbar.java b/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hotbar.java index 9d2d00ce..b2535750 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hotbar.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hotbar.java @@ -1,26 +1,17 @@ package com.interrupt.dungeoneer.ui; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.math.collision.Ray; -import com.badlogic.gdx.scenes.scene2d.InputEvent; -import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.interrupt.dungeoneer.GameInput; -import com.interrupt.dungeoneer.GameManager; -import com.interrupt.dungeoneer.collision.Collidor; import com.interrupt.dungeoneer.entities.Item; import com.interrupt.dungeoneer.game.Game; -import com.interrupt.dungeoneer.ui.Hud.DragAndDropResult; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; public class Hotbar { - public final Map itemButtons = new HashMap<>(); - public final Map wasPressedLast = new HashMap<>(); - public final Map isDragging = new HashMap<>(); + public final Map itemButtons = new HashMap<>(); public int columns = 6; public int rows = 1; public int invOffset = 0; @@ -32,7 +23,7 @@ public class Hotbar { private Integer mouseOverSlot = null; private Integer lastUiTouchPointer = null; - public Hotbar() {} + public Hotbar() { } public Hotbar(int columns, int rows, int invOffset) { this.columns = columns; @@ -45,15 +36,15 @@ public void init() { } public void refresh() { - for (Entry entry : itemButtons.entrySet()) { + GameInput input = Game.instance.input; + + for (Entry entry : itemButtons.entrySet()) { Game.ui.getActors().removeValue(entry.getValue(), true); } lastUiTouchPointer = null; itemButtons.clear(); - wasPressedLast.clear(); - isDragging.clear(); if (Game.ui == null || !visible) return; @@ -73,26 +64,9 @@ public void refresh() { if (btnLoc < Game.instance.player.inventorySize) { Item itm = Game.instance.player.inventory.get(btnLoc); if (itm != null) { - MultiTouchButton itmButton = new MultiTouchButton( - new TextureRegionDrawable(itm.getInventoryTextureRegion()), - new TextureRegionDrawable(itm.getInventoryTextureRegion())); + InventoryItemButton itmButton = new InventoryItemButton(null, btnLoc, new TextureRegionDrawable(itm.getInventoryTextureRegion())); itemButtons.put(btnLoc, itmButton); - Game.ui.addActor(itmButton); - - itmButton.addListener(new ClickListener() { - @Override - public void clicked(InputEvent event, float x, float y) { - if (Game.isMobile || !Game.instance.input.caughtCursor) { - if (!isDragging.containsKey(btnLoc) || !isDragging.get(btnLoc)) { - isDragging.clear(); - wasPressedLast.clear(); - mouseOverSlot = null; - Game.instance.player.UseInventoryItem(btnLoc); - } - } - } - }); } } } @@ -109,13 +83,14 @@ private void initButtons() { final int i = x + (y * columns) + invOffset; if (i < Game.instance.player.inventorySize) { if (itemButtons.containsKey(i)) { - MultiTouchButton hb = itemButtons.get(i); + InventoryItemButton hb = itemButtons.get(i); int yPos = (int) (((Gdx.graphics.getHeight() - uiSize) - (int) (y * uiSize)) - (yOffset * uiSize)); int xPos = (int) (uiSize * x + Gdx.graphics.getWidth() / 2.0 - uiSize * (columns / 2.0)); - if (!hb.isDragging) { + // Put the items not being dragged in the grid + if(Hud.getItemBeingDragged() != hb) { hb.setY(yPos); hb.setX(xPos); } @@ -128,6 +103,30 @@ private void initButtons() { } } + public Integer getMouseOverSlot(GameInput input, Integer uiTouchPointer) { + final float uiSize = Game.GetUiSize(); + final float xCursorPos = input.getPointerX(uiTouchPointer) - Gdx.graphics.getWidth() / 2.0f; + final float yCursorPos = input.getPointerY(uiTouchPointer); + + for (int y = 0; y < rows; y++) { + for (int x = 0; x < columns; x++) { + final int i = x + (y * columns); + float xPos = -((uiSize * columns) / 2.0f) + (uiSize * x); + float yPos = (y * uiSize) + (yOffset * uiSize); + + if (xCursorPos > xPos && xCursorPos <= xPos + uiSize + && yCursorPos > yPos && yCursorPos <= yPos + uiSize) { + final int btnLoc = x + (y * columns) + invOffset; + if (btnLoc < Game.instance.player.inventorySize) { + return i; + } + } + } + } + + return null; + } + public void tickUI(GameInput input) { Integer uiTouchPointer = input.uiTouchPointer; if (uiTouchPointer != null) @@ -138,100 +137,36 @@ else if (lastUiTouchPointer != null) uiTouchPointer = 0; final float uiSize = Game.GetUiSize(); - final float xCursorPos = input.getPointerX(uiTouchPointer) - Gdx.graphics.getWidth() / 2.0f; - final float yCursorPos = input.getPointerY(uiTouchPointer); mouseOverSlot = null; dragging = null; if (!visible) return; - if (Game.isMobile || !Game.instance.input.caughtCursor) { - for (int y = 0; y < rows; y++) { - for (int x = 0; x < columns; x++) { - final int i = x + (y * columns); - float xPos1 = -((uiSize * columns) / 2.0f) + uiSize * x; - float xPosD = xPos1; - - if ((Game.isMobile || !input.caughtCursor) && xCursorPos > xPosD && xCursorPos <= xPosD + uiSize - && yCursorPos <= ((y + 1) * uiSize) + (yOffset * uiSize) - && yCursorPos > (y * uiSize) + (yOffset * uiSize)) { - final int btnLoc = x + (y * columns) + invOffset; - if (btnLoc < Game.instance.player.inventorySize) { - mouseOverSlot = i; - } - } - } - } + // We are able to drag inventory items whenever in mobile mode, or whenever the cursor is showing + boolean canDrag = Game.isMobile || !Game.instance.input.caughtCursor; + + // If we can drag, figure out which inventory slot the cursor is over + if (canDrag) { + mouseOverSlot = getMouseOverSlot(input, uiTouchPointer); } + // Snap the button positions to the grid for (int y = 0; y < rows; y++) { for (int x = 0; x < columns; x++) { final int i = x + (y * columns) + invOffset; if (itemButtons.containsKey(i)) { - MultiTouchButton hb = itemButtons.get(i); - + InventoryItemButton hb = itemButtons.get(i); int yPos = (int) (((Gdx.graphics.getHeight() - uiSize) - (int) (y * uiSize)) - (yOffset * uiSize)); int xPos = (int) (uiSize * x + Gdx.graphics.getWidth() / 2.0 - uiSize * (columns / 2.0)); hb.setWidth(uiSize); hb.setHeight(uiSize); - if (hb.isDragging) { - if (Game.isMobile || !Game.instance.input.caughtCursor) { - if ((isDragging.containsKey(i) && isDragging.get(i)) - || Math.abs(input.getUiTouchPosition().x - - input.getPointerX(uiTouchPointer)) > uiSize / 8 - || Math.abs(input.getUiTouchPosition().y - - input.getPointerY(uiTouchPointer)) > uiSize / 8) { - hb.setY(-input.getPointerY(uiTouchPointer) + Gdx.graphics.getHeight() - uiSize / 2); - hb.setX(input.getPointerX(uiTouchPointer) - uiSize / 2); - isDragging.put(i, true); - - dragging = Game.instance.player.inventory.get(i); - Game.dragging = dragging; - } - } - - if (!isDragging.containsKey(i) || !isDragging.get(i)) { - hb.setX(xPos); - hb.setY(yPos); - } - } else { - // item was dragged, switch inv slot or drop item - if (wasPressedLast.containsKey(i) && wasPressedLast.get(i)) { - DragAndDropResult movedItem = Game - .DragAndDropInventoryItem(Game.instance.player.inventory.get(i), i, null); - - if (movedItem == DragAndDropResult.drop) { - Item draggedItem = Game.instance.player.inventory.get(i); - Game.instance.player.dropItemFromInv(i, Game.GetLevel(), 0, 0); - - Vector3 levelIntersection = new Vector3(); - Ray ray = Game.camera.getPickRay(input.getPointerX(uiTouchPointer), - input.getPointerY(uiTouchPointer)); - float distance = 0; - if (Collidor.intersectRayTriangles(ray, - GameManager.renderer.GetCollisionTrianglesAlong(ray, 20f), levelIntersection, - null)) { - distance = ray.origin.sub(levelIntersection).len(); - } - - draggedItem.xa = ray.direction.x * 0.28f * Math.min(1, distance / 6.0f); - draggedItem.za = ray.direction.y * 0.5f * Math.min(1, distance / 6.0f) + 0.04f; - draggedItem.ya = ray.direction.z * 0.28f * Math.min(1, distance / 6.0f); - } - - Game.RefreshUI(); - break; - } - - hb.setY(yPos); + // Snap items not being dragged to the grid + if(Hud.getItemBeingDragged() != hb) { hb.setX(xPos); - } - - if (Game.isMobile || !Game.instance.input.caughtCursor) { - wasPressedLast.put(i, hb.isDragging); + hb.setY(yPos); } } } diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hud.java b/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hud.java index 6609c150..da52ec35 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hud.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/ui/Hud.java @@ -32,15 +32,16 @@ public class Hud { MultiTouchButton inventoryBtn; MultiTouchButton mapBtn; - MultiTouchButton draggingBtn = null; + // Item currently being dragged public Item dragging = null; - private Integer lastUiTouchPointer; - public ArrayMap equipLocations = new ArrayMap(); - public enum DragAndDropResult { equip, drop, invalid, ignore }; + // Item Button being dragged + private static InventoryItemButton itemBeingDragged; + private static InventoryItemButton itemClickedInitially; + public void init(TextureRegion itemTextures[]) { this.itemTextures = itemTextures; @@ -107,17 +108,6 @@ public void clicked(InputEvent event, float x, float y) { }); Game.ui.addActor(mapBtn); - if(dragging != null) { - TextureAtlas atlas = TextureAtlas.cachedAtlases.get(dragging.spriteAtlas); - if(atlas == null) atlas = TextureAtlas.cachedAtlases.get("item"); - - draggingBtn = new MultiTouchButton(new TextureRegionDrawable(dragging.getInventoryTextureRegion())); - Game.ui.addActor(draggingBtn); - } else if(draggingBtn != null) { - Game.ui.getActors().removeValue(draggingBtn, true); - draggingBtn = null; - } - final float uiSize = Game.GetUiSize(); final float inventoryBtnSize = uiSize + (inventoryBtn.isPressed() ? 0f : 0.1f) * uiSize; @@ -146,116 +136,24 @@ public void tick(GameInput input) { float uiSize = Game.GetUiSize(); final float inventoryBtnSize = uiSize + (inventoryBtn.isPressed() ? 0.1f : 0f) * uiSize; - inventoryBtn.setWidth(inventoryBtnSize); - inventoryBtn.setHeight(inventoryBtnSize); - - mapBtn.setWidth(uiSize * 1.4f); - mapBtn.setHeight(uiSize * 1.4f); - - Integer uiTouchPointer = input.uiTouchPointer; - if(uiTouchPointer != null) lastUiTouchPointer = uiTouchPointer; - else if (lastUiTouchPointer != null) uiTouchPointer = lastUiTouchPointer; - else uiTouchPointer = 0; - - if(draggingBtn != null && input.uiTouchPointer != null) { - draggingBtn.setWidth(uiSize); - draggingBtn.setHeight(uiSize); - - draggingBtn.setY(-Game.instance.input.getPointerY(uiTouchPointer) + Gdx.graphics.getHeight() - uiSize / 2); - draggingBtn.setX(Game.instance.input.getPointerX(uiTouchPointer) - uiSize / 2); - } - else if(draggingBtn != null) - { - String equipOverSlot = null; - for(EquipLoc loc : equipLocations.values()) - { - equipOverSlot = loc.getMouseOverSlot(); - if(equipOverSlot != null) break; - } - - if(Game.hudManager.quickSlots.getMouseOverSlot() != null) { - Game.DragAndDropInventoryItem(dragging, null, null); - Game.hudManager.quickSlots.refresh(); - dragging = null; - } - else if(Game.hudManager.backpack.getMouseOverSlot() != null) { - Game.DragAndDropInventoryItem(dragging, null, null); - Game.hudManager.backpack.refresh(); - dragging = null; - } - else if(equipOverSlot != null) { - DragAndDropResult res = Game.DragAndDropInventoryItem(dragging, null, null); - - if(res == DragAndDropResult.invalid) - { - Game.instance.player.throwItem(dragging, Game.instance.level, 0, 0); - dragging.isActive = true; - } - - dragging = null; - - refresh(); - } - else if(Game.instance.input.getPointerX(uiTouchPointer) > Gdx.graphics.getWidth() - uiSize && Game.instance.input.getPointerY(uiTouchPointer) < uiSize) { - // dropping item into inventory - boolean foundSlot = false; - for(int i = 6; i < Game.instance.player.inventorySize; i++) { - if(Game.instance.player.inventory.get(i) == null) { - foundSlot = true; - Game.instance.player.inventory.set(i, dragging); - - if(dragging instanceof QuestItem) { - ((QuestItem)dragging).doQuestThing(); - } - - dragging = null; - - Game.hudManager.backpack.refresh(); - } - } - if(!foundSlot) Game.ShowMessage(StringManager.get("ui.Hud.noRoomText"), 0.6f, 1f); - } - else { - - Vector3 levelIntersection = new Vector3(); - Ray ray = Game.camera.getPickRay(Game.instance.input.getPointerX(uiTouchPointer), Game.instance.input.getPointerY(uiTouchPointer)); - float distance = 0; - if(Collidor.intersectRayTriangles(ray, GameManager.renderer.GetCollisionTrianglesAlong(ray, 20f), levelIntersection, null)) { - distance = ray.origin.sub(levelIntersection).len(); - } - - Game.instance.player.throwItem(dragging, Game.instance.level, 0, 0); - - dragging.xa = ray.direction.x * 0.28f * Math.min(1, distance / 6.0f); - dragging.za = ray.direction.y * 0.5f * Math.min(1, distance / 6.0f) + 0.04f; - dragging.ya = ray.direction.z * 0.28f * Math.min(1, distance / 6.0f); - dragging = null; - } - - refresh(); - } - + // Inventory Button position and sizing int yPos = (int) (Gdx.graphics.getHeight() - uiSize); int xPos = (int) (Gdx.graphics.getWidth() - uiSize); inventoryBtn.setY(yPos); inventoryBtn.setX(xPos); + inventoryBtn.setWidth(inventoryBtnSize); + inventoryBtn.setHeight(inventoryBtnSize); + + // Map Button position and sizing mapBtn.setX(0); mapBtn.setY((int) (Gdx.graphics.getHeight() - mapBtn.getHeight())); - } - - public Integer getMouseOverSlot() - { - if(Game.hudManager.backpack.getMouseOverSlot() != null) - return Game.hudManager.backpack.getMouseOverSlot() + Game.hudManager.backpack.invOffset; - if(Game.hudManager.quickSlots.getMouseOverSlot() != null) - return Game.hudManager.quickSlots.getMouseOverSlot() + Game.hudManager.quickSlots.invOffset; - - return null; + mapBtn.setWidth(uiSize * 1.4f); + mapBtn.setHeight(uiSize * 1.4f); } - public Item getMouseOverItem() + public Item getHoveredInventoryItem() { Integer bagOver = Game.hudManager.backpack.getMouseOverSlot(); if(bagOver != null) return Game.instance.player.inventory.get(bagOver + Game.hudManager.backpack.invOffset); @@ -263,9 +161,8 @@ public Item getMouseOverItem() Integer hotbarOver = Game.hudManager.quickSlots.getMouseOverSlot(); if(hotbarOver != null) return Game.instance.player.inventory.get(hotbarOver + Game.hudManager.quickSlots.invOffset); - for(EquipLoc loc : equipLocations.values()) - { - if(loc.getMouseOverSlot() != null) { + for(EquipLoc loc : equipLocations.values()) { + if(loc.isHovered()) { return Game.instance.player.equippedItems.get(loc.equipLoc); } } @@ -273,5 +170,51 @@ public Item getMouseOverItem() return null; } + public void startDragFromWorld(Item item, int cursor) { + InventoryItemButton newDraggingItem = new InventoryItemButton(null, null, new TextureRegionDrawable(item.getInventoryTextureRegion())); + newDraggingItem.itemFromWorld = item; + newDraggingItem.touchStarted(cursor); + newDraggingItem.dragStarted(); + + // Remove the item from the game world + item.isActive = false; + + // Add this new button to the UI + Game.ui.addActor(newDraggingItem); + } + public boolean isAttackPressed() { return false; } + + public static InventoryItemButton getItemBeingDragged() { + return itemBeingDragged; + } + + public static void setItemBeingDragged(InventoryItemButton itemButton) { + itemBeingDragged = itemButton; + + if(itemButton == null) { + Game.dragging = null; + return; + } + + if(itemBeingDragged.inventorySlot != null) { + Game.dragging = Game.instance.player.inventory.get(itemBeingDragged.inventorySlot); + return; + } + if(itemButton.equipLoc != null) { + Game.dragging = Game.instance.player.equippedItems.get(itemButton.equipLoc.equipLoc); + return; + } + if(itemButton.itemFromWorld != null) { + Game.dragging = itemButton.itemFromWorld; + } + } + + public static InventoryItemButton getItemAtTouchStart() { + return itemClickedInitially; + } + + public static void setItemAtTouchStart(InventoryItemButton clickStartItem) { + itemClickedInitially = clickStartItem; + } } diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/ui/InventoryItemButton.java b/Dungeoneer/src/com/interrupt/dungeoneer/ui/InventoryItemButton.java new file mode 100644 index 00000000..dec6c89c --- /dev/null +++ b/Dungeoneer/src/com/interrupt/dungeoneer/ui/InventoryItemButton.java @@ -0,0 +1,211 @@ +package com.interrupt.dungeoneer.ui; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Cursor; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.math.collision.Ray; +import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.InputListener; +import com.badlogic.gdx.scenes.scene2d.ui.Button; +import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; +import com.badlogic.gdx.scenes.scene2d.utils.Drawable; +import com.interrupt.dungeoneer.GameInput; +import com.interrupt.dungeoneer.GameManager; +import com.interrupt.dungeoneer.collision.Collidor; +import com.interrupt.dungeoneer.entities.Item; +import com.interrupt.dungeoneer.game.Game; + +public class InventoryItemButton extends Button { + + public boolean isTouched = false; + public boolean isBeingDragged = false; + public int cursor; + + // Either dragging an item from an inventory slot, an equip location, or from the world + public Integer inventorySlot; + public EquipLoc equipLoc; + public Item itemFromWorld; + + GameInput gameInput; + + public InventoryItemButton(EquipLoc inEquipLoc, Integer inInventorySlot, Drawable itemRegion) { + super(itemRegion); + + final InventoryItemButton thisButton = this; + + // Keep track of where we started + equipLoc = inEquipLoc; + gameInput = Game.instance.input; + inventorySlot = inInventorySlot; + + // Add a listener for drag events + addListener(new InputListener() { + @Override + public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { + // Start a drag, if nobody else has yet + if(button == 0 && Hud.getItemAtTouchStart() == null) { + thisButton.touchStarted(pointer); + } + return true; + } + }); + + // Also add a listener for click events + addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) { + if(thisButton.isBeingDragged) + return; + + if(inventorySlot != null) + Game.instance.player.UseInventoryItem(inventorySlot); + + // Reset for next time + thisButton.isTouched = false; + thisButton.isBeingDragged = false; + Hud.setItemBeingDragged(null); + Hud.setItemAtTouchStart(null); + } + }); + } + + public void touchStarted(int pointer) { + Gdx.app.log("DelverInventory", "Touch Started"); + Hud.setItemAtTouchStart(this); + isTouched = true; + cursor = pointer; + } + + public void dragStarted() { + // Set ourselves as the item being dragged. + Gdx.app.log("DelverInventory", "Drag started!"); + Hud.setItemBeingDragged(this); + isBeingDragged = true; + + final float uiSize = Game.GetUiSize(); + setWidth(uiSize); + setHeight(uiSize); + + setCursorVisibility(false); + } + + public void setCursorVisibility(boolean visible) { + if(Game.isMobile || gameInput.showingGamepadCursor) + return; + + if(visible) { + Gdx.graphics.setSystemCursor(Cursor.SystemCursor.Arrow); + } else { + Gdx.graphics.setSystemCursor(Cursor.SystemCursor.None); + } + } + + public void touchEnded() { + if(isBeingDragged) { + Hud.setItemBeingDragged(null); + finishDragAndDrop(); + } + + // Reset back to defaults + isTouched = false; + isBeingDragged = false; + Hud.setItemAtTouchStart(null); + } + + public void finishDragAndDrop() { + Item draggedItem = getItem(); + + // Either drop the item on the ground, or in the inventory somewhere + Hud.DragAndDropResult movedItem = Game + .DragAndDropInventoryItem(draggedItem, inventorySlot, equipLoc != null ? equipLoc.equipLoc : null); + + if (movedItem == Hud.DragAndDropResult.drop) { + // Can either be dropped from the inventory, or an equip slot + if(inventorySlot != null) + Game.instance.player.dropItemFromInv(inventorySlot, Game.GetLevel(), 0, 0); + else + Game.instance.player.dropItem(draggedItem, Game.instance.level, 0); + + Vector3 levelIntersection = new Vector3(); + Ray ray = Game.camera.getPickRay(gameInput.getPointerX(cursor), gameInput.getPointerY(cursor)); + float distance = 0; + if (Collidor.intersectRayTriangles(ray, + GameManager.renderer.GetCollisionTrianglesAlong(ray, 20f), levelIntersection, + null)) { + distance = ray.origin.sub(levelIntersection).len(); + } + + draggedItem.xa = ray.direction.x * 0.28f * Math.min(1, distance / 6.0f); + draggedItem.za = ray.direction.y * 0.5f * Math.min(1, distance / 6.0f) + 0.04f; + draggedItem.ya = ray.direction.z * 0.28f * Math.min(1, distance / 6.0f); + + // Clear the equip location when dropped + if(equipLoc != null) { + Game.instance.player.equippedItems.put(equipLoc.equipLoc, null); + } + } + + if(itemFromWorld != null) + getParent().removeActor(this); + + Game.RefreshUI(); + + // Show the cursor again + setCursorVisibility(true); + } + + @Override + public void act(float delta) { + super.act(delta); + + // Nothing to do if we we were not clicked on before + if(!isTouched) + return; + + // Check if we should end our touch / drag and drop + if(!gameInput.isPointerTouched(cursor)) { + touchEnded(); + return; + } + + final float uiSize = Game.GetUiSize(); + if(isBeingDragged) { + // If we are being dragged, our position should be the cursor position + setY(-gameInput.getPointerY(cursor) + Gdx.graphics.getHeight() - uiSize / 2); + setX(gameInput.getPointerX(cursor) - uiSize / 2); + setWidth(uiSize); + setHeight(uiSize); + return; + } + + float movedAmountX = Math.abs(gameInput.getUiTouchPosition().x - gameInput.getPointerX(cursor)); + float movedAmountY = Math.abs(gameInput.getUiTouchPosition().y - gameInput.getPointerY(cursor)); + + // Check if we have been moved far enough to start being dragged + boolean hasMovedEnoughX = movedAmountX > uiSize / 8; + boolean hasMovedEnoughY = movedAmountY > uiSize / 8; + boolean hasMovedEnough = hasMovedEnoughX || hasMovedEnoughY; + + //Gdx.app.log("DelverInventory", "Drag amount: " + moveAmountX + "," + moveAmountY); + + if(hasMovedEnough) { + dragStarted(); + } + } + + public Item getItem() { + if(itemFromWorld != null) { + return itemFromWorld; + } + + if(inventorySlot != null) { + return Game.instance.player.inventory.get(inventorySlot); + } + + if(equipLoc != null) { + return Game.instance.player.equippedItems.get(equipLoc.equipLoc); + } + + return null; + } +} diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/ui/MultiTouchButton.java b/Dungeoneer/src/com/interrupt/dungeoneer/ui/MultiTouchButton.java index e26947d4..fab5752a 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/ui/MultiTouchButton.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/ui/MultiTouchButton.java @@ -1,5 +1,6 @@ package com.interrupt.dungeoneer.ui; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; @@ -7,25 +8,25 @@ import com.badlogic.gdx.scenes.scene2d.utils.Drawable; public class MultiTouchButton extends Button { - + public boolean isDragging = false; public MultiTouchButton(Drawable region) { super(region); } - + public MultiTouchButton(Drawable regionUp, Drawable regionDown) { super(regionUp, regionDown); - + final MultiTouchButton btn = this; - + addListener(new InputListener() { @Override public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { btn.isDragging = button == 0; return true; } - + @Override public void touchUp (InputEvent event, float x, float y, int pointer, int button) { btn.isDragging = false; @@ -33,6 +34,21 @@ public void touchUp (InputEvent event, float x, float y, int pointer, int button }); } + public void touchStarted() { + isDragging = true; + } + + public void touchEnded() { + isDragging = false; + + // Do drag and drop here! + } + + @Override + public void act(float delta) { + super.act(delta); + } + /*@Override public boolean touchDown (float x, float y, int pointer) { pointer = 0; diff --git a/Dungeoneer/src/com/interrupt/dungeoneer/ui/Tooltip.java b/Dungeoneer/src/com/interrupt/dungeoneer/ui/Tooltip.java index 8959f7d0..78af8396 100644 --- a/Dungeoneer/src/com/interrupt/dungeoneer/ui/Tooltip.java +++ b/Dungeoneer/src/com/interrupt/dungeoneer/ui/Tooltip.java @@ -104,7 +104,8 @@ private void place(float x, float y) { // Might need to adjust for the gamepad cursor position if(Game.instance.input.getGamepadCursorPosition() != null) { - x += Options.instance.uiSize * 75f; + x += Options.instance.uiSize * 30f; + y += Options.instance.uiSize * -30f; } float fontScale = Options.instance != null ? Options.instance.uiSize : 1f;