Source code for simvx.editor.testing.hosting

"""The editor's side of hosting, reduced to what a play-mode test needs.

A running game is reached through a ``SceneHost`` in the tree around it, never
by publishing into the game directly. This builds that tree: one host over the
game's whole screen at the origin, so a position in it is the same position in
the game, holding focus so keys, text and pads reach the game too.
"""

from __future__ import annotations

from simvx.core import FocusMode, InputEvent, Node, SceneHost, SceneTree, Vec2
from simvx.core.input import InputRouter


[docs] class HostedGame: """A host tree with one focused SceneHost over the game play mode runs.""" def __init__(self, play_mode) -> None: self.play_mode = play_mode game = play_mode.game_tree width, height = game.screen_size if game is not None else (800, 600) self.tree = SceneTree(screen_size=(width, height)) root = Node(name="Editor") self.tree.set_root(root) self.host = SceneHost(game, name="GameHost", focus_mode=FocusMode.ALL) self.host.top_level = True self.host.position = Vec2(0.0, 0.0) self.host.size = Vec2(float(width), float(height)) root.add_child(self.host) self.tree.flush_layout() self.host.grab_focus() self.router = InputRouter(self.tree)
[docs] def forward(self, event: InputEvent) -> bool: """Publish *event* into the host tree, as the window would; True when a game is there to hear it. The host follows play mode's tree, as the editor's viewport does each frame, so after a stop it hosts nothing and forwards nothing. """ if self.host.guest is not self.play_mode.game_tree: self.host.guest = self.play_mode.game_tree if self.host.guest is None: return False self.router.publish(event) return True
[docs] def forward(play_mode, event: InputEvent) -> bool: """Forward *event* through the one host this play mode's game has in the test, built on first use.""" hosted = getattr(play_mode, "_test_hosted", None) if hosted is None: hosted = play_mode._test_hosted = HostedGame(play_mode) return hosted.forward(event)