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('text',
24                               delegate=self,
25                               attrs={
26                                   'text': '',
27                                   'scale': self._tip_scale,
28                                   'h_align': 'left',
29                                   'maxwidth': 800,
30                                   'vr_depth': -20,
31                                   'v_align': 'center',
32                                   'v_attach': 'bottom'
33                               })
34        tval = ba.Lstr(value='${A}:',
35                       subs=[('${A}', ba.Lstr(resource='tipText'))])
36        self.title_node = ba.newnode('text',
37                                     delegate=self,
38                                     attrs={
39                                         'text': tval,
40                                         'scale': self._tip_title_scale,
41                                         'maxwidth': 122,
42                                         'h_align': 'right',
43                                         'vr_depth': -20,
44                                         'v_align': 'center',
45                                         'v_attach': 'bottom'
46                                     })
47        self._message_duration = 10000
48        self._message_spacing = 3000
49        self._change_timer = ba.Timer(
50            0.001 * (self._message_duration + self._message_spacing),
51            ba.WeakCall(self.change_phrase),
52            repeat=True)
53        self._combine = ba.newnode('combine',
54                                   owner=self.node,
55                                   attrs={
56                                       'input0': 1.0,
57                                       'input1': 0.8,
58                                       'input2': 1.0,
59                                       'size': 4
60                                   })
61        self._combine.connectattr('output', self.node, 'color')
62        self._combine.connectattr('output', self.title_node, 'color')
63        self.change_phrase()
64
65    def change_phrase(self) -> None:
66        """Switch the visible tip phrase."""
67        from ba.internal import get_remote_app_name, get_next_tip
68        next_tip = ba.Lstr(translate=('tips', get_next_tip()),
69                           subs=[('${REMOTE_APP_NAME}', get_remote_app_name())
70                                 ])
71        spc = self._message_spacing
72        assert self.node
73        self.node.position = (-200, self._offs_y)
74        self.title_node.position = (-220, self._offs_y + 3)
75        keys = {
76            spc: 0,
77            spc + 1000: 1.0,
78            spc + self._message_duration - 1000: 1.0,
79            spc + self._message_duration: 0.0
80        }
81        ba.animate(self._combine,
82                   'input3', {k: v * 0.5
83                              for k, v in list(keys.items())},
84                   timeformat=ba.TimeFormat.MILLISECONDS)
85        self.node.text = next_tip
86
87    def handlemessage(self, msg: Any) -> Any:
88        assert not self.expired
89        if isinstance(msg, ba.DieMessage):
90            if self.node:
91                self.node.delete()
92            self.title_node.delete()
93            return None
94        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('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        tval = ba.Lstr(value='${A}:',
36                       subs=[('${A}', ba.Lstr(resource='tipText'))])
37        self.title_node = ba.newnode('text',
38                                     delegate=self,
39                                     attrs={
40                                         'text': tval,
41                                         'scale': self._tip_title_scale,
42                                         'maxwidth': 122,
43                                         'h_align': 'right',
44                                         'vr_depth': -20,
45                                         'v_align': 'center',
46                                         'v_attach': 'bottom'
47                                     })
48        self._message_duration = 10000
49        self._message_spacing = 3000
50        self._change_timer = ba.Timer(
51            0.001 * (self._message_duration + self._message_spacing),
52            ba.WeakCall(self.change_phrase),
53            repeat=True)
54        self._combine = ba.newnode('combine',
55                                   owner=self.node,
56                                   attrs={
57                                       'input0': 1.0,
58                                       'input1': 0.8,
59                                       'input2': 1.0,
60                                       'size': 4
61                                   })
62        self._combine.connectattr('output', self.node, 'color')
63        self._combine.connectattr('output', self.title_node, 'color')
64        self.change_phrase()
65
66    def change_phrase(self) -> None:
67        """Switch the visible tip phrase."""
68        from ba.internal import get_remote_app_name, get_next_tip
69        next_tip = ba.Lstr(translate=('tips', get_next_tip()),
70                           subs=[('${REMOTE_APP_NAME}', get_remote_app_name())
71                                 ])
72        spc = self._message_spacing
73        assert self.node
74        self.node.position = (-200, self._offs_y)
75        self.title_node.position = (-220, self._offs_y + 3)
76        keys = {
77            spc: 0,
78            spc + 1000: 1.0,
79            spc + self._message_duration - 1000: 1.0,
80            spc + self._message_duration: 0.0
81        }
82        ba.animate(self._combine,
83                   'input3', {k: v * 0.5
84                              for k, v in list(keys.items())},
85                   timeformat=ba.TimeFormat.MILLISECONDS)
86        self.node.text = next_tip
87
88    def handlemessage(self, msg: Any) -> Any:
89        assert not self.expired
90        if isinstance(msg, ba.DieMessage):
91            if self.node:
92                self.node.delete()
93            self.title_node.delete()
94            return None
95        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('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        tval = ba.Lstr(value='${A}:',
36                       subs=[('${A}', ba.Lstr(resource='tipText'))])
37        self.title_node = ba.newnode('text',
38                                     delegate=self,
39                                     attrs={
40                                         'text': tval,
41                                         'scale': self._tip_title_scale,
42                                         'maxwidth': 122,
43                                         'h_align': 'right',
44                                         'vr_depth': -20,
45                                         'v_align': 'center',
46                                         'v_attach': 'bottom'
47                                     })
48        self._message_duration = 10000
49        self._message_spacing = 3000
50        self._change_timer = ba.Timer(
51            0.001 * (self._message_duration + self._message_spacing),
52            ba.WeakCall(self.change_phrase),
53            repeat=True)
54        self._combine = ba.newnode('combine',
55                                   owner=self.node,
56                                   attrs={
57                                       'input0': 1.0,
58                                       'input1': 0.8,
59                                       'input2': 1.0,
60                                       'size': 4
61                                   })
62        self._combine.connectattr('output', self.node, 'color')
63        self._combine.connectattr('output', self.title_node, 'color')
64        self.change_phrase()

Instantiates an Actor in the current ba.Activity.

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

Switch the visible tip phrase.

def handlemessage(self, msg: Any) -> Any:
88    def handlemessage(self, msg: Any) -> Any:
89        assert not self.expired
90        if isinstance(msg, ba.DieMessage):
91            if self.node:
92                self.node.delete()
93            self.title_node.delete()
94            return None
95        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