def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # let's have a background background = ImageNode.from_image(0, 0, 'sprites.wallpaper') # we want a window to display with a HorizontalContainer node1 = GuiImage(Resources.colour_surface(100, 30, (200, 32, 32))) node2 = GuiImage(Resources.colour_surface(100, 30, (32, 200, 32))) node3 = GuiImage(Resources.colour_surface(100, 30, (32, 32, 200))) c1 = VerticalContainer([node1, node2, node3], (214, 214, 214), align_children=Align.TOP) node6 = GuiImage(Resources.colour_surface(100, 50, (32, 32, 200))) node5 = GuiImage(Resources.colour_surface(100, 50, (32, 200, 32))) node4 = GuiImage(Resources.colour_surface(100, 100, (200, 32, 32))) c2 = VerticalContainer([node6, node5, node4], (214, 214, 214), align_children=Align.TOP) node7 = GuiImage(Resources.colour_surface(100, 30, (200, 32, 32))) node8 = GuiImage(Resources.colour_surface(100, 30, (32, 200, 32))) node9 = GuiImage(Resources.colour_surface(100, 30, (32, 32, 200))) c3 = VerticalContainer([node7, node8, node9], (214, 214, 214), align_children=Align.TOP) hcontainer = HorizontalContainer([c1, c2, c3], (214, 214, 214), align_children=Align.TOP) window = Window(hcontainer) # add the window to a scene scene = Scene([background, window]) controller.add_scene('start', scene) controller.run('start')
def build_simple_image(self): # we should be the minimum size at least width, height, fill = self.minimum_size image = Resources.colour_surface(width, height, self.background) # now draw the nodes. Account for the border ypos = self.border xpos = self.border # this is the size of the largest widget max_width = width - (2 * self.border) for i in self.nodes: widget_xpos = xpos # we know the images will fit vertically, but they may differ horizontally # is the image smaller? if i.minimum_size[0] < max_width: # either it goes to the left, centre or None. First let's ask the widget if i.align != Align.NONE: widget_align = i.align else: widget_align = self.align_children direction = Align.horizontal(widget_align) if direction == Align.RIGHT: widget_xpos += max_width - i.minimum_size[0] elif direction == Align.CENTRE: widget_xpos += (max_width - i.minimum_size[0]) // 2 # if left, we don't need to do anything if i.image is None: i.build_image() # now we have the xpos and ypos i.update_position(widget_xpos, ypos) image.blit(i.image, (widget_xpos, ypos)) ypos += i.image.get_height() + (self.border * 2) return image
def build_image(self, width=0, height=0): # this function MUST be overridden. # to ensure no errors, we set a horrible red square if self.image is None: size = 128 self.image = Resources.colour_surface(size, size, (255, 0, 0)) self.rect = pygame.Rect(0, 0, size, size) self.visible = True
def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # we need 2 images to display mouse over and mouse not over red = Resources.colour_surface(200, 200, (255, 0, 0)) blue = Resources.colour_surface(200, 200, (0, 0, 255)) # we need a node node = MouseOverNode(blue, red, pygame.Rect(220, 140, 200, 200)) # I add the node to a SCENE scene = Scene([node]) # I add the scene to the ENGINE controller.add_scene('start', scene) # I tell the engine what scene to start and run the controller controller.run('start')
def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # let's have a background background = ImageNode.from_image(0, 0, 'sprites.wallpaper') # we want a window to display with a VerticalContainer node1 = GuiImage(Resources.colour_surface(200, 80, (200, 32, 32))) node2 = GuiImage(Resources.colour_surface(175, 80, (32, 200, 32)), align=Align.CENTRE_RIGHT) node3 = GuiImage(Resources.colour_surface(150, 80, (32, 32, 200))) container = VerticalContainer([node1, node2, node3], (214, 214, 214), align_children=Align.CENTRE_LEFT) window = Window(container) # add the window to a scene scene = Scene([background, window]) # I add the scene to the ENGINE controller.add_scene('start', scene) # I tell the engine what scene to start and run the controller controller.run('start')
def game(): resources = os.path.join(os.getcwd(), '../') controller = wargame.engine.init(resources) # this time, we want 1 node to control 2 other nodes # we need 2 images to display mouse over and mouse not over red = Resources.colour_surface(128, 128, (255, 0, 0)) blue = Resources.colour_surface(128, 128, (0, 0, 255)) green = Resources.colour_surface(128, 128, (0, 255, 0)) # we need a node sender = NodeTransmit(green, blue, pygame.Rect(256, 173, 128, 128)) receive1 = NodeReceive(blue, red, pygame.Rect(64, 176, 128, 128), sender.message_id) receive2 = NodeReceive(blue, red, pygame.Rect(448, 176, 128, 128), sender.message_id) # add the nodes to a SCENE scene = Scene([sender, receive1, receive2]) # I add the scene to the ENGINE controller.add_scene('start', scene) # I tell the engine what scene to start and run the controller controller.run('start')
def build_full_image(self, width, height): if height == -1: # use rect size width = self.minimum_size[1] image = Resources.colour_surface(width, height, self.background) # we fill the given space with the widgets. # what is our minimum size? sizes = [x.minimum_size for x in self.nodes] min_width = sum([x[0] for x in sizes]) if min_width < width: # we have spare space, since we only worry about the vertical size # how do we distribute the space? if self.align_children == Align.LEFT: # everything to the left node_image = self.build_simple_image() # (0,0) -> no need to update positions image.blit(node_image, (0, 0)) return image else: # must be same size return self.build_simple_image()
def from_colour(width, height, colour): image = Resources.colour_surface(width, height, colour) return ImageNode.from_surface(0, 0, image)