Label.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from ShowBaseGlobal import *
  2. from DirectObject import *
  3. from GuiGlobals import *
  4. ## These are the styles of labels we might commonly see. They control
  5. ## the way the label looks.
  6. ButtonUp = 1
  7. ButtonLit = 2
  8. ButtonDown = 3
  9. Sign = 4
  10. ScrollTitle = 5
  11. ScrollItem = 6
  12. def textLabel(string, style,
  13. scale = 0.1,
  14. width = None,
  15. drawOrder = getDefaultDrawOrder(),
  16. font = getDefaultFont()):
  17. """textLabel(string, int style, float scale, float width,
  18. int drawOrder, Node font)
  19. Generates a text label suitable for adding to a GuiButton or
  20. GuiSign.
  21. """
  22. (label, text) = \
  23. textLabelAndText(string, style, scale, width, drawOrder, font)
  24. return label
  25. def textLabelAndText(string, style,
  26. scale = 0.1,
  27. width = None,
  28. drawOrder = getDefaultDrawOrder(),
  29. font = getDefaultFont()):
  30. """textLabelAndText(string, int style, float scale, float width,
  31. int drawOrder, Node font)
  32. Generates a text label suitable for adding to a GuiButton or
  33. GuiSign.
  34. This function returns both the label and the TextNode that is
  35. within the label, allowing the calling function to update the
  36. label's text later. If there are a limited number of text
  37. strings, however, it would probably be better to create a separate
  38. GuiItem for each possible text string, and rotate them in and out.
  39. """
  40. text = TextNode()
  41. # Freeze the text so we can set up its properties.
  42. text.freeze()
  43. text.setFont(font)
  44. text.setAlign(TMALIGNCENTER)
  45. text.setDrawOrder(drawOrder)
  46. text.setTextColor(0.0, 0.0, 0.0, 1.0)
  47. text.setCardColor(1.0, 1.0, 1.0, 1.0)
  48. text.setCardAsMargin(0.1, 0.1, 0.0, 0.0)
  49. text.setTransform(Mat4.scaleMat(scale))
  50. if style == ButtonUp:
  51. # This is the default: black on white.
  52. pass
  53. elif style == ButtonLit:
  54. # When the mouse is over the button, the background turns
  55. # yellow.
  56. text.setCardColor(1.0, 1.0, 0.0, 1.0)
  57. elif style == ButtonDown:
  58. # When the button is being depressed, its turns to white
  59. # on black.
  60. text.setTextColor(1.0, 1.0, 1.0, 1.0)
  61. text.setCardColor(0.0, 0.0, 0.0, 1.0)
  62. elif style == Sign:
  63. # For a sign, we want red text with no background card.
  64. text.setTextColor(1., 0., 0., 1.)
  65. text.clearCard()
  66. elif style == ScrollTitle:
  67. text.setTextColor(1., 0., 0., 1.)
  68. text.setCardColor(1., 1., 1., 0.)
  69. elif style == ScrollItem:
  70. pass
  71. # Don't set the text until the very last thing, so the TextNode
  72. # has minimal work to do (even though it's frozen).
  73. text.setText(string)
  74. v = text.getCardActual()
  75. if width != None:
  76. # If the user specified a specific width to use, keep it.
  77. v = VBase4(-width / 2.0, width / 2.0, v[2], v[3])
  78. if text.hasCard():
  79. text.setCardActual(v[0], v[1], v[2], v[3])
  80. # Now we're completely done setting up the text, and we can safely
  81. # thaw it.
  82. text.thaw()
  83. # Now create a GuiLabel containing this text.
  84. label = GuiLabel.makeModelLabel(text, v[0] * scale, v[1] * scale,
  85. v[2] * scale, v[3] * scale)
  86. label.setDrawOrder(drawOrder)
  87. return (label, text)
  88. def modelLabel(model,
  89. geomRect = None,
  90. scale = 0.1,
  91. drawOrder = getDefaultDrawOrder()):
  92. # Preserve transitions on the arc by creating an intervening node.
  93. topnode = NamedNode('model')
  94. topnp = NodePath(topnode)
  95. mi = model.instanceTo(topnp)
  96. mi.setScale(scale)
  97. mi.setBin('fixed', drawOrder)
  98. if geomRect == None:
  99. geomRect = (1, 1)
  100. if len(geomRect) == 2:
  101. # If we got only two parameters, it's height and width.
  102. label = GuiLabel.makeModelLabel(topnode,
  103. geomRect[0] * scale,
  104. geomRect[1] * scale)
  105. elif len(geomRect) == 4:
  106. # If we got four, they're left, right, bottom, top.
  107. label = GuiLabel.makeModelLabel(topnode,
  108. geomRect[0] * scale,
  109. geomRect[1] * scale,
  110. geomRect[2] * scale,
  111. geomRect[3] * scale)
  112. else:
  113. raise ValueError
  114. label.setDrawOrder(drawOrder)
  115. return label