factoriax.playground.editor.dialogs#

The dialogs of the level editor.

There are three dialogs. The new level dialog asks for the size and the name. The file dialog opens and saves a level. The machine inspector shows the contents of one machine, and lets the user change them.

Each dialog draws an RGBA overlay at the center of the screen, and reads its own keyboard events.

The file dialog lists the files of the levels/ directory. It is therefore free of tkinter, and free of the file picker of the operating system.

class factoriax.playground.editor.dialogs.FileDialog(mode: str = 'save', filename_text: str = '', files: list[str] = <factory>, selected_index: int = -1, scroll_offset: int = 0)#

Dialog that asks for a file name, to save a level or to open one.

The dialog holds a field for the name, and a list of the .json files of the levels/ directory. W and S move through the list. Enter accepts the name, and Escape leaves the dialog.

mode#

"save" or "load". It sets the title and the button text.

Type:

str

filename_text#

Name field, as the text that the user typed.

Type:

str

files#

Names of the files in the levels/ directory.

Type:

list[str]

selected_index#

Index of the file that the list shows as selected.

Type:

int

scroll_offset#

Number of files that the list moved past the top edge.

Type:

int

get_path() → Path | None#

Return the path of the file that the user selected.

The function adds the .json suffix to a name without one. If the levels/ directory is absent, the function makes it.

Returns:

Path in the levels/ directory. It is None when the name field is empty.

Return type:

Path or None

handle_event(event: Event) → str | None#

Process a pygame event.

Parameters:
  • event (pygame.event.Event :) – A pygame.KEYDOWN event.

  • event

Return type:

"ok" on Enter, "cancel" on Escape, or None.

render(base_w: int, base_h: int) → ndarray#

Render the dialog as an RGBA overlay.

Parameters:
  • base_w (int :) – Base window width.

  • base_h (int :) – Base window height.

  • base_w

  • base_h

Return type:

RGBA uint8 array of shape (base_h, base_w, 4).

class factoriax.playground.editor.dialogs.MachineInspectorDialog(tile_x: int, tile_y: int, machine_type: int, inv_items: ndarray, inv_counts: ndarray, focused_slot: int = 0, editing_slot: int = -1, editing_count: bool = False, count_text: str = '', picker_scroll: int = 0)#

Dialog that shows the contents of one machine, and edits them.

The dialog holds one row for each slot. A row gives the part that the slot has in the recipe, the item, and the count. A and D move through the rows. Enter opens a picker for the item, and then takes a count.

The dialog edits its own copy of the rows. When the dialog closes, factoriax.playground.editor.main writes that copy back. The dialog therefore does not reach into the editor state.

tile_x, tile_y

Tile of the machine.

machine_type#

Machine value of the machine.

Type:

int

inv_items, inv_counts

Item and count of each row.

focused_slot#

Index of the row that A and D move.

Type:

int

editing_slot#

Index of the row that the picker is open for. It is -1 while the picker is closed.

Type:

int

editing_count#

True while the dialog takes a count for editing_slot.

Type:

bool

count_text#

Count field, as the text that the user typed.

Type:

str

picker_scroll#

Number of items that the picker moved past its top edge.

Type:

int

handle_event(event: Event) → str | None#

Process a pygame event.

Parameters:
  • event (pygame.event.Event :) – A pygame event (KEYDOWN or MOUSEBUTTONDOWN).

  • event

Return type:

"close" to dismiss, or None to stay open.

property max_count: int#

Per-slot count cap for this machine type (its buffer capacity).

property num_slots: int#

Number of active slots for this machine type.

render(base_w: int, base_h: int) → ndarray#

Render the inspector dialog as an RGBA overlay.

Parameters:
  • base_w (int :) – Base window width.

  • base_h (int :) – Base window height.

  • base_w

  • base_h

Return type:

RGBA uint8 array of shape (base_h, base_w, 4).

class factoriax.playground.editor.dialogs.NewLevelDialog(width_text: str = '15', height_text: str = '15', name_text: str = 'untitled', active_field: int = 0)#

Dialog that asks for the size and the name of a new level.

handle_event() returns "ok" when the user accepts the dialog, and "cancel" when the user leaves it. It returns None while the dialog is open.

width_text, height_text

Size fields, as the text that the user typed.

name_text#

Name field, as the text that the user typed.

Type:

str

active_field#

Index of the field that has the keyboard focus.

Type:

int

get_values() → tuple[int, int, str]#

Return the size and the name that the user typed.

An empty field, or a field that holds no number, gives the default value of that field.

Returns:

Width, height, and name of the new level.

Return type:

tuple[int, int, str]

handle_event(event: Event) → str | None#

Read one event, and report whether the dialog closes.

Parameters:

event – Event to read. The dialog reads a pygame.KEYDOWN event only.

Returns:

"ok" for Enter, and "cancel" for Escape. It is None while the dialog stays open.

Return type:

str or None

render(base_w: int, base_h: int) → ndarray#

Render the dialog as an RGBA overlay.

Parameters:
  • base_w (int :) – Base window width.

  • base_h (int :) – Base window height.

  • base_w

  • base_h

Return type:

RGBA uint8 array of shape (base_h, base_w, 4).

class factoriax.playground.editor.dialogs.NumberInputDialog(label: str = 'Value:', text: str = '', min_value: int = 0, max_value: int = 100, default: int = 0)#

Dialog that asks for one number.

The dialog takes a digit and a backspace. Enter accepts the value, and Escape leaves the dialog.

label#

Text that the dialog shows in front of the field.

Type:

str

text#

Field, as the text that the user typed.

Type:

str

min_value, max_value

Lowest and highest value that get_value() returns.

default#

Value that get_value() returns for an empty field.

Type:

int

get_value() → int#

Return the number that the user typed.

An empty field gives default. A value outside the limits moves to the nearest limit.

Returns:

A value from min_value to max_value.

Return type:

int

handle_event(event: Event) → str | None#

Process a pygame event.

Parameters:
  • event (pygame.event.Event :) – A pygame.KEYDOWN event.

  • event

Return type:

"ok" on Enter, "cancel" on Escape, or None.

render(base_w: int, base_h: int) → ndarray#

Render the dialog as an RGBA overlay.

Parameters:
  • base_w (int :) – Base window width.

  • base_h (int :) – Base window height.

  • base_w

  • base_h

Return type:

RGBA uint8 array of shape (base_h, base_w, 4).

factoriax.playground.editor.dialogs.render_help_overlay(base_w: int, base_h: int) → ndarray#

Render a controls reference overlay.

Parameters:
  • base_w (int :) – Base window width.

  • base_h (int :) – Base window height.

  • base_w

  • base_h

Return type:

RGBA uint8 array of shape (base_h, base_w, 4).