def get_bound_rect(self):
        origin_enum, align_x, align_y = self.sprite.get_alignment()
        align_x = -align_x
        origin = Origin.calc_origin(
            origin_enum, Size(self.sprite.width, self.sprite.height))

        return Rect(
            Point(self.pos) - origin - Point(align_x, align_y),
            Size(self.sprite.width, self.sprite.height))
Exemple #2
0
def make_rect_object(metadata, color=None):
    if color is None:
        color = Colorf(0, 0, 1.0, 0.5)
    pos = Point(0, 0)
    size = Size(64, 64)
    obj = ObjMapRectObject(Rect(pos, size), color, metadata)
    return obj
Exemple #3
0
    def draw(self, gc):
        gc.fill_rect(Rect(Point(self.pos) - Point(16, 16), Size(32, 32)),
                     Color(200, 255, 200))
        if self.next_node:
            gc.draw_line(int(self.pos.x), int(self.pos.y),
                         int((self.pos.x + self.next_node.pos.x) / 2),
                         int((self.pos.y + self.next_node.pos.y) / 2),
                         Color(255, 255, 0))

            gc.draw_line(int((self.pos.x + self.next_node.pos.x) / 2),
                         int((self.pos.y + self.next_node.pos.y) / 2),
                         int(self.next_node.pos.x), int(self.next_node.pos.y),
                         Color(255, 0, 0))
    def draw(self, gc):
        tilemap = ToolContext.current.tilemap_layer
        if not tilemap:
            return

        tile_size = tilemap.get_tileset().get_tile_size()

        # Draw the brush:
        for y in range(0, ToolContext.current.tile_brush.height):
            for x in range(0, ToolContext.current.tile_brush.width):
                tile = tilemap.get_tileset().create(
                    ToolContext.current.tile_brush.at(x, y))

                if tile:
                    sprite = tile.get_sprite()
                    sprite.set_alpha(0.5)
                    sprite.draw((self.current_tile.x + x) * tile_size,
                                (self.current_tile.y + y) * tile_size, gc)

                    gc.fill_rect(
                        Rect(
                            Point((self.current_tile.x + x) * tile_size,
                                  (self.current_tile.y + y) * tile_size),
                            Size(tile_size, tile_size)),
                        Color(255, 255, 255, 100))
                elif ToolContext.current.tile_brush.is_opaque():
                    gc.fill_rect(
                        Rect(
                            Point((self.current_tile.x + x) * tile_size,
                                  (self.current_tile.y + y) * tile_size),
                            Size(tile_size, tile_size)),
                        Color(255, 255, 255, 100))
                else:
                    gc.fill_rect(
                        Rect(
                            Point((self.current_tile.x + x) * tile_size,
                                  (self.current_tile.y + y) * tile_size),
                            Size(tile_size, tile_size)),
                        Color(255, 255, 255, 50))
Exemple #5
0
 def draw(self, x, y, gc):
     painter = gc.get_qt_painter()
     img = self.pixelbuffer.get_qimage()
     if not img:
         logging.error("Error: Sprite: Empty PixelBuffer: " + self.filename)
     else:
         scaled_width = self.width * self.scale[0]
         scaled_height = self.height * self.scale[1]
         origin = Origin.calc_origin(self.origin,
                                     Size(scaled_width, scaled_height))
         if self.width > self.height:
             img = img.scaledToWidth(scaled_width)
         else:
             img = img.scaledToHeight(scaled_height)
         painter.drawImage(QPoint(x - origin.x, y - origin.y), img)
    def paintEvent(self, event):
        painter = QPainter(self)
        gc = GraphicContext(painter)

        brush = ToolContext.current.tile_brush

        start_row = event.rect().top() // self.cell_size
        end_row = (event.rect().bottom() + self.cell_size - 1) // self.cell_size
        end_index = min(end_row * self.columns, len(self.tiles))

        # Draw tiles
        for i in range(start_row * self.columns, end_index):
            x = i % self.columns
            y = i // self.columns

            tile = self.tileset.create(self.tiles[i])

            rect = Rect(Point(int(x * self.cell_size),
                              int(y * self.cell_size)),
                        Size(self.cell_size,
                             self.cell_size))

            if tile:
                sprite = tile.get_sprite()
                sprite.set_scale(self.scale, self.scale)
                sprite.draw(int(x * self.cell_size),
                            int(y * self.cell_size), gc)

                # Use grid in the tileselector
                gc.draw_rect(rect, Color(0, 0, 0, 128))

            # mark the currently selected tile
            if brush.width == 1 and brush.height == 1 and brush.at(0, 0) == self.tiles[i]:
                gc.fill_rect(rect, Color(0, 0, 255, 100))
            elif self.mouse_over_tile == i and self.has_focus:
                gc.fill_rect(rect, Color(0, 0, 255, 20))

        # draw rectangle selection
        if self.region_select:
            rect = self.get_selection()

            rect.top *= self.cell_size
            rect.bottom *= self.cell_size
            rect.left *= self.cell_size
            rect.right *= self.cell_size

            gc.fill_rect(rect, Color(0, 0, 255, 100))
Exemple #7
0
    def draw(self, stroke, gc):
        if DrawerProperties.current.get_brush().is_null(
        ) or stroke.get_dab_count() == 0:
            return

        dabs = stroke.get_interpolated_dabs(
            DrawerProperties.current.get_spacing() *
            DrawerProperties.current.get_size(),
            DrawerProperties.current.get_spacing() *
            DrawerProperties.current.get_size())

        for i, dab in enumerate(self.dabs):
            sprite = DrawerProperties.current.get_brush().get_sprite()
            color = DrawerProperties.current.get_color()

            sprite.set_color(color)
            sprite.set_alpha((color.get_alpha() / 255.0) * dab.pressure)
            sprite.set_scale(
                DrawerProperties.current.get_size() * dab.pressure,
                DrawerProperties.current.get_size() * dab.pressure)

            if gc is not None:
                # DO Multipass:
                # 1: GL_ZERO, GL_DST_ALPHA
                # 2: GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA

                # brush.set_blend_func_separate(BlendFunc.zero, BlendFunc.dst_alpha,
                #                               BlendFunc.zero, BlendFunc.one)
                # brush.draw(dab.pos.x, dab.pos.y, gc)

                if self.mode == SpriteStrokeDrawer.DM_NORMAL:
                    sprite.set_blend_func_separate(
                        BlendFunc.src_alpha, BlendFunc.one_minus_src_alpha,
                        BlendFunc.one, BlendFunc.one_minus_src_alpha)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                elif self.mode == SpriteStrokeDrawer.DM_ADDITION:
                    sprite.set_blend_func_separate(BlendFunc.src_alpha,
                                                   BlendFunc.one,
                                                   BlendFunc.zero,
                                                   BlendFunc.one)
                    # BlendFunc.one, BlendFunc.one_minus_src_alpha)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                elif self.mode == SpriteStrokeDrawer.DM_ERASE:
                    sprite.set_blend_func(BlendFunc.zero,
                                          BlendFunc.one_minus_src_alpha)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                elif self.mode == SpriteStrokeDrawer.DM_SMUDGE:
                    if dab != dabs[0]:
                        canvas = BitmapLayer.current.get_canvas()
                        buffer = canvas.get_pixeldata(
                            Rect(
                                Point(
                                    int(self.dabs[i - 1].pos.x) -
                                    sprite.width / 2,
                                    int(self.dabs[i - 1].pos.y) -
                                    sprite.height / 2),
                                Size(sprite.width, sprite.height)))
                        surface = Surface(buffer)
                        # surface.set_blend_func_separate(BlendFunc.src_alpha, BlendFunc.one_minus_src_alpha,
                        #                                BlendFunc.one, BlendFunc.zero)
                        surface.set_alignment(Origin.center)
                        surface.set_alpha(0.5)
                        # surface.set_scale(DrawerProperties.current.get_size(),
                        #                 DrawerProperties.current.get_size())
                        surface.draw(dab.pos.x, dab.pos.y, gc.gc)
                else:
                    print("Error: SpriteStrokeDrawer: Unknown draw mode: ",
                          self.mode)
            else:
                if self.mode == SpriteStrokeDrawer.DM_NORMAL:
                    sprite.set_blend_func(BlendFunc.src_alpha,
                                          BlendFunc.one_minus_src_alpha)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                elif self.mode == SpriteStrokeDrawer.DM_ADDITION:
                    sprite.set_blend_func(BlendFunc.src_alpha, BlendFunc.one)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                elif self.mode == SpriteStrokeDrawer.DM_ERASE:
                    sprite.set_blend_func(BlendFunc.zero,
                                          BlendFunc.one_minus_src_alpha)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                elif self.mode == SpriteStrokeDrawer.DM_SMUDGE:
                    sprite.set_blend_func(BlendFunc.src_alpha,
                                          BlendFunc.one_minus_src_alpha)
                    sprite.draw(dab.pos.x, dab.pos.y, gc.gc)

                else:
                    print("Error: SpriteStrokeDrawer: Unknown draw mode:",
                          self.mode)
Exemple #8
0
 def get_bounding_rect(self):
     # FIXME: Do we need to handle its position here or does the Layer keep care of that?
     return Rect(Point(0, 0),
                 Size(self.surface.width, self.surface.height))
Exemple #9
0
 def test_level_resize(self):
     level = Level.from_file(test_levelfile)
     level.sectors[0].resize(Size(10, 10), Point(10, 10))
Exemple #10
0
 def on_callback(w, h, x, y):
     logging.info("Resize Callback")
     level.resize(Size(w, h), Point(x, y))
Exemple #11
0
 def on_a_key(x, y):
     pos = self.editor_map.screen2world(Point(x, y))
     rectobj = ObjMapRectObject(Rect(pos, Size(128, 64)),
                                Color(0, 255, 255, 155), None)
     self.workspace.get_map().metadata.objects.add_object(rectobj)
Exemple #12
0
    def paintEvent(self, event):
        if not self.parent.get_workspace().get_map():
            return

        if not self.parent or not self.parent.get_workspace():
            return

        painter = QPainter(self)
        gc = GraphicContext(painter)

        gc.push_modelview()

        # FIXME: Do this only on map changes
        if self.last_serial != self.parent.get_workspace().get_map(
        ).get_serial():
            # or editor_map != parent.get_workspace().get_map())
            self.update_minimap()
            self.last_serial = self.parent.get_workspace().get_map(
            ).get_serial()
            self.editor_map = self.parent.get_workspace().get_map()

        if True:
            # Draw background color
            gc.fill_rect(Rect(Point(0, 0), Size(self.width(), self.height())),
                         Color(200, 200, 200, 225))

        # FIXME: This doesn't work all that well
        tilemap = ToolContext.current.tilemap_layer
        if tilemap and tilemap.height != 0 and tilemap.width != 0:
            tile_size = tilemap.get_tileset().get_tile_size()

            map_width = tilemap.width * tile_size
            map_height = tilemap.height * tile_size

            small_tile_size = Size(tile_size * self.width() / map_width + 1,
                                   tile_size * self.height() / map_height + 1)

            field = tilemap.field

            # FIXME: No current tileset
            if False:
                for y in range(0, field.height):
                    for x in range(0, field.width):
                        tile = tilemap.get_tileset().create(field.at(x, y))
                        if tile:
                            gc.fill_rect(
                                Rect(
                                    Point((x * tile_size) * self.width() /
                                          map_width, (y * tile_size) *
                                          self.height() / map_height),
                                    small_tile_size), tile.get_color())

            if self.minimap_surface:
                self.minimap_surface.draw(
                    Rect(Point(0, 0), Size(self.width(), self.height())))

            # Draw cursor
            rect = self.parent.get_clip_rect()
            screen_rect = Rect(
                Point(rect.left * self.width() / map_width,
                      rect.top * self.height() / map_height),
                Size(rect.width * self.width() / map_width,
                     rect.height * self.height() / map_height))
            gc.fill_rect(screen_rect, Color(255, 255, 0, 50))
            gc.draw_rect(screen_rect, Color(0, 0, 0))

        gc.pop_modelview()
Exemple #13
0
def main():
    flexlay = Flexlay("Simple Editor")
    gui_manager = flexlay.create_gui_manager()

    config = Config.create("simple-editor")

    button_panel = gui_manager.create_button_panel(horizontal=True)

    def on_object_tool():
        workspace.set_tool(1, objtool)
        workspace.set_tool(2, workspace_move_tool)
        workspace.set_tool(3, TilePaintTool())

    button_panel.add_text("ObjectTool", on_object_tool)

    def on_tile_tool():
        workspace.set_tool(1, tilemap_paint_tool)
        workspace.set_tool(2, workspace_move_tool)
        workspace.set_tool(3, tilemap_paint_tool)

    button_panel.add_text("TileTool", on_tile_tool)

    def on_generic_dialog():
        dialog = gui_manager.create_generic_dialog("Generic Dialog")
        dialog.add_bool("An Bool:", True)
        dialog.add_int("An Int:", 5)
        dialog.add_float("An Int:", 5.0)
        dialog.add_string("An String:", "String")
        dialog.add_enum("An Enum:", ["String", "Foo", "bar"], 0)
        dialog.set_ok_callback(lambda: print(dialog.get_values()))

    button_panel.add_text("Generic Dialog", on_generic_dialog)

    menubar = gui_manager.create_menubar()

    file_menu = menubar.add_menu("&File")
    file_menu.add_item("Open...", lambda: print("Open"))
    file_menu.add_item("Save...", lambda: print("Save"))
    file_menu.add_item("Quit...", lambda: print("Quit"))

    view_menu = menubar.add_menu("&View")
    view_menu.add_item("Zoom In", lambda: print("Zoom In"))
    view_menu.add_item("Zoom Out", lambda: print("Zoom Out"))
    view_menu.add_item("Reset Zoom", lambda: print("Reset Zoom"))

    editor_map_component = gui_manager.create_editor_map_component()

    editormap = EditorMap()
    tileset = Tileset(32)
    tileset.add_tile(0, Tile(PixelBuffer.from_file("data/images/icons16/resize1.png"),
                             Sprite.from_file("data/images/icons16/resize1.png")))
    tileset.add_tile(1, Tile(PixelBuffer.from_file("data/images/icons16/resize_vert.png"),
                             Sprite.from_file("data/images/icons16/resize_vert.png")))

    tilemap = TilemapLayer(tileset, 20, 10)
    TilemapLayer.current = tilemap
    tilemap.set_draw_grid(True)
    object_layer = ObjectLayer()
    editormap.add_layer(tilemap)
    editormap.add_layer(object_layer)

    for i in range(20):
        obj = ObjMapRectObject(Rect(Point(random.randint(0, 499), random.randint(0, 499)),
                                    Size(32, 32)),
                               Color(0, 0, 255), None)
        object_layer.add_object(obj)

    workspace = editor_map_component.get_workspace()
    workspace.set_map(editormap)

    ToolContext()
    ToolContext.current.tile_brush.put(0, 0, 1)

    workspace_move_tool = WorkspaceMoveTool()
    objtool = ObjMapSelectTool(gui_manager)
    tilemap_paint_tool = TilePaintTool()
    # tilemap_paint_tool.set_brush(ToolContext.current.tile_brush)

    workspace.set_tool(1, objtool)
    workspace.set_tool(2, workspace_move_tool)
    # workspace.set_tool(1, tilemap_paint_tool)
    # workspace.set_tool(3, tilemap_paint_tool)

    object_selector = gui_manager.create_object_selector(40, 40)
    object_selector.add_brush(ObjectBrush(Sprite.from_file("data/images/icons16/resize1.png"), None))
    object_selector.add_brush(ObjectBrush(Sprite.from_file("data/images/icons16/resize2.png"), None))
    tile_selector = gui_manager.create_tile_selector()
    # tile_selector.set_tiles("All the tiles", tileset.get_tiles())
    # tile_selector.set_tiles("All the tiles again", tileset.get_tiles())
    # tile_selector.set_tiles("And again", tileset.get_tiles())

    print("Successs!")
    gui_manager.run()
Exemple #14
0
    def draw(self, gc, pos=Point()):
        if self.hidden:
            return
        tile_size = self.tileset.get_tile_size()

        if False and self.background_color.get_alpha() != 0:
            gc.fill_rect(Rect(pos,
                              Size(self.field.width * tile_size,
                                   self.field.height * tile_size)),
                         self.background_color)

        # The visible rectangle
        rect = Rect(gc.get_clip_rect())

        # max() here stops tiles off screen (below 0) from being drawn
        start_x = max(0, pos.x // tile_size)
        start_y = max(0, pos.y // tile_size)

        # min() here stops tiles off screen (above size of clip rect) from being drawn
        end_x = min(self.field.width + pos.x // tile_size, rect.right // tile_size + 1)
        end_y = min(self.field.height + pos.y // tile_size, rect.bottom // tile_size + 1)

        if self.foreground_color != Color(255, 255, 255, 255):
            for y in range(start_y, end_y):
                for x in range(start_x, end_x):
                    # The coordinates on the field are from 0 - field.width
                    tile_id = self.field.at(x - start_x, y - start_y)
                    if tile_id:
                        tile = self.tileset.create(tile_id)
                        if tile:  # skip transparent tile for faster draw
                            sprite = tile.get_sprite()
                            sprite.set_color(self.foreground_color)
                            sprite.draw(x * tile_size, y * tile_size, gc)

                            if self.draw_attribute:
                                gc.fill_rect(Rect(Point(x, y), Size(self.tileset.get_tile_size(),
                                                                    self.tileset.get_tile_size())),
                                             tile.get_attribute_color())
        else:
            for y in range(start_y, end_y):
                for x in range(start_x, end_x):
                    tile_id = self.field.at(x - start_x, y - start_y)
                    if tile_id:  # skip transparent tile for faster draw
                        tile = self.tileset.create(self.field.at(x - start_x, y - start_y))
                        if tile:
                            tile.get_sprite().draw(x * tile_size, y * tile_size, gc)

                            if self.draw_attribute:
                                gc.fill_rect(Rect(Point(x, y), Size(self.tileset.get_tile_size(),
                                                                    self.tileset.get_tile_size())),
                                             tile.get_attribute_color())

        if self.draw_grid:
            for y in range(start_y, end_y):
                gc.draw_line(start_x * tile_size,
                             y * tile_size,
                             end_x * tile_size,
                             y * tile_size,
                             Color(150, 150, 150))

            for x in range(start_x, end_x):
                gc.draw_line(x * tile_size,
                             start_y * tile_size,
                             x * tile_size,
                             end_y * tile_size,
                             Color(150, 150, 150))
Exemple #15
0
 def get_bounding_rect(self):
     return Rect(Point(0, 0),
                 Size(self.field.width * self.tileset.get_tile_size(),
                      self.field.height * self.tileset.get_tile_size()))