Class Canvas

java.lang.Object
de.tum.cit.ase.editor.drawing.Canvas

public class Canvas extends Object
Represents a canvas that allows drawing and manipulation of a virtual grid.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Canvas(EditorCanvas editorCanvas)
    Represents a canvas for an editor.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.badlogic.gdx.math.GridPoint2
    calculateGridPoint(float x, float y, boolean clampToGrid)
    Converts screen Coordinates to the Position relative to the grid
    com.badlogic.gdx.math.GridPoint2
    calculateGridPoint(com.badlogic.gdx.math.Vector2 position, boolean clampToGrid)
    Converts screen Coordinates to the Position relative to the grid
    protected void
    changeGrid(TileTypes[][] tileTypes)
    Changes the grid of the canvas to the given TileTypes array.
    com.badlogic.gdx.math.GridPoint2
    convertWorldPositionToGrid(com.badlogic.gdx.math.Vector2 position, boolean clampToGrid)
    Converts WORLD Coordinates to the Position relative to the grid
    void
    createNewGrid(int width, int height)
    Creates a new grid with the specified width and height.
    void
    Creates a new grid with the given TileTypes array.
    void
    draw(com.badlogic.gdx.graphics.glutils.ShapeRenderer shapeRenderer)
    Draws the canvas using the specified ShapeRenderer.
    void
    Ends a new grid epoch by either popping the top grid from pastVGrids stack if it is the same as the current virtual grid, or clearing the futureVGrids list.
    com.badlogic.gdx.math.Vector2
    Returns the starting point of the grid.
    float
     
    com.badlogic.gdx.math.GridPoint2
    getMouseGridPosition(boolean alwaysGrid)
    Retrieves the grid position of the mouse.
    float
     
    com.badlogic.gdx.math.Vector2
    Retrieves the unprotected mouse position on the canvas.
    float
     
    void
    Redoes the most recent action on the canvas.
    void
    setGridTile(int x, int y, TileTypes tileType)
     
    void
    Starts a new grid epoch by pushing the current virtual grid onto the pastVGrids stack.
    void
    Undoes the most recent action on the canvas.
    void
    update(float dt)
    Updates the canvas.

    Methods inherited from class java.lang.Object

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

    • virtualGrid

      public TileTypes[][] virtualGrid
  • Constructor Details

    • Canvas

      public Canvas(EditorCanvas editorCanvas)
      Represents a canvas for an editor.
  • Method Details

    • update

      public void update(float dt)
      Updates the canvas.
      Parameters:
      dt - the time difference between the current frame and the previous frame
    • changeGrid

      protected void changeGrid(TileTypes[][] tileTypes)
      Changes the grid of the canvas to the given TileTypes array. Manual version of startNewGridEpoch() The current virtual grid is pushed onto the pastVGrids stack, and the futureVGrids list is cleared.
      Parameters:
      tileTypes - the new TileTypes array representing the grid
      See Also:
    • undo

      public void undo()
      Undoes the most recent action on the canvas. If there are actions in the pastVGrids stack, the method will: 1. Push a clone of the current virtualGrid onto the futureVGrids stack. 2. Assign the top grid in the pastVGrids stack as the new virtualGrid. 3. Set the saved flag in the editorCanvas to false.
    • redo

      public void redo()
      Redoes the most recent action on the canvas. If there are actions in the futureVGrids list, the method will: 1. Push a clone of the current virtualGrid onto the pastVGrids stack. 2. Assign the top grid in the futureVGrids list as the new virtualGrid. 3. Set the saved flag in the editorCanvas to false.
    • createNewGrid

      public void createNewGrid(int width, int height)
      Creates a new grid with the specified width and height.
      Parameters:
      width - the width of the grid
      height - the height of the grid
    • createNewGrid

      public void createNewGrid(TileTypes[][] grid)
      Creates a new grid with the given TileTypes array.
      Parameters:
      grid - the TileTypes array representing the new grid
    • draw

      public void draw(com.badlogic.gdx.graphics.glutils.ShapeRenderer shapeRenderer)
      Draws the canvas using the specified ShapeRenderer. This method renders the grid and the selected tool on the canvas.
      Parameters:
      shapeRenderer - the ShapeRenderer used to draw the canvas
    • calculateGridPoint

      public com.badlogic.gdx.math.GridPoint2 calculateGridPoint(float x, float y, boolean clampToGrid)
      Converts screen Coordinates to the Position relative to the grid
      Parameters:
      clampToGrid - if true value will be clamped to grid else value can be null
      Returns:
      Grid Point
    • calculateGridPoint

      public com.badlogic.gdx.math.GridPoint2 calculateGridPoint(com.badlogic.gdx.math.Vector2 position, boolean clampToGrid)
      Converts screen Coordinates to the Position relative to the grid
      Parameters:
      clampToGrid - if true value will be clamped to grid else value can be null
      Returns:
      Grid Point
    • convertWorldPositionToGrid

      public com.badlogic.gdx.math.GridPoint2 convertWorldPositionToGrid(com.badlogic.gdx.math.Vector2 position, boolean clampToGrid)
      Converts WORLD Coordinates to the Position relative to the grid
      Parameters:
      clampToGrid - if true value will be clamped to grid else value can be null
      Returns:
      Grid Point
    • getMouseGridPosition

      public com.badlogic.gdx.math.GridPoint2 getMouseGridPosition(boolean alwaysGrid)
      Retrieves the grid position of the mouse.
      Parameters:
      alwaysGrid - specifies whether the mouse position should always be restricted to the grid
      Returns:
      the grid position of the mouse as a GridPoint2 object, or null if the mouse position is outside the grid
    • getUnprotectedMousePosition

      public com.badlogic.gdx.math.Vector2 getUnprotectedMousePosition()
      Retrieves the unprotected mouse position on the canvas. This method returns the mouse position on the canvas without any restrictions or clamping to the grid.
      Returns:
      the mouse position as a Vector2 object on the canvas
    • getGridStartPoint

      public com.badlogic.gdx.math.Vector2 getGridStartPoint()
      Returns the starting point of the grid.
      Returns:
      the starting point of the grid as a Vector2 object
    • startNewGridEpoch

      public void startNewGridEpoch()
      Starts a new grid epoch by pushing the current virtual grid onto the pastVGrids stack. Automatic version When operation is finished call endNewGridEpoch() Alternative: changeGrid(TileTypes[][])
      See Also:
    • endNewGridEpoch

      public void endNewGridEpoch()
      Ends a new grid epoch by either popping the top grid from pastVGrids stack if it is the same as the current virtual grid, or clearing the futureVGrids list.
      See Also:
    • getHeight

      public float getHeight()
    • getWidth

      public float getWidth()
    • setGridTile

      public void setGridTile(int x, int y, TileTypes tileType)
    • getTileSize

      public float getTileSize()