mousefox.app.userframe
Home of UserFrame
.
1"""Home of `UserFrame`.""" 2 3from typing import Optional 4from loguru import logger 5import kvex as kx 6import kvex.kivy 7import pgnet 8from .lobbyframe import LobbyFrame 9from .adminframe import AdminFrame 10 11 12class UserFrame(kx.XAnchor): 13 """Widget for connected clients. 14 15 Enables interacting with the server lobby and game widget(s). 16 """ 17 18 _conpath = "client.user" 19 20 def __init__(self, client: pgnet.client, game_widget_class: kvex.kivy.Widget): 21 """Initialize the class with the game widget class.""" 22 super().__init__() 23 self._game_widget_class = game_widget_class 24 self._client = client 25 self._make_widgets() 26 self._client.on_game = self._on_game 27 self._on_game(client.game) 28 self.app.menu.set_callback("app", "leave_game", self._leave_game) 29 self.app.menu.set_callback("app", "lobby", self._show_lobby) 30 self.app.menu.set_callback("app", "game", self._show_game) 31 self.app.menu.set_callback("app", "admin_panel", self._show_admin) 32 self.app.menu.get_button("app", "lobby").disabled = True 33 self.app.menu.get_button("app", "game").disabled = True 34 self.app.menu.get_button("app", "admin_panel").disabled = True 35 self.app.controller.bind(f"{self._conpath}.leave_game", self._leave_game) 36 self.app.controller.bind(f"{self._conpath}.show_lobby", self._show_lobby) 37 self.app.controller.bind(f"{self._conpath}.show_game", self._show_game) 38 self.app.controller.bind(f"{self._conpath}.show_admin", self._show_admin) 39 self.app.controller.set_active_callback(self._conpath, self._show_lobby) 40 41 def update(self): 42 """Background refresh tasks.""" 43 menu_get = self.app.menu.get_button 44 current = self._sm.current 45 disable = not self.app.controller.is_active(self._conpath) 46 menu_get("app", "lobby").disabled = current == "lobby" or disable 47 menu_get("app", "game").disabled = current == "game" or disable 48 menu_get("app", "admin_panel").disabled = current == "admin" or disable 49 menu_get("app", "leave_game").disabled = not bool(self._client.game) 50 self.lobby_frame.update() 51 52 def _make_widgets(self): 53 self.lobby_frame = LobbyFrame(self._client) 54 self.admin_frame = AdminFrame(self._client) 55 game_placeholder = kx.XPlaceholder( 56 label_text="No game in progress.", 57 button_text="Return to lobby", 58 callback=self._show_lobby, 59 ) 60 self.game_frame = kx.XContainer(game_placeholder) 61 self._sm = kx.XScreenManager.from_widgets(dict( 62 lobby=self.lobby_frame, 63 game=self.game_frame, 64 admin=self.admin_frame, 65 )) 66 self._sm.transition.duration = 0.1 67 self.add_widget(self._sm) 68 69 def _switch_screen(self, name: str): 70 self._sm.transition.direction = self._sm.screen_direction(name) 71 self._sm.current = name 72 is_game = name == "game" 73 self.app.controller.active = f"{self._conpath}.{name}" 74 self.app.game_controller.active = "" if is_game else None 75 76 def _show_admin(self, *args): 77 self._switch_screen("admin") 78 79 def _show_lobby(self, *args): 80 self._switch_screen("lobby") 81 82 def _show_game(self, *args): 83 self._switch_screen("game") 84 85 def _make_game(self): 86 game_frame = self._game_widget_class(self._client) 87 self.game_frame.content = game_frame 88 self._switch_screen("game") 89 90 def _on_game(self, game: Optional[str]): 91 logger.info(f"New game: {game}") 92 if game: 93 self._make_game() 94 else: 95 self.game_frame.content = None 96 self._show_lobby() 97 98 def _leave_game(self, *args): 99 if not self._client: 100 return 101 if self._client.game: 102 self._client.leave_game(callback=self.app.feedback_response) 103 else: 104 self._show_lobby() 105 106 107class _DummyFocus(kx.XFocusBehavior, kx.XLabel): 108 pass
13class UserFrame(kx.XAnchor): 14 """Widget for connected clients. 15 16 Enables interacting with the server lobby and game widget(s). 17 """ 18 19 _conpath = "client.user" 20 21 def __init__(self, client: pgnet.client, game_widget_class: kvex.kivy.Widget): 22 """Initialize the class with the game widget class.""" 23 super().__init__() 24 self._game_widget_class = game_widget_class 25 self._client = client 26 self._make_widgets() 27 self._client.on_game = self._on_game 28 self._on_game(client.game) 29 self.app.menu.set_callback("app", "leave_game", self._leave_game) 30 self.app.menu.set_callback("app", "lobby", self._show_lobby) 31 self.app.menu.set_callback("app", "game", self._show_game) 32 self.app.menu.set_callback("app", "admin_panel", self._show_admin) 33 self.app.menu.get_button("app", "lobby").disabled = True 34 self.app.menu.get_button("app", "game").disabled = True 35 self.app.menu.get_button("app", "admin_panel").disabled = True 36 self.app.controller.bind(f"{self._conpath}.leave_game", self._leave_game) 37 self.app.controller.bind(f"{self._conpath}.show_lobby", self._show_lobby) 38 self.app.controller.bind(f"{self._conpath}.show_game", self._show_game) 39 self.app.controller.bind(f"{self._conpath}.show_admin", self._show_admin) 40 self.app.controller.set_active_callback(self._conpath, self._show_lobby) 41 42 def update(self): 43 """Background refresh tasks.""" 44 menu_get = self.app.menu.get_button 45 current = self._sm.current 46 disable = not self.app.controller.is_active(self._conpath) 47 menu_get("app", "lobby").disabled = current == "lobby" or disable 48 menu_get("app", "game").disabled = current == "game" or disable 49 menu_get("app", "admin_panel").disabled = current == "admin" or disable 50 menu_get("app", "leave_game").disabled = not bool(self._client.game) 51 self.lobby_frame.update() 52 53 def _make_widgets(self): 54 self.lobby_frame = LobbyFrame(self._client) 55 self.admin_frame = AdminFrame(self._client) 56 game_placeholder = kx.XPlaceholder( 57 label_text="No game in progress.", 58 button_text="Return to lobby", 59 callback=self._show_lobby, 60 ) 61 self.game_frame = kx.XContainer(game_placeholder) 62 self._sm = kx.XScreenManager.from_widgets(dict( 63 lobby=self.lobby_frame, 64 game=self.game_frame, 65 admin=self.admin_frame, 66 )) 67 self._sm.transition.duration = 0.1 68 self.add_widget(self._sm) 69 70 def _switch_screen(self, name: str): 71 self._sm.transition.direction = self._sm.screen_direction(name) 72 self._sm.current = name 73 is_game = name == "game" 74 self.app.controller.active = f"{self._conpath}.{name}" 75 self.app.game_controller.active = "" if is_game else None 76 77 def _show_admin(self, *args): 78 self._switch_screen("admin") 79 80 def _show_lobby(self, *args): 81 self._switch_screen("lobby") 82 83 def _show_game(self, *args): 84 self._switch_screen("game") 85 86 def _make_game(self): 87 game_frame = self._game_widget_class(self._client) 88 self.game_frame.content = game_frame 89 self._switch_screen("game") 90 91 def _on_game(self, game: Optional[str]): 92 logger.info(f"New game: {game}") 93 if game: 94 self._make_game() 95 else: 96 self.game_frame.content = None 97 self._show_lobby() 98 99 def _leave_game(self, *args): 100 if not self._client: 101 return 102 if self._client.game: 103 self._client.leave_game(callback=self.app.feedback_response) 104 else: 105 self._show_lobby()
Widget for connected clients.
Enables interacting with the server lobby and game widget(s).
UserFrame( client: <module 'pgnet.client' from '/run/media/wiw/WHITE/ninja/prog/pgnet/pgnet/client.py'>, game_widget_class: kivy.uix.widget.Widget)
21 def __init__(self, client: pgnet.client, game_widget_class: kvex.kivy.Widget): 22 """Initialize the class with the game widget class.""" 23 super().__init__() 24 self._game_widget_class = game_widget_class 25 self._client = client 26 self._make_widgets() 27 self._client.on_game = self._on_game 28 self._on_game(client.game) 29 self.app.menu.set_callback("app", "leave_game", self._leave_game) 30 self.app.menu.set_callback("app", "lobby", self._show_lobby) 31 self.app.menu.set_callback("app", "game", self._show_game) 32 self.app.menu.set_callback("app", "admin_panel", self._show_admin) 33 self.app.menu.get_button("app", "lobby").disabled = True 34 self.app.menu.get_button("app", "game").disabled = True 35 self.app.menu.get_button("app", "admin_panel").disabled = True 36 self.app.controller.bind(f"{self._conpath}.leave_game", self._leave_game) 37 self.app.controller.bind(f"{self._conpath}.show_lobby", self._show_lobby) 38 self.app.controller.bind(f"{self._conpath}.show_game", self._show_game) 39 self.app.controller.bind(f"{self._conpath}.show_admin", self._show_admin) 40 self.app.controller.set_active_callback(self._conpath, self._show_lobby)
Initialize the class with the game widget class.
def
update(self):
42 def update(self): 43 """Background refresh tasks.""" 44 menu_get = self.app.menu.get_button 45 current = self._sm.current 46 disable = not self.app.controller.is_active(self._conpath) 47 menu_get("app", "lobby").disabled = current == "lobby" or disable 48 menu_get("app", "game").disabled = current == "game" or disable 49 menu_get("app", "admin_panel").disabled = current == "admin" or disable 50 menu_get("app", "leave_game").disabled = not bool(self._client.game) 51 self.lobby_frame.update()
Background refresh tasks.
Inherited Members
- kivy.uix.anchorlayout.AnchorLayout
- padding
- anchor_x
- anchor_y
- do_layout
- kivy.uix.layout.Layout
- add_widget
- remove_widget
- layout_hint_with_bounds
- kivy.uix.widget.Widget
- proxy_ref
- apply_class_lang_rules
- collide_point
- collide_widget
- on_motion
- on_touch_down
- on_touch_move
- on_touch_up
- on_kv_post
- clear_widgets
- register_for_motion_event
- unregister_for_motion_event
- export_to_png
- export_as_image
- get_root_window
- get_parent_window
- walk
- walk_reverse
- to_widget
- to_window
- to_parent
- to_local
- get_window_matrix
- x
- y
- width
- height
- pos
- size
- get_right
- set_right
- right
- get_top
- set_top
- top
- get_center_x
- set_center_x
- center_x
- get_center_y
- set_center_y
- center_y
- center
- cls
- children
- parent
- size_hint_x
- size_hint_y
- size_hint
- pos_hint
- size_hint_min_x
- size_hint_min_y
- size_hint_min
- size_hint_max_x
- size_hint_max_y
- size_hint_max
- ids
- opacity
- on_opacity
- canvas
- get_disabled
- set_disabled
- inc_disabled
- dec_disabled
- disabled
- motion_filter
- kivy._event.EventDispatcher
- register_event_type
- unregister_event_types
- unregister_event_type
- is_event_type
- bind
- unbind
- fbind
- funbind
- unbind_uid
- get_property_observers
- events
- dispatch
- dispatch_generic
- dispatch_children
- setter
- getter
- property
- properties
- create_property
- apply_property