bascenev1lib.actor.tipstext

Provides tip related Actor(s).

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Provides tip related Actor(s)."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING, override
  8
  9import bascenev1 as bs
 10
 11if TYPE_CHECKING:
 12    from typing import Any
 13
 14
 15class TipsText(bs.Actor):
 16    """A bit of text showing various helpful game tips."""
 17
 18    def __init__(self, offs_y: float = 100.0):
 19        super().__init__()
 20        self._tip_scale = 0.8
 21        self._tip_title_scale = 1.1
 22        self._offs_y = offs_y
 23        self.node = bs.newnode(
 24            'text',
 25            delegate=self,
 26            attrs={
 27                'text': '',
 28                'scale': self._tip_scale,
 29                'h_align': 'left',
 30                'maxwidth': 800,
 31                'vr_depth': -20,
 32                'v_align': 'center',
 33                'v_attach': 'bottom',
 34            },
 35        )
 36        tval = bs.Lstr(
 37            value='${A}:', subs=[('${A}', bs.Lstr(resource='tipText'))]
 38        )
 39        self.title_node = bs.newnode(
 40            'text',
 41            delegate=self,
 42            attrs={
 43                'text': tval,
 44                'scale': self._tip_title_scale,
 45                'maxwidth': 122,
 46                'h_align': 'right',
 47                'vr_depth': -20,
 48                'v_align': 'center',
 49                'v_attach': 'bottom',
 50            },
 51        )
 52        self._message_duration = 10000
 53        self._message_spacing = 3000
 54        self._change_timer = bs.Timer(
 55            0.001 * (self._message_duration + self._message_spacing),
 56            bs.WeakCall(self.change_phrase),
 57            repeat=True,
 58        )
 59        self._combine = bs.newnode(
 60            'combine',
 61            owner=self.node,
 62            attrs={'input0': 1.0, 'input1': 0.8, 'input2': 1.0, 'size': 4},
 63        )
 64        self._combine.connectattr('output', self.node, 'color')
 65        self._combine.connectattr('output', self.title_node, 'color')
 66        self.change_phrase()
 67
 68    def change_phrase(self) -> None:
 69        """Switch the visible tip phrase."""
 70        from babase import get_remote_app_name
 71
 72        next_tip = bs.Lstr(
 73            translate=(
 74                'tips',
 75                (
 76                    bs.app.classic.get_next_tip()
 77                    if bs.app.classic is not None
 78                    else ''
 79                ),
 80            ),
 81            subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
 82        )
 83        spc = self._message_spacing
 84        assert self.node
 85        self.node.position = (-200, self._offs_y)
 86        self.title_node.position = (-220, self._offs_y + 3)
 87        keys = {
 88            spc: 0,
 89            spc + 1000: 1.0,
 90            spc + self._message_duration - 1000: 1.0,
 91            spc + self._message_duration: 0.0,
 92        }
 93        bs.animate(
 94            self._combine,
 95            'input3',
 96            {k / 1000.0: v * 0.5 for k, v in list(keys.items())},
 97        )
 98        self.node.text = next_tip
 99
100    @override
101    def handlemessage(self, msg: Any) -> Any:
102        assert not self.expired
103        if isinstance(msg, bs.DieMessage):
104            if self.node:
105                self.node.delete()
106            self.title_node.delete()
107            return None
108        return super().handlemessage(msg)
class TipsText(bascenev1._actor.Actor):
 16class TipsText(bs.Actor):
 17    """A bit of text showing various helpful game tips."""
 18
 19    def __init__(self, offs_y: float = 100.0):
 20        super().__init__()
 21        self._tip_scale = 0.8
 22        self._tip_title_scale = 1.1
 23        self._offs_y = offs_y
 24        self.node = bs.newnode(
 25            'text',
 26            delegate=self,
 27            attrs={
 28                'text': '',
 29                'scale': self._tip_scale,
 30                'h_align': 'left',
 31                'maxwidth': 800,
 32                'vr_depth': -20,
 33                'v_align': 'center',
 34                'v_attach': 'bottom',
 35            },
 36        )
 37        tval = bs.Lstr(
 38            value='${A}:', subs=[('${A}', bs.Lstr(resource='tipText'))]
 39        )
 40        self.title_node = bs.newnode(
 41            'text',
 42            delegate=self,
 43            attrs={
 44                'text': tval,
 45                'scale': self._tip_title_scale,
 46                'maxwidth': 122,
 47                'h_align': 'right',
 48                'vr_depth': -20,
 49                'v_align': 'center',
 50                'v_attach': 'bottom',
 51            },
 52        )
 53        self._message_duration = 10000
 54        self._message_spacing = 3000
 55        self._change_timer = bs.Timer(
 56            0.001 * (self._message_duration + self._message_spacing),
 57            bs.WeakCall(self.change_phrase),
 58            repeat=True,
 59        )
 60        self._combine = bs.newnode(
 61            'combine',
 62            owner=self.node,
 63            attrs={'input0': 1.0, 'input1': 0.8, 'input2': 1.0, 'size': 4},
 64        )
 65        self._combine.connectattr('output', self.node, 'color')
 66        self._combine.connectattr('output', self.title_node, 'color')
 67        self.change_phrase()
 68
 69    def change_phrase(self) -> None:
 70        """Switch the visible tip phrase."""
 71        from babase import get_remote_app_name
 72
 73        next_tip = bs.Lstr(
 74            translate=(
 75                'tips',
 76                (
 77                    bs.app.classic.get_next_tip()
 78                    if bs.app.classic is not None
 79                    else ''
 80                ),
 81            ),
 82            subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
 83        )
 84        spc = self._message_spacing
 85        assert self.node
 86        self.node.position = (-200, self._offs_y)
 87        self.title_node.position = (-220, self._offs_y + 3)
 88        keys = {
 89            spc: 0,
 90            spc + 1000: 1.0,
 91            spc + self._message_duration - 1000: 1.0,
 92            spc + self._message_duration: 0.0,
 93        }
 94        bs.animate(
 95            self._combine,
 96            'input3',
 97            {k / 1000.0: v * 0.5 for k, v in list(keys.items())},
 98        )
 99        self.node.text = next_tip
100
101    @override
102    def handlemessage(self, msg: Any) -> Any:
103        assert not self.expired
104        if isinstance(msg, bs.DieMessage):
105            if self.node:
106                self.node.delete()
107            self.title_node.delete()
108            return None
109        return super().handlemessage(msg)

A bit of text showing various helpful game tips.

TipsText(offs_y: float = 100.0)
19    def __init__(self, offs_y: float = 100.0):
20        super().__init__()
21        self._tip_scale = 0.8
22        self._tip_title_scale = 1.1
23        self._offs_y = offs_y
24        self.node = bs.newnode(
25            'text',
26            delegate=self,
27            attrs={
28                'text': '',
29                'scale': self._tip_scale,
30                'h_align': 'left',
31                'maxwidth': 800,
32                'vr_depth': -20,
33                'v_align': 'center',
34                'v_attach': 'bottom',
35            },
36        )
37        tval = bs.Lstr(
38            value='${A}:', subs=[('${A}', bs.Lstr(resource='tipText'))]
39        )
40        self.title_node = bs.newnode(
41            'text',
42            delegate=self,
43            attrs={
44                'text': tval,
45                'scale': self._tip_title_scale,
46                'maxwidth': 122,
47                'h_align': 'right',
48                'vr_depth': -20,
49                'v_align': 'center',
50                'v_attach': 'bottom',
51            },
52        )
53        self._message_duration = 10000
54        self._message_spacing = 3000
55        self._change_timer = bs.Timer(
56            0.001 * (self._message_duration + self._message_spacing),
57            bs.WeakCall(self.change_phrase),
58            repeat=True,
59        )
60        self._combine = bs.newnode(
61            'combine',
62            owner=self.node,
63            attrs={'input0': 1.0, 'input1': 0.8, 'input2': 1.0, 'size': 4},
64        )
65        self._combine.connectattr('output', self.node, 'color')
66        self._combine.connectattr('output', self.title_node, 'color')
67        self.change_phrase()

Instantiates an Actor in the current bascenev1.Activity.

node
title_node
def change_phrase(self) -> None:
69    def change_phrase(self) -> None:
70        """Switch the visible tip phrase."""
71        from babase import get_remote_app_name
72
73        next_tip = bs.Lstr(
74            translate=(
75                'tips',
76                (
77                    bs.app.classic.get_next_tip()
78                    if bs.app.classic is not None
79                    else ''
80                ),
81            ),
82            subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
83        )
84        spc = self._message_spacing
85        assert self.node
86        self.node.position = (-200, self._offs_y)
87        self.title_node.position = (-220, self._offs_y + 3)
88        keys = {
89            spc: 0,
90            spc + 1000: 1.0,
91            spc + self._message_duration - 1000: 1.0,
92            spc + self._message_duration: 0.0,
93        }
94        bs.animate(
95            self._combine,
96            'input3',
97            {k / 1000.0: v * 0.5 for k, v in list(keys.items())},
98        )
99        self.node.text = next_tip

Switch the visible tip phrase.

@override
def handlemessage(self, msg: Any) -> Any:
101    @override
102    def handlemessage(self, msg: Any) -> Any:
103        assert not self.expired
104        if isinstance(msg, bs.DieMessage):
105            if self.node:
106                self.node.delete()
107            self.title_node.delete()
108            return None
109        return super().handlemessage(msg)

General message handling; can be passed any message object.

Inherited Members
bascenev1._actor.Actor
autoretain
on_expire
expired
exists
is_alive
activity
getactivity