So in my game I have an UI where a player can pick his weapon. I do this with buttons and I wanted a good system to build up upon. So I made a grid_layout of the entity that all buttons are parented to.
I have previously done this with the attachments so I thought it would work just fine with the gun-selection-buttons as well.
So it does align them nicely but the buttons only kinda work. When you hover over them, they blink and switch from the hovered to the unhovered state (aka changing the color back and forth really fast). And you can only click them when they show their tooltip (every button has a tooltip so its not that i added that to try and fix the problem), but they show their tooltip only sometimes when you hover over them.
So yeah it doesnt really break the game, its just annoying needing to hover over the button a couple of time to click it.
Could you show us the code that you use to create your buttons, please?
def gunSelection(side):
global buttonParentLeft
global buttonParentRight
if(side == âleftâ):
theParent = buttonParentLeft
enable_(buttonParentLeft ) â custom function: only enables and makes object visible
else:
theParent = buttonParentRight
enable_(buttonParentRight)
backButton = Button(text='<- Back', color=color.gray, scale=(0.15, 0.075, 0), text_origin=(0,0), y=0.42)
backButton.x = -0.78
saveAttach = Button(text='Save Gun', color=color.gray, scale=(0.2, 0.075, 0), text_origin=(0,0), y=-0.42)
saveAttach.x = -0.45
disable_(saveAttach) <- custom function:disables and hides(aka makes invisibe) object
options = []
ids = []
lines_ = []
with open('IMPL.dat', 'r') as f:
for line in f:
lines_.append(line)
elementLen = 11
for i in range(int(len(lines_)/elementLen)):
gunButton = Button(parent = theParent, text=<the text>, color=color.gray, scale=(0.75, 0.75, 0))
if(len(gunButton .text) > 9):
gunButton .text_entity.scale *= 0.3
elif(len(gunButton .text) > 6):
gunButton .text_entity.scale *= 0.35
else:
gunButton .text_entity.scale *= 0.4
tooltip = ""
for a in range(elementLen-2):
tooltip += lines_[((i*elementLen)+a+1)] <- special formating to get the right tooltips
gunButton.tooltip = Tooltip(tooltip)
options.append(gunButton)
ids.append(lines_[(i*elementLen)+eleLen-1][:len(lines_[(i*elementLen)+elementLen-1])-1])
if(side == 'right'):
c2.x = 0.6
c2.y = 2.8
backButton.x = 0.18
saveAttach.x = 0.45
for i in range(len(options)):
#options[i].x += 0.9
options[i].on_click = Func(select, ids[i], 'right', saveAttach)
grid_layout(c2.children, max_x=7, max_y=4, origin=(-0.6,0.6))
else:
buttonParentLeft .x = -6.7
buttonParentLeft .y = 2.8
for i in range(len(options)):
options[i].on_click = Func(select, ids[i], 'left', saveAttach)
grid_layout(buttonParentLeft .children, max_x=7, max_y=4, origin=(-0.6,0.6))
options.append(backButton)
options.append(saveAttach)
backButton.on_click = Func(back, 'd', side, options)
print(ids)
Hmmm⌠Iâm not sure!
I donât see anything obvious in that code at the momentâŚ
By the symptoms, it seems like it might be that the buttons have different sizes in their âhovered overâ state than in their ânormalâ state. As a result, when the mouse is moved over them, they move to their âhovered overâ state and change sizeâleaving the mouse no longer over them. But with the mouse no longer over them they return to their ânormalâ state, resulting in the mouse once again over them, and so move to their âhovered overâ stateâŚ
Is something perhaps changing the text of your buttons, or the scale, or the frame-size?
And thinking of which, what happens if you pass an explicit frame-size to your buttons when theyâre constructed?
in the code I check for the length of the buttons text and scale the text_entity accordingly. That is the only time i change the scale of one of the buttons elements (except of course when I create a button i automaticly asign it a scale).
May I ask what you mean with an explicit frame-size?
I am always surprised when people refuse to create a minimal sample of executable code to demonstrate the problem.
Believe me, itâs not difficult.
from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectGui import DirectButton
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
b = DirectButton(text=("OK", "click!", "rolling over", "disabled"), scale = 0.1)
app = MyApp()
app.run()
Supplement this with changes according to your code.
The DirectGUI widgetsâincluding DirectButtonâhave amongst their various keyword-arguments one called âframeSizeâ. This allows one to specifically set the size of the widget, regardless of what, if any, contents the widget may have.
(Out of curiosity, why do you change the button-scale according to the length of the text? DirectButton should automatically adjust its size to fit the given text, I do believe. (Presuming that the frame-size hasnât been explicitly set, as mentioned above.))
But serega does make a good point: a minimal example might be helpful here!
I am working with the ursina engine/module/library and when I just tried to add the buttons with text the text-size would be adjusted, but not in a way that I was hoping it would. By default, the text would be too big and stick out of the button.
And sorry if I provided too much code. I know it should be minimal but I thought the other parts of the code could be part of the problem.
In fact, Ursina has its own code layer above the standard panda API. Accordingly, it is impossible to determine the causes of the problem using only panda.
Oh. I was just posting my issues here because I couldnt really find an Ursina-Community page where I could post the issues. But since there is a lot of panda3d elements involved I thought id tried it here
Yes, they donât have a forum, but they do have a Discord.
Youâve already been directed over to the Ursina Discord, which is indeed likely the better place to ask, but let me add quickly:
While what you say is true, making a minimal example might actually help to isolate the problem. By slowly building up the example, you may perhaps encounter whatever it is thatâs causing the trouble.
Ok, I will ask in their discord.
Thanks for the tipps