def conditional_animation(interaction, value, xevt_id, animation): target = interaction.target did_set = False kill_handler = False def check_fn(): return target.get_state(value.state).value != value.value def set_fn(_): nonlocal did_set if did_set: return target.set_state(value.state, value) did_set = True if animation is None: return maybe(check_fn, set_fn) def set_handler(*_, **__): if kill_handler: return set_fn(None) def setup_asm(asm): if xevt_id is not None: asm.context.register_event_handler(set_handler, handler_id=xevt_id) def cleanup_asm(asm): nonlocal kill_handler if xevt_id is not None: kill_handler = True return maybe(check_fn, build_critical_section(animation(interaction, setup_asm=setup_asm, cleanup_asm=cleanup_asm), flush_all_animations, set_fn))
def conditional_animation(interaction, value, xevt_id, animation): target = interaction.target did_set = False kill_handler = False def check_fn(): return target.get_state(value.state).value != value.value def set_fn(_): nonlocal did_set if did_set: return target.set_state(value.state, value) did_set = True if animation is None: return maybe(check_fn, set_fn) def set_handler(*_, **__): if kill_handler: return set_fn(None) def setup_asm(asm): if xevt_id is not None: asm.context.register_event_handler(set_handler, handler_id=xevt_id) def cleanup_asm(asm): nonlocal kill_handler if xevt_id is not None: kill_handler = True return maybe( check_fn, build_critical_section( animation(interaction, setup_asm=setup_asm, cleanup_asm=cleanup_asm), flush_all_animations, set_fn))
def _run_interaction_gen(self, timeline): result = yield from element_utils.run_child( timeline, build_critical_section_with_finally( self._start_route_goal_suppression, build_critical_section( build_critical_section( self.ensure_state(self.affordance.required_channel), objects.components.state.with_on_state_changed( self.target, self.affordance.required_channel.state, self._changed_state_callback, super()._run_interaction_gen)), maybe( lambda: len(self.target.get_users(sims_only=True)) == 1, self.ensure_state(self.off_channel))), self._stop_route_goal_suppression)) return result yield
def _run(self, timeline): sequence = self.sequence resolver = self.interaction.get_resolver() if self.tests and not self.tests.run_tests(resolver): return timeline.run_child(sequence) if self.entry_outfit is not None: sequence = build_critical_section( self.outfit_change.get_on_entry_change(self.interaction, sim_info=self.sim_info), sequence) if self.exit_outfit is not None: def on_oufit_change_exit(_): if self.sim_info._current_outfit != self.exit_outfit and self.outfit_change_exit_test.run_tests( resolver): self.sim_info.set_current_outfit(self.exit_outfit) sequence = build_critical_section_with_finally( build_critical_section( sequence, maybe( lambda: self.outfit_change_exit_test.run_tests(resolver ), self.outfit_change.get_on_exit_change( self.interaction, sim_info=self.sim_info))), on_oufit_change_exit) if self.xevt_outfit is not None: def register_xevt(_): self._xevt_handle = self.interaction.animation_context.register_event_handler( lambda _: self._run_xevt_outfit_change(), handler_id=self.xevt_outfit_change.xevt_id) def release_xevt(_): self._xevt_handle.release() self._xevt_handle = None sequence = build_critical_section(register_xevt, sequence, release_xevt) return timeline.run_child(sequence)
def _get_behavior(self): posture = self._posture sim = posture.sim multi_sim_posture_transition = posture.multi_sim and not posture.is_puppet prev_posture_state = sim.posture_state self._prev_posture = prev_posture_state.get_aspect(posture.track) animate_in = None if multi_sim_posture_transition or self._animate_in is not None and not self._animate_in.empty: animate_in = create_run_animation(self._animate_in) my_stage = self._stage() def posture_change(timeline): posture.log_info('Change', msg='{}'.format(posture.track.name if posture.track is not None else 'NO TRACK!')) prev_posture_state = sim.posture_state prev_posture = prev_posture_state.get_aspect(posture.track) sim.posture_state = self._dest_state sim.on_posture_event(PostureEvent.POSTURE_CHANGED, self._dest_state, posture.track, prev_posture, posture) yield sim.si_state.notify_posture_change_and_remove_incompatible_gen(timeline, prev_posture_state, self._dest_state) prev_posture.clear_owning_interactions() if multi_sim_posture_transition: linked_posture_begin = posture.linked_posture.begin(self._animate_in, self._dest_state.linked_posture_state, posture._context) self._dest_state = None yield element_utils.run_child(timeline, linked_posture_begin) else: self._dest_state = None return True def end_posture_on_same_track(timeline): if self._prev_posture is not None and self._prev_posture is not posture: prev_posture = self._prev_posture self._prev_posture = None result = yield element_utils.run_child(timeline, build_element(prev_posture.end())) return result return True if services.current_zone().animate_instantly: flush = flush_all_animations_instantly else: flush = flush_all_animations sequence = (posture_change, animate_in, flush, end_posture_on_same_track, my_stage) sequence = build_element(sequence, critical=CleanupType.RunAll) sequence = build_critical_section_with_finally(sequence, lambda _: posture._release_animation_context()) if self._posture.jig is not None and self._posture.target is not None and self._posture.slot_constraint is not None: def create_and_place_jig(_): self._jig = objects.system.create_object(self._posture.jig) for constraint in self._posture.slot_constraint: self._jig.transform = constraint.containment_transform break sim.routing_context.ignore_footprint_contour(self._jig.routing_context.object_footprint_id) def destroy_jig(_): if self._jig is not None: sim.routing_context.remove_footprint_contour_override(self._jig.routing_context.object_footprint_id) self._jig.destroy(source=self, cause='Destroying jig for posture.') sequence = build_critical_section_with_finally(create_and_place_jig, sequence, destroy_jig) sequence = elements.GeneratorElement(self.with_censor_grid(sim, sequence)) if posture.target is not None: reserver = create_reserver(sim, posture.target, reserver=posture, handler=MultiReserveObjectHandler) sequence = reserver.do_reserve(sequence=sequence) def stage_on_fail(timeline): if not self.has_staged: yield element_utils.run_child(timeline, self._stage_fail()) sequence = element_utils.build_critical_section(sequence, stage_on_fail) sequence = maybe(posture.test, sequence) return sequence
def _run_interaction_gen(self, timeline): result = yield element_utils.run_child(timeline, build_critical_section_with_finally(self._start_route_goal_suppression, build_critical_section(build_critical_section(self.ensure_state(self.affordance.required_channel), objects.components.state.with_on_state_changed(self.target, self.affordance.required_channel.state, self._changed_state_callback, super()._run_interaction_gen)), maybe(lambda : len(self.target.get_users(sims_only=True)) == 1, self.ensure_state(Television.TV_OFF_STATE))), self._stop_route_goal_suppression)) return result
def _get_behavior(self): posture = self._posture sim = posture.sim multi_sim_posture_transition = posture.multi_sim and not posture.is_puppet prev_posture_state = sim.posture_state self._prev_posture = prev_posture_state.get_aspect(posture.track) animate_in = None if multi_sim_posture_transition or self._animate_in is not None and not self._animate_in.empty: animate_in = create_run_animation(self._animate_in) my_stage = self._stage() def posture_change(timeline): posture.log_info( 'Change', msg='{}'.format(posture.track.name if posture. track is not None else 'NO TRACK!')) prev_posture_state = sim.posture_state prev_posture = prev_posture_state.get_aspect(posture.track) sim.posture_state = self._dest_state sim.on_posture_event(PostureEvent.POSTURE_CHANGED, self._dest_state, posture.track, prev_posture, posture) yield sim.si_state.notify_posture_change_and_remove_incompatible_gen( timeline, prev_posture_state, self._dest_state) prev_posture.clear_owning_interactions() if multi_sim_posture_transition: linked_posture_begin = posture.linked_posture.begin( self._animate_in, self._dest_state.linked_posture_state, posture._context) self._dest_state = None yield element_utils.run_child(timeline, linked_posture_begin) else: self._dest_state = None return True def end_posture_on_same_track(timeline): if self._prev_posture is not None and self._prev_posture is not posture: prev_posture = self._prev_posture self._prev_posture = None result = yield element_utils.run_child( timeline, build_element(prev_posture.end())) return result return True if services.current_zone().animate_instantly: flush = flush_all_animations_instantly else: flush = flush_all_animations sequence = (posture_change, animate_in, flush, end_posture_on_same_track, my_stage) sequence = build_element(sequence, critical=CleanupType.RunAll) sequence = build_critical_section_with_finally( sequence, lambda _: posture._release_animation_context()) if self._posture.jig is not None and self._posture.target is not None and self._posture.slot_constraint is not None: def create_and_place_jig(_): self._jig = objects.system.create_object(self._posture.jig) for constraint in self._posture.slot_constraint: self._jig.transform = constraint.containment_transform break sim.routing_context.ignore_footprint_contour( self._jig.routing_context.object_footprint_id) def destroy_jig(_): if self._jig is not None: sim.routing_context.remove_footprint_contour_override( self._jig.routing_context.object_footprint_id) self._jig.destroy(source=self, cause='Destroying jig for posture.') sequence = build_critical_section_with_finally( create_and_place_jig, sequence, destroy_jig) sequence = elements.GeneratorElement( self.with_censor_grid(sim, sequence)) if posture.target is not None: reserver = create_reserver(sim, posture.target, reserver=posture, handler=MultiReserveObjectHandler) sequence = reserver.do_reserve(sequence=sequence) def stage_on_fail(timeline): if not self.has_staged: yield element_utils.run_child(timeline, self._stage_fail()) sequence = element_utils.build_critical_section( sequence, stage_on_fail) sequence = maybe(posture.test, sequence) return sequence