GridEditor

Prefab for pixel-style grid editing and ASCII art creation in-engine.

GridEditor

GridEditor(size=(32,32), palette=(' ', '#', '|', 'o'), canvas_color=color.white, edit_mode=True, **kwargs)

Located in ursina/prefabs/grid_editor.py

Overview

GridEditor is an interactive, in-game pixel/ASCII editor. It displays a 2D grid you can draw on, select regions, flood-fill, copy/paste, and undo. Great for level prototyping or quick texture tweaks without leaving your Ursina session.

Constructor Arguments

ArgumentTypeDefaultDescription
size(int, int)(32,32)Grid dimensions (width, height).
palettetuple[str]`(’ ’, ’#’, '', ‘o’)`
canvas_colorColorcolor.whiteBackground color of the drawing canvas.
edit_modeboolTrueIf False, editing shortcuts are disabled (read-only view).
**kwargsanyAny other Entity parameters (position, scale, parent, etc.).

Properties

PropertyDescription
.paletteCurrent drawing palette.
.edit_modeEditing enabled (True) or disabled (False).

Attributes

NameDescription
.w, .hWidth and height of the grid.
.canvasUnderlying quad that displays the grid.
.canvas_colliderInvisible box collider for mouse interaction.
.brush_sizeCurrent brush size in pixels.
.cursorVisual cursor that follows the mouse within the grid.
.outlineOutline rectangle for selection mode.
.selection_matrix2D array tracking selected cells.
.undo_stack, .undo_indexHistory for undo/redo operations.
.temp_paste_layerPreview layer when pasting clipboard data.
.hotreload_window_settings(Inherited from Entity) window parameters for editor UI.

Methods

MethodDescription
update()Per-frame updates (cursor tracking, render).
get_cursor_position()Return current grid cell under the mouse.
draw(x, y)Draw current palette character at grid cell (x,y).
input(key)Handle keyboard and mouse events for editing and shortcuts.
record_undo()Push current state onto the undo stack.
floodfill(matrix, x, y, first=True)Recursive flood-fill from (x,y).
copy()Copy selected region to clipboard.
enter_paste_mode()Begin paste mode with clipboard data.
exit_paste_mode(discard=False)End paste mode, optionally discard changes.
clear_selection()Clear any active selection.
render_selection()Redraw the selection outline.

Example

from ursina import *
from ursina.prefabs.grid_editor import GridEditor

app = Ursina(borderless=False)

# a 32×32 pixel editor with default palette
editor = GridEditor(size=(32,32), palette=(' ', '#', '*', '@'), scale=8)
editor.position = (-.5, .5, 0)

# enable orthographic view for crisp pixels
camera.orthographic = True
camera.fov = 20

EditorCamera(rotation_speed=0)

app.run()