找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 971|回复: 0

[原创] Magnify:可放大组件

[复制链接]
发表于 2024-8-16 23:16:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 ZYKsslm 于 2024-8-17 23:52 编辑

Magnify:可放大组件



因为有人在群里放了个链接,问如何实现一个放大镜,我看了之后发现写得不太好,于是就自己写了一个可视组件。


源码:
[RenPy] 纯文本查看 复制代码
python early:

    import pygame


    class Magnifying(renpy.Displayable):
        
        def __init__(self, diff_image=None, border=True, zoom_size=(50, 50), zoom_factor=1.5, **properties):
            super().__init__(**properties)
            
            self.zoom_size = zoom_size
            self.zoom_factor = zoom_factor
            self.diff_image = diff_image
            self.border = border
            self.zoom_displayable = None
            self.size = (0, 0)
            self.is_pressed = False
            self.mouse_pos = (0, 0)
            self.magnify_child = None

        def render(self, width, height, st, at):
            render = renpy.Render(width, height)
            child_render = renpy.render(self.child, width, height, st, at)
            self.size = child_render.get_size()
            render.blit(child_render, (0, 0))

            if self.is_pressed:
                if self.border:
                    canvas = render.canvas()
                    rect_pos = (self.mouse_pos[0] - 1, self.mouse_pos[1] - 1)
                    rect_size = (self.zoom_size[0] + 2, self.zoom_size[1] + 2)
                    canvas.rect(color=(0, 0, 0), rect=(*rect_pos, *rect_size), width=1)

                child = self.diff_image or self.child

                if self.zoom_factor == 1:
                    magnify_crop = Crop((*self.mouse_pos, *self.zoom_size), child)
                elif self.zoom_factor > 1:
                    magnify_crop = Crop((*self.mouse_pos, *self.zoom_size), child, zoom=self.zoom_factor)
                    magnify_crop = Crop((0, 0, *self.zoom_size), magnify_crop)
                elif self.zoom_factor < 1:
                    size = (int(self.zoom_size[0] / self.zoom_factor), int(self.zoom_size[1] / self.zoom_factor))
                    magnify_crop = Crop((*self.mouse_pos, *size), child, zoom=self.zoom_factor)

                magnify_render = renpy.render(magnify_crop, *self.zoom_size, st, at)
                render.blit(magnify_render, self.mouse_pos)

            return render

        def event(self, ev, x, y, st):
            if not ((0 <= x <= self.size[0]) and (0 <= y <= self.size[1])):
                return

            if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1:
                self.is_pressed = True

            elif ev.type == pygame.MOUSEBUTTONUP and ev.button == 1:
                self.is_pressed = False

            if self.is_pressed:
                self.mouse_pos = (x, y)
                if x + self.zoom_size[0] > self.size[0]:
                    self.mouse_pos = (self.size[0] - self.zoom_size[0], y)
                if y + self.zoom_size[1] > self.size[1]:
                    self.mouse_pos = (x, self.size[1] - self.zoom_size[1])

            renpy.redraw(self, 0)

        def add(self, i):
            self.child = i

        def visit(self):
            return [self.child]

        
    renpy.register_sl_displayable("magnify", Magnifying, "magnifying", 1
    ).add_property("diff_image"
    ).add_property("border"
    ).add_property("zoom_size"
    ).add_property("zoom_factor")


style magnifying:
    is fixed




使用方法:
  • 将源码复制到游戏目录中的 rpy 脚本文件中
  • 或者下载附件放到游戏目录中


这个不止能做放大镜哦,还能做类似壁纸引擎的xray差分(你懂的)🥵🥵🥵🥵

00Magnify.rpy

3.14 KB, 阅读权限: 10, 下载次数: 5, 下载积分: 活力 100

评分

参与人数 1干货 +2 收起 理由
烈林凤 + 2 感谢分享!

查看全部评分

本帖被以下淘专辑推荐:

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号|苏公网安备 32092302000068号 )

GMT+8, 2024-12-22 14:12 , Processed in 0.180145 second(s), 30 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表