factoriax.playground.editor.canvas#

The camera and the tile drawing of the editor canvas.

The canvas draws the part of the map that the camera shows, and no more.

The blocks and the machines come from the sprite atlas that the JAX renderer reads. The canvas is therefore the same image as the play view, down to new art in factoriax/assets/atlas.png. block_textures_rgba and machine_icon_rgba in factoriax.engine.renderer give the RGBA arrays at the tile size that the camera holds.

class factoriax.playground.editor.canvas.Viewport(camera_x: float = 0.0, camera_y: float = 0.0, tile_size: int = 32, canvas_w: int = 400, canvas_h: int = 400)#

Position and zoom of the editor camera.

camera_x, camera_y

Tile at the top left corner of the canvas. The value is a float, so the camera can stop between two tiles.

tile_size#

Width and height of one tile in pixels. It is a member of TILE_SIZES.

Type:

int

canvas_w, canvas_h

Width and height of the canvas in pixels.

factoriax.playground.editor.canvas.clamp_camera(vp: Viewport, map_w: int, map_h: int) → None#

Move the camera back until it shows the map only.

If the map is smaller than the canvas, the camera stops at the top left corner of the map.

Parameters:
  • vp – Camera. The function writes to it.

  • map_w – Map width in tiles.

  • map_h – Map height in tiles.

factoriax.playground.editor.canvas.pan(vp: Viewport, dx: float, dy: float, map_w: int, map_h: int) → None#

Move the camera across the map.

The function then calls clamp_camera(), so the camera shows the map only.

Parameters:
  • vp – Camera. The function writes to it.

  • dx – Distance to move in tiles. A positive value moves right.

  • dy – Distance to move in tiles. A positive value moves down.

  • map_w – Map width in tiles.

  • map_h – Map height in tiles.

factoriax.playground.editor.canvas.render_canvas(state: object, vp: Viewport, cursor_tile: tuple[int, int] | None, selection_rect: tuple[int, int, int, int] | None = None, show_resources: bool = False) → ndarray#

Draw the part of the map that the camera shows.

The function draws the blocks first, and then the machines, the grid lines, the players, and the highlights. A tile with contents in its machine gets a dot at the top right corner.

Parameters:
  • state – EditorState to read.

  • vp – Camera to read.

  • cursor_tile – Column and row of the tile under the mouse, or None when the mouse is off the canvas.

  • selection_rect – Corners of the fill rectangle, as (x0, y0, x1, y1) in tiles. It is None when the user selects no rectangle.

  • show_resources – True draws the ore amount on each tile that holds ore.

Returns:

RGBA image of shape (canvas_h, canvas_w, 4), uint8.

Return type:

numpy.ndarray

factoriax.playground.editor.canvas.screen_to_tile(vp: Viewport, sx: int, sy: int) → tuple[int, int]#

Return the tile at one pixel of the canvas.

Parameters:
  • vp – Camera to read.

  • sx – Pixel offset from the left edge of the canvas.

  • sy – Pixel offset from the top edge of the canvas.

Returns:

Column and row of the tile. The tile can be outside the map.

Return type:

tuple[int, int]

factoriax.playground.editor.canvas.tile_to_screen(vp: Viewport, tx: int, ty: int) → tuple[int, int]#

Return the pixel at the top left corner of one tile.

Parameters:
  • vp – Camera to read.

  • tx – Tile column.

  • ty – Tile row.

Returns:

Pixel offset from the top left corner of the canvas. The pixel can be outside the canvas.

Return type:

tuple[int, int]

factoriax.playground.editor.canvas.zoom(vp: Viewport, direction: int, mouse_sx: int, mouse_sy: int, map_w: int, map_h: int) → None#

Change the tile size, and hold the tile under the mouse in place.

The function moves one step along TILE_SIZES. At the first or the last size, a step past the end has no effect.

Parameters:
  • vp – Camera. The function writes to it.

  • direction – +1 makes the tiles larger. -1 makes them smaller.

  • mouse_sx – Mouse offset from the left edge of the canvas.

  • mouse_sy – Mouse offset from the top edge of the canvas.

  • map_w – Map width in tiles.

  • map_h – Map height in tiles.