Viewport Scrollbars not working [SOLVED]

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
Milkymalk
Miko-Class Veteran
Posts: 752
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Viewport Scrollbars not working [SOLVED]

#1 Post by Milkymalk » Tue Jun 16, 2015 12:07 pm

Hi,
I'm trying to make a simple mastermind style game; however, the viewport scrollbar doesn't work. It reacts to hovering, but neither the mousewheel nor dragging it changes the viewport.
Either I missed something, or the event handler from the Hackermind() object interferes with it. What bugs me is that the scrollbar handle is very small even when the viewport is almost empty, is that normal? I would like the handle to reflect how much there actually is to scroll.
Another issue: The scrollbar is located at y=0 even though the viewport starts at y=50.

EDIT:
It's not the event handler's fault, the same happens when I use that screen with static images.

Code: Select all

init python:
    hack_pref = 'hacking/arrow_'
    hack_suff = '.png'
    renpy.image('arrow_0', hack_pref+'0'+hack_suff)
    renpy.image('arrow_1', hack_pref+'1'+hack_suff)
    renpy.image('arrow_2', hack_pref+'2'+hack_suff)
    renpy.image('arrow_3', hack_pref+'3'+hack_suff)
    renpy.image('arrow_blank', hack_pref+'5'+hack_suff)
    renpy.image('arrow_correct', hack_pref+'correct'+hack_suff)
    renpy.image('arrow_wrongpos', hack_pref+'wrongpos'+hack_suff)
    renpy.image('arrow_wrong', hack_pref+'wrong'+hack_suff)
    hack_arrowwidth = 60    
    
    class Hackermind(object):
        def __init__(self, digits, time, turns, commentslabel, returnlabel):
            import pygame
            self.digits = digits
            self.time = time
            self.turns = turns
            self.currentturn = 0
            self.currentdigit = 0
            self.commentslabel = commentslabel
            self.returnlabel = returnlabel
            self.code = []
            for i in range(digits):
                self.code.append(renpy.random.randint(0,3))
            self.guesses = []
            temp = []
            self.guesses.append(self.emptyguess())
            self.results = []
            self.cycle = SpriteManager(event=self.hackevent)
            #renpy.show(self.cycle, tag='cycle')
            renpy.call_screen('hackinggrid', self)
            return
            
        def analyse(self, input):
            result = [0, 0, 0]
            target = self.code*1
            for i in range(self.digits):
                if target[i] == input[i]:
                    result[0] += 1
                    target[i] = -1
                    input[i] = -1
            for i in range(self.digits):
                for j in range(self.digits):
                    if target[i] == input[j] and target[i] >= 0:
                        result[1] += 1
                        target[i] = -1
                        input[j] = -1
            result[2] = self.digits - result[0] - result[1]
            return result

        def emptyguess(self):
            temp = []
            for i in range(self.digits):
                temp.append(5)
            return temp
            
        def hackevent(self, ev, x, y, st):
            # We only care about keydown events.
            if ev.type != pygame.KEYDOWN:
                return

            # If it's not the key we want, go back
            if ev.key not in [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT]:
                return

            # Otherwise, add key to sequence
            if ev.key == pygame.K_UP:
                temp = 0
            elif ev.key == pygame.K_RIGHT:
                temp = 1
            elif ev.key == pygame.K_DOWN:
                temp = 2
            elif ev.key == pygame.K_LEFT:
                temp = 3
            self.guesses[self.currentturn][self.currentdigit] = temp
            self.currentdigit += 1
            if self.currentdigit == self.digits:
                self.results.append(self.analyse(self.guesses[self.currentturn]))
                self.currentturn += 1
                self.currentdigit = 0
                self.guesses.append(self.emptyguess())
            renpy.restart_interaction()
            return


screen hackinggrid(hackgame):
    modal True
    viewport id "guesslist":
        mousewheel True
        scrollbars "vertical"
        area (50, 50, 1000, 700)
        add hackgame.cycle
        for i in range(len(hackgame.guesses)):
            for j in range(hackgame.digits):
                if hackgame.guesses[i][j] == 0:
                    add 'arrow_0' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 1:
                    add 'arrow_1' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 2:
                    add 'arrow_2' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 3:
                    add 'arrow_3' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 5:
                    add 'arrow_blank' xpos 20+j*62 ypos 20+i*70
            if hackgame.currentturn > i:
                for j in range(hackgame.digits):
                    if j < hackgame.results[i][0]:
                        add 'arrow_correct' xpos 700+(j/2)*30 ypos 20+(((j+1)%2==0)*30)+i*70
                    elif j >= hackgame.results[i][0] and j < hackgame.results[i][0]+hackgame.results[i][1]:
                        add 'arrow_wrongpos' xpos 700+(j/2)*30 ypos 20+(((j+1)%2==0)*30)+i*70
                    else:
                        add 'arrow_wrong' xpos 700+(j/2)*30 ypos 20+(((j+1)%2==0)*30)+i*70
To run it, just use:

Code: Select all

    $ game = Hackermind(4, 0, 0, "", "")
This creates a 4-digit code. I attached all image files below.
Attachments
hacking.rar
Extract to "Game/hacking/"
(13.9 KiB) Downloaded 15 times
Last edited by Milkymalk on Tue Jun 16, 2015 8:30 pm, edited 1 time in total.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Milkymalk
Miko-Class Veteran
Posts: 752
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Viewport Scrollbars not working

#2 Post by Milkymalk » Tue Jun 16, 2015 2:27 pm

EDIT:
This is semi-solved. I replaced the automatic scrollbar with a vbar (last line) and added child_size:

Code: Select all

screen hackinggrid(hackgame):
    modal True
    viewport id "guesslist":
        area (50, 50, 900, 500)
        mousewheel True
        child_size (900, len(hackgame.guesses)*70+30)
        add hackgame.cycle
        for i in range(len(hackgame.guesses)):
            for j in range(hackgame.digits):
                if hackgame.guesses[i][j] == 0:
                    add 'arrow_0' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 1:
                    add 'arrow_1' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 2:
                    add 'arrow_2' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 3:
                    add 'arrow_3' xpos 20+j*62 ypos 20+i*70
                elif hackgame.guesses[i][j] == 5:
                    add 'arrow_blank' xpos 20+j*62 ypos 20+i*70
            if hackgame.currentturn > i:
                for j in range(hackgame.digits):
                    if j < hackgame.results[i][0]:
                        add 'arrow_correct' xpos 700+(j/2)*30 ypos 20+(((j+1)%2==0)*30)+i*70
                    elif j >= hackgame.results[i][0] and j < hackgame.results[i][0]+hackgame.results[i][1]:
                        add 'arrow_wrongpos' xpos 700+(j/2)*30 ypos 20+(((j+1)%2==0)*30)+i*70
                    else:
                        add 'arrow_wrong' xpos 700+(j/2)*30 ypos 20+(((j+1)%2==0)*30)+i*70
    vbar value YScrollValue("guesslist") pos (950, 50) ysize 500
Now, how can I make it always scroll right to the end instead of the top? I tried "yinitial 1.0", but it does nothing. My guess is that maybe it actually works, but when the screen is shown y=1.0 is actually at the top as the images are only added after the screen is initially shown. But shouldn't the "renpy.restart_interaction()" redraw everything and re-evaluate the screen?
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Viewport Scrollbars not working

#3 Post by Alex » Tue Jun 16, 2015 4:21 pm

Try to set yinitial parameter to 1.0.
Also, is it ok to have an "area" inside a viewport?
http://www.renpy.org/doc/html/screens.html#viewport

User avatar
Milkymalk
Miko-Class Veteran
Posts: 752
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Viewport Scrollbars not working

#4 Post by Milkymalk » Tue Jun 16, 2015 4:49 pm

I tried that, just edited it in. It did nothing, but it works when the content of the viewport exceeds the visible area when the screen is first shown. I add more elements and then renpy.restart_interaction(), so the initial value isn't used again.

"area (a, b, c, d)" is a position style property, short for "pos (a, b) xysize (c, d)", so it should be okay. At least it does what it's supposed to do.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
SinnyROM
Regular
Posts: 166
Joined: Mon Jul 08, 2013 12:25 am
Projects: Blue Birth
Organization: Cosmic Static Games
Contact:

Re: Viewport Scrollbars not working

#5 Post by SinnyROM » Tue Jun 16, 2015 5:53 pm

I had this issue not long ago. http://lemmasoft.renai.us/forums/viewto ... =8&t=32584
Try setting the adjustment value to the range instead of 1.0?

User avatar
Milkymalk
Miko-Class Veteran
Posts: 752
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Viewport Scrollbars not working

#6 Post by Milkymalk » Tue Jun 16, 2015 8:29 pm

I took a swift look over your thread and will study it in detail (looks quite enlightening about the adjustment class), but this won't work here because I went the direkt approach.
As I said, it works fine the moment the screen is shown the first time, just not when it updates. Maybe it's not possible without the adjustment class?

I worked around it now by having the most recent results on top of the viewport instead of at the bottom :D But thank you for your input, I'm sure I will need adjustments again some time, and your code looks much easier to understand than the one in the documentation...
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Post Reply

Who is online

Users browsing this forum: Ocelot