bauiv1lib.settings.benchmarks
UIs for debugging purposes.
1# Released under the MIT License. See LICENSE for details. 2# 3"""UIs for debugging purposes.""" 4 5from __future__ import annotations 6 7import logging 8from typing import cast, override 9 10import bauiv1 as bui 11import bascenev1 as bs 12 13 14class BenchmarksAndStressTestsWindow(bui.MainWindow): 15 """Window for launching benchmarks or stress tests.""" 16 17 def __init__( 18 self, 19 transition: str | None = 'in_right', 20 origin_widget: bui.Widget | None = None, 21 ): 22 # pylint: disable=too-many-locals 23 # pylint: disable=too-many-statements 24 # pylint: disable=cyclic-import 25 from bauiv1lib import popup 26 27 uiscale = bui.app.ui_v1.uiscale 28 self._width = width = 1200 if uiscale is bui.UIScale.SMALL else 580 29 self._height = height = ( 30 900 31 if uiscale is bui.UIScale.SMALL 32 else 420 if uiscale is bui.UIScale.MEDIUM else 520 33 ) 34 35 self._stress_test_game_type = 'Random' 36 self._stress_test_playlist = '__default__' 37 self._stress_test_player_count = 8 38 self._stress_test_round_duration = 30 39 40 # Do some fancy math to fill all available screen area up to the 41 # size of our backing container. This lets us fit to the exact 42 # screen shape at small ui scale. 43 screensize = bui.get_virtual_screen_size() 44 scale = ( 45 2.32 46 if uiscale is bui.UIScale.SMALL 47 else 1.4 if uiscale is bui.UIScale.MEDIUM else 1.0 48 ) 49 # Calc screen size in our local container space and clamp to a 50 # bit smaller than our container size. 51 target_width = min(self._width - 70, screensize[0] / scale) 52 target_height = min(self._height - 70, screensize[1] / scale) 53 54 # To get top/left coords, go to the center of our window and 55 # offset by half the width/height of our target area. 56 yoffs = 0.5 * self._height + 0.5 * target_height + 30.0 57 58 self._scroll_width = target_width 59 self._scroll_height = target_height - 31 60 self._scroll_bottom = yoffs - 60 - self._scroll_height 61 62 self._sub_width = min(510.0, self._scroll_width) 63 self._sub_height = 520 64 65 self._r = 'debugWindow' 66 uiscale = bui.app.ui_v1.uiscale 67 super().__init__( 68 root_widget=bui.containerwidget( 69 size=(width, height), 70 scale=scale, 71 toolbar_visibility=( 72 'menu_minimal' 73 if uiscale is bui.UIScale.SMALL 74 else 'menu_full' 75 ), 76 ), 77 transition=transition, 78 origin_widget=origin_widget, 79 # We're affected by screen size only at small ui-scale. 80 refresh_on_screen_size_changes=uiscale is bui.UIScale.SMALL, 81 ) 82 83 if bui.app.ui_v1.uiscale is bui.UIScale.SMALL: 84 bui.containerwidget( 85 edit=self._root_widget, on_cancel_call=self.main_window_back 86 ) 87 self._back_button = bui.get_special_widget('back_button') 88 else: 89 self._back_button = btn = bui.buttonwidget( 90 parent=self._root_widget, 91 position=(40, yoffs - 53), 92 size=(60, 60), 93 scale=0.8, 94 autoselect=True, 95 label=bui.charstr(bui.SpecialChar.BACK), 96 button_type='backSmall', 97 on_activate_call=self.main_window_back, 98 ) 99 bui.containerwidget(edit=self._root_widget, cancel_button=btn) 100 101 bui.textwidget( 102 parent=self._root_widget, 103 position=( 104 self._width * 0.5, 105 yoffs - (45 if uiscale is bui.UIScale.SMALL else 30), 106 ), 107 size=(0, 0), 108 maxwidth=360, 109 scale=0.8 if uiscale is bui.UIScale.SMALL else 1.0, 110 text=bui.Lstr(resource=f'{self._r}.titleText'), 111 h_align='center', 112 v_align='center', 113 color=bui.app.ui_v1.title_color, 114 ) 115 116 self._scrollwidget = bui.scrollwidget( 117 parent=self._root_widget, 118 highlight=False, 119 size=(self._scroll_width, self._scroll_height), 120 position=( 121 self._width * 0.5 - self._scroll_width * 0.5, 122 self._scroll_bottom, 123 ), 124 border_opacity=0.4, 125 center_small_content_horizontally=True, 126 ) 127 bui.containerwidget(edit=self._scrollwidget, claims_left_right=True) 128 129 self._subcontainer = bui.containerwidget( 130 parent=self._scrollwidget, 131 size=(self._sub_width, self._sub_height), 132 background=False, 133 ) 134 135 v = self._sub_height - 70 136 button_width = 300 137 btn = bui.buttonwidget( 138 parent=self._subcontainer, 139 position=((self._sub_width - button_width) * 0.5, v), 140 size=(button_width, 60), 141 autoselect=True, 142 label=bui.Lstr(resource=f'{self._r}.runCPUBenchmarkText'), 143 on_activate_call=self._run_cpu_benchmark_pressed, 144 ) 145 bui.widget( 146 edit=btn, up_widget=self._back_button, left_widget=self._back_button 147 ) 148 v -= 60 149 150 bui.buttonwidget( 151 parent=self._subcontainer, 152 position=((self._sub_width - button_width) * 0.5, v), 153 size=(button_width, 60), 154 autoselect=True, 155 label=bui.Lstr(resource=f'{self._r}.runMediaReloadBenchmarkText'), 156 on_activate_call=self._run_media_reload_benchmark_pressed, 157 ) 158 v -= 60 159 160 bui.textwidget( 161 parent=self._subcontainer, 162 position=(self._sub_width * 0.5, v + 22), 163 size=(0, 0), 164 text=bui.Lstr(resource=f'{self._r}.stressTestTitleText'), 165 maxwidth=200, 166 color=bui.app.ui_v1.heading_color, 167 scale=0.85, 168 h_align='center', 169 v_align='center', 170 ) 171 v -= 45 172 173 x_offs = 165 174 bui.textwidget( 175 parent=self._subcontainer, 176 position=(x_offs - 10, v + 22), 177 size=(0, 0), 178 text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistTypeText'), 179 maxwidth=130, 180 color=bui.app.ui_v1.heading_color, 181 scale=0.65, 182 h_align='right', 183 v_align='center', 184 ) 185 186 popup.PopupMenu( 187 parent=self._subcontainer, 188 position=(x_offs, v), 189 width=150, 190 choices=['Random', 'Teams', 'Free-For-All'], 191 choices_display=[ 192 bui.Lstr(resource=a) 193 for a in [ 194 'randomText', 195 'playModes.teamsText', 196 'playModes.freeForAllText', 197 ] 198 ], 199 current_choice='Auto', 200 on_value_change_call=self._stress_test_game_type_selected, 201 ) 202 203 v -= 46 204 bui.textwidget( 205 parent=self._subcontainer, 206 position=(x_offs - 10, v + 22), 207 size=(0, 0), 208 text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistNameText'), 209 maxwidth=130, 210 color=bui.app.ui_v1.heading_color, 211 scale=0.65, 212 h_align='right', 213 v_align='center', 214 ) 215 216 self._stress_test_playlist_name_field = bui.textwidget( 217 parent=self._subcontainer, 218 position=(x_offs + 5, v - 5), 219 size=(250, 46), 220 text=self._stress_test_playlist, 221 h_align='left', 222 v_align='center', 223 autoselect=True, 224 color=(0.9, 0.9, 0.9, 1.0), 225 description=bui.Lstr( 226 resource=f'{self._r}.stressTestPlaylistDescriptionText' 227 ), 228 editable=True, 229 padding=4, 230 ) 231 v -= 29 232 x_sub = 60 233 234 # Player count. 235 bui.textwidget( 236 parent=self._subcontainer, 237 position=(x_offs - 10, v), 238 size=(0, 0), 239 text=bui.Lstr(resource=f'{self._r}.stressTestPlayerCountText'), 240 color=(0.8, 0.8, 0.8, 1.0), 241 h_align='right', 242 v_align='center', 243 scale=0.65, 244 maxwidth=130, 245 ) 246 self._stress_test_player_count_text = bui.textwidget( 247 parent=self._subcontainer, 248 position=(246 - x_sub, v - 14), 249 size=(60, 28), 250 editable=False, 251 color=(0.3, 1.0, 0.3, 1.0), 252 h_align='right', 253 v_align='center', 254 text=str(self._stress_test_player_count), 255 padding=2, 256 ) 257 bui.buttonwidget( 258 parent=self._subcontainer, 259 position=(330 - x_sub, v - 11), 260 size=(28, 28), 261 label='-', 262 autoselect=True, 263 on_activate_call=bui.Call(self._stress_test_player_count_decrement), 264 repeat=True, 265 enable_sound=True, 266 ) 267 bui.buttonwidget( 268 parent=self._subcontainer, 269 position=(380 - x_sub, v - 11), 270 size=(28, 28), 271 label='+', 272 autoselect=True, 273 on_activate_call=bui.Call(self._stress_test_player_count_increment), 274 repeat=True, 275 enable_sound=True, 276 ) 277 v -= 42 278 279 # Round duration. 280 bui.textwidget( 281 parent=self._subcontainer, 282 position=(x_offs - 10, v), 283 size=(0, 0), 284 text=bui.Lstr(resource=f'{self._r}.stressTestRoundDurationText'), 285 color=(0.8, 0.8, 0.8, 1.0), 286 h_align='right', 287 v_align='center', 288 scale=0.65, 289 maxwidth=130, 290 ) 291 self._stress_test_round_duration_text = bui.textwidget( 292 parent=self._subcontainer, 293 position=(246 - x_sub, v - 14), 294 size=(60, 28), 295 editable=False, 296 color=(0.3, 1.0, 0.3, 1.0), 297 h_align='right', 298 v_align='center', 299 text=str(self._stress_test_round_duration), 300 padding=2, 301 ) 302 bui.buttonwidget( 303 parent=self._subcontainer, 304 position=(330 - x_sub, v - 11), 305 size=(28, 28), 306 label='-', 307 autoselect=True, 308 on_activate_call=bui.Call( 309 self._stress_test_round_duration_decrement 310 ), 311 repeat=True, 312 enable_sound=True, 313 ) 314 bui.buttonwidget( 315 parent=self._subcontainer, 316 position=(380 - x_sub, v - 11), 317 size=(28, 28), 318 label='+', 319 autoselect=True, 320 on_activate_call=bui.Call( 321 self._stress_test_round_duration_increment 322 ), 323 repeat=True, 324 enable_sound=True, 325 ) 326 v -= 82 327 btn = bui.buttonwidget( 328 parent=self._subcontainer, 329 position=((self._sub_width - button_width) * 0.5, v), 330 size=(button_width, 60), 331 autoselect=True, 332 label=bui.Lstr(resource=f'{self._r}.runStressTestText'), 333 on_activate_call=self._stress_test_pressed, 334 ) 335 bui.widget(edit=btn, show_buffer_bottom=50) 336 337 @override 338 def get_main_window_state(self) -> bui.MainWindowState: 339 # Support recreating our window for back/refresh purposes. 340 cls = type(self) 341 return bui.BasicMainWindowState( 342 create_call=lambda transition, origin_widget: cls( 343 transition=transition, origin_widget=origin_widget 344 ) 345 ) 346 347 def _stress_test_player_count_decrement(self) -> None: 348 self._stress_test_player_count = max( 349 1, self._stress_test_player_count - 1 350 ) 351 bui.textwidget( 352 edit=self._stress_test_player_count_text, 353 text=str(self._stress_test_player_count), 354 ) 355 356 def _stress_test_player_count_increment(self) -> None: 357 self._stress_test_player_count = self._stress_test_player_count + 1 358 bui.textwidget( 359 edit=self._stress_test_player_count_text, 360 text=str(self._stress_test_player_count), 361 ) 362 363 def _stress_test_round_duration_decrement(self) -> None: 364 self._stress_test_round_duration = max( 365 10, self._stress_test_round_duration - 10 366 ) 367 bui.textwidget( 368 edit=self._stress_test_round_duration_text, 369 text=str(self._stress_test_round_duration), 370 ) 371 372 def _stress_test_round_duration_increment(self) -> None: 373 self._stress_test_round_duration = self._stress_test_round_duration + 10 374 bui.textwidget( 375 edit=self._stress_test_round_duration_text, 376 text=str(self._stress_test_round_duration), 377 ) 378 379 def _stress_test_game_type_selected(self, game_type: str) -> None: 380 self._stress_test_game_type = game_type 381 382 def _run_cpu_benchmark_pressed(self) -> None: 383 if bui.app.classic is None: 384 logging.warning('run-cpu-benchmark requires classic') 385 return 386 bui.app.classic.run_cpu_benchmark() 387 388 def _run_media_reload_benchmark_pressed(self) -> None: 389 if bui.app.classic is None: 390 logging.warning('run-media-reload-benchmark requires classic') 391 return 392 bui.app.classic.run_media_reload_benchmark() 393 394 def _stress_test_pressed(self) -> None: 395 from bascenev1lib.mainmenu import MainMenuActivity 396 397 if bui.app.classic is None: 398 logging.warning('stress-test requires classic') 399 return 400 401 activity = bs.get_foreground_host_activity() 402 if isinstance(activity, MainMenuActivity): 403 bui.app.classic.run_stress_test( 404 playlist_type=self._stress_test_game_type, 405 playlist_name=cast( 406 str, 407 bui.textwidget(query=self._stress_test_playlist_name_field), 408 ), 409 player_count=self._stress_test_player_count, 410 round_duration=self._stress_test_round_duration, 411 ) 412 bui.containerwidget(edit=self._root_widget, transition='out_right') 413 else: 414 bui.screenmessage( 415 bui.Lstr(value='Already present in another activity.') 416 )
class
BenchmarksAndStressTestsWindow(bauiv1._uitypes.MainWindow):
15class BenchmarksAndStressTestsWindow(bui.MainWindow): 16 """Window for launching benchmarks or stress tests.""" 17 18 def __init__( 19 self, 20 transition: str | None = 'in_right', 21 origin_widget: bui.Widget | None = None, 22 ): 23 # pylint: disable=too-many-locals 24 # pylint: disable=too-many-statements 25 # pylint: disable=cyclic-import 26 from bauiv1lib import popup 27 28 uiscale = bui.app.ui_v1.uiscale 29 self._width = width = 1200 if uiscale is bui.UIScale.SMALL else 580 30 self._height = height = ( 31 900 32 if uiscale is bui.UIScale.SMALL 33 else 420 if uiscale is bui.UIScale.MEDIUM else 520 34 ) 35 36 self._stress_test_game_type = 'Random' 37 self._stress_test_playlist = '__default__' 38 self._stress_test_player_count = 8 39 self._stress_test_round_duration = 30 40 41 # Do some fancy math to fill all available screen area up to the 42 # size of our backing container. This lets us fit to the exact 43 # screen shape at small ui scale. 44 screensize = bui.get_virtual_screen_size() 45 scale = ( 46 2.32 47 if uiscale is bui.UIScale.SMALL 48 else 1.4 if uiscale is bui.UIScale.MEDIUM else 1.0 49 ) 50 # Calc screen size in our local container space and clamp to a 51 # bit smaller than our container size. 52 target_width = min(self._width - 70, screensize[0] / scale) 53 target_height = min(self._height - 70, screensize[1] / scale) 54 55 # To get top/left coords, go to the center of our window and 56 # offset by half the width/height of our target area. 57 yoffs = 0.5 * self._height + 0.5 * target_height + 30.0 58 59 self._scroll_width = target_width 60 self._scroll_height = target_height - 31 61 self._scroll_bottom = yoffs - 60 - self._scroll_height 62 63 self._sub_width = min(510.0, self._scroll_width) 64 self._sub_height = 520 65 66 self._r = 'debugWindow' 67 uiscale = bui.app.ui_v1.uiscale 68 super().__init__( 69 root_widget=bui.containerwidget( 70 size=(width, height), 71 scale=scale, 72 toolbar_visibility=( 73 'menu_minimal' 74 if uiscale is bui.UIScale.SMALL 75 else 'menu_full' 76 ), 77 ), 78 transition=transition, 79 origin_widget=origin_widget, 80 # We're affected by screen size only at small ui-scale. 81 refresh_on_screen_size_changes=uiscale is bui.UIScale.SMALL, 82 ) 83 84 if bui.app.ui_v1.uiscale is bui.UIScale.SMALL: 85 bui.containerwidget( 86 edit=self._root_widget, on_cancel_call=self.main_window_back 87 ) 88 self._back_button = bui.get_special_widget('back_button') 89 else: 90 self._back_button = btn = bui.buttonwidget( 91 parent=self._root_widget, 92 position=(40, yoffs - 53), 93 size=(60, 60), 94 scale=0.8, 95 autoselect=True, 96 label=bui.charstr(bui.SpecialChar.BACK), 97 button_type='backSmall', 98 on_activate_call=self.main_window_back, 99 ) 100 bui.containerwidget(edit=self._root_widget, cancel_button=btn) 101 102 bui.textwidget( 103 parent=self._root_widget, 104 position=( 105 self._width * 0.5, 106 yoffs - (45 if uiscale is bui.UIScale.SMALL else 30), 107 ), 108 size=(0, 0), 109 maxwidth=360, 110 scale=0.8 if uiscale is bui.UIScale.SMALL else 1.0, 111 text=bui.Lstr(resource=f'{self._r}.titleText'), 112 h_align='center', 113 v_align='center', 114 color=bui.app.ui_v1.title_color, 115 ) 116 117 self._scrollwidget = bui.scrollwidget( 118 parent=self._root_widget, 119 highlight=False, 120 size=(self._scroll_width, self._scroll_height), 121 position=( 122 self._width * 0.5 - self._scroll_width * 0.5, 123 self._scroll_bottom, 124 ), 125 border_opacity=0.4, 126 center_small_content_horizontally=True, 127 ) 128 bui.containerwidget(edit=self._scrollwidget, claims_left_right=True) 129 130 self._subcontainer = bui.containerwidget( 131 parent=self._scrollwidget, 132 size=(self._sub_width, self._sub_height), 133 background=False, 134 ) 135 136 v = self._sub_height - 70 137 button_width = 300 138 btn = bui.buttonwidget( 139 parent=self._subcontainer, 140 position=((self._sub_width - button_width) * 0.5, v), 141 size=(button_width, 60), 142 autoselect=True, 143 label=bui.Lstr(resource=f'{self._r}.runCPUBenchmarkText'), 144 on_activate_call=self._run_cpu_benchmark_pressed, 145 ) 146 bui.widget( 147 edit=btn, up_widget=self._back_button, left_widget=self._back_button 148 ) 149 v -= 60 150 151 bui.buttonwidget( 152 parent=self._subcontainer, 153 position=((self._sub_width - button_width) * 0.5, v), 154 size=(button_width, 60), 155 autoselect=True, 156 label=bui.Lstr(resource=f'{self._r}.runMediaReloadBenchmarkText'), 157 on_activate_call=self._run_media_reload_benchmark_pressed, 158 ) 159 v -= 60 160 161 bui.textwidget( 162 parent=self._subcontainer, 163 position=(self._sub_width * 0.5, v + 22), 164 size=(0, 0), 165 text=bui.Lstr(resource=f'{self._r}.stressTestTitleText'), 166 maxwidth=200, 167 color=bui.app.ui_v1.heading_color, 168 scale=0.85, 169 h_align='center', 170 v_align='center', 171 ) 172 v -= 45 173 174 x_offs = 165 175 bui.textwidget( 176 parent=self._subcontainer, 177 position=(x_offs - 10, v + 22), 178 size=(0, 0), 179 text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistTypeText'), 180 maxwidth=130, 181 color=bui.app.ui_v1.heading_color, 182 scale=0.65, 183 h_align='right', 184 v_align='center', 185 ) 186 187 popup.PopupMenu( 188 parent=self._subcontainer, 189 position=(x_offs, v), 190 width=150, 191 choices=['Random', 'Teams', 'Free-For-All'], 192 choices_display=[ 193 bui.Lstr(resource=a) 194 for a in [ 195 'randomText', 196 'playModes.teamsText', 197 'playModes.freeForAllText', 198 ] 199 ], 200 current_choice='Auto', 201 on_value_change_call=self._stress_test_game_type_selected, 202 ) 203 204 v -= 46 205 bui.textwidget( 206 parent=self._subcontainer, 207 position=(x_offs - 10, v + 22), 208 size=(0, 0), 209 text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistNameText'), 210 maxwidth=130, 211 color=bui.app.ui_v1.heading_color, 212 scale=0.65, 213 h_align='right', 214 v_align='center', 215 ) 216 217 self._stress_test_playlist_name_field = bui.textwidget( 218 parent=self._subcontainer, 219 position=(x_offs + 5, v - 5), 220 size=(250, 46), 221 text=self._stress_test_playlist, 222 h_align='left', 223 v_align='center', 224 autoselect=True, 225 color=(0.9, 0.9, 0.9, 1.0), 226 description=bui.Lstr( 227 resource=f'{self._r}.stressTestPlaylistDescriptionText' 228 ), 229 editable=True, 230 padding=4, 231 ) 232 v -= 29 233 x_sub = 60 234 235 # Player count. 236 bui.textwidget( 237 parent=self._subcontainer, 238 position=(x_offs - 10, v), 239 size=(0, 0), 240 text=bui.Lstr(resource=f'{self._r}.stressTestPlayerCountText'), 241 color=(0.8, 0.8, 0.8, 1.0), 242 h_align='right', 243 v_align='center', 244 scale=0.65, 245 maxwidth=130, 246 ) 247 self._stress_test_player_count_text = bui.textwidget( 248 parent=self._subcontainer, 249 position=(246 - x_sub, v - 14), 250 size=(60, 28), 251 editable=False, 252 color=(0.3, 1.0, 0.3, 1.0), 253 h_align='right', 254 v_align='center', 255 text=str(self._stress_test_player_count), 256 padding=2, 257 ) 258 bui.buttonwidget( 259 parent=self._subcontainer, 260 position=(330 - x_sub, v - 11), 261 size=(28, 28), 262 label='-', 263 autoselect=True, 264 on_activate_call=bui.Call(self._stress_test_player_count_decrement), 265 repeat=True, 266 enable_sound=True, 267 ) 268 bui.buttonwidget( 269 parent=self._subcontainer, 270 position=(380 - x_sub, v - 11), 271 size=(28, 28), 272 label='+', 273 autoselect=True, 274 on_activate_call=bui.Call(self._stress_test_player_count_increment), 275 repeat=True, 276 enable_sound=True, 277 ) 278 v -= 42 279 280 # Round duration. 281 bui.textwidget( 282 parent=self._subcontainer, 283 position=(x_offs - 10, v), 284 size=(0, 0), 285 text=bui.Lstr(resource=f'{self._r}.stressTestRoundDurationText'), 286 color=(0.8, 0.8, 0.8, 1.0), 287 h_align='right', 288 v_align='center', 289 scale=0.65, 290 maxwidth=130, 291 ) 292 self._stress_test_round_duration_text = bui.textwidget( 293 parent=self._subcontainer, 294 position=(246 - x_sub, v - 14), 295 size=(60, 28), 296 editable=False, 297 color=(0.3, 1.0, 0.3, 1.0), 298 h_align='right', 299 v_align='center', 300 text=str(self._stress_test_round_duration), 301 padding=2, 302 ) 303 bui.buttonwidget( 304 parent=self._subcontainer, 305 position=(330 - x_sub, v - 11), 306 size=(28, 28), 307 label='-', 308 autoselect=True, 309 on_activate_call=bui.Call( 310 self._stress_test_round_duration_decrement 311 ), 312 repeat=True, 313 enable_sound=True, 314 ) 315 bui.buttonwidget( 316 parent=self._subcontainer, 317 position=(380 - x_sub, v - 11), 318 size=(28, 28), 319 label='+', 320 autoselect=True, 321 on_activate_call=bui.Call( 322 self._stress_test_round_duration_increment 323 ), 324 repeat=True, 325 enable_sound=True, 326 ) 327 v -= 82 328 btn = bui.buttonwidget( 329 parent=self._subcontainer, 330 position=((self._sub_width - button_width) * 0.5, v), 331 size=(button_width, 60), 332 autoselect=True, 333 label=bui.Lstr(resource=f'{self._r}.runStressTestText'), 334 on_activate_call=self._stress_test_pressed, 335 ) 336 bui.widget(edit=btn, show_buffer_bottom=50) 337 338 @override 339 def get_main_window_state(self) -> bui.MainWindowState: 340 # Support recreating our window for back/refresh purposes. 341 cls = type(self) 342 return bui.BasicMainWindowState( 343 create_call=lambda transition, origin_widget: cls( 344 transition=transition, origin_widget=origin_widget 345 ) 346 ) 347 348 def _stress_test_player_count_decrement(self) -> None: 349 self._stress_test_player_count = max( 350 1, self._stress_test_player_count - 1 351 ) 352 bui.textwidget( 353 edit=self._stress_test_player_count_text, 354 text=str(self._stress_test_player_count), 355 ) 356 357 def _stress_test_player_count_increment(self) -> None: 358 self._stress_test_player_count = self._stress_test_player_count + 1 359 bui.textwidget( 360 edit=self._stress_test_player_count_text, 361 text=str(self._stress_test_player_count), 362 ) 363 364 def _stress_test_round_duration_decrement(self) -> None: 365 self._stress_test_round_duration = max( 366 10, self._stress_test_round_duration - 10 367 ) 368 bui.textwidget( 369 edit=self._stress_test_round_duration_text, 370 text=str(self._stress_test_round_duration), 371 ) 372 373 def _stress_test_round_duration_increment(self) -> None: 374 self._stress_test_round_duration = self._stress_test_round_duration + 10 375 bui.textwidget( 376 edit=self._stress_test_round_duration_text, 377 text=str(self._stress_test_round_duration), 378 ) 379 380 def _stress_test_game_type_selected(self, game_type: str) -> None: 381 self._stress_test_game_type = game_type 382 383 def _run_cpu_benchmark_pressed(self) -> None: 384 if bui.app.classic is None: 385 logging.warning('run-cpu-benchmark requires classic') 386 return 387 bui.app.classic.run_cpu_benchmark() 388 389 def _run_media_reload_benchmark_pressed(self) -> None: 390 if bui.app.classic is None: 391 logging.warning('run-media-reload-benchmark requires classic') 392 return 393 bui.app.classic.run_media_reload_benchmark() 394 395 def _stress_test_pressed(self) -> None: 396 from bascenev1lib.mainmenu import MainMenuActivity 397 398 if bui.app.classic is None: 399 logging.warning('stress-test requires classic') 400 return 401 402 activity = bs.get_foreground_host_activity() 403 if isinstance(activity, MainMenuActivity): 404 bui.app.classic.run_stress_test( 405 playlist_type=self._stress_test_game_type, 406 playlist_name=cast( 407 str, 408 bui.textwidget(query=self._stress_test_playlist_name_field), 409 ), 410 player_count=self._stress_test_player_count, 411 round_duration=self._stress_test_round_duration, 412 ) 413 bui.containerwidget(edit=self._root_widget, transition='out_right') 414 else: 415 bui.screenmessage( 416 bui.Lstr(value='Already present in another activity.') 417 )
Window for launching benchmarks or stress tests.
BenchmarksAndStressTestsWindow( transition: str | None = 'in_right', origin_widget: _bauiv1.Widget | None = None)
18 def __init__( 19 self, 20 transition: str | None = 'in_right', 21 origin_widget: bui.Widget | None = None, 22 ): 23 # pylint: disable=too-many-locals 24 # pylint: disable=too-many-statements 25 # pylint: disable=cyclic-import 26 from bauiv1lib import popup 27 28 uiscale = bui.app.ui_v1.uiscale 29 self._width = width = 1200 if uiscale is bui.UIScale.SMALL else 580 30 self._height = height = ( 31 900 32 if uiscale is bui.UIScale.SMALL 33 else 420 if uiscale is bui.UIScale.MEDIUM else 520 34 ) 35 36 self._stress_test_game_type = 'Random' 37 self._stress_test_playlist = '__default__' 38 self._stress_test_player_count = 8 39 self._stress_test_round_duration = 30 40 41 # Do some fancy math to fill all available screen area up to the 42 # size of our backing container. This lets us fit to the exact 43 # screen shape at small ui scale. 44 screensize = bui.get_virtual_screen_size() 45 scale = ( 46 2.32 47 if uiscale is bui.UIScale.SMALL 48 else 1.4 if uiscale is bui.UIScale.MEDIUM else 1.0 49 ) 50 # Calc screen size in our local container space and clamp to a 51 # bit smaller than our container size. 52 target_width = min(self._width - 70, screensize[0] / scale) 53 target_height = min(self._height - 70, screensize[1] / scale) 54 55 # To get top/left coords, go to the center of our window and 56 # offset by half the width/height of our target area. 57 yoffs = 0.5 * self._height + 0.5 * target_height + 30.0 58 59 self._scroll_width = target_width 60 self._scroll_height = target_height - 31 61 self._scroll_bottom = yoffs - 60 - self._scroll_height 62 63 self._sub_width = min(510.0, self._scroll_width) 64 self._sub_height = 520 65 66 self._r = 'debugWindow' 67 uiscale = bui.app.ui_v1.uiscale 68 super().__init__( 69 root_widget=bui.containerwidget( 70 size=(width, height), 71 scale=scale, 72 toolbar_visibility=( 73 'menu_minimal' 74 if uiscale is bui.UIScale.SMALL 75 else 'menu_full' 76 ), 77 ), 78 transition=transition, 79 origin_widget=origin_widget, 80 # We're affected by screen size only at small ui-scale. 81 refresh_on_screen_size_changes=uiscale is bui.UIScale.SMALL, 82 ) 83 84 if bui.app.ui_v1.uiscale is bui.UIScale.SMALL: 85 bui.containerwidget( 86 edit=self._root_widget, on_cancel_call=self.main_window_back 87 ) 88 self._back_button = bui.get_special_widget('back_button') 89 else: 90 self._back_button = btn = bui.buttonwidget( 91 parent=self._root_widget, 92 position=(40, yoffs - 53), 93 size=(60, 60), 94 scale=0.8, 95 autoselect=True, 96 label=bui.charstr(bui.SpecialChar.BACK), 97 button_type='backSmall', 98 on_activate_call=self.main_window_back, 99 ) 100 bui.containerwidget(edit=self._root_widget, cancel_button=btn) 101 102 bui.textwidget( 103 parent=self._root_widget, 104 position=( 105 self._width * 0.5, 106 yoffs - (45 if uiscale is bui.UIScale.SMALL else 30), 107 ), 108 size=(0, 0), 109 maxwidth=360, 110 scale=0.8 if uiscale is bui.UIScale.SMALL else 1.0, 111 text=bui.Lstr(resource=f'{self._r}.titleText'), 112 h_align='center', 113 v_align='center', 114 color=bui.app.ui_v1.title_color, 115 ) 116 117 self._scrollwidget = bui.scrollwidget( 118 parent=self._root_widget, 119 highlight=False, 120 size=(self._scroll_width, self._scroll_height), 121 position=( 122 self._width * 0.5 - self._scroll_width * 0.5, 123 self._scroll_bottom, 124 ), 125 border_opacity=0.4, 126 center_small_content_horizontally=True, 127 ) 128 bui.containerwidget(edit=self._scrollwidget, claims_left_right=True) 129 130 self._subcontainer = bui.containerwidget( 131 parent=self._scrollwidget, 132 size=(self._sub_width, self._sub_height), 133 background=False, 134 ) 135 136 v = self._sub_height - 70 137 button_width = 300 138 btn = bui.buttonwidget( 139 parent=self._subcontainer, 140 position=((self._sub_width - button_width) * 0.5, v), 141 size=(button_width, 60), 142 autoselect=True, 143 label=bui.Lstr(resource=f'{self._r}.runCPUBenchmarkText'), 144 on_activate_call=self._run_cpu_benchmark_pressed, 145 ) 146 bui.widget( 147 edit=btn, up_widget=self._back_button, left_widget=self._back_button 148 ) 149 v -= 60 150 151 bui.buttonwidget( 152 parent=self._subcontainer, 153 position=((self._sub_width - button_width) * 0.5, v), 154 size=(button_width, 60), 155 autoselect=True, 156 label=bui.Lstr(resource=f'{self._r}.runMediaReloadBenchmarkText'), 157 on_activate_call=self._run_media_reload_benchmark_pressed, 158 ) 159 v -= 60 160 161 bui.textwidget( 162 parent=self._subcontainer, 163 position=(self._sub_width * 0.5, v + 22), 164 size=(0, 0), 165 text=bui.Lstr(resource=f'{self._r}.stressTestTitleText'), 166 maxwidth=200, 167 color=bui.app.ui_v1.heading_color, 168 scale=0.85, 169 h_align='center', 170 v_align='center', 171 ) 172 v -= 45 173 174 x_offs = 165 175 bui.textwidget( 176 parent=self._subcontainer, 177 position=(x_offs - 10, v + 22), 178 size=(0, 0), 179 text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistTypeText'), 180 maxwidth=130, 181 color=bui.app.ui_v1.heading_color, 182 scale=0.65, 183 h_align='right', 184 v_align='center', 185 ) 186 187 popup.PopupMenu( 188 parent=self._subcontainer, 189 position=(x_offs, v), 190 width=150, 191 choices=['Random', 'Teams', 'Free-For-All'], 192 choices_display=[ 193 bui.Lstr(resource=a) 194 for a in [ 195 'randomText', 196 'playModes.teamsText', 197 'playModes.freeForAllText', 198 ] 199 ], 200 current_choice='Auto', 201 on_value_change_call=self._stress_test_game_type_selected, 202 ) 203 204 v -= 46 205 bui.textwidget( 206 parent=self._subcontainer, 207 position=(x_offs - 10, v + 22), 208 size=(0, 0), 209 text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistNameText'), 210 maxwidth=130, 211 color=bui.app.ui_v1.heading_color, 212 scale=0.65, 213 h_align='right', 214 v_align='center', 215 ) 216 217 self._stress_test_playlist_name_field = bui.textwidget( 218 parent=self._subcontainer, 219 position=(x_offs + 5, v - 5), 220 size=(250, 46), 221 text=self._stress_test_playlist, 222 h_align='left', 223 v_align='center', 224 autoselect=True, 225 color=(0.9, 0.9, 0.9, 1.0), 226 description=bui.Lstr( 227 resource=f'{self._r}.stressTestPlaylistDescriptionText' 228 ), 229 editable=True, 230 padding=4, 231 ) 232 v -= 29 233 x_sub = 60 234 235 # Player count. 236 bui.textwidget( 237 parent=self._subcontainer, 238 position=(x_offs - 10, v), 239 size=(0, 0), 240 text=bui.Lstr(resource=f'{self._r}.stressTestPlayerCountText'), 241 color=(0.8, 0.8, 0.8, 1.0), 242 h_align='right', 243 v_align='center', 244 scale=0.65, 245 maxwidth=130, 246 ) 247 self._stress_test_player_count_text = bui.textwidget( 248 parent=self._subcontainer, 249 position=(246 - x_sub, v - 14), 250 size=(60, 28), 251 editable=False, 252 color=(0.3, 1.0, 0.3, 1.0), 253 h_align='right', 254 v_align='center', 255 text=str(self._stress_test_player_count), 256 padding=2, 257 ) 258 bui.buttonwidget( 259 parent=self._subcontainer, 260 position=(330 - x_sub, v - 11), 261 size=(28, 28), 262 label='-', 263 autoselect=True, 264 on_activate_call=bui.Call(self._stress_test_player_count_decrement), 265 repeat=True, 266 enable_sound=True, 267 ) 268 bui.buttonwidget( 269 parent=self._subcontainer, 270 position=(380 - x_sub, v - 11), 271 size=(28, 28), 272 label='+', 273 autoselect=True, 274 on_activate_call=bui.Call(self._stress_test_player_count_increment), 275 repeat=True, 276 enable_sound=True, 277 ) 278 v -= 42 279 280 # Round duration. 281 bui.textwidget( 282 parent=self._subcontainer, 283 position=(x_offs - 10, v), 284 size=(0, 0), 285 text=bui.Lstr(resource=f'{self._r}.stressTestRoundDurationText'), 286 color=(0.8, 0.8, 0.8, 1.0), 287 h_align='right', 288 v_align='center', 289 scale=0.65, 290 maxwidth=130, 291 ) 292 self._stress_test_round_duration_text = bui.textwidget( 293 parent=self._subcontainer, 294 position=(246 - x_sub, v - 14), 295 size=(60, 28), 296 editable=False, 297 color=(0.3, 1.0, 0.3, 1.0), 298 h_align='right', 299 v_align='center', 300 text=str(self._stress_test_round_duration), 301 padding=2, 302 ) 303 bui.buttonwidget( 304 parent=self._subcontainer, 305 position=(330 - x_sub, v - 11), 306 size=(28, 28), 307 label='-', 308 autoselect=True, 309 on_activate_call=bui.Call( 310 self._stress_test_round_duration_decrement 311 ), 312 repeat=True, 313 enable_sound=True, 314 ) 315 bui.buttonwidget( 316 parent=self._subcontainer, 317 position=(380 - x_sub, v - 11), 318 size=(28, 28), 319 label='+', 320 autoselect=True, 321 on_activate_call=bui.Call( 322 self._stress_test_round_duration_increment 323 ), 324 repeat=True, 325 enable_sound=True, 326 ) 327 v -= 82 328 btn = bui.buttonwidget( 329 parent=self._subcontainer, 330 position=((self._sub_width - button_width) * 0.5, v), 331 size=(button_width, 60), 332 autoselect=True, 333 label=bui.Lstr(resource=f'{self._r}.runStressTestText'), 334 on_activate_call=self._stress_test_pressed, 335 ) 336 bui.widget(edit=btn, show_buffer_bottom=50)
Create a MainWindow given a root widget and transition info.
Automatically handles in and out transitions on the provided widget, so there is no need to set transitions when creating it.
338 @override 339 def get_main_window_state(self) -> bui.MainWindowState: 340 # Support recreating our window for back/refresh purposes. 341 cls = type(self) 342 return bui.BasicMainWindowState( 343 create_call=lambda transition, origin_widget: cls( 344 transition=transition, origin_widget=origin_widget 345 ) 346 )
Return a WindowState to recreate this window, if supported.