bascenev1lib.mainmenu

Session and Activity for displaying the main menu bg.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Session and Activity for displaying the main menu bg."""
  4
  5from __future__ import annotations
  6
  7import time
  8import random
  9import weakref
 10from typing import TYPE_CHECKING, override
 11
 12import bascenev1 as bs
 13import bauiv1 as bui
 14
 15if TYPE_CHECKING:
 16    from typing import Any
 17
 18
 19class MainMenuActivity(bs.Activity[bs.Player, bs.Team]):
 20    """Activity showing the rotating main menu bg stuff."""
 21
 22    _stdassets = bs.Dependency(bs.AssetPackage, 'stdassets@1')
 23
 24    def __init__(self, settings: dict):
 25        super().__init__(settings)
 26        self._logo_node: bs.Node | None = None
 27        self._custom_logo_tex_name: str | None = None
 28        self._word_actors: list[bs.Actor] = []
 29        self.my_name: bs.NodeActor | None = None
 30        self._host_is_navigating_text: bs.NodeActor | None = None
 31        self.version: bs.NodeActor | None = None
 32        self.beta_info: bs.NodeActor | None = None
 33        self.beta_info_2: bs.NodeActor | None = None
 34        self.bottom: bs.NodeActor | None = None
 35        self.vr_bottom_fill: bs.NodeActor | None = None
 36        self.vr_top_fill: bs.NodeActor | None = None
 37        self.terrain: bs.NodeActor | None = None
 38        self.trees: bs.NodeActor | None = None
 39        self.bgterrain: bs.NodeActor | None = None
 40        self._ts = 0.86
 41        self._language: str | None = None
 42        self._update_timer: bs.Timer | None = None
 43        self._news: NewsDisplay | None = None
 44        self._attract_mode_timer: bs.Timer | None = None
 45        self._logo_rotate_timer: bs.Timer | None = None
 46
 47    @override
 48    def on_transition_in(self) -> None:
 49        # pylint: disable=too-many-locals
 50        # pylint: disable=too-many-statements
 51        super().on_transition_in()
 52        random.seed(123)
 53        app = bs.app
 54        env = app.env
 55        assert app.classic is not None
 56
 57        plus = bs.app.plus
 58        assert plus is not None
 59
 60        # Throw up some text that only clients can see so they know that
 61        # the host is navigating menus while they're just staring at an
 62        # empty-ish screen.
 63        tval = bs.Lstr(
 64            resource='hostIsNavigatingMenusText',
 65            subs=[('${HOST}', plus.get_v1_account_display_string())],
 66        )
 67        self._host_is_navigating_text = bs.NodeActor(
 68            bs.newnode(
 69                'text',
 70                attrs={
 71                    'text': tval,
 72                    'client_only': True,
 73                    'position': (0, -200),
 74                    'flatness': 1.0,
 75                    'h_align': 'center',
 76                },
 77            )
 78        )
 79        if (
 80            not app.classic.main_menu_did_initial_transition
 81            and self.my_name is not None
 82        ):
 83            assert self.my_name.node
 84            bs.animate(self.my_name.node, 'opacity', {2.3: 0, 3.0: 1.0})
 85
 86        # Throw in test build info.
 87        self.beta_info = self.beta_info_2 = None
 88        if env.test:
 89            pos = (230, -5)
 90            self.beta_info = bs.NodeActor(
 91                bs.newnode(
 92                    'text',
 93                    attrs={
 94                        'v_attach': 'center',
 95                        'h_align': 'center',
 96                        'color': (1, 1, 1, 1),
 97                        'shadow': 0.5,
 98                        'flatness': 0.5,
 99                        'scale': 1,
100                        'vr_depth': -60,
101                        'position': pos,
102                        'text': bs.Lstr(resource='testBuildText'),
103                    },
104                )
105            )
106            if not app.classic.main_menu_did_initial_transition:
107                assert self.beta_info.node
108                bs.animate(self.beta_info.node, 'opacity', {1.3: 0, 1.8: 1.0})
109
110        mesh = bs.getmesh('thePadLevel')
111        trees_mesh = bs.getmesh('trees')
112        bottom_mesh = bs.getmesh('thePadLevelBottom')
113        color_texture = bs.gettexture('thePadLevelColor')
114        trees_texture = bs.gettexture('treesColor')
115        bgtex = bs.gettexture('menuBG')
116        bgmesh = bs.getmesh('thePadBG')
117
118        # Load these last since most platforms don't use them.
119        vr_bottom_fill_mesh = bs.getmesh('thePadVRFillBottom')
120        vr_top_fill_mesh = bs.getmesh('thePadVRFillTop')
121
122        gnode = self.globalsnode
123        gnode.camera_mode = 'rotate'
124
125        tint = (1.14, 1.1, 1.0)
126        gnode.tint = tint
127        gnode.ambient_color = (1.06, 1.04, 1.03)
128        gnode.vignette_outer = (0.45, 0.55, 0.54)
129        gnode.vignette_inner = (0.99, 0.98, 0.98)
130
131        self.bottom = bs.NodeActor(
132            bs.newnode(
133                'terrain',
134                attrs={
135                    'mesh': bottom_mesh,
136                    'lighting': False,
137                    'reflection': 'soft',
138                    'reflection_scale': [0.45],
139                    'color_texture': color_texture,
140                },
141            )
142        )
143        self.vr_bottom_fill = bs.NodeActor(
144            bs.newnode(
145                'terrain',
146                attrs={
147                    'mesh': vr_bottom_fill_mesh,
148                    'lighting': False,
149                    'vr_only': True,
150                    'color_texture': color_texture,
151                },
152            )
153        )
154        self.vr_top_fill = bs.NodeActor(
155            bs.newnode(
156                'terrain',
157                attrs={
158                    'mesh': vr_top_fill_mesh,
159                    'vr_only': True,
160                    'lighting': False,
161                    'color_texture': bgtex,
162                },
163            )
164        )
165        self.terrain = bs.NodeActor(
166            bs.newnode(
167                'terrain',
168                attrs={
169                    'mesh': mesh,
170                    'color_texture': color_texture,
171                    'reflection': 'soft',
172                    'reflection_scale': [0.3],
173                },
174            )
175        )
176        self.trees = bs.NodeActor(
177            bs.newnode(
178                'terrain',
179                attrs={
180                    'mesh': trees_mesh,
181                    'lighting': False,
182                    'reflection': 'char',
183                    'reflection_scale': [0.1],
184                    'color_texture': trees_texture,
185                },
186            )
187        )
188        self.bgterrain = bs.NodeActor(
189            bs.newnode(
190                'terrain',
191                attrs={
192                    'mesh': bgmesh,
193                    'color': (0.92, 0.91, 0.9),
194                    'lighting': False,
195                    'background': True,
196                    'color_texture': bgtex,
197                },
198            )
199        )
200
201        self._update_timer = bs.Timer(1.0, self._update, repeat=True)
202        self._update()
203
204        # Hopefully this won't hitch but lets space these out anyway.
205        bs.add_clean_frame_callback(bs.WeakCall(self._start_preloads))
206
207        random.seed()
208
209        # Need to update this for toolbar mode; currenly doesn't fit.
210        if bool(False):
211            if not (env.demo or env.arcade):
212                self._news = NewsDisplay(self)
213
214        self._attract_mode_timer = bs.Timer(
215            3.12, self._update_attract_mode, repeat=True
216        )
217
218        app.classic.invoke_main_menu_ui()
219
220        app.classic.main_menu_did_initial_transition = True
221
222    def _update(self) -> None:
223        # pylint: disable=too-many-locals
224        # pylint: disable=too-many-statements
225        app = bs.app
226        env = app.env
227        assert app.classic is not None
228
229        # Update logo in case it changes.
230        if self._logo_node:
231            custom_texture = self._get_custom_logo_tex_name()
232            if custom_texture != self._custom_logo_tex_name:
233                self._custom_logo_tex_name = custom_texture
234                self._logo_node.texture = bs.gettexture(
235                    custom_texture if custom_texture is not None else 'logo'
236                )
237                self._logo_node.mesh_opaque = (
238                    None if custom_texture is not None else bs.getmesh('logo')
239                )
240                self._logo_node.mesh_transparent = (
241                    None
242                    if custom_texture is not None
243                    else bs.getmesh('logoTransparent')
244                )
245
246        # If language has changed, recreate our logo text/graphics.
247        lang = app.lang.language
248        if lang != self._language:
249            self._language = lang
250            y = -15
251            base_scale = 1.1
252            self._word_actors = []
253            base_delay = 0.8
254            delay = base_delay
255            delay_inc = 0.02
256
257            # Come on faster after the first time.
258            if app.classic.main_menu_did_initial_transition:
259                base_delay = 0.0
260                delay = base_delay
261                delay_inc = 0.02
262
263            # We draw higher in kiosk mode (make sure to test this
264            # when making adjustments) for now we're hard-coded for
265            # a few languages.. should maybe look into generalizing this?..
266            if app.lang.language == 'Chinese':
267                base_x = -270.0
268                x = base_x - 20.0
269                spacing = 85.0 * base_scale
270                y_extra = 0.0 if (env.demo or env.arcade) else 0.0
271                self._make_logo(
272                    x - 110 + 50,
273                    113 + y + 1.2 * y_extra,
274                    0.34 * base_scale,
275                    delay=base_delay + 0.1,
276                    custom_texture='chTitleChar1',
277                    jitter_scale=2.0,
278                    vr_depth_offset=-30,
279                )
280                x += spacing
281                delay += delay_inc
282                self._make_logo(
283                    x - 10 + 50,
284                    110 + y + 1.2 * y_extra,
285                    0.31 * base_scale,
286                    delay=base_delay + 0.15,
287                    custom_texture='chTitleChar2',
288                    jitter_scale=2.0,
289                    vr_depth_offset=-30,
290                )
291                x += 2.0 * spacing
292                delay += delay_inc
293                self._make_logo(
294                    x + 180 - 140,
295                    110 + y + 1.2 * y_extra,
296                    0.3 * base_scale,
297                    delay=base_delay + 0.25,
298                    custom_texture='chTitleChar3',
299                    jitter_scale=2.0,
300                    vr_depth_offset=-30,
301                )
302                x += spacing
303                delay += delay_inc
304                self._make_logo(
305                    x + 241 - 120,
306                    110 + y + 1.2 * y_extra,
307                    0.31 * base_scale,
308                    delay=base_delay + 0.3,
309                    custom_texture='chTitleChar4',
310                    jitter_scale=2.0,
311                    vr_depth_offset=-30,
312                )
313                x += spacing
314                delay += delay_inc
315                self._make_logo(
316                    x + 300 - 90,
317                    105 + y + 1.2 * y_extra,
318                    0.34 * base_scale,
319                    delay=base_delay + 0.35,
320                    custom_texture='chTitleChar5',
321                    jitter_scale=2.0,
322                    vr_depth_offset=-30,
323                )
324                self._make_logo(
325                    base_x + 155,
326                    146 + y + 1.2 * y_extra,
327                    0.28 * base_scale,
328                    delay=base_delay + 0.2,
329                    rotate=-7,
330                )
331            else:
332                base_x = -170
333                x = base_x - 20
334                spacing = 55 * base_scale
335                y_extra = 0 if (env.demo or env.arcade) else 0
336                xv1 = x
337                delay1 = delay
338                for shadow in (True, False):
339                    x = xv1
340                    delay = delay1
341                    self._make_word(
342                        'B',
343                        x - 50,
344                        y - 14 + 0.8 * y_extra,
345                        scale=1.3 * base_scale,
346                        delay=delay,
347                        vr_depth_offset=3,
348                        shadow=shadow,
349                    )
350                    x += spacing
351                    delay += delay_inc
352                    self._make_word(
353                        'm',
354                        x,
355                        y + y_extra,
356                        delay=delay,
357                        scale=base_scale,
358                        shadow=shadow,
359                    )
360                    x += spacing * 1.25
361                    delay += delay_inc
362                    self._make_word(
363                        'b',
364                        x,
365                        y + y_extra - 10,
366                        delay=delay,
367                        scale=1.1 * base_scale,
368                        vr_depth_offset=5,
369                        shadow=shadow,
370                    )
371                    x += spacing * 0.85
372                    delay += delay_inc
373                    self._make_word(
374                        'S',
375                        x,
376                        y - 15 + 0.8 * y_extra,
377                        scale=1.35 * base_scale,
378                        delay=delay,
379                        vr_depth_offset=14,
380                        shadow=shadow,
381                    )
382                    x += spacing
383                    delay += delay_inc
384                    self._make_word(
385                        'q',
386                        x,
387                        y + y_extra,
388                        delay=delay,
389                        scale=base_scale,
390                        shadow=shadow,
391                    )
392                    x += spacing * 0.9
393                    delay += delay_inc
394                    self._make_word(
395                        'u',
396                        x,
397                        y + y_extra,
398                        delay=delay,
399                        scale=base_scale,
400                        vr_depth_offset=7,
401                        shadow=shadow,
402                    )
403                    x += spacing * 0.9
404                    delay += delay_inc
405                    self._make_word(
406                        'a',
407                        x,
408                        y + y_extra,
409                        delay=delay,
410                        scale=base_scale,
411                        shadow=shadow,
412                    )
413                    x += spacing * 0.64
414                    delay += delay_inc
415                    self._make_word(
416                        'd',
417                        x,
418                        y + y_extra - 10,
419                        delay=delay,
420                        scale=1.1 * base_scale,
421                        vr_depth_offset=6,
422                        shadow=shadow,
423                    )
424                self._make_logo(
425                    base_x - 28,
426                    125 + y + 1.2 * y_extra,
427                    0.32 * base_scale,
428                    delay=base_delay,
429                )
430
431    def _make_word(
432        self,
433        word: str,
434        x: float,
435        y: float,
436        *,
437        scale: float = 1.0,
438        delay: float = 0.0,
439        vr_depth_offset: float = 0.0,
440        shadow: bool = False,
441    ) -> None:
442        # pylint: disable=too-many-branches
443        # pylint: disable=too-many-locals
444        # pylint: disable=too-many-statements
445        if shadow:
446            word_obj = bs.NodeActor(
447                bs.newnode(
448                    'text',
449                    attrs={
450                        'position': (x, y),
451                        'big': True,
452                        'color': (0.0, 0.0, 0.2, 0.08),
453                        'tilt_translate': 0.09,
454                        'opacity_scales_shadow': False,
455                        'shadow': 0.2,
456                        'vr_depth': -130,
457                        'v_align': 'center',
458                        'project_scale': 0.97 * scale,
459                        'scale': 1.0,
460                        'text': word,
461                    },
462                )
463            )
464            self._word_actors.append(word_obj)
465        else:
466            word_obj = bs.NodeActor(
467                bs.newnode(
468                    'text',
469                    attrs={
470                        'position': (x, y),
471                        'big': True,
472                        'color': (1.2, 1.15, 1.15, 1.0),
473                        'tilt_translate': 0.11,
474                        'shadow': 0.2,
475                        'vr_depth': -40 + vr_depth_offset,
476                        'v_align': 'center',
477                        'project_scale': scale,
478                        'scale': 1.0,
479                        'text': word,
480                    },
481                )
482            )
483            self._word_actors.append(word_obj)
484
485        # Add a bit of stop-motion-y jitter to the logo (unless we're in
486        # VR mode in which case its best to leave things still).
487        if not bs.app.env.vr:
488            cmb: bs.Node | None
489            cmb2: bs.Node | None
490            if not shadow:
491                cmb = bs.newnode(
492                    'combine', owner=word_obj.node, attrs={'size': 2}
493                )
494            else:
495                cmb = None
496            if shadow:
497                cmb2 = bs.newnode(
498                    'combine', owner=word_obj.node, attrs={'size': 2}
499                )
500            else:
501                cmb2 = None
502            if not shadow:
503                assert cmb and word_obj.node
504                cmb.connectattr('output', word_obj.node, 'position')
505            if shadow:
506                assert cmb2 and word_obj.node
507                cmb2.connectattr('output', word_obj.node, 'position')
508            keys = {}
509            keys2 = {}
510            time_v = 0.0
511            for _i in range(10):
512                val = x + (random.random() - 0.5) * 0.8
513                val2 = x + (random.random() - 0.5) * 0.8
514                keys[time_v * self._ts] = val
515                keys2[time_v * self._ts] = val2 + 5
516                time_v += random.random() * 0.1
517            if cmb is not None:
518                bs.animate(cmb, 'input0', keys, loop=True)
519            if cmb2 is not None:
520                bs.animate(cmb2, 'input0', keys2, loop=True)
521            keys = {}
522            keys2 = {}
523            time_v = 0
524            for _i in range(10):
525                val = y + (random.random() - 0.5) * 0.8
526                val2 = y + (random.random() - 0.5) * 0.8
527                keys[time_v * self._ts] = val
528                keys2[time_v * self._ts] = val2 - 9
529                time_v += random.random() * 0.1
530            if cmb is not None:
531                bs.animate(cmb, 'input1', keys, loop=True)
532            if cmb2 is not None:
533                bs.animate(cmb2, 'input1', keys2, loop=True)
534
535        if not shadow:
536            assert word_obj.node
537            bs.animate(
538                word_obj.node,
539                'project_scale',
540                {delay: 0.0, delay + 0.1: scale * 1.1, delay + 0.2: scale},
541            )
542        else:
543            assert word_obj.node
544            bs.animate(
545                word_obj.node,
546                'project_scale',
547                {delay: 0.0, delay + 0.1: scale * 1.1, delay + 0.2: scale},
548            )
549
550    def _get_custom_logo_tex_name(self) -> str | None:
551        plus = bui.app.plus
552        assert plus is not None
553
554        if plus.get_v1_account_misc_read_val('easter', False):
555            return 'logoEaster'
556        return None
557
558    # Pop the logo and menu in.
559    def _make_logo(
560        self,
561        x: float,
562        y: float,
563        scale: float,
564        delay: float,
565        *,
566        custom_texture: str | None = None,
567        jitter_scale: float = 1.0,
568        rotate: float = 0.0,
569        vr_depth_offset: float = 0.0,
570    ) -> None:
571        # pylint: disable=too-many-locals
572        if custom_texture is None:
573            custom_texture = self._get_custom_logo_tex_name()
574        self._custom_logo_tex_name = custom_texture
575        ltex = bs.gettexture(
576            custom_texture if custom_texture is not None else 'logo'
577        )
578        mopaque = None if custom_texture is not None else bs.getmesh('logo')
579        mtrans = (
580            None
581            if custom_texture is not None
582            else bs.getmesh('logoTransparent')
583        )
584        logo_attrs = {
585            'position': (x, y),
586            'texture': ltex,
587            'mesh_opaque': mopaque,
588            'mesh_transparent': mtrans,
589            'vr_depth': -10 + vr_depth_offset,
590            'rotate': rotate,
591            'attach': 'center',
592            'tilt_translate': 0.21,
593            'absolute_scale': True,
594        }
595        if custom_texture is None:
596            logo_attrs['scale'] = (2000.0, 2000.0)
597        logo = bs.NodeActor(bs.newnode('image', attrs=logo_attrs))
598        self._logo_node = logo.node
599        self._word_actors.append(logo)
600
601        # Add a bit of stop-motion-y jitter to the logo (unless we're in
602        # VR mode in which case its best to leave things still).
603        assert logo.node
604
605        def jitter() -> None:
606            if not bs.app.env.vr:
607                cmb = bs.newnode('combine', owner=logo.node, attrs={'size': 2})
608                cmb.connectattr('output', logo.node, 'position')
609                keys = {}
610                time_v = 0.0
611
612                # Gen some random keys for that stop-motion-y look
613                for _i in range(10):
614                    keys[time_v] = (
615                        x + (random.random() - 0.5) * 0.7 * jitter_scale
616                    )
617                    time_v += random.random() * 0.1
618                bs.animate(cmb, 'input0', keys, loop=True)
619                keys = {}
620                time_v = 0.0
621                for _i in range(10):
622                    keys[time_v * self._ts] = (
623                        y + (random.random() - 0.5) * 0.7 * jitter_scale
624                    )
625                    time_v += random.random() * 0.1
626                bs.animate(cmb, 'input1', keys, loop=True)
627
628        # Do a fun spinny animation on the logo the first time in.
629        if (
630            custom_texture is None
631            and bs.app.classic is not None
632            and not bs.app.classic.main_menu_did_initial_transition
633        ):
634            jitter()
635            cmb = bs.newnode('combine', owner=logo.node, attrs={'size': 2})
636
637            delay = 0.0
638            keys = {
639                delay: 5000.0 * scale,
640                delay + 0.4: 530.0 * scale,
641                delay + 0.45: 620.0 * scale,
642                delay + 0.5: 590.0 * scale,
643                delay + 0.55: 605.0 * scale,
644                delay + 0.6: 600.0 * scale,
645            }
646            bs.animate(cmb, 'input0', keys)
647            bs.animate(cmb, 'input1', keys)
648            cmb.connectattr('output', logo.node, 'scale')
649
650            keys = {
651                delay: 100.0,
652                delay + 0.4: 370.0,
653                delay + 0.45: 357.0,
654                delay + 0.5: 360.0,
655            }
656            bs.animate(logo.node, 'rotate', keys)
657        else:
658            # For all other cases do a simple scale up animation.
659            jitter()
660            cmb = bs.newnode('combine', owner=logo.node, attrs={'size': 2})
661
662            keys = {
663                delay: 0.0,
664                delay + 0.1: 700.0 * scale,
665                delay + 0.2: 600.0 * scale,
666            }
667            bs.animate(cmb, 'input0', keys)
668            bs.animate(cmb, 'input1', keys)
669            cmb.connectattr('output', logo.node, 'scale')
670
671    def _start_preloads(self) -> None:
672        # FIXME: The func that calls us back doesn't save/restore state
673        #  or check for a dead activity so we have to do that ourself.
674        if self.expired:
675            return
676        with self.context:
677            _preload1()
678
679        def _start_menu_music() -> None:
680            assert bs.app.classic is not None
681            bs.setmusic(bs.MusicType.MENU)
682
683        bui.apptimer(0.5, _start_menu_music)
684
685    def _update_attract_mode(self) -> None:
686        if bui.app.classic is None:
687            return
688
689        if not bui.app.config.resolve('Show Demos When Idle'):
690            return
691
692        threshold = 20.0
693
694        # If we're idle *and* have been in this activity for that long,
695        # flip over to our cpu demo.
696        if bui.get_input_idle_time() > threshold and bs.time() > threshold:
697            bui.app.classic.run_stress_test(
698                playlist_type='Random',
699                playlist_name='__default__',
700                player_count=8,
701                round_duration=20,
702                attract_mode=True,
703            )
704
705
706class NewsDisplay:
707    """Wrangles news display."""
708
709    def __init__(self, activity: bs.Activity):
710        self._valid = True
711        self._message_duration = 10.0
712        self._message_spacing = 2.0
713        self._text: bs.NodeActor | None = None
714        self._activity = weakref.ref(activity)
715        self._phrases: list[str] = []
716        self._used_phrases: list[str] = []
717        self._phrase_change_timer: bs.Timer | None = None
718
719        # If we're signed in, fetch news immediately. Otherwise wait
720        # until we are signed in.
721        self._fetch_timer: bs.Timer | None = bs.Timer(
722            1.0, bs.WeakCall(self._try_fetching_news), repeat=True
723        )
724        self._try_fetching_news()
725
726    # We now want to wait until we're signed in before fetching news.
727    def _try_fetching_news(self) -> None:
728        plus = bui.app.plus
729        assert plus is not None
730
731        if plus.get_v1_account_state() == 'signed_in':
732            self._fetch_news()
733            self._fetch_timer = None
734
735    def _fetch_news(self) -> None:
736        plus = bui.app.plus
737        assert plus is not None
738
739        assert bs.app.classic is not None
740        bs.app.classic.main_menu_last_news_fetch_time = time.time()
741
742        # UPDATE - We now just pull news from MRVs.
743        news = plus.get_v1_account_misc_read_val('n', None)
744        if news is not None:
745            self._got_news(news)
746
747    def _change_phrase(self) -> None:
748        from bascenev1lib.actor.text import Text
749
750        app = bs.app
751        assert app.classic is not None
752
753        # If our news is way out of date, lets re-request it; otherwise,
754        # rotate our phrase.
755        assert app.classic.main_menu_last_news_fetch_time is not None
756        if time.time() - app.classic.main_menu_last_news_fetch_time > 600.0:
757            self._fetch_news()
758            self._text = None
759        else:
760            if self._text is not None:
761                if not self._phrases:
762                    for phr in self._used_phrases:
763                        self._phrases.insert(0, phr)
764                val = self._phrases.pop()
765                if val == '__ACH__':
766                    vrmode = app.env.vr
767                    Text(
768                        bs.Lstr(resource='nextAchievementsText'),
769                        color=((1, 1, 1, 1) if vrmode else (0.95, 0.9, 1, 0.4)),
770                        host_only=True,
771                        maxwidth=200,
772                        position=(-300, -35),
773                        h_align=Text.HAlign.RIGHT,
774                        transition=Text.Transition.FADE_IN,
775                        scale=0.9 if vrmode else 0.7,
776                        flatness=1.0 if vrmode else 0.6,
777                        shadow=1.0 if vrmode else 0.5,
778                        h_attach=Text.HAttach.CENTER,
779                        v_attach=Text.VAttach.TOP,
780                        transition_delay=1.0,
781                        transition_out_delay=self._message_duration,
782                    ).autoretain()
783                    achs = [
784                        a
785                        for a in app.classic.ach.achievements
786                        if not a.complete
787                    ]
788                    if achs:
789                        ach = achs.pop(random.randrange(min(4, len(achs))))
790                        ach.create_display(
791                            -180,
792                            -35,
793                            1.0,
794                            outdelay=self._message_duration,
795                            style='news',
796                        )
797                    if achs:
798                        ach = achs.pop(random.randrange(min(8, len(achs))))
799                        ach.create_display(
800                            180,
801                            -35,
802                            1.25,
803                            outdelay=self._message_duration,
804                            style='news',
805                        )
806                else:
807                    spc = self._message_spacing
808                    keys = {
809                        spc: 0.0,
810                        spc + 1.0: 1.0,
811                        spc + self._message_duration - 1.0: 1.0,
812                        spc + self._message_duration: 0.0,
813                    }
814                    assert self._text.node
815                    bs.animate(self._text.node, 'opacity', keys)
816                    # {k: v
817                    #  for k, v in list(keys.items())})
818                    self._text.node.text = val
819
820    def _got_news(self, news: str) -> None:
821        # Run this stuff in the context of our activity since we need to
822        # make nodes and stuff.. should fix the serverget call so it.
823        activity = self._activity()
824        if activity is None or activity.expired:
825            return
826        with activity.context:
827            self._phrases.clear()
828
829            # Show upcoming achievements in non-vr versions (currently
830            # too hard to read in vr).
831            self._used_phrases = (['__ACH__'] if not bs.app.env.vr else []) + [
832                s for s in news.split('<br>\n') if s != ''
833            ]
834            self._phrase_change_timer = bs.Timer(
835                (self._message_duration + self._message_spacing),
836                bs.WeakCall(self._change_phrase),
837                repeat=True,
838            )
839
840            assert bs.app.classic is not None
841            scl = (
842                1.2
843                if (bs.app.ui_v1.uiscale is bs.UIScale.SMALL or bs.app.env.vr)
844                else 0.8
845            )
846
847            color2 = (1, 1, 1, 1) if bs.app.env.vr else (0.7, 0.65, 0.75, 1.0)
848            shadow = 1.0 if bs.app.env.vr else 0.4
849            self._text = bs.NodeActor(
850                bs.newnode(
851                    'text',
852                    attrs={
853                        'v_attach': 'top',
854                        'h_attach': 'center',
855                        'h_align': 'center',
856                        'vr_depth': -20,
857                        'shadow': shadow,
858                        'flatness': 0.8,
859                        'v_align': 'top',
860                        'color': color2,
861                        'scale': scl,
862                        'maxwidth': 900.0 / scl,
863                        'position': (0, -10),
864                    },
865                )
866            )
867            self._change_phrase()
868
869
870def _preload1() -> None:
871    """Pre-load some assets a second or two into the main menu.
872
873    Helps avoid hitches later on.
874    """
875    for mname in [
876        'plasticEyesTransparent',
877        'playerLineup1Transparent',
878        'playerLineup2Transparent',
879        'playerLineup3Transparent',
880        'playerLineup4Transparent',
881        'angryComputerTransparent',
882        'scrollWidgetShort',
883        'windowBGBlotch',
884    ]:
885        bs.getmesh(mname)
886    for tname in ['playerLineup', 'lock']:
887        bs.gettexture(tname)
888    for tex in [
889        'iconRunaround',
890        'iconOnslaught',
891        'medalComplete',
892        'medalBronze',
893        'medalSilver',
894        'medalGold',
895        'characterIconMask',
896    ]:
897        bs.gettexture(tex)
898    bs.gettexture('bg')
899    from bascenev1lib.actor.powerupbox import PowerupBoxFactory
900
901    PowerupBoxFactory.get()
902    bui.apptimer(0.1, _preload2)
903
904
905def _preload2() -> None:
906    # FIXME: Could integrate these loads with the classes that use them
907    #  so they don't have to redundantly call the load
908    #  (even if the actual result is cached).
909    for mname in ['powerup', 'powerupSimple']:
910        bs.getmesh(mname)
911    for tname in [
912        'powerupBomb',
913        'powerupSpeed',
914        'powerupPunch',
915        'powerupIceBombs',
916        'powerupStickyBombs',
917        'powerupShield',
918        'powerupImpactBombs',
919        'powerupHealth',
920    ]:
921        bs.gettexture(tname)
922    for sname in [
923        'powerup01',
924        'boxDrop',
925        'boxingBell',
926        'scoreHit01',
927        'scoreHit02',
928        'dripity',
929        'spawn',
930        'gong',
931    ]:
932        bs.getsound(sname)
933    from bascenev1lib.actor.bomb import BombFactory
934
935    BombFactory.get()
936    bui.apptimer(0.1, _preload3)
937
938
939def _preload3() -> None:
940    from bascenev1lib.actor.spazfactory import SpazFactory
941
942    for mname in ['bomb', 'bombSticky', 'impactBomb']:
943        bs.getmesh(mname)
944    for tname in [
945        'bombColor',
946        'bombColorIce',
947        'bombStickyColor',
948        'impactBombColor',
949        'impactBombColorLit',
950    ]:
951        bs.gettexture(tname)
952    for sname in ['freeze', 'fuse01', 'activateBeep', 'warnBeep']:
953        bs.getsound(sname)
954    SpazFactory.get()
955    bui.apptimer(0.2, _preload4)
956
957
958def _preload4() -> None:
959    for tname in ['bar', 'meter', 'null', 'flagColor', 'achievementOutline']:
960        bs.gettexture(tname)
961    for mname in ['frameInset', 'meterTransparent', 'achievementOutline']:
962        bs.getmesh(mname)
963    for sname in ['metalHit', 'metalSkid', 'refWhistle', 'achievement']:
964        bs.getsound(sname)
965    from bascenev1lib.actor.flag import FlagFactory
966
967    FlagFactory.get()
968
969
970class MainMenuSession(bs.Session):
971    """Session that runs the main menu environment."""
972
973    def __init__(self) -> None:
974        # Gather dependencies we'll need (just our activity).
975        self._activity_deps = bs.DependencySet(bs.Dependency(MainMenuActivity))
976
977        super().__init__([self._activity_deps])
978        self._locked = False
979        self.setactivity(bs.newactivity(MainMenuActivity))
980
981    @override
982    def on_activity_end(self, activity: bs.Activity, results: Any) -> None:
983        if self._locked:
984            bui.unlock_all_input()
985
986        # Any ending activity leads us into the main menu one.
987        self.setactivity(bs.newactivity(MainMenuActivity))
988
989    @override
990    def on_player_request(self, player: bs.SessionPlayer) -> bool:
991        # Reject all player requests.
992        return False
class NewsDisplay:
707class NewsDisplay:
708    """Wrangles news display."""
709
710    def __init__(self, activity: bs.Activity):
711        self._valid = True
712        self._message_duration = 10.0
713        self._message_spacing = 2.0
714        self._text: bs.NodeActor | None = None
715        self._activity = weakref.ref(activity)
716        self._phrases: list[str] = []
717        self._used_phrases: list[str] = []
718        self._phrase_change_timer: bs.Timer | None = None
719
720        # If we're signed in, fetch news immediately. Otherwise wait
721        # until we are signed in.
722        self._fetch_timer: bs.Timer | None = bs.Timer(
723            1.0, bs.WeakCall(self._try_fetching_news), repeat=True
724        )
725        self._try_fetching_news()
726
727    # We now want to wait until we're signed in before fetching news.
728    def _try_fetching_news(self) -> None:
729        plus = bui.app.plus
730        assert plus is not None
731
732        if plus.get_v1_account_state() == 'signed_in':
733            self._fetch_news()
734            self._fetch_timer = None
735
736    def _fetch_news(self) -> None:
737        plus = bui.app.plus
738        assert plus is not None
739
740        assert bs.app.classic is not None
741        bs.app.classic.main_menu_last_news_fetch_time = time.time()
742
743        # UPDATE - We now just pull news from MRVs.
744        news = plus.get_v1_account_misc_read_val('n', None)
745        if news is not None:
746            self._got_news(news)
747
748    def _change_phrase(self) -> None:
749        from bascenev1lib.actor.text import Text
750
751        app = bs.app
752        assert app.classic is not None
753
754        # If our news is way out of date, lets re-request it; otherwise,
755        # rotate our phrase.
756        assert app.classic.main_menu_last_news_fetch_time is not None
757        if time.time() - app.classic.main_menu_last_news_fetch_time > 600.0:
758            self._fetch_news()
759            self._text = None
760        else:
761            if self._text is not None:
762                if not self._phrases:
763                    for phr in self._used_phrases:
764                        self._phrases.insert(0, phr)
765                val = self._phrases.pop()
766                if val == '__ACH__':
767                    vrmode = app.env.vr
768                    Text(
769                        bs.Lstr(resource='nextAchievementsText'),
770                        color=((1, 1, 1, 1) if vrmode else (0.95, 0.9, 1, 0.4)),
771                        host_only=True,
772                        maxwidth=200,
773                        position=(-300, -35),
774                        h_align=Text.HAlign.RIGHT,
775                        transition=Text.Transition.FADE_IN,
776                        scale=0.9 if vrmode else 0.7,
777                        flatness=1.0 if vrmode else 0.6,
778                        shadow=1.0 if vrmode else 0.5,
779                        h_attach=Text.HAttach.CENTER,
780                        v_attach=Text.VAttach.TOP,
781                        transition_delay=1.0,
782                        transition_out_delay=self._message_duration,
783                    ).autoretain()
784                    achs = [
785                        a
786                        for a in app.classic.ach.achievements
787                        if not a.complete
788                    ]
789                    if achs:
790                        ach = achs.pop(random.randrange(min(4, len(achs))))
791                        ach.create_display(
792                            -180,
793                            -35,
794                            1.0,
795                            outdelay=self._message_duration,
796                            style='news',
797                        )
798                    if achs:
799                        ach = achs.pop(random.randrange(min(8, len(achs))))
800                        ach.create_display(
801                            180,
802                            -35,
803                            1.25,
804                            outdelay=self._message_duration,
805                            style='news',
806                        )
807                else:
808                    spc = self._message_spacing
809                    keys = {
810                        spc: 0.0,
811                        spc + 1.0: 1.0,
812                        spc + self._message_duration - 1.0: 1.0,
813                        spc + self._message_duration: 0.0,
814                    }
815                    assert self._text.node
816                    bs.animate(self._text.node, 'opacity', keys)
817                    # {k: v
818                    #  for k, v in list(keys.items())})
819                    self._text.node.text = val
820
821    def _got_news(self, news: str) -> None:
822        # Run this stuff in the context of our activity since we need to
823        # make nodes and stuff.. should fix the serverget call so it.
824        activity = self._activity()
825        if activity is None or activity.expired:
826            return
827        with activity.context:
828            self._phrases.clear()
829
830            # Show upcoming achievements in non-vr versions (currently
831            # too hard to read in vr).
832            self._used_phrases = (['__ACH__'] if not bs.app.env.vr else []) + [
833                s for s in news.split('<br>\n') if s != ''
834            ]
835            self._phrase_change_timer = bs.Timer(
836                (self._message_duration + self._message_spacing),
837                bs.WeakCall(self._change_phrase),
838                repeat=True,
839            )
840
841            assert bs.app.classic is not None
842            scl = (
843                1.2
844                if (bs.app.ui_v1.uiscale is bs.UIScale.SMALL or bs.app.env.vr)
845                else 0.8
846            )
847
848            color2 = (1, 1, 1, 1) if bs.app.env.vr else (0.7, 0.65, 0.75, 1.0)
849            shadow = 1.0 if bs.app.env.vr else 0.4
850            self._text = bs.NodeActor(
851                bs.newnode(
852                    'text',
853                    attrs={
854                        'v_attach': 'top',
855                        'h_attach': 'center',
856                        'h_align': 'center',
857                        'vr_depth': -20,
858                        'shadow': shadow,
859                        'flatness': 0.8,
860                        'v_align': 'top',
861                        'color': color2,
862                        'scale': scl,
863                        'maxwidth': 900.0 / scl,
864                        'position': (0, -10),
865                    },
866                )
867            )
868            self._change_phrase()

Wrangles news display.

NewsDisplay(activity: bascenev1.Activity)
710    def __init__(self, activity: bs.Activity):
711        self._valid = True
712        self._message_duration = 10.0
713        self._message_spacing = 2.0
714        self._text: bs.NodeActor | None = None
715        self._activity = weakref.ref(activity)
716        self._phrases: list[str] = []
717        self._used_phrases: list[str] = []
718        self._phrase_change_timer: bs.Timer | None = None
719
720        # If we're signed in, fetch news immediately. Otherwise wait
721        # until we are signed in.
722        self._fetch_timer: bs.Timer | None = bs.Timer(
723            1.0, bs.WeakCall(self._try_fetching_news), repeat=True
724        )
725        self._try_fetching_news()