Suppose I have an imagebutton which I want to show a screen and pass its own position coordinates to it. Fake code:
Code: Select all
imagebutton idle "button" action ToggleScreen ("screen", x=imagebutton.xpos, y=imagebutton.ypos)
Code: Select all
imagebutton idle "button" action ToggleScreen ("screen", x=imagebutton.xpos, y=imagebutton.ypos)
Code: Select all
renpy.focus_coordinates()
This attempts to find the coordinates of the currently-focused displayable.
If it can, it will return them as a (x, y, w, h) tuple.
If not, it will return a (None, None, None, None) tuple.
Code: Select all
label start:
call screen testMenu
jump start
default testVariable = "test pending"
screen testMenu:
frame:
pos (0.5, 0.5)
$ buttons = ("button1", "button2", "button3")
$ buttonPos = 0
for button in buttons:
imagebutton:
idle "button"
ypos buttonPos
action ToggleScreen("testMenu2")
$ buttonPos += 120
screen testMenu2:
modal True
python:
x = int(renpy.focus_coordinates()[0])
y = int(renpy.focus_coordinates()[1])
buttons2 = ("buttonA", "buttonB", "buttonC")
imagebutton:
idle "transparent"
action (
ToggleScreen("testMenu2")
)
frame:
pos(x+200,y)
xsize 200
ysize 200
$ buttonPos2 = 0
for button in buttons2:
imagebutton:
idle "button"
ypos buttonPos2
action (
SetVariable("testVariable", "test complete"),
print(testVariable),
)
$ buttonPos2 += 120
Code: Select all
ToggleScreen("test2", renpy.focus_coordinates()),
Code: Select all
define buttons = ["button1", "button2", "button3"]
define subbuttons = [
# subbuttons for button1
["buttonA"],
# subbuttons for button2
["buttonB", "buttonC"],
# subbuttons for button3
["buttonD", "buttonE", "buttonF"]
]
screen testMenu():
default submenu = None
frame:
background None
padding (0,0)
xpos 0.5 ypos 0.5
hbox:
spacing 5
vbox:
spacing 5
for i, b in enumerate(buttons):
imagebutton idle Solid("#f005") action [Notify("You pressed "+b), If(submenu == i, SetLocalVariable("submenu", None), SetLocalVariable("submenu", i))] xsize 200 ysize 50
if submenu is not None:
use subMenu(submenu)
screen subMenu(sub):
vbox:
spacing 5
for i, b in enumerate(subbuttons):
if i == sub:
for sb in b:
imagebutton idle Solid("#0f05") action Notify("This is button "+sb) xsize 200 ysize 50
else:
null width 200 height 50
Code: Select all
default itemlist = [
{"name": "itemType1", "items": ("item1", "item2", "item3")},
{"name": "itemType2", "items": ("item4", "item5")},
{"name": "itemType3", "items": ("item6", "item7")},
]
default optionlist = ["option1", "option2", "option3", "option4", "option5"]
screen listMenu():
frame:
xalign 1.0
vbox:
spacing 20
for list in itemlist:
text list["name"]
imagebutton idle "button" action ToggleScreen("subMenu")
for item in list["items"]:
textbutton item action Notify(item+" equpped")
screen subMenu():
imagebutton idle "transparent" action ToggleScreen("subMenu")
vpgrid:
cols 3
for option in optionlist:
imagebutton idle "button2" action Notify(option+" picked")
Code: Select all
default menuActive = False
screen listMenu():
(...)
for list in itemlist:
text list["name"]
imagebutton idle "button" action (SetVariable("menuActive", True), ToggleScreen("subMenu"))
(...)
screen subMenu():
imagebutton idle "transparent" action ToggleScreen("subMenu")
python:
if menuActive:
x = int(renpy.focus_coordinates()[0])
y = int(renpy.focus_coordinates()[1])
menuActive = False
vpgrid:
xalign 1.0
xpos x
ypos y
(...)
The second argument is transition. ToggleScreen("test2", transition=None, renpy.focus_coordinates())Vitalis wrote: ↑Sun May 15, 2022 7:59 am I tried passing the coordinates from the button itself as an argument for testMenu2(coordinates), like this:
but RenPy just throws "TypeError: 'tuple' object is not callable".Code: Select all
ToggleScreen("test2", renpy.focus_coordinates()),
Well, Renpy is for making visual novels, not for creating an operating system. When Renpy is not able to do what you want, just do it with python and classes. You can create your own Displayables.
Code: Select all
screen testMenu:
vbox:
imagebutton "button1" action ToggleScreen("submenu_button1")
imagebutton "button2" action ToggleScreen("submenu_button2")
screen submenu_button1:
tag submenu
vbox:
xpos 0 ypos 0
imagebutton "buttonA":
...
screen submenu_button2:
tag submenu
vbox:
xpos 0 ypos 50
imagebutton "buttonB":
...
Oooh ok, thanks.
This actually still doesn't sound like you need a function that dynamically creates things out of lists (and I say this to make your life easier and also because manually crafting menus will not stress renpy execution as much as calculating everything everytime the screen is drawn).Vitalis wrote: ↑Thu May 19, 2022 4:43 pmMy menu is scrollable so the button summoning the submenu can be at almost any y coordinate. The menu also contains multiple lists of items that will change often as I add more options in subsequent builds. It just makes no sense to me to create the menu and input the positions manually.
Code: Select all
screen testMenu:
textbutton "button1" action NullAction() xpos 0 ypos 0
textbutton "button2" action NullAction() xpos 0 ypos 20
screen centerscreen:
frame:
background None
xpos 0.5 ypos 0.5
use testMenu
label start:
# will be placed on the top left of the screen
show screen testMenu
# will show testMenu in the middle, but all buttons are shifted accordingly
show screen centerscreen
Users browsing this forum: Google [Bot]