Done with utilities. Need some testing on ultraparty and few fix on auto stunt and quick customgame

This commit is contained in:
brostosjoined 2024-01-16 15:06:45 +03:00
parent 39845e7b61
commit 77d16e63c7
19 changed files with 1221 additions and 1093 deletions

View file

@ -1,22 +1,25 @@
# ba_meta require api 7
# Porting to api 8 made easier by baport.(https://github.com/bombsquad-community/baport)
# ba_meta require api 8
# (see https://ballistica.net/wiki/meta-tag-system)
from __future__ import annotations
from typing import TYPE_CHECKING
import ba
import _ba
from bastd.ui.play import PlayWindow
from bastd.ui.playlist.addgame import PlaylistAddGameWindow
from ba._freeforallsession import FreeForAllSession
from bastd.activity.multiteamjoin import MultiTeamJoinActivity
import babase
import bauiv1 as bui
import bascenev1 as bs
import _babase
from bauiv1lib.play import PlayWindow
from bauiv1lib.playlist.addgame import PlaylistAddGameWindow
from bascenev1._freeforallsession import FreeForAllSession
from bascenev1lib.activity.multiteamjoin import MultiTeamJoinActivity
if TYPE_CHECKING:
pass
lang = ba.app.lang.language
lang = bs.app.lang.language
if lang == 'Spanish':
custom_txt = 'personalizar...'
@ -24,30 +27,30 @@ else:
custom_txt = 'custom...'
if 'quick_game_button' in ba.app.config:
config = ba.app.config['quick_game_button']
if 'quick_game_button' in babase.app.config:
config = babase.app.config['quick_game_button']
else:
config = {'selected': None, 'config': None}
ba.app.config['quick_game_button'] = config
ba.app.config.commit()
babase.app.config['quick_game_button'] = config
babase.app.config.commit()
def start_game(session: ba.Session, fadeout: bool = True):
def start_game(session: bs.Session, fadeout: bool = True):
def callback():
if fadeout:
_ba.unlock_all_input()
_babase.unlock_all_input()
try:
_ba.new_host_session(session)
bs.new_host_session(session)
except Exception:
from bastd import mainmenu
ba.print_exception('exception running session', session)
from bascenev1lib import mainmenu
babase.print_exception('exception running session', session)
# Drop back into a main menu session.
_ba.new_host_session(mainmenu.MainMenuSession)
bs.new_host_session(mainmenu.MainMenuSession)
if fadeout:
_ba.fade_screen(False, time=0.25, endcall=callback)
_ba.lock_all_input()
_babase.fade_screen(False, time=0.25, endcall=callback)
_babase.lock_all_input()
else:
callback()
@ -56,7 +59,7 @@ class SimplePlaylist:
def __init__(self,
settings: dict,
gametype: type[ba.GameActivity]):
gametype: type[bs.GameActivity]):
self.settings = settings
self.gametype = gametype
@ -75,7 +78,7 @@ class CustomSession(FreeForAllSession):
# pylint: disable=cyclic-import
self.use_teams = False
self._tutorial_activity_instance = None
ba.Session.__init__(self, depsets=[],
bs.Session.__init__(self, depsets=[],
team_names=None,
team_colors=None,
min_players=1,
@ -89,12 +92,12 @@ class CustomSession(FreeForAllSession):
self._playlist = SimplePlaylist(self._config, self._gametype)
config['selected'] = self._gametype.__name__
config['config'] = self._config
ba.app.config.commit()
babase.app.config.commit()
# Get a game on deck ready to go.
self._current_game_spec: Optional[Dict[str, Any]] = None
self._next_game_spec: Dict[str, Any] = self._playlist.pull_next()
self._next_game: Type[ba.GameActivity] = (
self._next_game: Type[bs.GameActivity] = (
self._next_game_spec['resolved_type'])
# Go ahead and instantiate the next game we'll
@ -102,71 +105,71 @@ class CustomSession(FreeForAllSession):
self._instantiate_next_game()
# Start in our custom join screen.
self.setactivity(_ba.newactivity(MultiTeamJoinActivity))
self.setactivity(bs.newactivity(MultiTeamJoinActivity))
class SelectGameWindow(PlaylistAddGameWindow):
def __init__(self, transition: str = 'in_right'):
class EditController:
_sessiontype = ba.FreeForAllSession
_sessiontype = bs.FreeForAllSession
def get_session_type(self) -> Type[ba.Session]:
def get_session_type(self) -> Type[bs.Session]:
return self._sessiontype
self._editcontroller = EditController()
self._r = 'addGameWindow'
uiscale = ba.app.ui.uiscale
self._width = 750 if uiscale is ba.UIScale.SMALL else 650
x_inset = 50 if uiscale is ba.UIScale.SMALL else 0
self._height = (346 if uiscale is ba.UIScale.SMALL else
380 if uiscale is ba.UIScale.MEDIUM else 440)
top_extra = 30 if uiscale is ba.UIScale.SMALL else 20
uiscale = bui.app.ui_v1.uiscale
self._width = 750 if uiscale is babase.UIScale.SMALL else 650
x_inset = 50 if uiscale is babase.UIScale.SMALL else 0
self._height = (346 if uiscale is babase.UIScale.SMALL else
380 if uiscale is babase.UIScale.MEDIUM else 440)
top_extra = 30 if uiscale is babase.UIScale.SMALL else 20
self._scroll_width = 210
self._root_widget = ba.containerwidget(
self._root_widget = bui.containerwidget(
size=(self._width, self._height + top_extra),
transition=transition,
scale=(2.17 if uiscale is ba.UIScale.SMALL else
1.5 if uiscale is ba.UIScale.MEDIUM else 1.0),
stack_offset=(0, 1) if uiscale is ba.UIScale.SMALL else (0, 0))
scale=(2.17 if uiscale is babase.UIScale.SMALL else
1.5 if uiscale is babase.UIScale.MEDIUM else 1.0),
stack_offset=(0, 1) if uiscale is babase.UIScale.SMALL else (0, 0))
self._back_button = ba.buttonwidget(parent=self._root_widget,
self._back_button = bui.buttonwidget(parent=self._root_widget,
position=(58 + x_inset,
self._height - 53),
size=(165, 70),
scale=0.75,
text_scale=1.2,
label=ba.Lstr(resource='backText'),
label=babase.Lstr(resource='backText'),
autoselect=True,
button_type='back',
on_activate_call=self._back)
self._select_button = select_button = ba.buttonwidget(
self._select_button = select_button = bui.buttonwidget(
parent=self._root_widget,
position=(self._width - (172 + x_inset), self._height - 50),
autoselect=True,
size=(160, 60),
scale=0.75,
text_scale=1.2,
label=ba.Lstr(resource='selectText'),
label=babase.Lstr(resource='selectText'),
on_activate_call=self._add)
if ba.app.ui.use_toolbars:
ba.widget(edit=select_button,
right_widget=_ba.get_special_widget('party_button'))
if bui.app.ui_v1.use_toolbars:
bui.widget(edit=select_button,
right_widget=bui.get_special_widget('party_button'))
ba.textwidget(parent=self._root_widget,
bui.textwidget(parent=self._root_widget,
position=(self._width * 0.5, self._height - 28),
size=(0, 0),
scale=1.0,
text=ba.Lstr(resource=self._r + '.titleText'),
text=babase.Lstr(resource=self._r + '.titleText'),
h_align='center',
color=ba.app.ui.title_color,
color=bui.app.ui_v1.title_color,
maxwidth=250,
v_align='center')
v = self._height - 64
self._selected_title_text = ba.textwidget(
self._selected_title_text = bui.textwidget(
parent=self._root_widget,
position=(x_inset + self._scroll_width + 50 + 30, v - 15),
size=(0, 0),
@ -177,7 +180,7 @@ class SelectGameWindow(PlaylistAddGameWindow):
v_align='center')
v -= 30
self._selected_description_text = ba.textwidget(
self._selected_description_text = bui.textwidget(
parent=self._root_widget,
position=(x_inset + self._scroll_width + 50 + 30, v),
size=(0, 0),
@ -190,31 +193,31 @@ class SelectGameWindow(PlaylistAddGameWindow):
v = self._height - 60
self._scrollwidget = ba.scrollwidget(parent=self._root_widget,
self._scrollwidget = bui.scrollwidget(parent=self._root_widget,
position=(x_inset + 61,
v - scroll_height),
size=(self._scroll_width,
scroll_height),
highlight=False)
ba.widget(edit=self._scrollwidget,
bui.widget(edit=self._scrollwidget,
up_widget=self._back_button,
left_widget=self._back_button,
right_widget=select_button)
self._column: Optional[ba.Widget] = None
self._column: Optional[bui.Widget] = None
v -= 35
ba.containerwidget(edit=self._root_widget,
bui.containerwidget(edit=self._root_widget,
cancel_button=self._back_button,
start_button=select_button)
self._selected_game_type: Optional[Type[ba.GameActivity]] = None
self._selected_game_type: Optional[Type[bs.GameActivity]] = None
ba.containerwidget(edit=self._root_widget,
bui.containerwidget(edit=self._root_widget,
selected_child=self._scrollwidget)
self._game_types: list[type[ba.GameActivity]] = []
self._game_types: list[type[bs.GameActivity]] = []
# Get actual games loading in the bg.
ba.app.meta.load_exported_classes(ba.GameActivity,
babase.app.meta.load_exported_classes(bs.GameActivity,
self._on_game_types_loaded,
completion_cb_in_bg_thread=True)
@ -231,12 +234,12 @@ class SelectGameWindow(PlaylistAddGameWindow):
def _refresh(self,
select_get_more_games_button: bool = False,
selected: bool = None) -> None:
# from ba.internal import get_game_types
# from babase.internal import get_game_types
if self._column is not None:
self._column.delete()
self._column = ba.columnwidget(parent=self._scrollwidget,
self._column = bui.columnwidget(parent=self._scrollwidget,
border=2,
margin=0)
@ -244,11 +247,10 @@ class SelectGameWindow(PlaylistAddGameWindow):
def _doit() -> None:
if self._select_button:
ba.timer(0.1,
self._select_button.activate,
timetype=ba.TimeType.REAL)
bs.apptimer(0.1,
self._select_button.activate)
txt = ba.textwidget(parent=self._column,
txt = bui.textwidget(parent=self._column,
position=(0, 0),
size=(self._width - 88, 24),
text=gametype.get_display_string(),
@ -256,30 +258,30 @@ class SelectGameWindow(PlaylistAddGameWindow):
v_align='center',
color=(0.8, 0.8, 0.8, 1.0),
maxwidth=self._scroll_width * 0.8,
on_select_call=ba.Call(
on_select_call=babase.Call(
self._set_selected_game_type, gametype),
always_highlight=True,
selectable=True,
on_activate_call=_doit)
if i == 0:
ba.widget(edit=txt, up_widget=self._back_button)
bui.widget(edit=txt, up_widget=self._back_button)
self._get_more_games_button = ba.buttonwidget(
self._get_more_games_button = bui.buttonwidget(
parent=self._column,
autoselect=True,
label=ba.Lstr(resource=self._r + '.getMoreGamesText'),
label=babase.Lstr(resource=self._r + '.getMoreGamesText'),
color=(0.54, 0.52, 0.67),
textcolor=(0.7, 0.65, 0.7),
on_activate_call=self._on_get_more_games_press,
size=(178, 50))
if select_get_more_games_button:
ba.containerwidget(edit=self._column,
bui.containerwidget(edit=self._column,
selected_child=self._get_more_games_button,
visible_child=self._get_more_games_button)
def _add(self) -> None:
_ba.lock_all_input() # Make sure no more commands happen.
ba.timer(0.1, _ba.unlock_all_input, timetype=ba.TimeType.REAL)
_babase.lock_all_input() # Make sure no more commands happen.
bs.apptimer(0.1, _babase.unlock_all_input)
gameconfig = {}
if config['selected'] == self._selected_game_type.__name__:
if config['config']:
@ -297,13 +299,13 @@ class SelectGameWindow(PlaylistAddGameWindow):
CustomSession._gametype = self._selected_game_type
start_game(CustomSession)
else:
ba.app.ui.clear_main_menu_window(transition='out_right')
ba.app.ui.set_main_menu_window(
bui.app.ui_v1.clear_main_menu_window(transition='out_right')
bui.app.ui_v1.set_main_menu_window(
SelectGameWindow(transition='in_left').get_root_widget())
def _back(self) -> None:
ba.containerwidget(edit=self._root_widget, transition='out_right')
ba.app.ui.set_main_menu_window(
bui.containerwidget(edit=self._root_widget, transition='out_right')
bui.app.ui_v1.set_main_menu_window(
PlayWindow(transition='in_left').get_root_widget())
@ -318,11 +320,11 @@ def __init__(self, *args, **kwargs):
def do_quick_game() -> None:
self._save_state()
ba.containerwidget(edit=self._root_widget, transition='out_left')
ba.app.ui.set_main_menu_window(
bui.containerwidget(edit=self._root_widget, transition='out_left')
bui.app.ui_v1.set_main_menu_window(
SelectGameWindow().get_root_widget())
self._quick_game_button = ba.buttonwidget(
self._quick_game_button = bui.buttonwidget(
parent=self._root_widget,
position=(width - 55 - 120, height - 132),
autoselect=True,
@ -350,32 +352,32 @@ def states(self) -> None:
def _save_state(self) -> None:
swapped = {v: k for k, v in states(self).items()}
if self._root_widget.get_selected_child() in swapped:
ba.app.ui.window_states[
bui.app.ui_v1.window_states[
self.__class__.__name__] = swapped[
self._root_widget.get_selected_child()]
else:
ba.print_exception(f'Error saving state for {self}.')
babase.print_exception(f'Error saving state for {self}.')
def _restore_state(self) -> None:
if not hasattr(self, '_quick_game_button'):
return # ensure that our monkey patched init ran
if self.__class__.__name__ not in ba.app.ui.window_states:
ba.containerwidget(edit=self._root_widget,
if self.__class__.__name__ not in bui.app.ui_v1.window_states:
bui.containerwidget(edit=self._root_widget,
selected_child=self._coop_button)
return
sel = states(self).get(
ba.app.ui.window_states[self.__class__.__name__], None)
bui.app.ui_v1.window_states[self.__class__.__name__], None)
if sel:
ba.containerwidget(edit=self._root_widget, selected_child=sel)
bui.containerwidget(edit=self._root_widget, selected_child=sel)
else:
ba.containerwidget(edit=self._root_widget,
bui.containerwidget(edit=self._root_widget,
selected_child=self._coop_button)
ba.print_exception(f'Error restoring state for {self}.')
babase.print_exception(f'Error restoring state for {self}.')
# ba_meta export plugin
class QuickGamePlugin(ba.Plugin):
class QuickGamePlugin(babase.Plugin):
PlayWindow.__init__ = __init__
PlayWindow._save_state = _save_state
PlayWindow._restore_state = _restore_state