DirectOptionMenu: Fix popup menu reset of the popup marker's pos

Also defines popupMarker_pos and raises an assertion error if #showPopupMenu() is called when no items have been specified.

Fixes #636
Closes #637
This commit is contained in:
Maverick Liberty 2019-04-28 16:00:17 -04:00 committed by rdb
parent 16c3ca5c87
commit 4d33db2028

View File

@ -22,10 +22,12 @@ class DirectOptionMenu(DirectButton):
# List of items to display on the popup menu
('items', [], self.setItems),
# Initial item to display on menu button
# Can be an interger index or the same string as the button
# Can be an integer index or the same string as the button
('initialitem', None, DGG.INITOPT),
# Amount of padding to place around popup button indicator
('popupMarkerBorder', (.1, .1), None),
# The initial position of the popup marker
('popupMarker_pos', (0, 0, 0), None),
# Background color to use to highlight popup menu items
('highlightColor', (.5, .5, .5, 1), None),
# Extra scale to use on highlight popup menu items
@ -42,6 +44,8 @@ class DirectOptionMenu(DirectButton):
DirectButton.__init__(self, parent)
# Record any user specified frame size
self.initFrameSize = self['frameSize']
# Record any user specified popup marker position
self.initPopupMarkerPos = self['popupMarker_pos']
# Create a small rectangular marker to distinguish this button
# as a popup menu button
self.popupMarker = self.createcomponent(
@ -168,8 +172,13 @@ class DirectOptionMenu(DirectButton):
else:
# Or base it upon largest item
bounds = [self.minX, self.maxX, self.minZ, self.maxZ]
pm.setPos(bounds[1] + pmw/2.0, 0,
bounds[2] + (bounds[3] - bounds[2])/2.0)
if self.initPopupMarkerPos:
# Use specified position
pmPos = list(self.initPopupMarkerPos)
else:
# Or base the position on the frame size.
pmPos = [bounds[1] + pmw/2.0, 0, bounds[2] + (bounds[3] - bounds[2])/2.0]
pm.setPos(pmPos[0], pmPos[1], pmPos[2])
# Adjust popup menu button to fit all items (or use user specified
# frame size
bounds[1] += pmw
@ -184,6 +193,12 @@ class DirectOptionMenu(DirectButton):
Adjust popup position if default position puts it outside of
visible screen region
"""
# Needed attributes (such as minZ) won't be set unless the user has specified
# items to display. Let's assert that we've given items to work with.
items = self['items']
assert items and len(items) > 0, 'Cannot show an empty popup menu! You must add items!'
# Show the menu
self.popupMenu.show()
# Make sure its at the right scale