Class SquidMouse

java.lang.Object
com.badlogic.gdx.InputAdapter
com.github.yellowstonegames.press.SquidMouse
All Implemented Interfaces:
com.badlogic.gdx.InputProcessor

public class SquidMouse extends com.badlogic.gdx.InputAdapter
This mouse processor allows for easy conversion to a grid based system. This class covers all aspects of mouse movement and clicking, passing those off to a given InputProcessor after converting to cell-based grid coordinates instead of pixel-based screen coordinates. It also passes off scroll events to the InputProcessor without additional changes.
This class is meant to be used as a wrapper to your own mouse InputProcessor; it simply converts the coordinates from screen-space x,y to grid-based x,y .
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected float
     
    protected float
     
    protected float
     
    protected float
     
    protected int
     
    protected int
     
    protected com.badlogic.gdx.InputProcessor
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    SquidMouse(float cellWidth, float cellHeight, float gridWidth, float gridHeight, int offsetX, int offsetY, com.badlogic.gdx.InputProcessor processor)
    Sets the size of the cell so that all mouse input can be evaluated as relative to the grid.
    SquidMouse(float cellWidth, float cellHeight, com.badlogic.gdx.InputProcessor processor)
    Sets the size of the cell so that all mouse input can be evaluated as relative to the grid.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
     
    float
     
    float
     
    float
     
    int
     
    int
     
    com.badlogic.gdx.InputProcessor
    Gets the InputProcessor this object uses to handle mouse input.
    boolean
    mouseMoved(int screenX, int screenY)
     
    boolean
    onGrid(int gridX, int gridY)
    Returns true if the provided screen coordinate is on the grid, ignoring the current offset.
    void
    reinitialize(float cellWidth, float cellHeight)
     
    void
    reinitialize(float cellWidth, float cellHeight, float gridWidth, float gridHeight, int offsetX, int offsetY)
     
    boolean
    scrolled(float amountX, float amountY)
     
    void
    setCellHeight(float cellHeight)
     
    void
    setCellWidth(float cellWidth)
     
    void
    setGridHeight(float gridHeight)
     
    void
    setGridWidth(float gridWidth)
     
    void
    setOffsetX(int offsetX)
     
    void
    setOffsetY(int offsetY)
     
    void
    setProcessor(com.badlogic.gdx.InputProcessor processor)
    Sets the InputProcessor this object uses to handle mouse input.
    boolean
    touchDown(int screenX, int screenY, int pointer, int button)
     
    boolean
    touchDragged(int screenX, int screenY, int pointer)
     
    boolean
    touchUp(int screenX, int screenY, int pointer, int button)
     
    int
    translateX(int screenX)
    Translates the given screen pixel coordinate to the underlying grid coordinate, taking into account the related offset.
    int
    translateY(int screenY)
    Translates the given screen pixel coordinate to the underlying grid coordinate, taking into account the related offset.

    Methods inherited from class com.badlogic.gdx.InputAdapter

    keyDown, keyTyped, keyUp, touchCancelled

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • cellWidth

      protected float cellWidth
    • cellHeight

      protected float cellHeight
    • gridWidth

      protected float gridWidth
    • gridHeight

      protected float gridHeight
    • offsetX

      protected int offsetX
    • offsetY

      protected int offsetY
    • processor

      protected com.badlogic.gdx.InputProcessor processor
  • Constructor Details

    • SquidMouse

      public SquidMouse(float cellWidth, float cellHeight, com.badlogic.gdx.InputProcessor processor)
      Sets the size of the cell so that all mouse input can be evaluated as relative to the grid. All input is passed to the provided InputProcessor once it has had its coordinates translated to grid coordinates. Offsets are initialized to 0 here, and the grid is assumed to take up the full game window.
      Parameters:
      cellWidth - the width of one cell in screen coordinates, usually pixels
      cellHeight - the height of one cell in screen coordinates, usually pixels
      processor - an InputProcessor that implements some of touchUp(), touchDown(), touchDragged(), mouseMoved(), or scrolled().
    • SquidMouse

      public SquidMouse(float cellWidth, float cellHeight, float gridWidth, float gridHeight, int offsetX, int offsetY, com.badlogic.gdx.InputProcessor processor)
      Sets the size of the cell so that all mouse input can be evaluated as relative to the grid. Offsets can be specified for x and y if the grid is displayed at a position other than the full screen. Specify the width and height in grid cells of the area to receive input, as well as the offsets from the bottom and left edges also measured in screen coordinates, which are often pixels but may be stretched or shrunk. All input is passed to the provided InputProcessor once it's had its coordinates translated to grid coordinates. If the input is not within the bounds of the grid as determined by gridWidth, gridHeight, offsetX, and offsetY, the input will be clamped.
      Parameters:
      cellWidth - the width of one cell in screen coordinates, usually pixels
      cellHeight - the height of one cell in screen coordinates, usually pixels
      gridWidth - in number of cells horizontally on the grid
      gridHeight - in number of cells vertically on the grid
      offsetX - the horizontal offset in screen coordinates, usually pixels
      offsetY - the vertical offset in screen coordinates, usually pixels
      processor - an InputProcessor that implements some of touchUp(), touchDown(), touchDragged(), mouseMoved(), or scrolled().
  • Method Details

    • getCellWidth

      public float getCellWidth()
    • getCellHeight

      public float getCellHeight()
    • getOffsetX

      public int getOffsetX()
    • getOffsetY

      public int getOffsetY()
    • getGridWidth

      public float getGridWidth()
    • getGridHeight

      public float getGridHeight()
    • setCellWidth

      public void setCellWidth(float cellWidth)
    • setCellHeight

      public void setCellHeight(float cellHeight)
    • setOffsetX

      public void setOffsetX(int offsetX)
    • setOffsetY

      public void setOffsetY(int offsetY)
    • setGridWidth

      public void setGridWidth(float gridWidth)
    • setGridHeight

      public void setGridHeight(float gridHeight)
    • reinitialize

      public void reinitialize(float cellWidth, float cellHeight)
    • reinitialize

      public void reinitialize(float cellWidth, float cellHeight, float gridWidth, float gridHeight, int offsetX, int offsetY)
    • getProcessor

      public com.badlogic.gdx.InputProcessor getProcessor()
      Gets the InputProcessor this object uses to handle mouse input.
      Returns:
      the wrapped InputProcessor.
    • setProcessor

      public void setProcessor(com.badlogic.gdx.InputProcessor processor)
      Sets the InputProcessor this object uses to handle mouse input.
      Parameters:
      processor - an InputProcessor that implements some of touchUp(), touchDown(), touchDragged(), mouseMoved(), or scrolled().
    • translateX

      public int translateX(int screenX)
      Translates the given screen pixel coordinate to the underlying grid coordinate, taking into account the related offset.
      Parameters:
      screenX - a screen x position, usually in pixels
      Returns:
      a grid x position
    • translateY

      public int translateY(int screenY)
      Translates the given screen pixel coordinate to the underlying grid coordinate, taking into account the related offset.
      Parameters:
      screenY - a screen y position, usually in pixels
      Returns:
      a grid y position
    • onGrid

      public boolean onGrid(int gridX, int gridY)
      Returns true if the provided screen coordinate is on the grid, ignoring the current offset.
      Parameters:
      gridX - x-coordinate measured in grid cells, not pixels
      gridY - y-coordinate measured in grid cells, not pixels
      Returns:
      true if the given gridX and gridY are on the grid, or false if they are outside it
    • touchDown

      public boolean touchDown(int screenX, int screenY, int pointer, int button)
      Specified by:
      touchDown in interface com.badlogic.gdx.InputProcessor
      Overrides:
      touchDown in class com.badlogic.gdx.InputAdapter
    • touchUp

      public boolean touchUp(int screenX, int screenY, int pointer, int button)
      Specified by:
      touchUp in interface com.badlogic.gdx.InputProcessor
      Overrides:
      touchUp in class com.badlogic.gdx.InputAdapter
    • touchDragged

      public boolean touchDragged(int screenX, int screenY, int pointer)
      Specified by:
      touchDragged in interface com.badlogic.gdx.InputProcessor
      Overrides:
      touchDragged in class com.badlogic.gdx.InputAdapter
    • mouseMoved

      public boolean mouseMoved(int screenX, int screenY)
      Specified by:
      mouseMoved in interface com.badlogic.gdx.InputProcessor
      Overrides:
      mouseMoved in class com.badlogic.gdx.InputAdapter
    • scrolled

      public boolean scrolled(float amountX, float amountY)
      Specified by:
      scrolled in interface com.badlogic.gdx.InputProcessor
      Overrides:
      scrolled in class com.badlogic.gdx.InputAdapter