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

A bit of text showing various helpful game tips.

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

Instantiates an Actor in the current bascenev1.Activity.

node
title_node
def change_phrase(self) -> None:
70    def change_phrase(self) -> None:
71        """Switch the visible tip phrase."""
72        from babase import get_remote_app_name
73
74        next_tip = bs.Lstr(
75            translate=(
76                'tips',
77                bs.app.classic.get_next_tip()
78                if bs.app.classic is not None
79                else '',
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

Switch the visible tip phrase.

@override
def handlemessage(self, msg: Any) -> Any:
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)

General message handling; can be passed any message object.

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