[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