Repository navigation
182 mod tool 4 drawing art
Question: the S7 test game's art is drawn by the user, in the toolset, and nothing generates it. Until now the mod tool could only bring art in as a finished PNG. What does it need to start art from nothing, draw it, animate it, make it team-coloured and tileable, and keep track of where every frame came from (issues #144, #12, #9, #8; tracking issue #143)?
The mod tool series: stage S7 builds the mod tool into a toolset that makes a whole game by hand. It began as the last stage of the deterministic simulation plan (whose stages S0 to S6 are documents 156 to 178), because netplay is tested on a game made with the tool, but the work is the mod tool's. S7 began with the audit (document 179) and the plan for the rest of S7 (document 180). Document 181 is S7.1. This document is S7.2, art.
Nothing here is traced from the original: the mod tool is the port's own. There is no decompiled code to quote, only the port's.
| Term | Meaning |
|---|---|
| mod tool | The engine's editor scene (editor/editor_main.tscn). It opens one pack layer (a mod, or a standalone game) as a ModWorkspace and writes only that layer. |
| canvas |
SpriteCanvas (editor/sprite_canvas.gd): the zoomed view of one sprite in the Assets tab. It now holds the drawing tools. |
| stroke | One press, drag and release of a tool (or one click of Fill). One stroke is one undo step. |
| working copy | The sprite's pixels that a stroke draws into. The pack itself changes only when the stroke ends. |
| frame sequence | Sprites whose ids end in a number, <name>.00, <name>.01 and so on. The engine animates by these numbered ids. |
| provenance | Where a frame came from, saved as its "source" in sprites/sprites.json: drawn, imported, or copied (with "from"). |
| team-paint mask | A second sprite, the drawing's size, whose opaque pixels mark the paint that changes with the side's colour (PORTING_PLAN 2.7.7). |
| engine tick | One step of the simulation, 1/62.5 s. Animation speeds in the engine's tables are in frames per tick. |
- Start art: New blank sprite (32 x 32, 16 x 16, 8 x 8, or any size, transparent), Duplicate as, and Import a sheet, which cuts a PNG into a frame sequence on a grid.
- Draw: the canvas has pencil, eraser, line, rectangle (outline or filled), fill, colour picker and rectangular select. A selection can be moved, copied, cut, pasted, flipped and turned by 90 degrees. Brushes are 1 to 16 px, and every tool has a key. Each stroke is one undo step that holds only the changed rectangle's pixels, before and after.
-
Palette: a strip of swatches under the canvas, from the pack's
sprites/palette.json. A mod saves its own list. Return Fire's original indexed palette is offered as swatches, as an aid and not a limit. - Animate: a sequence is one item in the tree and a strip of frames under the canvas. Add frame, Insert blank, Delete frame and moving a frame renumber the ids in one undo step. Play runs the frames at a chosen number of engine ticks per frame, and Onion skin shows the previous frame faintly.
- Team colour: make the mask from colours in the drawing. Pick colours, set a tolerance, see the mask on the canvas, then Make the mask. Paint the mask opens the mask with its drawing under it, for the pixel tools (#9).
- Tiles: Tile 3 x 3 repeats the sprite so seams show, or puts a neighbour sprite around it. Painting any copy paints the sprite.
- Where used: the inspector lists every pack table that names the sprite, or the sequence it belongs to, and its team pair or mask role (#8).
-
Provenance: every frame the tool writes says
drawn,importedorcopied(and from what). Validate warns about a frame with none. S7.8's copyright check (#141) will read this.
Mermaid source
flowchart TB
P["press / drag / release<br/>(mouse, keys or a check)"] --> W["SpriteCanvas<br/>draws into a working copy,<br/>grows the dirty rectangle"]
W -->|"release: painted(pos, region, tool)"| WS["ModWorkspace.paint<br/>before = pixels in the rectangle now<br/>after = the region"]
WS --> U["one UndoRedo action<br/>do: after, undo: before"]
U --> A["_apply_paint<br/>mod entry (+ provenance),<br/>pending frame"]
A --> R["Pack.replace_frame<br/>live in every view"]
A -->|Save| S["PackWriter<br/>PNG + sprites.json"]
Every frame the tool writes now records its provenance. Engine editor/workspace.gd:
## Where a mod's frame came from, kept as its sprites.json "source": "drawn" (made in this tool), "imported" (from a PNG
## the user picked) or "copied" (pixels taken from another sprite, named by "from" -- a base sprite painted over or
## duplicated). Anything else is a sprite from below the mod, which has no provenance of its own here.
const PROVENANCES := ["drawn", "imported", "copied"]
const SIZE_PRESETS := [Vector2i(32, 32), Vector2i(16, 16), Vector2i(8, 8)] ## New blank sprite's sizes: tile, soldier, small part
## The provenance of pixels taken from `from_id`: the same as its own when it is the mod's drawn or imported art,
## otherwise "copied" from it.
func derived_provenance(from_id: String) -> Dictionary:
var p := provenance(from_id)
if String(p.get("source", "")) in ["drawn", "imported"]:
return {"source": p["source"]}
if String(p.get("source", "")) == "copied":
return p
return {"source": "copied", "from": from_id}
## A new transparent sprite of `size`, provenance "drawn". Returns false (and does nothing) if the id is taken or bad.
func new_blank_sprite(sprite_id: String, size: Vector2i, action := "New blank sprite") -> bool:
if sprite_id == "" or pack.sprites.has(sprite_id) or size.x < 1 or size.y < 1 or size.x > Pack.PACKED_PAGE_SIZE or size.y > Pack.PACKED_PAGE_SIZE:
return false
var img := Image.create_empty(size.x, size.y, false, Image.FORMAT_RGBA8)
import_frame(sprite_id, img, action, {"source": "drawn"})
return trueTranslation: a blank sprite is a transparent image saved like an imported one, but marked drawn. A copy of the mod's own drawing stays drawn. A copy of anything else, such as Return Fire's art or a base sprite painted over, is marked copied and names its source. So a later check can tell the user's own art from anything that started as someone else's. "Open in image editor" and its live reload keep the frame's provenance: an outside editor is still the user drawing. Only Replace with PNG and Import a sheet mark a frame imported.
The plan (EDITOR_PLAN 3.1) had proposed original | imported | edited. Issue #144 asked for drawn and imported. copied with from is added because "edited" would not say whose pixels were edited.
The canvas never changes the pack. It draws into a working copy and, on release, hands the changed rectangle out. Engine editor/sprite_canvas.gd:
## Ends the stroke: the changed rectangle goes out through `painted` (the workspace makes it one undo step).
func _finish(action: String) -> void:
if _work == null:
return
var img := _work
var dirty := _dirty
_work = null
_dirty = Rect2i()
_end_stroke()
if dirty.has_area():
painted.emit(dirty.position, img.get_region(dirty), action)
queue_redraw()The workspace turns it into one undoable action that holds just that rectangle (engine editor/workspace.gd):
func paint(sprite_id: String, pos: Vector2i, region: Image, action := "Paint") -> bool:
var cur := pack.get_sprite_image(sprite_id)
if cur == null or region == null:
return false
if region.get_format() != Image.FORMAT_RGBA8:
region = region.duplicate() as Image
region.convert(Image.FORMAT_RGBA8)
var rect := Rect2i(pos, region.get_size()).intersection(Rect2i(Vector2i.ZERO, cur.get_size()))
if rect.size.x <= 0 or rect.size.y <= 0:
return false
var after := region.get_region(Rect2i(rect.position - pos, rect.size))
var before := cur.get_region(rect)
if after.get_data() == before.get_data():
return false
var old: Variant = mod_sprites.get(sprite_id)
var entry: Dictionary
if old is Dictionary and old.has("file"):
entry = (old as Dictionary).duplicate()
else:
entry = _frame_entry(sprite_id, cur, pack.get_sprite(sprite_id), derived_provenance(sprite_id))
if old is Dictionary: # a metadata-only override (a moved pivot) stays as it was
for k in old:
if not k in ["file", "w", "h", "source", "from"]:
entry[k] = old[k]
undo.create_action(action)
undo.add_do_method(_apply_paint.bind(sprite_id, rect.position, after, entry))
undo.add_undo_method(_apply_paint.bind(sprite_id, rect.position, before, (old as Dictionary).duplicate() if old is Dictionary else null))
undo.commit_action()
return trueTranslation: the rectangle is clipped to the sprite. Its pixels as they are now become the undo half, and the new pixels become the redo half, so a stroke on a 256 x 256 sprite stores only what it touched. Painting the same pixels again is no step at all. The first stroke on a base sprite gives the mod its own frame, marked copied from that sprite. Undoing that stroke removes the mod's entry again, so the base art shows unchanged. The undo entry is named after the tool ("Pencil", "Fill", "Move selection"), as the top bar's Undo shows it.
How the tools draw into the working copy:
| Tool (key) | What a stroke does |
|---|---|
| Pencil (B), Eraser (E) | stamps a square brush (1-16 px, [ ]) along every pixel between mouse positions (Bresenham), so a fast drag leaves no gaps. The eraser stamps transparency. |
| Line (L), Rectangle (U) | redrawn from the pixels as they were when the stroke began, on every drag, so only the last line or rectangle stays. Filled fills the rectangle. |
| Fill (G) | the 4-connected area of the clicked pixel's exact colour |
| Pick colour (I) | takes the pixel's colour into the colour button. This is no edit. |
| Select (M) | drag a rectangle. Dragging inside it lifts the pixels (a transparent hole is left) and moves them. Copy, cut, paste (Ctrl+C / X / V), flip (X, Y), turn (R) and Delete work on it. Enter or another tool puts it down. Its transparent pixels leave what is under them. |
| Pivot (P), View (V) | as before: set the pivot, or only pan and zoom |
The engine animates by numbered ids, so a sequence is simply the sprites <name>.NN. Every edit that changes the order rewrites the sequence in one step. Engine editor/workspace.gd:
## Makes `frames` the sequence `name`, numbered from 0: a frame already at its id is left alone, a moved one gets a copy
## at its new id, and ids past the end are removed (a frame from below is deleted by the mod). One undo step.
func _set_sequence(action: String, name: String, frames: Array) -> void:
var old := sequence(name)
var digits := 2
for id in old:
digits = maxi(digits, int(sequence_parts(id)[2]))
var states := {}
for i in frames.size():
var id := "%s.%0*d" % [name, digits, i]
var f: Dictionary = frames[i]
if f["id"] == id:
continue
var img: Image = f["image"]
var entry := _frame_entry(id, img, f["meta"], f["provenance"])
states[id] = {"mod": entry, "frame": img, "image": img, "meta": _meta_of(entry)}
for i in range(frames.size(), old.size()):
var id := "%s.%0*d" % [name, digits, i]
var from_below := not _lower_sprite(id).is_empty()
states[id] = {"mod": null, "removed": from_below, "frame": null, "image": null, "meta": {}}
if not states.is_empty():
_do_sprites(action, states)Translation: insert, delete, move and Import a sheet all build the new list of frames and hand it here. Frames that stay at their number are not touched, so moving the last frame of a base sequence does not copy the others into the mod. When a sequence gets shorter, the id past the end must go. If that id comes from a layer below, the mod deletes it with a null entry in its sprites.json, which Pack has always read as "remove this id". The workspace could not write one before.
_do_sprites is the old one-sprite undo action made general: it changes several sprites in one step, and it can carry team-table changes in the same step (step 5 uses that).
Playback builds a SpriteFrames of the sequence's frames for an AnimatedSprite2D, at 62.5 / ticks per frame frames a second. The default is 5 ticks, the 0x3333 frames a tick (0.2) that the soldiers' and the edge guard's tables use. Onion skin draws the previous frame over the canvas in a faint blue.
sprites/palette.json is a new table for the engine, but not a new file. Return Fire's importer already writes the original's indexed palette there as "rgb", so the engine reads both forms. Engine game/pack.gd:
## A sprites/palette.json document's colours as "#rrggbbaa" strings, or null when it has none: its "colours" list, else
## its "rgb" list (an indexed palette, [r, g, b] 0-255 per index), offered once per distinct colour.
static func palette_of(doc: Dictionary) -> Variant:
if doc.get("colours") is Array:
return (doc["colours"] as Array).duplicate()
if not (doc.get("rgb") is Array):
return null
var out: Array = []
for c in doc["rgb"]:
if c is Array and c.size() >= 3:
var html := "#" + Color8(int(c[0]), int(c[1]), int(c[2])).to_html(true)
if not html in out:
out.append(html)
return outTranslation: the top layer that has a palette gives the whole list. A mod that changes the palette saves its own list as "colours" (with alpha), and starts from the one below. An empty mod list hands the palette back to the layers below. Swatches: click to draw with one, right-click to remove it, + adds the drawing colour, Add from sprite adds every colour a sprite uses (up to 256). A standalone game, such as the S7 test game, has no palette until the user makes one.
## A mask the size of `sprite_id`: white where a pixel is within `tolerance` (0-1, per channel) of one of `colours`,
## transparent elsewhere.
func mask_from_colours(sprite_id: String, colours: Array, tolerance := 0.0) -> Image:
var img := pack.get_sprite_image(sprite_id)
if img == null:
return null
var m := Image.create_empty(img.get_width(), img.get_height(), false, Image.FORMAT_RGBA8)
for y in img.get_height():
for x in img.get_width():
var p := img.get_pixel(x, y)
if p.a <= 0.0:
continue
for c in colours:
var k: Color = c
if absf(p.r - k.r) <= tolerance + 0.001 and absf(p.g - k.g) <= tolerance + 0.001 and absf(p.b - k.b) <= tolerance + 0.001:
m.set_pixel(x, y, Color.WHITE)
break
return mTranslation: the user picks one or more colours from the drawing with the picker and adds them to the range. While the range is not empty, the canvas shows the mask it would make, in magenta, as the tolerance changes. Make the mask saves it as <id>_mask (or over the drawing's existing mask) and points the drawing at it, in one undo step (make_mask, the sprite and the team_sets.json change together). The generated colours are remade from it at once, as for an imported mask. Paint the mask selects the mask sprite. The canvas always draws a mask's drawing faintly under it, so the pixel tools can fix the edges.
Tile 3 x 3 draws the sprite nine times, and the canvas maps any copy back onto the sprite (to_pixel wraps the coordinates), so the user can paint across a seam. A neighbour sprite id draws that sprite in the eight cells around instead, so a road tile can be seen next to grass. This is a preview only; no table is changed.
Where used searches every table the pack loaded, not a list of known fields, so it covers a game's own tables too (engine editor/workspace.gd):
func where_used(sprite_id: String) -> Array[String]:
var names := [sprite_id]
var seq := sequence_parts(sprite_id)
if not seq.is_empty():
names.append(String(seq[0]))
var out: Array[String] = []
for prop in pack.get_property_list():
var n := String(prop["name"])
if n.begins_with("_") or n in _NOT_USES or not (int(prop["usage"]) & PROPERTY_USAGE_SCRIPT_VARIABLE):
continue
var v: Variant = pack.get(n)
if v is Dictionary or v is Array:
_find_uses(v, n, names, out)
var tan: String = pack._team_set_of.get(sprite_id, "")
if pack.team_sets.has(sprite_id):
out.append("team pair: side 1 of %s" % pack.team_sets[sprite_id])
elif tan != "":
out.append("team pair: side 2 of %s" % tan)
for id in pack.team_masks:
if String(pack.team_masks[id].get("mask", "")) == sprite_id:
out.append("team mask of %s" % id)
out.sort()
return outTranslation: every table of the pack (the vehicle definitions, the tileset, decorations, effects, the HUD panels and so on) is walked, and every string equal to the sprite's id, or to the name of the sequence it is a frame of, is listed with its path, for example vehicle_defs > fx.tank > render > parts > 1 > sprite_ids. vehicle_types (a view built from the definitions) and the sprite and team tables themselves are skipped. What it cannot find: sprites the engine's code draws by a fixed id. There should be none left after S7.1. The inspector shows the first 30 paths.
Engine, tests/mod_art_check.gd (68 checks, the workspace API on the synthetic test pack):
- a blank sprite (size, transparency,
drawn, refused ids and sizes, undo); a duplicate of drawn art stays drawn, and a duplicate of a base sprite iscopiedfrom it; - painting: the region only, clipped at the edge, one step per stroke named after the tool, no step for unchanged pixels, undo and redo; painting a base sprite gives the mod a
copiedframe, and undo hands the base sprite back; - sequences: a sheet of 4 x 2 cells with three drawn cells makes
.00to.02in reading order; insert, move and delete renumber, and each undoes in one step; a base sequence shortened by the mod deletes the last id with anullentry, which survives a save and reload; - the palette: the base's indexed
rgbpalette offered once per colour, the mod's list on top, add from image, removal, undo, and an emptied list handing the base's back; - a mask from a colour range (exact, and with a tolerance), made and used in one undo step, the masked pixels recolouring in blue, and erasing a mask pixel taking it out of the team paint;
- where used: the turret in each vehicle's render parts, the tile in the tileset, the team pair's second half, a mask's owner, a new sprite used nowhere;
- after a save: provenance, PNG pixels and the palette on disk, and all of it read back by a fresh workspace; Validate reports no error, and reports a palette entry that is not a colour.
Engine, tests/mod_art_ui_check.gd (43 checks, the Assets panel in the editor scene): every tool driven on the canvas in pixel coordinates (pencil with a 1 and a 3 px brush, eraser, a line that keeps only its last position, outline and filled rectangles, fill, the picker into the colour button, select, move, copy and paste, rotate, flip), each stroke one undo step; the keys (B, M, [ ], Escape); palette swatches; the sequence's tree item and strip, onion skin, Add frame, Move, Delete and playback at the engine's tick rate; the tiling preview's coordinate wrap; the inspector's provenance and where-used lines; a mask from a colour range with its canvas preview; and a blank sprite exported for an outside image editor.
Suites: engine 64 checks pass, openfire 78 pass, no SCRIPT ERROR in any log.
Open the mod tool from the main checkout (godot --path . res://addons/openfire_engine/editor/editor_main.tscn, or the tool's own launcher) on a new mod (New mod...) or a new standalone game (New game...).
-
New blank sprite...: id
<your mod>.test.tile.00, preset 32 x 32. Pick Pencil (B), choose a colour, draw. Undo (Ctrl+Z) should take back one stroke at a time. - Try Line, Rectangle (and Filled), Fill and Pick colour. Select (M) a part, drag it, press X to flip it and R to turn it, then Enter.
- Click + under the canvas to keep the colour in the palette. On Return Fire's base, the original palette's colours are already there.
- Tick Tile 3 x 3 and paint across the edge of a copy to see the seam close.
- Add frame twice under the canvas, change each frame a little, tick Onion skin, then Play.
- In the inspector, pick two colours with the picker, Add colour each, raise the tolerance, and watch the magenta preview. Make the mask, then look at the colour strip below it. Paint the mask to fix it by hand.
- Select a Return Fire sprite, such as a Tank part, and read Used by. Paint one pixel on it, and the inspector should say "Copied from ..." and offer Revert to original.
-
Save, then open the mod's
sprites/sprites.json: the frames you drew say"source": "drawn".
What the Assets tab looks like with a three-frame sequence selected (flat test colours, made for this screenshot only, not saved anywhere):

- Document 180 had the palette as the
ColorPicker's own presets. Outside the Godot editor those presets are not saved anywhere, so the palette is its own strip of swatches saved in the pack, beside a plain colour button. - Document 180 called a mask "a sprite of
kindmask". The engine has no such kind and needs none: a mask is an ordinary sprite thatteam_sets.jsonnames as a mask, as it already was. - Provenance gained
copied(step 1).
- The "original palette only" mode the plan mentioned (drawing restricted to the original's colours) is not built. Nothing needs it for S7.
- Layers and timelines stay the job of outside editors (Aseprite, Pixelorama), as #12 said. Open in image editor and Import a sheet serve them.
- Flash and team variants made by a helper (EDITOR_PLAN 3.1) are not built. A team-paint mask covers team colours, and the hit flash is drawn by the engine.
- Engine
editor/sprite_canvas.gd: the tools (press,drag,release,line_points,_flood,lift,flip_h,rotate_90,paste,commit), tiling, onion skin,_shortcut. - Engine
editor/workspace.gd:PROVENANCES,new_blank_sprite,duplicate_sprite,paint,sequence,insert_frame,delete_frame,move_frame,import_sheet,_set_sequence,_do_sprites,set_palette,palette_from_image,mask_from_colours,make_mask,where_used. - Engine
editor/assets_panel.gd: the toolbars, palette strip, sequence strip and player, the New blank sprite and sheet dialogs, the inspector's provenance, Used by and mask-from-colours sections. - Engine
game/pack.gd:palette,palette_of. Engineeditor/pack_writer.gd:sprites/palette.json. Engineeditor/validator.gd: provenance and palette rules. - Engine
tests/mod_art_check.gd,tests/mod_art_ui_check.gd. - openfire
docs/EDITOR_PLAN.mdsection 0 ("Drawing art"), 3.1 and E6.
Next: S7.3, maps, after the user's try-out.
Return Fire, rebuilt
Elsewhere