bastd.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
  8
  9import ba
 10
 11if TYPE_CHECKING:
 12    from typing import Any
 13
 14
 15class TipsText(ba.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 = ba.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 = ba.Lstr(
 37            value='${A}:', subs=[('${A}', ba.Lstr(resource='tipText'))]
 38        )
 39        self.title_node = ba.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 = ba.Timer(
 55            0.001 * (self._message_duration + self._message_spacing),
 56            ba.WeakCall(self.change_phrase),
 57            repeat=True,
 58        )
 59        self._combine = ba.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 ba.internal import get_remote_app_name, get_next_tip
 71
 72        next_tip = ba.Lstr(
 73            translate=('tips', get_next_tip()),
 74            subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
 75        )
 76        spc = self._message_spacing
 77        assert self.node
 78        self.node.position = (-200, self._offs_y)
 79        self.title_node.position = (-220, self._offs_y + 3)
 80        keys = {
 81            spc: 0,
 82            spc + 1000: 1.0,
 83            spc + self._message_duration - 1000: 1.0,
 84            spc + self._message_duration: 0.0,
 85        }
 86        ba.animate(
 87            self._combine,
 88            'input3',
 89            {k: v * 0.5 for k, v in list(keys.items())},
 90            timeformat=ba.TimeFormat.MILLISECONDS,
 91        )
 92        self.node.text = next_tip
 93
 94    def handlemessage(self, msg: Any) -> Any:
 95        assert not self.expired
 96        if isinstance(msg, ba.DieMessage):
 97            if self.node:
 98                self.node.delete()
 99            self.title_node.delete()
100            return None
101        return super().handlemessage(msg)
class TipsText(ba._actor.Actor):
 16class TipsText(ba.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 = ba.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 = ba.Lstr(
 38            value='${A}:', subs=[('${A}', ba.Lstr(resource='tipText'))]
 39        )
 40        self.title_node = ba.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 = ba.Timer(
 56            0.001 * (self._message_duration + self._message_spacing),
 57            ba.WeakCall(self.change_phrase),
 58            repeat=True,
 59        )
 60        self._combine = ba.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 ba.internal import get_remote_app_name, get_next_tip
 72
 73        next_tip = ba.Lstr(
 74            translate=('tips', get_next_tip()),
 75            subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
 76        )
 77        spc = self._message_spacing
 78        assert self.node
 79        self.node.position = (-200, self._offs_y)
 80        self.title_node.position = (-220, self._offs_y + 3)
 81        keys = {
 82            spc: 0,
 83            spc + 1000: 1.0,
 84            spc + self._message_duration - 1000: 1.0,
 85            spc + self._message_duration: 0.0,
 86        }
 87        ba.animate(
 88            self._combine,
 89            'input3',
 90            {k: v * 0.5 for k, v in list(keys.items())},
 91            timeformat=ba.TimeFormat.MILLISECONDS,
 92        )
 93        self.node.text = next_tip
 94
 95    def handlemessage(self, msg: Any) -> Any:
 96        assert not self.expired
 97        if isinstance(msg, ba.DieMessage):
 98            if self.node:
 99                self.node.delete()
100            self.title_node.delete()
101            return None
102        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 = ba.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 = ba.Lstr(
38            value='${A}:', subs=[('${A}', ba.Lstr(resource='tipText'))]
39        )
40        self.title_node = ba.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 = ba.Timer(
56            0.001 * (self._message_duration + self._message_spacing),
57            ba.WeakCall(self.change_phrase),
58            repeat=True,
59        )
60        self._combine = ba.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 ba.Activity.

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

Switch the visible tip phrase.

def handlemessage(self, msg: Any) -> Any:
 95    def handlemessage(self, msg: Any) -> Any:
 96        assert not self.expired
 97        if isinstance(msg, ba.DieMessage):
 98            if self.node:
 99                self.node.delete()
100            self.title_node.delete()
101            return None
102        return super().handlemessage(msg)

General message handling; can be passed any message object.

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