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

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
tim640
Regular
Posts: 49
Joined: Wed May 23, 2018 4:47 pm
Contact:

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

#1 Post 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!

User avatar
tim640
Regular
Posts: 49
Joined: Wed May 23, 2018 4:47 pm
Contact:

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

#2 Post 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?

User avatar
Kia
Eileen-Class Veteran
Posts: 1050
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

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

#3 Post 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

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot]