[Solved]Opacity update after slider moving

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
Nazon
Regular
Posts: 32
Joined: Thu Sep 22, 2016 10:03 am
Contact:

[Solved]Opacity update after slider moving

#1 Post by Nazon »

I tried to create bar slider, that could change the frame opacity in real time according to this instruction:
viewtopic.php?f=8&t=22229

But something goes wrong. (This issue looks slightly different than original, so I created a new topic.)
The code:

Code: Select all

init python: 
    style.opaqbar = Style(style.default)    
    style.opaqbar.idle_left_bar = Frame(config_font_meter_off, 0,0) 
    style.opaqbar.idle_right_bar = Frame(config_font_meter_off, 0,0)
    style.opaqbar.hover_left_bar = Frame(config_font_meter_on, 0,0) 
    style.opaqbar.hover_right_bar = Frame(config_font_meter_on, 0,0) 
    style.opaqbar.xmaximum = opaqbar_x
    style.opaqbar.ymaximum = opaqbar_y
    style.opaqbar.idle_thumb = config_meter_handle_off
    style.opaqbar.hover_thumb = config_meter_handle_on
    style.opaqbar.thumb_shadow = None
    style.opaqbar.thumb_offset = 16 #half of the wide of the thumb image

    style.opaq_mask = Style(style.default)    
    style.opaq_mask.background = Transform(Frame(config_opaque_mask, 0,0), alpha=persistent.saywindow_opacity)

Code: Select all

screen fontback_opaque_bar:
    bar:
        value FieldValue(persistent, "saywindow_opacity", range=1.0, style="slider")
        xpos x_p ypos y_p 
        style "opaqbar"
        changed update_mask_opacity()


screen window_opaque_test:    
    frame:
        style "opaq_mask"
        pos(x_f, y_f)

Code: Select all

init -2 python:
     #Function for the frame opacity state updating 
     def update_mask_opacity():
            style.opaq_mask.background = Transform(Frame(config_opaque_mask, 0,0), alpha=persistent.saywindow_opacity)
            
            style.rebuild()
The screen with the slider became unresponsive after running and I could not press any button.
Looks like the update_mask_opacity() function runs not only on slider position change, but repeatedly after the program run.
Is there another way to run style.rebuild() to update the opacity state of the frame?
Last edited by Nazon on Fri Jan 13, 2017 5:32 pm, edited 1 time in total.

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Opacity update after slider moving - unresponsive screen

#2 Post by nyaatrap »

Try: changed update_mask_opacity #without ()
Though style.rebuild is a slow operation, it's better to avoid to use it.

Nazon
Regular
Posts: 32
Joined: Thu Sep 22, 2016 10:03 am
Contact:

Re: Opacity update after slider moving - unresponsive screen

#3 Post by Nazon »

nyaatrap wrote:Try: changed update_mask_opacity #without ()
Though style.rebuild is a slow operation, it's better to avoid to use it.
Thank you for this piece of advice.
Unfortunately, it doesn't work this way.
The function simply doesn't run. If I try something like this:

Code: Select all

init -2 python:
     #Function for the frame opacity state updating
     def update_mask_opacity():
            style.opaq_mask.background = Transform(Frame(config_opaque_mask, 0,0), alpha=persistent.saywindow_opacity)
            print "Function run!"
            style.rebuild()

Code: Select all

screen fontback_opaque_bar:
    bar:
        value FieldValue(persistent, "saywindow_opacity", range=1.0, style="slider")
        xpos x_p ypos y_p
        style "opaqbar"
        changed update_mask_opacity
I don't see "Function run!" in the console window every time I move the slider.

It's strange though. If I use it like
changed update_mask_opacity()
the function runs, but not on slider knob position change.

Nazon
Regular
Posts: 32
Joined: Thu Sep 22, 2016 10:03 am
Contact:

Re: Opacity update after slider moving - unresponsive screen

#4 Post by Nazon »

Hmm...
Now it's look like a some kind of issue with 'changed' preference.
Every time I use it, the function works incorrectly.
For example, if I want to play a sound after moving a bar knob, I add a new preference.
Full code:

Code: Select all

screen title_menu:
    if (show_title_menu):        
        imagebutton idle title_option_off hover title_option_on xpos 870 ypos 546 focus_mask True hovered Play("audio", sys_onmouse) action [Play("audio", sys_onclick), ShowMenu('configs_scr')]

Code: Select all

screen configs_scr():
    tag menu
   
    use fontback_opaque_bar(x_p=355, y_p =198)

Code: Select all

init python: 
    style.opaq_mask = Style(style.default)    
    style.opaq_mask.background = Transform(Frame(config_opaque_mask, 0,0), alpha=persistent.saywindow_opacity)
    style.opaq_mask.xysize = alpha_back_size #size of config_opaque_mask
screen fontback_opaque_bar:
    bar:
        value FieldValue(persistent, "saywindow_opacity", range=1.0, style="slider")
        xpos x_p ypos y_p
     
        changed renpy.play(scroll_button_sys_onmouse, "audio") #the same with Play("audio", scroll_button_sys_onmouse)
        style "opaqbar"
And after running I hear the sound(scroll_button_sys_onmouse) playing every time I hover any imagebutton even on the title screen.
The same on the "configs_scr" screen, but the same sound is playing when I move any other slider knob(not only on the bar, described at "fontback_opaque_bar" screen).
May be I used the "changed" preference not in the right way?

Nazon
Regular
Posts: 32
Joined: Thu Sep 22, 2016 10:03 am
Contact:

Re: Opacity update after slider moving - unresponsive screen

#5 Post by Nazon »

I tried to do the same with the new empty renpy project.
Here is the part from the default screen.rpy

Code: Select all

        vbox:
            frame:
                style_group "pref"
                has vbox

                label _("Music Volume")
                bar:
                    value Preference("music volume")
                    changed renpy.play("/sounds/misc/scroll_button.b_Sys_OnMouse.wav", "audio")
I added only

Code: Select all

changed renpy.play("/sounds/misc/scroll_button.b_Sys_OnMouse.wav", "audio")
and scroll_button.b_Sys_OnMouse.wav to the project directory.
The result is quite the same.

I can hear the wav file playing not only when I try to move the music volume slider, but also during the screen changing and after clicking on buttons in the Preference menu.

I am still puzzled over this... Am I using the "changed" property wrong, or is it some kind of bug?
Attachments
test_changed_func.zip
(334.19 KiB) Downloaded 64 times

Nazon
Regular
Posts: 32
Joined: Thu Sep 22, 2016 10:03 am
Contact:

Re: Opacity update after slider moving - unresponsive screen

#6 Post by Nazon »

I've found a way out of this after all. At least changing window opacity works perfectly now.

The code from the RenPy for Dummies blog(thanks to Ruslan Nebykov):

options.rpy

Code: Select all

screen quick_menu:    
    hbox:
        style_group "quick"

        xalign 0.9
        yalign 0.98

        textbutton _("Transparency %s:" % (wnd_alpha_s())) action []
        bar adjustment wnd_alpha_adjust xmaximum 200 yoffset -4

init -2 python:
    style.quick_button.set_parent('default')
    style.quick_button.background = None
    style.quick_button.xpadding = 5

    style.quick_button_text.set_parent('default')
    style.quick_button_text.size = 18
    style.quick_button_text.outlines = [(2, "888d", 0, 0), (1, "000", 0, 0)]
    style.quick_button_text.idle_color = "#8888"
    style.quick_button_text.hover_color = "#ccc"
    style.quick_button_text.selected_idle_color = "#cc08"
    style.quick_button_text.selected_hover_color = "#cc0"
    style.quick_button_text.insensitive_color = "#4448"
script.rpy

Code: Select all

init python:
    # window background
    bg_wnd = 'textbox.png'
    # background height
    w, h = renpy.image_size(bg_wnd)
    style.window.yminimum = h
    # default transparency = 25% → alpha= 75%
    if persistent.wnd_alpha == None:
        persistent.wnd_alpha = .75
    def wnd_alpha_s():
        return str(100 - int(persistent.wnd_alpha * 100)) + "%"
    # bind function to background
    style.window.background = Frame(im.MatrixColor(bg_wnd, im.matrix.opacity(persistent.wnd_alpha)), 300, 60)
    def wnd_alpha_update(value):
        persistent.wnd_alpha = value
        style.window.background = Frame(im.MatrixColor(bg_wnd, im.matrix.opacity(persistent.wnd_alpha)), 300, 60)
        style.rebuild()
    # bind value to slider
    wnd_alpha_adjust = ui.adjustment(range=1.0, value=persistent.wnd_alpha, adjustable=True, changed=wnd_alpha_update)

    narrator = Character(' ')
    style.default.size = 24

init:
    image bg bgg = "bg_bgg.jpg"
label start:
    scene bg bgg
    "You can change window transparency using the slider."
   
    return
Still don't know what is going on with the changed property though...

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Opacity update after slider moving - unresponsive screen

#7 Post by xela »

xela wrote:You could forgo styles and work with the screen directly through (for example) a persistent value. It should work...
From the previous thread, it went unanswered :(

Code: Select all

define persistent.say_window_alpha = 1.0

screen say(who, what):
    style_prefix "say"

    window:
        background Transform(style.window.background, alpha=persistent.say_window_alpha)

Code: Select all

screen preferences():
    ...
                    label _("Auto-Forward Time")

                    bar value Preference("auto-forward time")
                    
                    label _("Say Window Alpha")
                    
                    bar value FieldValue(persistent, 'say_window_alpha', 1.0, max_is_zero=False, offset=0, step=1)
should do the same thing and the whole required setup is 3 lines of new code (4 with the label).
Like what we're doing? Support us at:
Image

Nazon
Regular
Posts: 32
Joined: Thu Sep 22, 2016 10:03 am
Contact:

Re: Opacity update after slider moving - unresponsive screen

#8 Post by Nazon »

xela wrote:
xela wrote:You could forgo styles and work with the screen directly through (for example) a persistent value. It should work...
From the previous thread, it went unanswered :(

should do the same thing and the whole required setup is 3 lines of new code (4 with the label).
Yes!
It works! Even better then the previous solution!
Thank you very much!

Post Reply

Who is online

Users browsing this forum: Galo223344, Google [Bot], Majestic-12 [Bot]