ui as a displayable : Solved

In this forum we discuss the future of Ren'Py, both bug fixes and longer-term development. Pre-releases are announced and discussed here.
Post Reply
Message
Author
jdeisenberg
Newbie
Posts: 14
Joined: Tue Mar 16, 2010 7:18 pm
Location: San Jose, CA
Contact:

ui as a displayable : Solved

#1 Post by jdeisenberg »

My script contains a Python function that creates a ui.fixed depending on certain variables. Here's an oversimplified version of the function:

Code: Select all

def make_drawing(a, b):
  ui.fixed()
  if (a < b):
    ui.image("a.jpg", xpos=100, ypos=50)
  else:
    ui.image("b.jpg", xpos=150, ypos=20)
  # more code involving loops and other stuff (not just a simple "if" statement)
  ui.close()
In my script, I do something like this:

Code: Select all

$ make_drawing(3, 5)
joe "Wow, 3 really is less than 5."
fred "You got that right!"
The problem is that the image created by make_drawing() disappears after joe's dialog. I'd like to be able to make the code generate a displayable so that I could say (pseudo-code)

Code: Select all

$ displayable_thing = make_drawing(3, 5)
show displayable_thing
joe "Wow, 3 really is less than 5."
fred "Yes, and the diagram is still there, too!"
If someone can point me at the correct part of the reference manual to read, that would be great. Sample code would work for me too! :D
Last edited by jdeisenberg on Tue Jun 22, 2010 1:52 pm, edited 1 time in total.

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: ui as a displayable

#2 Post by Jake »

jdeisenberg wrote: The problem is that the image created by make_drawing() disappears after joe's dialog. I'd like to be able to make the code generate a displayable so that I could say (pseudo-code)

Code: Select all

$ displayable_thing = make_drawing(3, 5)
show displayable_thing
joe "Wow, 3 really is less than 5."
fred "Yes, and the diagram is still there, too!"
All of the ui.* functions have Displayable equivalents... (or more accurately, I believe, Displayable classes which are used by the ui.* classes to create the UI).

If you check the Layout section of the Displayables page of the manual it'll show you how to create the Displayable instances you need - so you'll create the Image you want first, and then create the Fixed and pass the Image as the first parameter.

However, because you're not using the Ren'Py 'image' keyword and you [presumably] won't be doing this in an init block anyway, you won't be able to just use Ren'Py-script to "show myDisplayableVariableName"; instead you can call renpy.show from Python, passing in a string tag name by which you identify your displayed Displayable (and can use later to hide it with renpy.hide), and the Displayable instance you created as the 'what' parameter. If you need to position it, you could pass a 'style' parameter to each UI element you create, or you can pass a Transform object in a list into the 'at_list' parameter, e.g.:

Code: Select all

  python:
    myFixed = Fixed(...)
    tran = Transform(xpos=0.25, ypos=0.5, xanchor=0.5, yanchor=0.5)
    renpy.show("myFixedImageTag", what=myFixed, at_list=[tran])
  joe "Hey, there's the diagram"
  fred "Indeed. Pretty cool!"
  
  $ renpy.hide("myFixedImageTag")
  joe "Now it's gone."
Bear in mind that if you just show stuff like that, it'll go into the 'master' layer. UI normally goes into the 'transient' layer (which IIRC gets cleared automatically after every interaction, such as clicking away dialogue, which is why it was disappearing after one line before), which is above 'master', so a UI element displayed like that will go behind the dialogue box. If you want it in front (without adding new layers yourself) then you could add the parameter 'layer="overlay"' to both the 'renpy.show' and the 'renpy.hide' calls.
Server error: user 'Jake' not found

jdeisenberg
Newbie
Posts: 14
Joined: Tue Mar 16, 2010 7:18 pm
Location: San Jose, CA
Contact:

Re: ui as a displayable

#3 Post by jdeisenberg »

Jake wrote: If you check the Layout section of the Displayables page of the manual it'll show you how to create the Displayable instances you need - so you'll create the Image you want first, and then create the Fixed and pass the Image as the first parameter.
Thank you. This starts me in the right direction. I have more than one image to display in my Fixed() layout; what I am doing is putting together several images to make a drawing of a scale showing the amounts on the left and right. I'm attaching the script code as it currently stands. I shall experiment further.
Attachments
script.rpy
Algebra tutorial as a visual novel, showing a scale drawn out of parts.
(8.61 KiB) Downloaded 36 times

jdeisenberg
Newbie
Posts: 14
Joined: Tue Mar 16, 2010 7:18 pm
Location: San Jose, CA
Contact:

Re: ui as a displayable

#4 Post by jdeisenberg »

Solved. Here's an example of code that lets me add several images to the Fixed layout.

Code: Select all

scale_layout = Fixed()
if (left_total > right_total):
  image_name = "images/scale2.png"
else:
  image_name = "images/scale1.png"
scale_layout.add(im.Image(image_name, xpos=x_base, ypos=y_base))
for i in xrange(3):
  image_name = "images/weight%d.png" % i
  scale_layout.add(im.Image(image_name, xpos = x_base + i * 30, ypos = y_base))
tran = Transform(xpos=0, ypos=0)
renpy.show("theScale", what=scale_layout, at_list=[tran])

Post Reply

Who is online

Users browsing this forum: No registered users