Pressing and releasing buttons.

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
gofa4a
Newbie
Posts: 10
Joined: Thu Jan 02, 2020 6:47 am
Contact:

Pressing and releasing buttons.

#1 Post by gofa4a »

Is there a way to make a button press action and a button release action?

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Pressing and releasing buttons.

#2 Post by _ticlock_ »

gofa4a wrote: Sun Nov 20, 2022 3:05 pm Is there a way to make a button press action and a button release action?
Release action is just a normal button action.

There is no simple way to do a button press action. For example, "press" event is used to distinguish touch and long touch that correspond to normal action and alternate action.

Here is rough example how you can create such functionality using CDD:

Code: Select all

init python:
    import pygame
    class OnPressAction(renpy.Displayable):
        def __init__(self, action, button_dispalyable, **kwargs):
            super().__init__(**kwargs)
            self.action = action
            self.width = 0
            self.height = 0
            self.child = button_dispalyable
        def render(self, width, height, st, at):
            t = Transform(child=self.child, alpha=0)
            cr = renpy.render(t, width, height, st, at)
            self.width, self.height = cr.get_size()
            r = renpy.Render(self.width, self.height)
            r.blit(cr, (0, 0))
            return r
        def event(self, ev, x, y, st):
            if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1:
                if x > 0 and x < self.width and y > 0 and y < self.height:
                    return renpy.run(self.action)
And screen example:

Code: Select all

screen test:
    default var = 0
    label "[var]" xalign 0.5 yalign 0.0
    fixed:
        xfit True yfit True
        xalign 0.5 yalign 0.5
        imagebutton:
            idle Solid("#f00", xysize=(200,200))
            hover Solid("#ff0", xysize=(200,200))
            action SetScreenVariable("var", var + 10)
        add OnPressAction(SetScreenVariable("var", var + 1), Solid("#f00", xysize=(200,200)))

Code: Select all

label start:
    call screen test
You can treat fixed as a button with a press action that is done by OnPressAction CDD and release action that is done by imagebutton. If you want to apply transform you need to apply to the fixed:

Code: Select all

transform some_transform:
    zoom 2.0
screen test:
    default var = 0
    label "[var]" xalign 0.5 yalign 0.0
    fixed:
        at some_transform
        ...

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot, Shie