bauiv1lib.gather.nearbytab
Defines the nearby tab in the gather UI.
1# Released under the MIT License. See LICENSE for details. 2# 3"""Defines the nearby tab in the gather UI.""" 4 5from __future__ import annotations 6 7import weakref 8from typing import TYPE_CHECKING 9 10from bauiv1lib.gather import GatherTab 11import bauiv1 as bui 12import bascenev1 as bs 13 14if TYPE_CHECKING: 15 from typing import Any 16 17 from bauiv1lib.gather import GatherWindow 18 19 20class NetScanner: 21 """Class for scanning for nearby games (lan, bluetooth, etc).""" 22 23 def __init__( 24 self, 25 tab: GatherTab, 26 scrollwidget: bui.Widget, 27 tab_button: bui.Widget, 28 width: float, 29 ): 30 self._tab = weakref.ref(tab) 31 self._scrollwidget = scrollwidget 32 self._tab_button = tab_button 33 self._columnwidget = bui.columnwidget( 34 parent=self._scrollwidget, border=2, margin=0, left_border=10 35 ) 36 bui.widget(edit=self._columnwidget, up_widget=tab_button) 37 self._width = width 38 self._last_selected_host: dict[str, Any] | None = None 39 40 self._update_timer = bui.AppTimer( 41 1.0, bui.WeakCall(self.update), repeat=True 42 ) 43 # Go ahead and run a few *almost* immediately so we don't 44 # have to wait a second. 45 self.update() 46 bui.apptimer(0.25, bui.WeakCall(self.update)) 47 48 def __del__(self) -> None: 49 bs.end_host_scanning() 50 51 def _on_select(self, host: dict[str, Any]) -> None: 52 self._last_selected_host = host 53 54 def _on_activate(self, host: dict[str, Any]) -> None: 55 bs.connect_to_party(host['address']) 56 57 def update(self) -> None: 58 """(internal)""" 59 60 # In case our UI was killed from under us. 61 if not self._columnwidget: 62 print( 63 f'ERROR: NetScanner running without UI at time {bui.apptime()}.' 64 ) 65 return 66 67 t_scale = 1.6 68 for child in self._columnwidget.get_children(): 69 child.delete() 70 71 # Grab this now this since adding widgets will change it. 72 last_selected_host = self._last_selected_host 73 hosts = bs.host_scan_cycle() 74 for i, host in enumerate(hosts): 75 txt3 = bui.textwidget( 76 parent=self._columnwidget, 77 size=(self._width / t_scale, 30), 78 selectable=True, 79 color=(1, 1, 1), 80 on_select_call=bui.Call(self._on_select, host), 81 on_activate_call=bui.Call(self._on_activate, host), 82 click_activate=True, 83 text=host['display_string'], 84 h_align='left', 85 v_align='center', 86 corner_scale=t_scale, 87 maxwidth=(self._width / t_scale) * 0.93, 88 ) 89 if host == last_selected_host: 90 bui.containerwidget( 91 edit=self._columnwidget, 92 selected_child=txt3, 93 visible_child=txt3, 94 ) 95 if i == 0: 96 bui.widget(edit=txt3, up_widget=self._tab_button) 97 98 99class NearbyGatherTab(GatherTab): 100 """The nearby tab in the gather UI""" 101 102 def __init__(self, window: GatherWindow) -> None: 103 super().__init__(window) 104 self._net_scanner: NetScanner | None = None 105 self._container: bui.Widget | None = None 106 107 def on_activate( 108 self, 109 parent_widget: bui.Widget, 110 tab_button: bui.Widget, 111 region_width: float, 112 region_height: float, 113 region_left: float, 114 region_bottom: float, 115 ) -> bui.Widget: 116 c_width = region_width 117 c_height = region_height - 20 118 sub_scroll_height = c_height - 85 119 sub_scroll_width = 650 120 self._container = bui.containerwidget( 121 parent=parent_widget, 122 position=( 123 region_left, 124 region_bottom + (region_height - c_height) * 0.5, 125 ), 126 size=(c_width, c_height), 127 background=False, 128 selection_loops_to_parent=True, 129 ) 130 v = c_height - 30 131 bui.textwidget( 132 parent=self._container, 133 position=(c_width * 0.5, v - 3), 134 color=(0.6, 1.0, 0.6), 135 scale=1.3, 136 size=(0, 0), 137 maxwidth=c_width * 0.9, 138 h_align='center', 139 v_align='center', 140 text=bui.Lstr( 141 resource='gatherWindow.' 'localNetworkDescriptionText' 142 ), 143 ) 144 v -= 15 145 v -= sub_scroll_height + 23 146 scrollw = bui.scrollwidget( 147 parent=self._container, 148 position=((region_width - sub_scroll_width) * 0.5, v), 149 size=(sub_scroll_width, sub_scroll_height), 150 ) 151 152 self._net_scanner = NetScanner( 153 self, scrollw, tab_button, width=sub_scroll_width 154 ) 155 156 bui.widget(edit=scrollw, autoselect=True, up_widget=tab_button) 157 return self._container 158 159 def on_deactivate(self) -> None: 160 self._net_scanner = None
class
NetScanner:
21class NetScanner: 22 """Class for scanning for nearby games (lan, bluetooth, etc).""" 23 24 def __init__( 25 self, 26 tab: GatherTab, 27 scrollwidget: bui.Widget, 28 tab_button: bui.Widget, 29 width: float, 30 ): 31 self._tab = weakref.ref(tab) 32 self._scrollwidget = scrollwidget 33 self._tab_button = tab_button 34 self._columnwidget = bui.columnwidget( 35 parent=self._scrollwidget, border=2, margin=0, left_border=10 36 ) 37 bui.widget(edit=self._columnwidget, up_widget=tab_button) 38 self._width = width 39 self._last_selected_host: dict[str, Any] | None = None 40 41 self._update_timer = bui.AppTimer( 42 1.0, bui.WeakCall(self.update), repeat=True 43 ) 44 # Go ahead and run a few *almost* immediately so we don't 45 # have to wait a second. 46 self.update() 47 bui.apptimer(0.25, bui.WeakCall(self.update)) 48 49 def __del__(self) -> None: 50 bs.end_host_scanning() 51 52 def _on_select(self, host: dict[str, Any]) -> None: 53 self._last_selected_host = host 54 55 def _on_activate(self, host: dict[str, Any]) -> None: 56 bs.connect_to_party(host['address']) 57 58 def update(self) -> None: 59 """(internal)""" 60 61 # In case our UI was killed from under us. 62 if not self._columnwidget: 63 print( 64 f'ERROR: NetScanner running without UI at time {bui.apptime()}.' 65 ) 66 return 67 68 t_scale = 1.6 69 for child in self._columnwidget.get_children(): 70 child.delete() 71 72 # Grab this now this since adding widgets will change it. 73 last_selected_host = self._last_selected_host 74 hosts = bs.host_scan_cycle() 75 for i, host in enumerate(hosts): 76 txt3 = bui.textwidget( 77 parent=self._columnwidget, 78 size=(self._width / t_scale, 30), 79 selectable=True, 80 color=(1, 1, 1), 81 on_select_call=bui.Call(self._on_select, host), 82 on_activate_call=bui.Call(self._on_activate, host), 83 click_activate=True, 84 text=host['display_string'], 85 h_align='left', 86 v_align='center', 87 corner_scale=t_scale, 88 maxwidth=(self._width / t_scale) * 0.93, 89 ) 90 if host == last_selected_host: 91 bui.containerwidget( 92 edit=self._columnwidget, 93 selected_child=txt3, 94 visible_child=txt3, 95 ) 96 if i == 0: 97 bui.widget(edit=txt3, up_widget=self._tab_button)
Class for scanning for nearby games (lan, bluetooth, etc).
NetScanner( tab: bauiv1lib.gather.GatherTab, scrollwidget: _bauiv1.Widget, tab_button: _bauiv1.Widget, width: float)
24 def __init__( 25 self, 26 tab: GatherTab, 27 scrollwidget: bui.Widget, 28 tab_button: bui.Widget, 29 width: float, 30 ): 31 self._tab = weakref.ref(tab) 32 self._scrollwidget = scrollwidget 33 self._tab_button = tab_button 34 self._columnwidget = bui.columnwidget( 35 parent=self._scrollwidget, border=2, margin=0, left_border=10 36 ) 37 bui.widget(edit=self._columnwidget, up_widget=tab_button) 38 self._width = width 39 self._last_selected_host: dict[str, Any] | None = None 40 41 self._update_timer = bui.AppTimer( 42 1.0, bui.WeakCall(self.update), repeat=True 43 ) 44 # Go ahead and run a few *almost* immediately so we don't 45 # have to wait a second. 46 self.update() 47 bui.apptimer(0.25, bui.WeakCall(self.update))
100class NearbyGatherTab(GatherTab): 101 """The nearby tab in the gather UI""" 102 103 def __init__(self, window: GatherWindow) -> None: 104 super().__init__(window) 105 self._net_scanner: NetScanner | None = None 106 self._container: bui.Widget | None = None 107 108 def on_activate( 109 self, 110 parent_widget: bui.Widget, 111 tab_button: bui.Widget, 112 region_width: float, 113 region_height: float, 114 region_left: float, 115 region_bottom: float, 116 ) -> bui.Widget: 117 c_width = region_width 118 c_height = region_height - 20 119 sub_scroll_height = c_height - 85 120 sub_scroll_width = 650 121 self._container = bui.containerwidget( 122 parent=parent_widget, 123 position=( 124 region_left, 125 region_bottom + (region_height - c_height) * 0.5, 126 ), 127 size=(c_width, c_height), 128 background=False, 129 selection_loops_to_parent=True, 130 ) 131 v = c_height - 30 132 bui.textwidget( 133 parent=self._container, 134 position=(c_width * 0.5, v - 3), 135 color=(0.6, 1.0, 0.6), 136 scale=1.3, 137 size=(0, 0), 138 maxwidth=c_width * 0.9, 139 h_align='center', 140 v_align='center', 141 text=bui.Lstr( 142 resource='gatherWindow.' 'localNetworkDescriptionText' 143 ), 144 ) 145 v -= 15 146 v -= sub_scroll_height + 23 147 scrollw = bui.scrollwidget( 148 parent=self._container, 149 position=((region_width - sub_scroll_width) * 0.5, v), 150 size=(sub_scroll_width, sub_scroll_height), 151 ) 152 153 self._net_scanner = NetScanner( 154 self, scrollw, tab_button, width=sub_scroll_width 155 ) 156 157 bui.widget(edit=scrollw, autoselect=True, up_widget=tab_button) 158 return self._container 159 160 def on_deactivate(self) -> None: 161 self._net_scanner = None
The nearby tab in the gather UI
NearbyGatherTab(window: bauiv1lib.gather.GatherWindow)
def
on_activate( self, parent_widget: _bauiv1.Widget, tab_button: _bauiv1.Widget, region_width: float, region_height: float, region_left: float, region_bottom: float) -> _bauiv1.Widget:
108 def on_activate( 109 self, 110 parent_widget: bui.Widget, 111 tab_button: bui.Widget, 112 region_width: float, 113 region_height: float, 114 region_left: float, 115 region_bottom: float, 116 ) -> bui.Widget: 117 c_width = region_width 118 c_height = region_height - 20 119 sub_scroll_height = c_height - 85 120 sub_scroll_width = 650 121 self._container = bui.containerwidget( 122 parent=parent_widget, 123 position=( 124 region_left, 125 region_bottom + (region_height - c_height) * 0.5, 126 ), 127 size=(c_width, c_height), 128 background=False, 129 selection_loops_to_parent=True, 130 ) 131 v = c_height - 30 132 bui.textwidget( 133 parent=self._container, 134 position=(c_width * 0.5, v - 3), 135 color=(0.6, 1.0, 0.6), 136 scale=1.3, 137 size=(0, 0), 138 maxwidth=c_width * 0.9, 139 h_align='center', 140 v_align='center', 141 text=bui.Lstr( 142 resource='gatherWindow.' 'localNetworkDescriptionText' 143 ), 144 ) 145 v -= 15 146 v -= sub_scroll_height + 23 147 scrollw = bui.scrollwidget( 148 parent=self._container, 149 position=((region_width - sub_scroll_width) * 0.5, v), 150 size=(sub_scroll_width, sub_scroll_height), 151 ) 152 153 self._net_scanner = NetScanner( 154 self, scrollw, tab_button, width=sub_scroll_width 155 ) 156 157 bui.widget(edit=scrollw, autoselect=True, up_widget=tab_button) 158 return self._container
Called when the tab becomes the active one.
The tab should create and return a container widget covering the specified region.