PopUp menu efficiency [SOLVED]

I’ve decided to put a pop up menu in my game that will exist at certain times and vanish at others. My question is, should I destroy/create the menu each time it pops up, which could be quite frequent, or is there a way to show/hide a DirectFrame and everything attached to it?

Since DirectFrame inherits from NodePath, you can just do:

myFrame.show()
myFrame.hide()

David

I’ll take it that you mean it’s better to use .show() and .hide() than to create/destroy the menu each time. I figured that would be the case. Thanks David.

Hi PiratePanda, do you mind sharing your menu code? I’m sure it’ll be very useful for my project bcause I’m pretty confused about making interfaces right now… Thanks in advance :slight_smile:

Uh, sure. It’s nothing fancy though.

Here’s the main menu.

from direct.gui.DirectButton import DirectButton
from direct.gui.DirectFrame import DirectFrame

class MainMenu:
	def __init__(self, assetLoader):
		self.notebook = assetLoader.modelLoader("/GUI/notebook.egg")
		self.buttons = assetLoader.modelLoader("/GUI/buttons.egg")
		self.score = assetLoader.modelLoader("/GUI/score.egg")
		#loads the egg files for the different componants of the menu
				
		self.notebookFrame = DirectFrame(geom = self.notebook, pos = (0,0,-.91), relief = None)
		#creates and positions the frame which supplies the notebook background for the menu and encapsulates the rest of the menu.
		
		self.menuButton = DirectButton(geom = self.buttons.find("**/menu_ready"), borderWidth = (0,0), frameSize = (-.115,.1,-.05,.05), pos = (-.5,0,.05), rolloverSound = None, clickSound = None, relief = None)
		self.upButton = DirectButton(geom = (self.buttons.find("**/up_ready"),self.buttons.find("**/up_ready"),self.buttons.find("**/up_ready"),self.buttons.find( "**/up_disabled")), borderWidth = (0,0), frameSize = (-.1,.1,-.05,.05), pos = (-.18,0,.05), rolloverSound = None, clickSound = None, relief = None)
		self.downButton = DirectButton(geom = (self.buttons.find("**/down_ready"),self.buttons.find("**/down_ready"),self.buttons.find("**/down_ready"), self.buttons.find("**/down_disabled")), borderWidth = (0,0), frameSize = (-.1,.1,-.05,.05),  pos = (.07,0,.05), rolloverSound = None, clickSound = None, relief = None)
		self.plusButton = DirectButton(geom = self.buttons.find("**/plus_ready"), borderWidth = (0,0), frameSize = (-.055,.055,-.05,.05), pos =	(.35,0,.05), rolloverSound = None, clickSound = None, relief = None)
		self.arrowsButton = DirectButton(geom = self.buttons.find("**/arrows_ready"), borderWidth = (0,0), frameSize = (-.055,.055,-.05,.05), pos =	(.515,0,.05), rolloverSound = None, clickSound = None, relief = None)
		self.minusButton = DirectButton(geom = self.buttons.find("**/minus_ready"), borderWidth = (0,0), frameSize = (-.055,.055,-.05,.05), pos = (.67,0,.05), rolloverSound = None, clickSound = None, relief = None)
		self.starButton = DirectButton(geom = (self.buttons.find("**/star_ready"),self.buttons.find("**/star_ready"),self.buttons.find("**/star_ready"), self.buttons.find("**/star_disabled")), borderWidth = (0,0), frameSize = (-.1,.1,-.05,.05),  pos = (.89,0,.05), rolloverSound = None, clickSound = None, relief = None)
		#creates each of the buttons and sets their positions
		
		
		self.menuButton.reparentTo(self.notebookFrame)
		self.upButton.reparentTo(self.notebookFrame)
		self.downButton.reparentTo(self.notebookFrame)
		self.plusButton.reparentTo(self.notebookFrame)
		self.arrowsButton.reparentTo(self.notebookFrame)
		self.minusButton.reparentTo(self.notebookFrame)
		self.starButton.reparentTo(self.notebookFrame)
		#parents all the buttons to the notebook frame, so their positions are relative to it.
		
		self.notebookFrame.setScale(.75, 1, .75)

Here’s the popup:

from direct.gui.DirectButton import DirectButton
from direct.gui.DirectFrame import DirectFrame

class PopUpMenu:
	def __init__(self, assetLoader):
		self.pin = assetLoader.modelLoader("/GUI/pin.egg")
		self.buttons = assetLoader.modelLoader("/GUI/buttons.egg")
		# Load the graphics for the menu
		
		self.kidManager = None			# Containr for a reference to the KidManager.
		
		self.popUpFrame = DirectFrame(geom = self.pin, pos = (-1,0,.75), frameSize = (-.45,.45,.45,-.45), relief = None)
		self.popUpFrame.setScale(.5,1,.5)
		# Create, position, and scale the frame that contains the menu.
		
		self.smallPlus = DirectButton(geom = self.buttons.find("**/plus_ready"), borderWidth = (0,0), frameSize = (-.055,.055,-.05,.05), pos = (-.3,0,-.35), rolloverSound = None, clickSound = None, relief = None, command = self.setKidMode, extraArgs = ["R"])
		self.smallArrows = DirectButton(geom = self.buttons.find("**/arrows_ready"), borderWidth = (0,0), frameSize = (-.055,.055,-.05,.05), pos =	(0,0,-.33), rolloverSound = None, clickSound = None, relief = None, command = self.setKidMode, extraArgs = ["G"])
		self.smallMinus = DirectButton(geom = self.buttons.find("**/minus_ready"), borderWidth = (0,0), frameSize = (-.055,.055,-.05,.05), pos = (.3,0,-.31), rolloverSound = None, clickSound = None, relief = None, command = self.setKidMode, extraArgs = ["S"])
		self.smallPlus.reparentTo(self.popUpFrame)
		self.smallArrows.reparentTo(self.popUpFrame)
		self.smallMinus.reparentTo(self.popUpFrame)
		# Create and position the buttons on the menu, set their associated function, and parent them to the frame so they're are contained within it.
		

		self.hide()
		
		
	def setKidManager(self, kidManager):
		self.kidManager = kidManager
	
	def hide(self):
		self.popUpFrame.hide()
	
	def show(self):
		self.popUpFrame.show()
	
	def setKidMode(self, mode):
		self.kidManager.setKidMode(mode)