From de6e8fba8cbb99fdb22472da18cffcc1df118c33 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 9 Mar 2001 23:03:51 +0000 Subject: [PATCH] *** empty log message *** --- direct/src/gui/Button.py | 49 ++++++------ direct/src/gui/GuiGlobals.py | 7 ++ direct/src/gui/Label.py | 130 +++++++++++++++++++++++++++++++ direct/src/gui/PickList.py | 29 +++++-- direct/src/gui/ScrollingLabel.py | 47 +++++------ direct/src/gui/Sign.py | 43 +++++----- 6 files changed, 229 insertions(+), 76 deletions(-) create mode 100644 direct/src/gui/Label.py diff --git a/direct/src/gui/Button.py b/direct/src/gui/Button.py index 3bd4e1aa42..7b9e944530 100644 --- a/direct/src/gui/Button.py +++ b/direct/src/gui/Button.py @@ -1,13 +1,18 @@ from ShowBaseGlobal import * from DirectObject import * from GuiGlobals import * -import GuiLabel import GuiButton +import Label class Button(DirectObject): - def __init__(self, name, label=None, font=getDefaultFont()): + def __init__(self, name, + label = None, + scale = 0.1, + width = None, + drawOrder = getDefaultDrawOrder(), + font = getDefaultFont()): self.name = name # if no label given, use the button name if (label == None): @@ -17,31 +22,27 @@ class Button(DirectObject): if (type(label) == type('')): # text label, make text button self.label = label - # up - self.l1 = GuiLabel.GuiLabel.makeSimpleTextLabel(self.label, - font) - self.l1.setForegroundColor(0., 0., 0., 1.) - self.l1.thaw() - # roll-over up - self.l2 = GuiLabel.GuiLabel.makeSimpleTextLabel(self.label, - font) - self.l2.setForegroundColor(0., 0., 0., 1.) - self.l2.setBackgroundColor(1., 1., 0., 1.) - self.l2.thaw() - # roll-over down - self.l3 = GuiLabel.GuiLabel.makeSimpleTextLabel(self.label, - font) - self.l3.setForegroundColor(1., 1., 1., 1.) - self.l3.setBackgroundColor(0., 0., 0., 1.) - self.l3.thaw() + + self.l1 = Label.textLabel(self.label, Label.ButtonUp, + scale, width, drawOrder, font) + self.l2 = Label.textLabel(self.label, Label.ButtonLit, + scale, width, drawOrder, font) + self.l3 = Label.textLabel(self.label, Label.ButtonDown, + scale, width, drawOrder, font) + + elif (isinstance(label, NodePath)): + # If it's a NodePath, assume it's a little texture card. + self.l1 = Label.modelLabel(label, 1, 1, scale, drawOrder) + self.l2 = self.l1 + self.l3 = self.l1 + else: # label provided, use it for all labels self.l1 = self.l2 = self.l3 = label self.button = GuiButton.GuiButton(self.name, self.l1, self.l2, self.l3, self.l3, self.l1) - - self.setScale(0.1) + self.button.setDrawOrder(drawOrder) self.managed = 0 return None @@ -111,9 +112,3 @@ class Button(DirectObject): v3 = Vec3(mat.xformPoint(Point3(x, 0., y))) self.button.setPos(v3) - - def setScale(self, scale): - self.button.setScale(scale) - - def setDrawOrder(self, drawOrder): - self.button.setDrawOrder(drawOrder) diff --git a/direct/src/gui/GuiGlobals.py b/direct/src/gui/GuiGlobals.py index 0b60ab73ac..1efa1caa24 100644 --- a/direct/src/gui/GuiGlobals.py +++ b/direct/src/gui/GuiGlobals.py @@ -7,6 +7,7 @@ guiMgr = GuiManager.GuiManager.getPtr(base.win, base.mak.node(), base.render2d.node()) font = loader.loadModelNode("models/fonts/ttf-comic") +drawOrder = 100 def getDefaultFont(): return font @@ -15,3 +16,9 @@ def setDefaultFont(newFont): global font font = newFont +def getDefaultDrawOrder(): + return drawOrder + +def setDefaultDrawOrder(newDrawOrder): + global drawOrder + drawOrder = newDrawOrder diff --git a/direct/src/gui/Label.py b/direct/src/gui/Label.py new file mode 100644 index 0000000000..86bb1f2f0e --- /dev/null +++ b/direct/src/gui/Label.py @@ -0,0 +1,130 @@ +from ShowBaseGlobal import * +from DirectObject import * +from GuiGlobals import * + +## These are the styles of labels we might commonly see. They control +## the way the label looks. + +ButtonUp = 1 +ButtonLit = 2 +ButtonDown = 3 +Sign = 4 +ScrollTitle = 5 +ScrollItem = 6 + + +def textLabel(string, style, + scale = 0.1, + width = None, + drawOrder = getDefaultDrawOrder(), + font = getDefaultFont()): + """textLabel(string, int style, float scale, float width, + int drawOrder, Node font) + + Generates a text label suitable for adding to a GuiButton or + GuiSign. + + """ + + (label, text) = \ + textLabelAndText(string, style, scale, width, drawOrder, font) + return label + +def textLabelAndText(string, style, + scale = 0.1, + width = None, + drawOrder = getDefaultDrawOrder(), + font = getDefaultFont()): + """textLabelAndText(string, int style, float scale, float width, + int drawOrder, Node font) + + Generates a text label suitable for adding to a GuiButton or + GuiSign. + + This function returns both the label and the TextNode that is + within the label, allowing the calling function to update the + label's text later. If there are a limited number of text + strings, however, it would probably be better to create a separate + GuiItem for each possible text string, and rotate them in and out. + + """ + text = TextNode() + + # Freeze the text so we can set up its properties. + text.freeze() + + text.setFont(font) + text.setAlign(TMALIGNCENTER) + text.setDrawOrder(drawOrder) + text.setTextColor(0.0, 0.0, 0.0, 1.0) + text.setCardColor(1.0, 1.0, 1.0, 1.0) + text.setCardAsMargin(0.1, 0.1, 0.0, 0.0) + text.setTransform(Mat4.scaleMat(scale)) + + if style == ButtonUp: + # This is the default: black on white. + pass + + elif style == ButtonLit: + # When the mouse is over the button, the background turns + # yellow. + text.setCardColor(1.0, 1.0, 0.0, 1.0) + + elif style == ButtonDown: + # When the button is being depressed, its turns to white + # on black. + text.setTextColor(1.0, 1.0, 1.0, 1.0) + text.setCardColor(0.0, 0.0, 0.0, 1.0) + + elif style == Sign: + # For a sign, we want red text with no background card. + text.setTextColor(1., 0., 0., 1.) + text.clearCard() + + elif style == ScrollTitle: + text.setTextColor(1., 0., 0., 1.) + text.setCardColor(1., 1., 1., 0.) + + elif style == ScrollItem: + pass + + + # Don't set the text until the very last thing, so the TextNode + # has minimal work to do (even though it's frozen). + text.setText(string) + + v = text.getCardActual() + + if width != None: + # If the user specified a specific width to use, keep it. + v = VBase4(-width / 2.0, width / 2.0, v[2], v[3]) + if text.hasCard(): + text.setCardActual(v[0], v[1], v[2], v[3]) + + # Now we're completely done setting up the text, and we can safely + # thaw it. + text.thaw() + + # Now create a GuiLabel containing this text. + label = GuiLabel.makeModelLabel(text, v[0] * scale, v[1] * scale, + v[2] * scale, v[3] * scale) + + label.setDrawOrder(drawOrder) + return (label, text) + + +def modelLabel(model, height, width, + scale = 0.1, + drawOrder = getDefaultDrawOrder()): + + # Preserve transitions on the arc by creating an intervening node. + topnode = NamedNode('model') + top = NodePath(topnode) + mi = model.instanceTo(top) + mi.setScale(scale) + mi.setBin('fixed', drawOrder) + + label = GuiLabel.makeModelLabel(topnode, height * scale, width * scale) + label.setDrawOrder(drawOrder) + return label + diff --git a/direct/src/gui/PickList.py b/direct/src/gui/PickList.py index faddf3f706..98c78a1865 100644 --- a/direct/src/gui/PickList.py +++ b/direct/src/gui/PickList.py @@ -1,5 +1,6 @@ """PickList module: contains the PickList class""" from ShowBaseGlobal import * +from GuiGlobals import * import PandaObject import Frame import Button @@ -11,7 +12,11 @@ class PickList(PandaObject.PandaObject): """ # special methods - def __init__(self, name, choiceList): + def __init__(self, name, choiceList, + scale = 0.1, + width = None, + drawOrder = getDefaultDrawOrder(), + font = getDefaultFont()): #print "In pick list init: t = %.3f" % clock.getRealTime() @@ -25,7 +30,7 @@ class PickList(PandaObject.PandaObject): self.frame.setOffset(0.015) # display the menu - self.__displayChoices(choiceList) + self.__displayChoices(choiceList, scale, width, drawOrder, font) self.isClean = 0 return None @@ -62,13 +67,28 @@ class PickList(PandaObject.PandaObject): self.eventName = eventName # actions - def __displayChoices(self, choiceList): + def __displayChoices(self, choiceList, + scale, width, drawOrder, font): """__displayChoices(self, string[]) Display the list of choices """ + + if width == None: + # First, compute the maximum width of the buttons. We do this + # ahead of time so the Gui code doesn't have to do it and take + # forever about it. + width = 0 + text = TextNode() + text.setFont(font) + for choice in choiceList: + w = text.calcWidth(choice) + 0.2 + width = max(width, w) + + # Now create all the buttons. for choice in choiceList: # create a button for each choice - button = Button.Button(choice) + button = Button.Button(choice, scale = scale, width = width, + drawOrder = drawOrder, font = font) #print "done with button cons: t = %.3f" % clock.getRealTime() choiceIndex = choiceList.index(choice) # set the rollover-up event @@ -88,7 +108,6 @@ class PickList(PandaObject.PandaObject): self.choiceList.append(button) # set up the frame - self.frame.makeWideAsWidest() self.frame.makeVertical() return None diff --git a/direct/src/gui/ScrollingLabel.py b/direct/src/gui/ScrollingLabel.py index d29c9642fb..b2c144112a 100644 --- a/direct/src/gui/ScrollingLabel.py +++ b/direct/src/gui/ScrollingLabel.py @@ -6,13 +6,19 @@ import GuiFrame import Button import GuiLabel import Sign +import Label class ScrollingLabel(PandaObject.PandaObject): # special methods - def __init__(self, name, itemList, font=getDefaultFont()): + def __init__(self, name, itemList, + label = None, + scale = 0.1, + width = None, + drawOrder = getDefaultDrawOrder(), + font = getDefaultFont()): self.name = name self.eventName = self.name @@ -22,24 +28,22 @@ class ScrollingLabel(PandaObject.PandaObject): self.items = itemList self.keyFocus = 1 - # create the new title - label = GuiLabel.GuiLabel.makeSimpleTextLabel(self.name, font) - label.setForegroundColor(1., 0., 0., 1.) - label.setBackgroundColor(1., 1., 1., 0.) - label.thaw() - self.title = Sign.Sign(self.name, label) - self.frame.addItem(self.title) + if width == None: + # Compute the maximum width of the all the items. + width = 0 + text = TextNode() + text.setFont(font) + for item in itemList: + w = text.calcWidth(item) + 0.2 + width = max(width, w) - longest = self.items[0] - for item in self.items: - if len(item) > len(longest): - longest = item + # create the new title + self.title = Sign.Sign(self.name, self.name, Label.ScrollTitle, + scale, width, drawOrder, font) + self.frame.addItem(self.title) - label = GuiLabel.GuiLabel.makeSimpleTextLabel(longest, font) - label.setForegroundColor(0., 0., 0., 1.) - label.setBackgroundColor(1., 1., 1., 1.) - label.thaw() - self.itemSign = Sign.Sign(longest, label) + self.itemSign = Sign.Sign('item', '', Label.ScrollItem, + scale, width, drawOrder, font) self.frame.addItem(self.itemSign) # pack the first label under the name @@ -47,12 +51,10 @@ class ScrollingLabel(PandaObject.PandaObject): self.title) self.frame.packItem(self.itemSign, GuiFrame.GuiFrame.ALIGNLEFT, self.title) - - # make the title and label the same length - self.frame.makeWideAsWidest() # create the scroll buttons - self.leftButton = Button.Button(self.eventName + "-left", " < ") + self.leftButton = Button.Button(self.eventName + "-left", " < ", + scale, None, drawOrder, font) self.leftButton.getGuiItem().setDownRolloverEvent(self.eventName + "-left") self.leftButton.getGuiItem().setUpRolloverEvent(self.eventName + "-rollover") self.frame.addItem(self.leftButton) @@ -60,7 +62,8 @@ class ScrollingLabel(PandaObject.PandaObject): self.title) self.frame.packItem(self.leftButton, GuiFrame.GuiFrame.LEFT, self.title) - self.rightButton = Button.Button(self.eventName + "-right", " > ") + self.rightButton = Button.Button(self.eventName + "-right", " > ", + scale, None, drawOrder, font) self.rightButton.getGuiItem().setDownRolloverEvent(self.eventName + "-right") self.rightButton.getGuiItem().setUpRolloverEvent(self.eventName + "-rollover") diff --git a/direct/src/gui/Sign.py b/direct/src/gui/Sign.py index e52e59569d..5a8ce9a89e 100644 --- a/direct/src/gui/Sign.py +++ b/direct/src/gui/Sign.py @@ -3,26 +3,31 @@ from DirectObject import * from GuiGlobals import * import GuiSign import GuiLabel +import Label class Sign(DirectObject): - def __init__(self, name, label=None, font=getDefaultFont()): + def __init__(self, name, + label = None, + style = Label.Sign, + scale = 0.1, + width = None, + drawOrder = getDefaultDrawOrder(), + font = getDefaultFont()): self.name = name - # label in this case means GuiLabel + self.labelText = None + if not label: - self.label = GuiLabel.GuiLabel.makeSimpleTextLabel(self.name, font) - self.label.setForegroundColor(1., 0., 0., 1.) - self.label.setBackgroundColor(1., 1., 1., 0.) - self.label.thaw() - elif (type(label) == type('')): - self.label = GuiLabel.GuiLabel.makeSimpleTextLabel(label, font) - self.label.setForegroundColor(1., 0., 0., 1.) - self.label.setBackgroundColor(1., 1., 1., 0.) - self.label.thaw() + label = self.name + + if (type(label) == type('')): + (self.label, self.labelText) = \ + Label.textLabelAndText(label, style, + scale, width, drawOrder, font) else: self.label = label + self.sign = GuiSign.GuiSign(self.name, self.label) - self.setScale(0.1) self.managed = 0 return None @@ -35,14 +40,14 @@ class Sign(DirectObject): return None def __str__(self): - return "sign: %s contains label: %s" % (self.name, self.label.name) + return "sign: %s contains label: %s" % (self.name, self.label) # accessing def getName(self): return self.name def setText(self, text): - self.label.setText(text) + self.labelText.setText(text) def getLabel(self): return self.label @@ -51,19 +56,13 @@ class Sign(DirectObject): return self.sign def getPos(self): - self.label.getPos() + self.sign.getPos() def setPos(self, x, y): - self.label.setPos(x, 0, y) - - def setScale(self, scale): - self.sign.setScale(scale) + self.sign.setPos(Vec3(x, 0, y)) def getWidth(self): return self.label.getWidth() - - def setWidth(self, width): - self.label.setWidth(width) # actions def manage(self, nodepath = aspect2d):