Page 1 of 1

Making a parallax effect react to the buttons focused rather than the mouse?

Posted: Fri Apr 19, 2024 9:00 pm
by tim640
Hi guys!

So i've spend the night on this, but can't seem to solve the problem. I have an imagemap screen with multiple hotspots (an "explorable" location), which i show on a layer with a parallax effect applied to it. The code is this:

Code: Select all

init python:
    class MouseParallax(renpy.Displayable):
        def __init__(self,layer_info):
            super(renpy.Displayable,self).__init__()
            self.xoffset,self.yoffset=0.0,0.0
            self.sort_layer=sorted(layer_info,reverse=True)
            cflayer=[]
            masteryet=False
            for m,n in self.sort_layer:
                if(not masteryet)and(m<0):
                    cflayer.append("master")
                    masteryet=True
                cflayer.append(n)
            if not masteryet:
                cflayer.append("master")
            cflayer.extend(["transient","screens","overlay"])
            config.layers=cflayer
            config.overlay_functions.append(self.overlay)
            return
        def render(self,width,height,st,at):
            return renpy.Render(width,height)
        def parallax(self,m):
            func = renpy.curry(trans)(disp=self, m=m)
            return Transform(function=func)
        def trans(d,st,at):
            d.xoffset,d.yoffset=(int)(m*self.xoffset),(int)(m*self.yoffset)
            return 0
        def overlay(self):
            ui.add(self)
            for m,n in self.sort_layer:
                renpy.layer_at_list([self.parallax(m)],n)
            return
        def event(self,ev,x,y,st):
            import pygame
            if ev.type==pygame.MOUSEMOTION:
                self.xoffset,self.yoffset=((float)(x)/(config.screen_width))-0.5,((float)(y)/(config.screen_height))-0.5
            return
    MouseParallax([(-50,"l4"),(-100,"l3"),(-150,"l2"),(-200,"l1")])

    def trans(d, st, at, disp=None, m=None):
        d.xoffset, d.yoffset = int(round(m*disp.xoffset)), int(round(m*disp.yoffset))
        return 0
    
And it works great, honestly, no problem here. However whenever you play with a keyboard or a gamepad, focusing on hotspots, the effect doesn't work, since, well, obviously it's tracking the mouse, not focused elements. My first idea was to use the MouseMove() on "hovered" hotspot, and it works, but ofc if you return to the mouse, the hotspot won't let it go since it's hovered and it drags the mouse back to it.

Is there a better way to do this? since this code is a written class, did anyone tried writing something similar for button focus? ButtonParallax?.. Really at a loss here.

The goal is to move the screen accordingly with the hotpot in focus.

Thank you in advance!

Re: Making a parallax effect react to the buttons focused rather than the mouse?

Posted: Thu Apr 25, 2024 2:01 pm
by tim640
No luck so far :(

Since the goal is to move the parallax screen around to "explore" the location, in the meanwhile i've tried to make a solution for a gamepad by doing MouseMove() to the corners of the screen according to the movement of the joystick, but a) it works only on PC and won't work on Switch\Mobile and b) it just sucks lmao, going over all the interactable points and triggering them on it's way is pretty annoying.

Is there really no way to move the screen basing on the focused hotspots?

Re: Making a parallax effect react to the buttons focused rather than the mouse?

Posted: Sun Apr 28, 2024 3:03 am
by Kia
I think you can use `hovered, unhovered` to trigger a function that moves your parallax, if you move mouse detection outside of your main code and have it receive the x,y coordination from those functions

Re: Making a parallax effect react to the buttons focused rather than the mouse?

Posted: Fri May 10, 2024 5:54 am
by TaylorSullivan
Any update on the problem?