예제 #1
0
    def snapshot(self,
                 snapshot: Gtk.Snapshot,
                 bounds: Graphene.Rect,
                 fgcolor: Gdk.RGBA, scale: float):
        # Drawing cached textures is in large scale faster than rendering
        # the svg over and over
        if self._cached_texture_scale < scale:
            scaled_w = int(WIDTH * scale)
            scaled_h = int(HEIGHT * scale)

            cr = cairo.ImageSurface(
                cairo.Format.ARGB32, scaled_w, scaled_h)
            ctx = cairo.Context(cr)
            rect = Rsvg.Rectangle()
            rect.x = 0
            rect.y = 0
            rect.width = scaled_w
            rect.height = scaled_h
            self._handle.render_document(ctx, rect)

            pixbuf = Gdk.pixbuf_get_from_surface(cr, 0, 0, scaled_w, scaled_h)
            self._cached_texture = Gdk.Texture.new_for_pixbuf(pixbuf)
            self._cached_texture_scale = scale

        snapshot.append_texture(self._cached_texture, bounds)
예제 #2
0
def append_to_snapshot(cmd: lib.Command,
                       snapshot: Gtk.Snapshot, x, y,
                       widget: Gtk.Widget, colors: lib.icon.CommandColorScheme,
                       code: lib.CodeBuffer = None) -> int:
    defin: lib.CommandDefinition = cmd.definition
    bg, fg = colors[defin.color]
    # Extend the width of the icon for icons with data
    data_layout, icon_width = _create_data_layout(cmd, widget)

    area = Graphene.Rect.init(
        Graphene.Rect(), x, y, icon_width * icon.WIDTH, icon.HEIGHT)

    # Background
    snapshot.append_color(bg, area)
    # Foreground icon
    if not (defin.data_only and cmd.data):
        if icon_width > 1:
            icon_area = Graphene.Rect.init(
                Graphene.Rect(), x + (icon_width - 1) * icon.WIDTH / 2, y,
                icon.WIDTH, icon.HEIGHT)
        else:
            icon_area = area
        defin.icon.snapshot(snapshot, icon_area, fg, widget.get_scale_factor())
    # Data
    if cmd.data and defin.show_data:
        if defin.id == 'img' and code is not None:
            texture = code.get_command_data_preview(cmd)
            if texture is not None:
                t_scale = max(
                    1,
                    texture.get_width() / area.get_width(),
                    texture.get_height() / area.get_height())
                ta_width = texture.get_width() / t_scale
                ta_height = texture.get_height() / t_scale
                t_area = Graphene.Rect.init(
                    Graphene.Rect(),
                    area.get_x() + area.get_width() / 2 - ta_width / 2,
                    area.get_y() + area.get_height() / 2 - ta_height / 2,
                    ta_width, ta_height)
                snapshot.append_texture(texture, t_area)
        if defin.id == 'color':
            data_color = utils.rgba(f'rgb({cmd.data})')
            layout = widget.create_pango_layout('⬤')
            _append_layout(
                snapshot, layout, icon.FONT_NORMAL, data_color, area)
        if data_layout:
            _append_layout(
                snapshot, data_layout, None, fg, area,
                0 if defin.data_only else 5)
    return icon_width
예제 #3
0
    def do_snapshot(self, snapshot: Gtk.Snapshot):
        # Scroll
        tx = -int(self.props.hadjustment.props.value)
        ty = -int(self.props.vadjustment.props.value)

        if self.scenebuffer is None or self.scenebuffer.scene_name is None:
            snapshot.render_background(self.get_style_context(), 0, 0,
                                       self.get_width(), self.get_height())
            return

        area = Graphene.Rect().init(0, 0, self.get_width(), self.get_height())

        snapshot.append_color(self.BACKGROUND_COLOR, area)

        snapshot.push_clip(area)

        # Background
        scene_area = Graphene.Rect().init(tx, ty,
                                          self.scenebuffer.props.scene_width,
                                          self.scenebuffer.props.scene_height)
        snapshot.append_color(self.SCENE_COLOR, scene_area)
        # Sprites
        for s in self.scenebuffer.scene_sprites:
            sprite_area, texture = self.get_sprite_info(s)
            if texture is not None:
                snapshot.append_texture(texture, sprite_area)
            else:
                snapshot.append_color(self.MISSING_TEXTURE_COLOR, sprite_area)
        # Selection
        if self.props.selection is not None:
            selection_area = self.get_sprite_info(self.props.selection)[0]
            snapshot.append_color(self.SELECTION_COLOR, selection_area)

        snapshot.pop()

        snapshot.render_focus(self.get_style_context(), 0, 0, self.get_width(),
                              self.get_height())
예제 #4
0
    def snapshot(self,
                 snapshot: Gtk.Snapshot,
                 bounds: Graphene.Rect,
                 fgcolor: Gdk.RGBA, scale: float):
        if (self._cached_texture_scale < scale
                or not self._fgcolor.equal(fgcolor)):
            PangoCairo.context_set_resolution(
                _text_texture_pango_ctx, 96 * scale)
            layout = Pango.Layout.new(_text_texture_pango_ctx)
            layout.set_text(self._text)
            layout.set_font_description(FONT_NORMAL)
            layout_width, layout_height = layout.get_pixel_size()

            scaled_w = int(WIDTH * scale)
            scaled_h = int(HEIGHT * scale)

            cr = cairo.ImageSurface(
                cairo.Format.ARGB32,
                scaled_w, scaled_h)
            ctx = cairo.Context(cr)
            Gdk.cairo_set_source_rgba(ctx, fgcolor)

            x = int(scaled_w / 2 - layout_width / 2)
            y = int(scaled_h / 2 - layout_height / 2)
            ctx.move_to(x, y)

            PangoCairo.show_layout(ctx, layout)

            pixbuf = Gdk.pixbuf_get_from_surface(
                cr, 0, 0, scaled_w, scaled_h)

            del self._cached_texture
            self._cached_texture = Gdk.Texture.new_for_pixbuf(pixbuf)
            self._cached_texture_scale = scale
            self._fgcolor = fgcolor

        snapshot.append_texture(self._cached_texture, bounds)