bastd.actor.background
Defines Actor(s).
1# Released under the MIT License. See LICENSE for details. 2# 3"""Defines Actor(s).""" 4 5from __future__ import annotations 6 7import random 8import weakref 9from typing import TYPE_CHECKING 10 11import ba 12 13if TYPE_CHECKING: 14 from typing import Any 15 16 17class Background(ba.Actor): 18 """Simple Fading Background Actor.""" 19 20 def __init__(self, 21 fade_time: float = 0.5, 22 start_faded: bool = False, 23 show_logo: bool = False): 24 super().__init__() 25 self._dying = False 26 self.fade_time = fade_time 27 # We're special in that we create our node in the session 28 # scene instead of the activity scene. 29 # This way we can overlap multiple activities for fades 30 # and whatnot. 31 session = ba.getsession() 32 self._session = weakref.ref(session) 33 with ba.Context(session): 34 self.node = ba.newnode('image', 35 delegate=self, 36 attrs={ 37 'fill_screen': True, 38 'texture': ba.gettexture('bg'), 39 'tilt_translate': -0.3, 40 'has_alpha_channel': False, 41 'color': (1, 1, 1) 42 }) 43 if not start_faded: 44 ba.animate(self.node, 45 'opacity', { 46 0.0: 0.0, 47 self.fade_time: 1.0 48 }, 49 loop=False) 50 if show_logo: 51 logo_texture = ba.gettexture('logo') 52 logo_model = ba.getmodel('logo') 53 logo_model_transparent = ba.getmodel('logoTransparent') 54 self.logo = ba.newnode( 55 'image', 56 owner=self.node, 57 attrs={ 58 'texture': logo_texture, 59 'model_opaque': logo_model, 60 'model_transparent': logo_model_transparent, 61 'scale': (0.7, 0.7), 62 'vr_depth': -250, 63 'color': (0.15, 0.15, 0.15), 64 'position': (0, 0), 65 'tilt_translate': -0.05, 66 'absolute_scale': False 67 }) 68 self.node.connectattr('opacity', self.logo, 'opacity') 69 # add jitter/pulse for a stop-motion-y look unless we're in VR 70 # in which case stillness is better 71 if not ba.app.vr_mode: 72 self.cmb = ba.newnode('combine', 73 owner=self.node, 74 attrs={'size': 2}) 75 for attr in ['input0', 'input1']: 76 ba.animate(self.cmb, 77 attr, { 78 0.0: 0.693, 79 0.05: 0.7, 80 0.5: 0.693 81 }, 82 loop=True) 83 self.cmb.connectattr('output', self.logo, 'scale') 84 cmb = ba.newnode('combine', 85 owner=self.node, 86 attrs={'size': 2}) 87 cmb.connectattr('output', self.logo, 'position') 88 # Gen some random keys for that stop-motion-y look. 89 keys = {} 90 timeval = 0.0 91 for _i in range(10): 92 keys[timeval] = (random.random() - 0.5) * 0.0015 93 timeval += random.random() * 0.1 94 ba.animate(cmb, 'input0', keys, loop=True) 95 keys = {} 96 timeval = 0.0 97 for _i in range(10): 98 keys[timeval] = (random.random() - 0.5) * 0.0015 + 0.05 99 timeval += random.random() * 0.1 100 ba.animate(cmb, 'input1', keys, loop=True) 101 102 def __del__(self) -> None: 103 # Normal actors don't get sent DieMessages when their 104 # activity is shutting down, but we still need to do so 105 # since our node lives in the session and it wouldn't die 106 # otherwise. 107 self._die() 108 super().__del__() 109 110 def _die(self, immediate: bool = False) -> None: 111 session = self._session() 112 if session is None and self.node: 113 # If session is gone, our node should be too, 114 # since it was part of the session's scene. 115 # Let's make sure that's the case. 116 # (since otherwise we have no way to kill it) 117 ba.print_error('got None session on Background _die' 118 ' (and node still exists!)') 119 elif session is not None: 120 with ba.Context(session): 121 if not self._dying and self.node: 122 self._dying = True 123 if immediate: 124 self.node.delete() 125 else: 126 ba.animate(self.node, 127 'opacity', { 128 0.0: 1.0, 129 self.fade_time: 0.0 130 }, 131 loop=False) 132 ba.timer(self.fade_time + 0.1, self.node.delete) 133 134 def handlemessage(self, msg: Any) -> Any: 135 assert not self.expired 136 if isinstance(msg, ba.DieMessage): 137 self._die(msg.immediate) 138 else: 139 super().handlemessage(msg)
class
Background(ba._actor.Actor):
18class Background(ba.Actor): 19 """Simple Fading Background Actor.""" 20 21 def __init__(self, 22 fade_time: float = 0.5, 23 start_faded: bool = False, 24 show_logo: bool = False): 25 super().__init__() 26 self._dying = False 27 self.fade_time = fade_time 28 # We're special in that we create our node in the session 29 # scene instead of the activity scene. 30 # This way we can overlap multiple activities for fades 31 # and whatnot. 32 session = ba.getsession() 33 self._session = weakref.ref(session) 34 with ba.Context(session): 35 self.node = ba.newnode('image', 36 delegate=self, 37 attrs={ 38 'fill_screen': True, 39 'texture': ba.gettexture('bg'), 40 'tilt_translate': -0.3, 41 'has_alpha_channel': False, 42 'color': (1, 1, 1) 43 }) 44 if not start_faded: 45 ba.animate(self.node, 46 'opacity', { 47 0.0: 0.0, 48 self.fade_time: 1.0 49 }, 50 loop=False) 51 if show_logo: 52 logo_texture = ba.gettexture('logo') 53 logo_model = ba.getmodel('logo') 54 logo_model_transparent = ba.getmodel('logoTransparent') 55 self.logo = ba.newnode( 56 'image', 57 owner=self.node, 58 attrs={ 59 'texture': logo_texture, 60 'model_opaque': logo_model, 61 'model_transparent': logo_model_transparent, 62 'scale': (0.7, 0.7), 63 'vr_depth': -250, 64 'color': (0.15, 0.15, 0.15), 65 'position': (0, 0), 66 'tilt_translate': -0.05, 67 'absolute_scale': False 68 }) 69 self.node.connectattr('opacity', self.logo, 'opacity') 70 # add jitter/pulse for a stop-motion-y look unless we're in VR 71 # in which case stillness is better 72 if not ba.app.vr_mode: 73 self.cmb = ba.newnode('combine', 74 owner=self.node, 75 attrs={'size': 2}) 76 for attr in ['input0', 'input1']: 77 ba.animate(self.cmb, 78 attr, { 79 0.0: 0.693, 80 0.05: 0.7, 81 0.5: 0.693 82 }, 83 loop=True) 84 self.cmb.connectattr('output', self.logo, 'scale') 85 cmb = ba.newnode('combine', 86 owner=self.node, 87 attrs={'size': 2}) 88 cmb.connectattr('output', self.logo, 'position') 89 # Gen some random keys for that stop-motion-y look. 90 keys = {} 91 timeval = 0.0 92 for _i in range(10): 93 keys[timeval] = (random.random() - 0.5) * 0.0015 94 timeval += random.random() * 0.1 95 ba.animate(cmb, 'input0', keys, loop=True) 96 keys = {} 97 timeval = 0.0 98 for _i in range(10): 99 keys[timeval] = (random.random() - 0.5) * 0.0015 + 0.05 100 timeval += random.random() * 0.1 101 ba.animate(cmb, 'input1', keys, loop=True) 102 103 def __del__(self) -> None: 104 # Normal actors don't get sent DieMessages when their 105 # activity is shutting down, but we still need to do so 106 # since our node lives in the session and it wouldn't die 107 # otherwise. 108 self._die() 109 super().__del__() 110 111 def _die(self, immediate: bool = False) -> None: 112 session = self._session() 113 if session is None and self.node: 114 # If session is gone, our node should be too, 115 # since it was part of the session's scene. 116 # Let's make sure that's the case. 117 # (since otherwise we have no way to kill it) 118 ba.print_error('got None session on Background _die' 119 ' (and node still exists!)') 120 elif session is not None: 121 with ba.Context(session): 122 if not self._dying and self.node: 123 self._dying = True 124 if immediate: 125 self.node.delete() 126 else: 127 ba.animate(self.node, 128 'opacity', { 129 0.0: 1.0, 130 self.fade_time: 0.0 131 }, 132 loop=False) 133 ba.timer(self.fade_time + 0.1, self.node.delete) 134 135 def handlemessage(self, msg: Any) -> Any: 136 assert not self.expired 137 if isinstance(msg, ba.DieMessage): 138 self._die(msg.immediate) 139 else: 140 super().handlemessage(msg)
Simple Fading Background Actor.
Background( fade_time: float = 0.5, start_faded: bool = False, show_logo: bool = False)
21 def __init__(self, 22 fade_time: float = 0.5, 23 start_faded: bool = False, 24 show_logo: bool = False): 25 super().__init__() 26 self._dying = False 27 self.fade_time = fade_time 28 # We're special in that we create our node in the session 29 # scene instead of the activity scene. 30 # This way we can overlap multiple activities for fades 31 # and whatnot. 32 session = ba.getsession() 33 self._session = weakref.ref(session) 34 with ba.Context(session): 35 self.node = ba.newnode('image', 36 delegate=self, 37 attrs={ 38 'fill_screen': True, 39 'texture': ba.gettexture('bg'), 40 'tilt_translate': -0.3, 41 'has_alpha_channel': False, 42 'color': (1, 1, 1) 43 }) 44 if not start_faded: 45 ba.animate(self.node, 46 'opacity', { 47 0.0: 0.0, 48 self.fade_time: 1.0 49 }, 50 loop=False) 51 if show_logo: 52 logo_texture = ba.gettexture('logo') 53 logo_model = ba.getmodel('logo') 54 logo_model_transparent = ba.getmodel('logoTransparent') 55 self.logo = ba.newnode( 56 'image', 57 owner=self.node, 58 attrs={ 59 'texture': logo_texture, 60 'model_opaque': logo_model, 61 'model_transparent': logo_model_transparent, 62 'scale': (0.7, 0.7), 63 'vr_depth': -250, 64 'color': (0.15, 0.15, 0.15), 65 'position': (0, 0), 66 'tilt_translate': -0.05, 67 'absolute_scale': False 68 }) 69 self.node.connectattr('opacity', self.logo, 'opacity') 70 # add jitter/pulse for a stop-motion-y look unless we're in VR 71 # in which case stillness is better 72 if not ba.app.vr_mode: 73 self.cmb = ba.newnode('combine', 74 owner=self.node, 75 attrs={'size': 2}) 76 for attr in ['input0', 'input1']: 77 ba.animate(self.cmb, 78 attr, { 79 0.0: 0.693, 80 0.05: 0.7, 81 0.5: 0.693 82 }, 83 loop=True) 84 self.cmb.connectattr('output', self.logo, 'scale') 85 cmb = ba.newnode('combine', 86 owner=self.node, 87 attrs={'size': 2}) 88 cmb.connectattr('output', self.logo, 'position') 89 # Gen some random keys for that stop-motion-y look. 90 keys = {} 91 timeval = 0.0 92 for _i in range(10): 93 keys[timeval] = (random.random() - 0.5) * 0.0015 94 timeval += random.random() * 0.1 95 ba.animate(cmb, 'input0', keys, loop=True) 96 keys = {} 97 timeval = 0.0 98 for _i in range(10): 99 keys[timeval] = (random.random() - 0.5) * 0.0015 + 0.05 100 timeval += random.random() * 0.1 101 ba.animate(cmb, 'input1', keys, loop=True)
Instantiates an Actor in the current ba.Activity.
def
handlemessage(self, msg: Any) -> Any:
135 def handlemessage(self, msg: Any) -> Any: 136 assert not self.expired 137 if isinstance(msg, ba.DieMessage): 138 self._die(msg.immediate) 139 else: 140 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