private void OnKeyDown(InputState inputState, Keys key) { if (consoleOpen) { String s = keyToString(inputState, key); if(s.Length!=0){ if (inputPosition == input.Length) { input += s; } else { input = input.Substring(0, inputPosition) + s + input.Substring(inputPosition); } inputPosition++; } } }
public override InputState update(Game game, InputState inputState) { updateKeys(inputState); if(consoleOpen){ if (inputState.pressed(Game.KEY_CONSOLE_UP)) { if (historyLookBackIndex>0) { historyLookBackIndex--; } } if (inputState.pressed(Game.KEY_CONSOLE_DOWN)) { if (historyLookBackIndex < previousInputs.Count()-1) { historyLookBackIndex++; } } if (inputState.pressed(Game.KEY_CONSOLE_UP) || inputState.pressed(Game.KEY_CONSOLE_DOWN)) { if (historyLookBackIndex >= 0 && historyLookBackIndex < previousInputs.Count()) { input = previousInputs[historyLookBackIndex]; inputPosition = input.Length; consoleOpen = true; inputState.eatKeyboard(); //inputState.keysOn = false; } } if (inputState.pressed(Game.KEY_CONSOLE_ENTER)) { previousInputs.Add(input); historyLookBackIndex = previousInputs.Count(); consoleOpen = false; proccessInput(input); //inputState.keysOn = true; input = ""; inputPosition = 0; } } else { if (inputState.pressed(Game.KEY_CONSOLE_OPEN) || inputState.pressed(Game.KEY_CONSOLE_ENTER)) { input = ""; inputPosition = 0; consoleOpen = true; //inputState.keysOn = false; } if (inputState.pressed(Game.KEY_CONSOLE_OPEN_COMMAND)) { input = "/"; inputPosition = 1; consoleOpen = true; //inputState.keysOn = false; } textFade -= .01f; } if(consoleOpen){ inputState.eatKeyboard(); } return inputState; }
private string keyToString(InputState inputState, Keys key) { bool shift = false; if(inputState.down(Keys.LeftShift) || inputState.down(Keys.RightShift)){ shift = !shift; } switch (key) { case Keys.Back: { if (inputPosition > 0) { input = input.Substring(0, inputPosition - 1) + input.Substring(inputPosition); inputPosition--; } return ""; } case Keys.A: return shift ? "A" : "a"; case Keys.B: return shift ? "B" : "b"; case Keys.C: return shift ? "C" : "c"; case Keys.D: return shift ? "D" : "d"; case Keys.E: return shift ? "E" : "e"; case Keys.F: return shift ? "F" : "f"; case Keys.G: return shift ? "G" : "g"; case Keys.H: return shift ? "H" : "h"; case Keys.I: return shift ? "I" : "i"; case Keys.J: return shift ? "J" : "j"; case Keys.K: return shift ? "K" : "k"; case Keys.L: return shift ? "L" : "l"; case Keys.M: return shift ? "M" : "m"; case Keys.N: return shift ? "N" : "n"; case Keys.O: return shift ? "O" : "o"; case Keys.P: return shift ? "P" : "p"; case Keys.Q: return shift ? "Q" : "q"; case Keys.R: return shift ? "R" : "r"; case Keys.S: return shift ? "S" : "s"; case Keys.T: return shift ? "T" : "t"; case Keys.U: return shift ? "U" : "u"; case Keys.V: return shift ? "V" : "v"; case Keys.W: return shift ? "W" : "w"; case Keys.X: return shift ? "X" : "x"; case Keys.Y: return shift ? "Y" : "y"; case Keys.Z: return shift ? "Z" : "z"; case Keys.D1: return shift ? "!" : "1"; case Keys.D2: return shift ? "@" : "2"; case Keys.D3: return shift ? "#" : "3"; case Keys.D4: return shift ? "$" : "4"; case Keys.D5: return shift ? "%" : "5"; case Keys.D6: return shift ? "^" : "6"; case Keys.D7: return shift ? "&" : "7"; case Keys.D8: return shift ? "*" : "8"; case Keys.D9: return shift ? "(" : "9"; case Keys.D0: return shift ? ")" : "0"; case Keys.OemMinus: return shift ? "_" : "-"; case Keys.OemPlus: return shift ? "+" : "="; case Keys.OemQuestion: return shift ? "?" : "/"; case Keys.OemComma: return shift ? "<" : ","; case Keys.OemPeriod: return shift ? ">" : "."; case Keys.OemSemicolon: return shift ? ":" : ";"; case Keys.OemTilde: return shift ? "\"" : "\'"; case Keys.OemQuotes: return shift ? "|" : "\\"; case Keys.OemOpenBrackets: return shift ? "{" : "["; case Keys.OemCloseBrackets: return shift ? "}" : "]"; case Keys.Tab: return " "; case Keys.Space: return " "; case Keys.Left: { inputPosition--; return ""; } case Keys.Right: { inputPosition++; return ""; } } return ""; }
public void updateKeys(InputState inputState) { Keys[] pressedKeys = inputState.getKeyboardState().GetPressedKeys(); foreach (Keys key in lastPressedKeys) { if (!pressedKeys.Contains(key)) OnKeyUp(key); } foreach (Keys key in pressedKeys) { if (!lastPressedKeys.Contains(key)) OnKeyDown(inputState, key); } lastPressedKeys = pressedKeys; }
private bool mouseInCraftingArea(InputState inputState) { int xx = x + (-2 * gridSize) + inventoryMove - (gridSize * 2); int yy = y + craftingAreaY-22; return new Rectangle(xx, yy, gridSize * 5, gridSize * 4).Contains(inputState.mouseX(), inputState.mouseY()); }
public override InputState update(Game game, InputState inputState) { mouseInCrafting = mouseInCraftingArea(inputState); if(game.currentWorld.player==null){ return inputState; } #region Scrolling and Fading int xx = 0; int yy = 0; int scrollDif = inputState.getScrollWheelValue() - lastScrollValue; if (scrollDif != 0) { if (mouseInCrafting && inventoryFadingIn) { if (scrollDif > 0) { scrollDif -= 1; craftingScroll -= 1; } if (scrollDif < 0) { scrollDif += 1; craftingScroll += 1; } while (craftingScroll < 0) { craftingScroll++; } while (craftingScroll >= valaidRecipes.Count()) { craftingScroll--; } }else{ if (scrollDif > 0) { scrollDif -= 1; selectedSlot -= 1; } if (scrollDif < 0) { scrollDif += 1; selectedSlot += 1; } while (selectedSlot >= 10) { selectedSlot -= 10; } while (selectedSlot < 0) { selectedSlot += 10; } hotBarFadingIn = true; } } if (hotBarFadingIn) { if (hotBarFade < 2f) { hotBarFade += .05f; } else { hotBarFadingIn = false; } } else { if (hotBarFade > .2f) { hotBarFade -= .01f; } } if(inputState.pressed(Game.KEY_INV)){ inventoryFadingIn = !inventoryFadingIn; } if (inventoryFadingIn) { if (inventoryFade < 1f) { inventoryFade += .05f; } } else { if (inventoryFade > 0f) { inventoryFade -= .05f; } else { dropItem(game, cursorItem); cursorItem = new ItemStack(Item.itemEmpty); } } inventoryMove = (int)(inventoryFade * ((gridSize * 3) + gridSize + 2)); if (hotBarFade<0) { hotBarFade = 0; } if (inventoryFade < 0) { inventoryFade = 0; } float f = Math.Max(hotBarFade, inventoryFade); hotBarColor = new Color(f, f, f, f); inventoryColor = new Color(inventoryFade, inventoryFade, inventoryFade, inventoryFade); lastScrollValue = inputState.getScrollWheelValue(); if(inputState.pressed(Game.KEY_D1)){ selectedSlot = 0; } else if (inputState.pressed(Game.KEY_D2)) { selectedSlot = 1; } else if (inputState.pressed(Game.KEY_D3)) { selectedSlot = 2; } else if (inputState.pressed(Game.KEY_D4)) { selectedSlot = 3; } else if (inputState.pressed(Game.KEY_D5)) { selectedSlot = 4; } else if (inputState.pressed(Game.KEY_D6)) { selectedSlot = 5; } else if (inputState.pressed(Game.KEY_D7)) { selectedSlot = 6; } else if (inputState.pressed(Game.KEY_D8)) { selectedSlot = 7; } else if (inputState.pressed(Game.KEY_D9)) { selectedSlot = 8; } else if (inputState.pressed(Game.KEY_D0)) { selectedSlot = 9; } #endregion mouseItemSlot = -1; mouseCraftSlot = -1; int itemIndex = 0; for (int i = 0; i < 10; i++) { xx = x + (selectedSlot == i ? 4 : 0) + inventoryMove + 4; yy = y + (i * gridSize); if (new Rectangle(xx, yy, gridSize, gridSize).Contains(inputState.mouseX(), inputState.mouseY())) { mouseItemSlot = itemIndex; } itemIndex++; } if (inventoryFade > 0) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 4; j++) { xx = x + (j * gridSize) + inventoryMove - (gridSize * 4); yy = y + (i * gridSize); if (new Rectangle(xx, yy, gridSize, gridSize).Contains(inputState.mouseX(), inputState.mouseY())) { mouseItemSlot = itemIndex; } itemIndex++; } } for (int i = -2; i <= 2; i++) { int j = i + craftingScroll; if (j >= 0 && j < valaidRecipes.Count()) { xx = x + (i * gridSize) + inventoryMove - (gridSize * 2); yy = y + craftingAreaY; if (new Rectangle(xx, yy, gridSize, gridSize).Contains(inputState.mouseX(), inputState.mouseY())) { mouseCraftSlot = j; } } } } bool isCursor = !cursorItem.isEmpty(); ItemStack currentItem = isCursor ? cursorItem : items[selectedSlot]; if (game.currentWorld != null) { mouseTileX = (inputState.mouseX() + game.currentWorld.viewOffset.X) / World.tileSizeInPixels; mouseTileY = (inputState.mouseY() + game.currentWorld.viewOffset.Y) / World.tileSizeInPixels; mouseTileDistanceFromPlayer = (int)Vector2.Distance(new Vector2(mouseTileX * World.tileSizeInPixels, mouseTileY * World.tileSizeInPixels), game.currentWorld.player.position + (game.currentWorld.player.size / 2)); } if (mouseItemSlot == -1 && mouseCraftSlot == -1) { if (inputState.downMouse(InputState.Left)) { setCurrentItem(currentItem.getItem().leftClick(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer), isCursor); } if (inputState.downMouse(InputState.Right)) { setCurrentItem(currentItem.getItem().rightClick(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer), isCursor); } if(inputState.pressed(Game.KEY_USE)){ ItemStack useItem = currentItem; Tile tileWall = game.currentWorld.getTileObject(mouseTileX, mouseTileY, World.TileDepth.wall); Tile tileTile = game.currentWorld.getTileObject(mouseTileX, mouseTileY, World.TileDepth.tile); if (tileWall.index != Tile.TileAir.index || tileTile.index != Tile.TileAir.index) { useItem = (tileTile.index == Tile.TileAir.index ? tileWall : tileTile).use(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer, tileTile.index == Tile.TileAir.index?World.TileDepth.wall:World.TileDepth.tile); } if(useItem!=null){ setCurrentItem(useItem); } } } currentItem.getItem().updateAfterClick(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer); bool leftClick = false; bool rightClick = false; int speed1Time = 40; int speed2Time = 200; int speed3Time = 400; if (inputState.downMouse(InputState.Left)) { if (leftClickTimer == 0) { leftClick = true; } else if (leftClickTimer > speed1Time) { if (leftClickTimer > speed3Time) { leftClick = true; } else if (leftClickTimer > speed2Time) { if (leftClickTimer % 2 == 0) { leftClick = true; } } else if (leftClickTimer % 10 == 0) { leftClick = true; } } leftClickTimer++; } else { leftClickTimer = 0; } if (inputState.downMouse(InputState.Right)) { if (rightClickTimer == 0) { rightClick = true; } else if (rightClickTimer > speed1Time) { if (rightClickTimer > speed3Time) { rightClick = true; } else if (rightClickTimer > speed2Time) { if (rightClickTimer % 2 == 0) { rightClick = true; } } else if (rightClickTimer % 10 == 0) { rightClick = true; } } rightClickTimer++; } else { rightClickTimer = 0; } if(inventoryFade == 0){ if (mouseItemSlot >= 0 && mouseItemSlot < 10) { hotBarFadingIn = true; if (leftClick) { selectedSlot = mouseItemSlot; } } }else if (!(mouseItemSlot == -1 && mouseCraftSlot == -1)) { if (mouseItemSlot == -1 && mouseCraftSlot == craftingScroll) { if (leftClick) { if (inputState.getKeyboardState().IsKeyDown(Game.KEY_INV_MOVE_ITEM_TO_OTHER)) { removeItemStacks(0, 49, valaidRecipes[mouseCraftSlot].ingredients); pickUp(valaidRecipes[mouseCraftSlot].result.clone()); } else { if (cursorItem.sameItem(valaidRecipes[mouseCraftSlot].result)) { if (cursorItem.getCount() + valaidRecipes[mouseCraftSlot].result.getCount() <= cursorItem.getItem().maxStack) { cursorItem.setCount(cursorItem.getCount() + valaidRecipes[mouseCraftSlot].result.getCount()); removeItemStacks(0, 49, valaidRecipes[mouseCraftSlot].ingredients); } } else if (cursorItem.isEmpty()) { cursorItem = valaidRecipes[mouseCraftSlot].result.clone(); removeItemStacks(0, 49, valaidRecipes[mouseCraftSlot].ingredients); } } updateValaidRecipes(); } } else if (mouseItemSlot == -1 && (mouseCraftSlot >= 0)) { if (leftClick) { craftingScroll = mouseCraftSlot; } } else { if (leftClick) { if (cursorItem.sameItem(items[mouseItemSlot])) { if (items[mouseItemSlot].getCount() < cursorItem.getItem().maxStack && cursorItem.getCount() > 0) { cursorItem.setCount(items[mouseItemSlot].setCount(items[mouseItemSlot].getCount() + cursorItem.getCount())); } else { ItemStack c = cursorItem.clone(); cursorItem = items[mouseItemSlot].clone(); items[mouseItemSlot] = c.clone(); } } else { ItemStack c = cursorItem.clone(); cursorItem = items[mouseItemSlot].clone(); items[mouseItemSlot] = c.clone(); } updateValaidRecipes(); } if (rightClick) { if (cursorItem.isEmpty() && items[mouseItemSlot].isEmpty()) { }else if (cursorItem.isEmpty()) { double c = items[mouseItemSlot].getCount() / 2.0; Game.updateMessage += c; cursorItem = items[mouseItemSlot].clone(); cursorItem.setCount((int)Math.Ceiling(c)); items[mouseItemSlot].setCount((int)Math.Floor(c)); } else if (items[mouseItemSlot].isEmpty()) { items[mouseItemSlot] = cursorItem.clone(); items[mouseItemSlot].setCount(1); cursorItem.setCount(cursorItem.getCount() - 1); } else { if (cursorItem.sameItem(items[mouseItemSlot])) { if (!items[mouseItemSlot].isMax()) { items[mouseItemSlot].setCount(items[mouseItemSlot].getCount()+1); cursorItem.setCount(cursorItem.getCount() - 1); } } else { ItemStack c = cursorItem.clone(); cursorItem = items[mouseItemSlot].clone(); items[mouseItemSlot] = c.clone(); } } updateValaidRecipes(); } } } if(inputState.pressed(Game.KEY_DROP)){ game.currentWorld.EntityAddingList.Add(new EntityItem(game.currentWorld.player.position, new ItemStack(currentItem.getItem(), 1, currentItem.getData()), 60)); currentItem.setCount(currentItem.getCount()-1); setCurrentItem(currentItem); } tick++; inputState.eatMouse(); inputState.eatKey(Game.KEY_INV); inputState.eatKey(Game.KEY_INV_MOVE_ITEM_TO_OTHER); inputState.eatKey(Game.KEY_DROP); inputState.eatKey(Game.KEY_D0); inputState.eatKey(Game.KEY_D1); inputState.eatKey(Game.KEY_D2); inputState.eatKey(Game.KEY_D3); inputState.eatKey(Game.KEY_D4); inputState.eatKey(Game.KEY_D5); inputState.eatKey(Game.KEY_D6); inputState.eatKey(Game.KEY_D7); inputState.eatKey(Game.KEY_D8); inputState.eatKey(Game.KEY_D9); return inputState; }
public abstract InputState update(Game game, InputState inputState);