def __init__(self, **kwargs): super(SpriteWindow, self).__init__(**kwargs) self.subloc = None self.sprite_organizer = SpriteOrganizer() self.center_sprite = Image(allow_stretch=True, keep_ratio=False, opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.5, 'y': 0}) self.left_sprite = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.25, 'y': 0}) self.right_sprite = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.75, 'y': 0}) self.foreground = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.5, 'y': 0}) self.overlay = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.5, 'y': 0}) self.sprite_organizer.add_sprite(self.center_sprite) self.sprite_organizer.add_sprite(self.left_sprite) self.sprite_organizer.add_sprite(self.right_sprite) self.sprite_organizer.add_sprite(self.foreground) self.sprite_organizer.add_sprite(self.overlay)
class SpriteTest(unittest.TestCase): def setUp(self): self.so = SpriteOrganizer() def test_add_sprites(self): self.so.add_sprite(MockSprite("1")) self.so.add_sprite(MockSprite("2")) self.assertEqual(2, len(self.so.get_sprites())) def test_sprites_sorted(self): ms1 = MockSprite("1") ms2 = MockSprite("2") ms3 = MockSprite("3") self.so.add_sprite(ms1) self.so.add_sprite(ms2) self.so.add_sprite(ms3) self.assertIs(ms3, self.so.get_sprites()[0]) self.assertIs(ms2, self.so.get_sprites()[1]) self.assertIs(ms1, self.so.get_sprites()[2]) def test_same_sprite_readded(self): ms1 = MockSprite("1") ms2 = MockSprite("2") ms3 = MockSprite("3") self.so.add_sprite(ms1) self.so.add_sprite(ms2) self.so.add_sprite(ms3) self.so.add_sprite(ms1) self.assertEqual(3, len(self.so.get_sprites())) self.assertIs(ms1, self.so.get_sprites()[0]) self.assertIs(ms3, self.so.get_sprites()[1]) self.assertIs(ms2, self.so.get_sprites()[2])
class SpriteWindow(Widget): background = ObjectProperty(None) sprite_layout = ObjectProperty(None) def __init__(self, **kwargs): super(SpriteWindow, self).__init__(**kwargs) self.subloc = None self.sprite_organizer = SpriteOrganizer() self.center_sprite = Image(allow_stretch=True, keep_ratio=False, opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.5, 'y': 0}) self.left_sprite = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.25, 'y': 0}) self.right_sprite = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.75, 'y': 0}) self.foreground = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.5, 'y': 0}) self.overlay = Image(opacity=0, size_hint=(None, None), size=(800, 600), pos_hint={'center_x': 0.5, 'y': 0}) self.sprite_organizer.add_sprite(self.center_sprite) self.sprite_organizer.add_sprite(self.left_sprite) self.sprite_organizer.add_sprite(self.right_sprite) self.sprite_organizer.add_sprite(self.foreground) self.sprite_organizer.add_sprite(self.overlay) def set_sprite(self, user, display_sub=True): sprite = user.get_current_sprite() if sprite.is_cg(): self.set_cg(sprite, user) return subloc = user.get_subloc() pos = user.get_pos() char = user.get_char() if char.name != 'Narrator': subloc.remove_o_user(user) if display_sub: self.sprite_layout.clear_widgets() if char.name == 'Narrator': self.sprite_organizer.add_sprite(self.foreground) self.sprite_organizer.add_sprite(self.overlay) subloc.add_o_user(user) elif pos == 'right': self.sprite_organizer.add_sprite(self.right_sprite) self.sprite_organizer.add_sprite(self.foreground) subloc.add_r_user(user) elif pos == 'left': self.sprite_organizer.add_sprite(self.left_sprite) self.sprite_organizer.add_sprite(self.foreground) subloc.add_l_user(user) else: self.sprite_organizer.add_sprite(self.center_sprite) self.sprite_organizer.add_sprite(self.foreground) subloc.add_c_user(user) for index, organized_sprite in enumerate(self.sprite_organizer.get_sprites()): self.sprite_layout.add_widget(organized_sprite, index=index) self.display_sub(subloc) else: if char.name == 'Narrator': subloc.add_o_user(user) elif pos == 'right': subloc.add_r_user(user) elif pos == 'left': subloc.add_l_user(user) else: subloc.add_c_user(user) def set_cg(self, sprite, user): self.set_all_sprites_opacity(0) option = user.get_sprite_option() main_scr = App.get_running_app().get_main_screen() sprite = main_scr.sprite_settings.apply_post_processing(sprite, option) self.center_sprite.texture = None self.center_sprite.texture = sprite.get_texture() self.center_sprite.opacity = 1 self.center_sprite.size = 800, 600 def set_all_sprites_opacity(self, value: float): self.left_sprite.opacity = value self.right_sprite.opacity = value self.center_sprite.opacity = value self.foreground.opacity = value self.overlay.opacity = value def set_subloc(self, subloc): self.subloc = subloc self.background.texture = subloc.get_img().texture def display_sub(self, subloc: SubLocation): if subloc is None: return main_scr = App.get_running_app().get_main_screen() self.subloc = subloc if subloc.o_users: sprite = subloc.get_o_user().get_current_sprite() option = subloc.get_o_user().get_sprite_option() sprite = main_scr.sprite_settings.apply_post_processing(sprite, option) if sprite is not None: self.overlay.texture = None self.overlay.texture = sprite.get_texture() self.overlay.opacity = 1 self.overlay.size = self.overlay.texture.size else: self.overlay.texture = None self.overlay.opacity = 0 self.foreground.opacity = 0 if subloc.has_foreground(): self.foreground.texture = None self.foreground.texture = subloc.get_foreground_img().texture self.foreground.opacity = 1 if subloc.c_users: sprite = subloc.get_c_user().get_current_sprite() option = subloc.get_c_user().get_sprite_option() sprite = main_scr.sprite_settings.apply_post_processing(sprite, option) if sprite is not None: if sprite.is_cg(): self.set_cg(sprite, subloc.get_c_user()) return self.center_sprite.texture = None self.center_sprite.texture = sprite.get_texture() self.center_sprite.opacity = 1 self.center_sprite.size = self.center_sprite.texture.size else: self.center_sprite.texture = None self.center_sprite.opacity = 0 if subloc.l_users: sprite = subloc.get_l_user().get_current_sprite() option = subloc.get_l_user().get_sprite_option() sprite = main_scr.sprite_settings.apply_post_processing(sprite, option) if sprite is not None: self.left_sprite.texture = None self.left_sprite.texture = sprite.get_texture() self.left_sprite.opacity = 1 self.left_sprite.size = self.left_sprite.texture.size else: self.left_sprite.texture = None self.left_sprite.opacity = 0 if subloc.r_users: sprite = subloc.get_r_user().get_current_sprite() option = subloc.get_r_user().get_sprite_option() sprite = main_scr.sprite_settings.apply_post_processing(sprite, option) if sprite is not None: self.right_sprite.texture = None self.right_sprite.texture = sprite.get_texture() self.right_sprite.opacity = 1 self.right_sprite.size = self.right_sprite.texture.size else: self.right_sprite.texture = None self.right_sprite.opacity = 0 def refresh_sub(self): self.display_sub(self.subloc)
def setUp(self): self.so = SpriteOrganizer()