def _draw(self): winWidth = base.getAspectRatio() * 2 winHeight = 2 # Draw main container: cFSizeX, cFSizeY = winWidth / 4, winHeight * (3 / 4) self._containFrame = DirectFrame(pos=(0, 0, cFSizeY * (2 / 3)), frameSize=(-cFSizeX, cFSizeX, -cFSizeY, 0), frameTexture=UI_WINDOW) self._containFrame.setTransparency(TransparencyAttrib.MAlpha) # Title: titleWidth, titleHeight = cFSizeX - CPKR_CONTROL_SIDES_MARGIN,\ cFSizeY*CPKR_TITLE_HEIGHT_RATIO titleFontSize = (0.15, 0.15) title = DirectLabel(parent=self._containFrame, pos=(0, 0, -CPKR_CONTROL_TOP_MARGIN), frameSize=(-titleWidth, titleWidth, -titleHeight, 0), text_align=TextNode.ACenter, text_font=self._font, text_scale=titleFontSize, text_pos=CPKR_PIERCEROMAN_OFFSET_TC, text="Choose a Class!", frameTexture=IMG_GRADIENT_1) # Information Box: infoHeight = cFSizeY * CPKR_INFO_HEIGHT_RATIO # Calculate Button Frame vertical size first! bFSizeY = cFSizeY - (CPKR_CONTROL_TOP_MARGIN*2 + titleHeight\ + infoHeight) self._infoText = DirectLabel(parent=self._containFrame, pos=(0, 0, -CPKR_CONTROL_TOP_MARGIN - titleHeight - bFSizeY), frameSize=(-titleWidth, titleWidth,\ -infoHeight, 0), text_align=TextNode.ALeft, text_font=self._font, text_scale=CPKR_INFO_FONT_SIZE_DEFAULT, text_pos=CPKR_PIERCEROMAN_OFFSET_TL, text_wordwrap=CPKR_INFO_WRAP_DEFAULT, frameColor=(1,1,1,0.8), text="Please select a class above") # Class Radio Button Frame: bFColor = (0.25, 0.25, 0.25, 0.5) bFWidth = self._containFrame.getWidth()\ *CPKR_BUTTONCONTAINER_WIDTH_PERCENTAGE buttonFrame = DirectFrame(parent=self._containFrame, pos=(0, 0, -CPKR_CONTROL_TOP_MARGIN - titleHeight), frameSize=(-bFWidth / 2, bFWidth / 2, -bFSizeY, 0), frameColor=bFColor) # Create Class Radio Buttons: (Buttons are height dependent) bSizeY = (buttonFrame.getHeight() - CPKR_BUTTONCONTAINER_MARGIN*2)\ / len(CPKR_CLASSES_LIST) bSizeX = (buttonFrame.getWidth() - CPKR_BUTTONCONTAINER_MARGIN*2)\ / len(CPKR_CLASSES_LIST[0]) for row in range(len(CPKR_CLASSES_LIST)): for col in range(len(CPKR_CLASSES_LIST[row])): buttonClass = CPKR_CLASSES_LIST[row][col] buttonImage = buttonClass.classIcon buttonID = buttonClass.classID verticalPos = -CPKR_BUTTONCONTAINER_MARGIN - bSizeY * col horizontalPos = -CPKR_BUTTONCONTAINER_MARGIN * 2 + bSizeX * row newButton = DirectButton(parent=buttonFrame, pos=(horizontalPos, 0, verticalPos), frameSize=(-bSizeX / 2, bSizeX / 2, -bSizeY, 0), frameTexture=buttonImage, relief=DGG.FLAT, command=self._selectClass, extraArgs=[buttonID])
class NamePicker(): """ A UI element that lets the players pick their name. Also has a confirmation element to the right of the entry box. """ def __init__(self, classSelectionMenu): self._classSelectionMenu = classSelectionMenu self._nameEntry = None self._confirmButton = None self._rootFrame = None self._font = loader.loadFont(PIERCEROMAN_FONT) self._draw() def _draw(self): winWidth = base.getAspectRatio() * 2 winHeight = 2 # Draw container: frameHeight = winHeight * NPKR_HEIGHT_PERCENTAGE frameWidth = winWidth * NPKR_WIDTH_PERCENTAGE cFCenterY = -winHeight / 2 + frameHeight / 2 self._rootFrame = DirectFrame( pos=(0, 0, cFCenterY), frameSize=(-frameWidth / 2, frameWidth / 2, -frameHeight / 2, frameHeight / 2), frameTexture=IMG_GRADIENT_1) self._rootFrame.setTransparency(TransparencyAttrib.MAlpha) # Draw Name Entry: entryWidth = self._rootFrame.getWidth() * NPKR_ENTRY_WIDTH_PERCENTAGE entryHeight = self._rootFrame.getHeight() entryCX = -(self._rootFrame.getWidth() - entryWidth) / 2 self._nameEntry = DirectEntry( parent=self._rootFrame, pos=(entryCX, 0, 0), frameSize=(-entryWidth / 2, entryWidth / 2, -entryHeight / 2, entryHeight / 2), frameColor=(0.25, 0.25, 0.25, 1), text_align=TextNode.ACenter, text_font=self._font, text_scale=NPKR_ENTRY_FONT_SIZE, text_pos=NPKR_ENTRY_FONT_OFFSET, initialText=NPKR_ENTRY_INITIAL_TEXT, numLines=1, focusOutCommand=self._syncName, focusInCommand=self._checkPlaceholderText, command=self._syncName, frameTexture=UI_WINDOW) # Draw Confirm Button: confirmWidth = self._rootFrame.getWidth()\ * (1-NPKR_ENTRY_WIDTH_PERCENTAGE) confirmHeight = self._rootFrame.getHeight() confirmCX = (self._rootFrame.getWidth() - confirmWidth) / 2 self._confirmButton = DirectButton( parent=self._rootFrame, pos=(confirmCX, 0, 0), frameSize=(-confirmWidth / 2, confirmWidth / 2, -confirmHeight / 2, confirmHeight / 2), command=self._onConfirmPressed, text_font=self._font, text_scale=NPKR_BUTTON_FONT_SIZE, text_pos=NPKR_BUTTON_FONT_OFFSET, text="Create Character", borderWidth=NPKR_BUTTON_BORDER_WIDTH) def getName(self): return str(self._nameEntry.get()) def _onConfirmPressed(self): """ Reroutes this input to the parent ClassSelectionMenu """ self._classSelectionMenu.createCharacter() def _checkPlaceholderText(self): """ Makes the nameEntry behave more like a real name entry by removing the placeholder text when the player begins to interact. """ # If the text entered is the placeholder text, simply remove it: if self._nameEntry.get() == NPKR_ENTRY_INITIAL_TEXT: self._nameEntry.set("") def _syncName(self, extraArgs=None): """ Syncs name with this game's host/client manager """ self._classSelectionMenu.syncInfo(cName=self.getName()) def close(self): self._rootFrame.destroy() # Destroy ui del self # Destroy this instance