class AnimatedLayer(Layer): def __init__(self, parent=None, position=(0, 0), size=(0, 0)): super(AnimatedLayer, self).__init__(parent, position, size) self._frames = {} self._frame_index = None def set_animation(self, animation_id): self._frames = ResourceLoader().get_animation_frames(animation_id) self.rewind() def rewind(self): self._frame_index = self._frames.keys()[0] self.set_image(self._frames[self._frame_index]) def step(self): self._frame_index += 1 if self._frame_index not in self._frames.keys(): self.rewind() else: self.set_image(self._frames[self._frame_index]) def blit(self): super(AnimatedLayer, self).blit() if self._frame_index is not None: self.step()
def __init__(self, path): super(Interface, self).__init__( position=(0, 0), size=Widget.get_resolution() ) self.set_transparent(True) self._dom = ResourceLoader().get_raw_resource(path) self._index = 0 self._ids = {} self._root = self._dom.getElementsByTagName('interface')[0] for node in self._root.childNodes: self._build_node(node, None)
class Interface(Layer): NODES = { 'section': Widget, 'label': Label } def __init__(self, path): super(Interface, self).__init__( position=(0, 0), size=Widget.get_resolution() ) self.set_transparent(True) self._dom = ResourceLoader().get_raw_resource(path) self._index = 0 self._ids = {} self._root = self._dom.getElementsByTagName('interface')[0] for node in self._root.childNodes: self._build_node(node, None) def get(self, id): return self._ids[id] def _build_node(self, node, parent_widget): tag_name = None try: tag_name = node.tagName except: pass if tag_name in Interface.NODES: widget = Interface.NODES[node.tagName]() if node.getAttribute('id'): self._ids[node.getAttribute('id')] = widget for attr_name in widget.get_allowed_attributes(): widget.attr(attr_name, node.getAttribute(attr_name)) raw_style = node.getAttribute('style') for definition in raw_style.split(';'): if definition.find(':') != -1: name = definition.split(':')[0].strip() value = definition.split(':')[1].strip() widget.style(name, value) if parent_widget is None: self.add_layer(widget, self._index) else: parent_widget.add_layer(widget, self._index) for child in node.childNodes: self._build_node(child, widget) self._index += 1
def blit(self): if self._cropped_rect is not None: if self._image_id is not None: from bloodyhell.resourceloader import ResourceLoader self._surface = ResourceLoader().get_resource( self._image_id, self._rect, self._cropped_rect ) if self._parent is not None: self._screen.blit(self._surface, ( self._parent.rect().x + self._rect.x + self._cropped_rect.x, self._parent.rect().y + self._rect.y + self._cropped_rect.y )) else: self._screen.blit( self._surface, (self._rect.x, self._rect.y) )
def set_animation(self, animation_id): self._frames = ResourceLoader().get_animation_frames(animation_id) self.rewind()
class Layer(EventDispatcher): def __init__(self, parent=None, position=(0, 0), size=(0, 0)): super(Layer, self).__init__() self._parent = parent self._screen = pygame.display.get_surface() self._surface = pygame.Surface(size) self._rect = Rect(position, size) self._cropped_rect = Rect((0, 0), size) self._slots = {} self._image_id = None self._transparent = False def set_transparent(self, transparent): self._transparent = transparent def loader(self): from bloodyhell.resourceloader import ResourceLoader return ResourceLoader() def add_layer(self, layer, slot=0): if not slot in self._slots: self._slots[slot] = [] layer.set_parent(self) self._slots[slot].append(layer) self.add(layer) def remove_layer(self, layer_to_remove): for slot in self._slots.keys(): for layer in self._slots[slot]: index = 0 if layer is layer_to_remove: self._slots[slot].pop(index) index += 1 self.remove(layer_to_remove) def set_parent(self, parent): self._parent = parent def surface(self): return self._surface def blit(self): if self._cropped_rect is not None: if self._image_id is not None: from bloodyhell.resourceloader import ResourceLoader self._surface = ResourceLoader().get_resource( self._image_id, self._rect, self._cropped_rect ) if self._parent is not None: self._screen.blit(self._surface, ( self._parent.rect().x + self._rect.x + self._cropped_rect.x, self._parent.rect().y + self._rect.y + self._cropped_rect.y )) else: self._screen.blit( self._surface, (self._rect.x, self._rect.y) ) def on_frame(self, delta): if not self._transparent: self.blit() for slot in self._slots.keys(): for layer in self._slots[slot]: layer.on_frame(delta) def rect(self): return self._rect def set_cropped_rect(self, cropped_rect): self._cropped_rect = cropped_rect def set_image(self, image_id): self._image_id = image_id def fill(self, hexcolor): hexcolor = hexcolor.replace('#', '') self._surface.fill(( int(hexcolor[0:2], 16), int(hexcolor[2:4], 16), int(hexcolor[4:6], 16), )) return self