Example #1
0
    def on_draw(self, _widget: Gtk.Widget, cr: cairo.Context) -> bool:
        if self.frozen:
            return False

        if not self.row.valid():
            if self.sig is not None:
                self.tw.disconnect(self.sig)
                self.sig = None

        assert self.tw.liststore is not None
        path = self.row.get_path()
        if path is None:
            return False

        path = self.tw.filter.convert_child_path_to_path(path)
        if path is None:
            return False

        # FIXME Use Gtk.render_background to render background.
        # However it does not use the correct colors/gradient.
        for col in self.columns:
            bg_rect = self.tw.get_background_area(path, col)
            rect = self.tw.get_cell_area(path, col)
            rect.y = bg_rect.y
            rect.height = bg_rect.height

            cr.rectangle(rect.x, rect.y, rect.width, rect.height)

        cr.clip()

        maybe_selected = self.tw.selected()
        if maybe_selected is not None:
            selected = self.tw.liststore.get_path(maybe_selected) == path
        else:
            selected = False

        stylecontext = self.tw.get_style_context()

        if selected:
            bg_color = stylecontext.get_background_color(
                Gtk.StateFlags.SELECTED)
        else:
            bg_color = stylecontext.get_background_color(Gtk.StateFlags.NORMAL)

        cr.set_source_rgb(bg_color.red, bg_color.green, bg_color.blue)
        cr.paint_with_alpha(1.0 - self.get_state())

        return False
Example #2
0
    def on_draw(self, _widget: Gtk.Widget, cr: cairo.Context) -> bool:
        if self.frozen:
            return False

        if not self.row.valid():
            self.tw.disconnect(self.sig)
            self.sig = None

        path = self.row.get_path()

        # FIXME Use Gtk.render_background to render background.
        # However it does not use the correct colors/gradient.
        for col in self.columns:
            bg_rect = self.tw.get_background_area(path, col)
            rect = self.tw.get_cell_area(path, col)
            rect.y = bg_rect.y
            rect.height = bg_rect.height

            cr.rectangle(rect.x, rect.y, rect.width, rect.height)

        cr.clip()

        selected = self.selection.get_selected()[1] and \
            self.tw.props.model.get_path(self.selection.get_selected()[1]) == path

        stylecontext = self.tw.get_style_context()

        if selected:
            bg_color = stylecontext.get_background_color(
                Gtk.StateFlags.SELECTED)
        else:
            bg_color = stylecontext.get_background_color(Gtk.StateFlags.NORMAL)

        cr.set_source_rgb(bg_color.red, bg_color.green, bg_color.blue)
        cr.paint_with_alpha(1.0 - self.get_state())

        return False
Example #3
0
    def draw(self, wdg, ctx: cairo.Context, do_translates=True):
        ctx.set_antialias(cairo.Antialias.NONE)
        ctx.scale(self.scale, self.scale)
        chunk_width = self.tiling_width * BPC_TILE_DIM
        chunk_height = self.tiling_height * BPC_TILE_DIM
        # Background
        if not self.use_pink_bg:
            ctx.set_source_rgb(0, 0, 0)
        else:
            ctx.set_source_rgb(1.0, 0, 1.0)
        ctx.rectangle(
            0, 0, self.width_in_chunks * self.tiling_width * BPC_TILE_DIM,
            self.height_in_chunks * self.tiling_height * BPC_TILE_DIM)
        ctx.fill()

        if self._tileset_drawer_overlay is not None and self._tileset_drawer_overlay.enabled:
            self._tileset_drawer_overlay.draw_full(ctx, self.mappings[0],
                                                   self.width_in_chunks,
                                                   self.height_in_chunks)
        else:
            # Layers
            for layer_idx, chunks_at_frame in enumerate(
                    self.animation_context.current()):
                if self.show_only_edited_layer and layer_idx != self.edited_layer:
                    continue
                current_layer_mappings = self.mappings[layer_idx]
                for i, chunk_at_pos in enumerate(current_layer_mappings):
                    if 0 < chunk_at_pos < len(chunks_at_frame):
                        chunk = chunks_at_frame[chunk_at_pos]
                        ctx.set_source_surface(chunk, 0, 0)
                        ctx.get_source().set_filter(cairo.Filter.NEAREST)
                        if self.edited_layer != -1 and layer_idx > 0 and layer_idx != self.edited_layer:
                            # For Layer 1 if not the current edited: Set an alpha mask
                            ctx.paint_with_alpha(0.7)
                        else:
                            ctx.paint()
                    if (i + 1) % self.width_in_chunks == 0:
                        # Move to beginning of next line
                        if do_translates:
                            ctx.translate(
                                -chunk_width * (self.width_in_chunks - 1),
                                chunk_height)
                    else:
                        # Move to next tile in line
                        if do_translates:
                            ctx.translate(chunk_width, 0)

                # Move back to beginning
                if do_translates:
                    ctx.translate(0, -chunk_height * self.height_in_chunks)

                if (self.edited_layer != -1 and layer_idx < 1 and layer_idx != self.edited_layer) \
                    or (layer_idx == 1 and self.dim_layers) \
                    or (layer_idx == 0 and self.animation_context.num_layers < 2 and self.dim_layers):
                    # For Layer 0 if not the current edited: Draw dark rectangle
                    # or for layer 1 if dim layers
                    # ...or for layer 0 if dim layers and no second layer
                    ctx.set_source_rgba(0, 0, 0, 0.5)
                    ctx.rectangle(
                        0, 0, self.width_in_chunks * self.tiling_width *
                        BPC_TILE_DIM, self.height_in_chunks *
                        self.tiling_height * BPC_TILE_DIM)
                    ctx.fill()

        # Col 1 and 2
        for col_index, should_draw in enumerate(
            [self.draw_collision1, self.draw_collision2]):
            if should_draw:
                if col_index == 0:
                    ctx.set_source_rgba(1, 0, 0, 0.4)
                    col: Sequence[bool] = self.collision1  # type: ignore
                else:
                    ctx.set_source_rgba(0, 1, 0, 0.4)
                    col = self.collision2  # type: ignore

                for i, c in enumerate(col):
                    if c:
                        ctx.rectangle(0, 0, BPC_TILE_DIM, BPC_TILE_DIM)
                        ctx.fill()
                    if (i + 1) % self.width_in_tiles == 0:  # type: ignore
                        # Move to beginning of next line
                        if do_translates:
                            ctx.translate(-BPC_TILE_DIM *
                                          (self.width_in_tiles - 1),
                                          BPC_TILE_DIM)  # type: ignore
                    else:
                        # Move to next tile in line
                        if do_translates:
                            ctx.translate(BPC_TILE_DIM, 0)
                # Move back to beginning
                if do_translates:
                    ctx.translate(0, -BPC_TILE_DIM *
                                  self.height_in_tiles)  # type: ignore

        # Data
        if self.draw_data_layer:
            ctx.select_font_face("monospace", cairo.FONT_SLANT_NORMAL,
                                 cairo.FONT_WEIGHT_NORMAL)
            ctx.set_font_size(6)
            ctx.set_source_rgb(0, 0, 1)
            assert self.data_layer is not None
            for i, dat in enumerate(self.data_layer):
                if dat > 0:
                    ctx.move_to(0, BPC_TILE_DIM - 2)
                    ctx.show_text(f"{dat:02x}")
                if (i + 1) % self.width_in_tiles == 0:  # type: ignore
                    # Move to beginning of next line
                    if do_translates:
                        ctx.translate(-BPC_TILE_DIM *
                                      (self.width_in_tiles - 1),
                                      BPC_TILE_DIM)  # type: ignore
                else:
                    # Move to next tile in line
                    if do_translates:
                        ctx.translate(BPC_TILE_DIM, 0)
            # Move back to beginning
            if do_translates:
                ctx.translate(0, -BPC_TILE_DIM *
                              self.height_in_tiles)  # type: ignore

        size_w, size_h = self.draw_area.get_size_request()
        size_w /= self.scale
        size_h /= self.scale
        # Selection
        if self.interaction_mode == DrawerInteraction.CHUNKS:
            self.selection_plugin.set_size(self.tiling_width * BPC_TILE_DIM,
                                           self.tiling_height * BPC_TILE_DIM)
        else:
            self.selection_plugin.set_size(BPC_TILE_DIM, BPC_TILE_DIM)
        self.selection_plugin.draw(ctx, size_w, size_h, self.mouse_x,
                                   self.mouse_y)

        # Tile Grid
        if self.draw_tile_grid:
            self.tile_grid_plugin.draw(ctx, size_w, size_h, self.mouse_x,
                                       self.mouse_y)

        # Chunk Grid
        if self.draw_chunk_grid:
            self.chunk_grid_plugin.draw(ctx, size_w, size_h, self.mouse_x,
                                        self.mouse_y)
        return True