Code: Select all
init python:
import math
class SmoothHover(renpy.Displayable):
def __init__(self, text, action, text_colors, outline_colors, **kwargs):
super(SmoothHover, self).__init__(**kwargs)
self.hover = False
self.action = action
self.text_colors = text_colors
self.outline_colors = outline_colors
self.current_text_color = text_colors[0]
self.current_outline_color = outline_colors[0]
self.child = TextButton(text, clicked=action)
self.width = 0
self.height = 0
def render(self, width, height, st, at):
t = Transform(child=self.child, matrixcolor=TintMatrix(self.current_text_color))
child_render = renpy.render(t, width, height, st, at)
self.width, self.height = child_render.get_size()
render = renpy.Render(self.width, self.height)
render.blit(child_render, (0, 0))
return render
def event(self, ev, x, y, st):
if 0 <= x <= self.width and 0 <= y <= self.height:
self.hover = True
current_text_color = self.text_colors[1]
else:
self.hover = False
current_text_color = self.text_colors[0]
if self.current_text_color != current_text_color:
self.current_text_color = self.lerp_color(self.current_text_color, current_text_color, 0.09)
renpy.timeout(0)
renpy.redraw(self, 0)
return self.child.event(ev, x, y, st)
def visit(self):
return [ self.child ]
@staticmethod
def hex_to_rgb(hex_color):
return tuple(int(hex_color[i:i+2], 16) for i in (1, 3, 5))
@staticmethod
def rgb_to_hex(rgb):
return '#{:02x}{:02x}{:02x}'.format(*rgb)
@staticmethod
def colors_equal(color1, color2):
return color1[0] != color2[0] and color1[1] != color2[1] and color1[2] != color2[2]
@staticmethod
def lerp_color(color1, color2, t):
color1 = SmoothHover.hex_to_rgb(color1)
color2 = SmoothHover.hex_to_rgb(color2)
r = math.floor(color1[0] + (color2[0] - color1[0]) * t)
g = math.floor(color1[1] + (color2[1] - color1[1]) * t)
b = math.floor(color1[2] + (color2[2] - color1[2]) * t)
return SmoothHover.rgb_to_hex((r, g, b))
Code: Select all
add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"])