LUIFrame.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from __future__ import print_function
  2. from LUIObject import LUIObject
  3. from LUISprite import LUISprite
  4. from LUILayouts import LUICornerLayout
  5. from LUIInitialState import LUIInitialState
  6. from LUIScrollableRegion import LUIScrollableRegion
  7. __all__ = ["LUIFrame"]
  8. class LUIFrame(LUIObject):
  9. """ A container which can store multiple ui-elements. If you don't want a
  10. border/background, you should use an empty LUIObject as container instead.
  11. """
  12. FS_sunken = 1
  13. FS_raised = 2
  14. def __init__(self, inner_padding=5, scrollable=False, style=FS_raised,
  15. **kwargs):
  16. """ Creates a new frame with the given options and style. If scrollable
  17. is True, the contents of the frame will scroll if they don't fit into
  18. the frame height. inner_padding only has effect if scrollable is True.
  19. You can call fit_to_children() to make the frame fit automatically to
  20. it's contents."""
  21. LUIObject.__init__(self)
  22. # Each *style* has a different border size (size of the shadow). The
  23. # border size shouldn't get calculated to the actual framesize, so we
  24. # are determining it first and then substracting it.
  25. # TODO: We could do this automatically, determined by the sprite size
  26. # probably?
  27. self._border_size = 0
  28. self.padding = 10
  29. self.solid = True
  30. prefix = ""
  31. if style == LUIFrame.FS_raised:
  32. temp = LUISprite(self, "Frame_Left", "skin")
  33. self._border_size = temp.width
  34. self.remove_child(temp)
  35. prefix = "Frame_"
  36. elif style == LUIFrame.FS_sunken:
  37. self._border_size = 0
  38. prefix = "SunkenFrame_"
  39. else:
  40. raise Exception("Unkown LUIFrame style: " + style)
  41. self._scrollable = scrollable
  42. self._layout = LUICornerLayout(parent=self, image_prefix=prefix)
  43. self._layout.margin = -(self.padding.top + self._border_size)
  44. if self._scrollable:
  45. self._content = LUIObject(self)
  46. self._content.size = (self.width, self.height)
  47. self._content.pos = (self._border_size, self._border_size)
  48. self._scroll_content = LUIScrollableRegion(
  49. self._content,
  50. width=self.width - 2 * self.padding.left,
  51. height=self.height - 2 * self.padding.left,
  52. padding=inner_padding)
  53. self.content_node = self._scroll_content.content_node
  54. LUIInitialState.init(self, kwargs)
  55. def hasMouse(self):
  56. if not self.visible: return False
  57. if base.mouseWatcherNode.hasMouse():
  58. mpos = base.mouseWatcherNode.getMouse()
  59. mousePos = (mpos.getX() * base.getAspectRatio(), mpos.getY())
  60. screenWidth = base.win.getProperties().getXSize()
  61. screenHeight = base.win.getProperties().getYSize()
  62. ratio = base.getAspectRatio()
  63. relLeft = -ratio
  64. relTop = 1
  65. zeroXPx = screenWidth / 2
  66. zeroZPx = screenHeight / 2
  67. objWidth = self.get_width()
  68. objHeight = self.get_height()
  69. objX, objZ = [0,0]
  70. if self.center_horizontal:
  71. objX = zeroXPx - (objWidth / 2)
  72. else:
  73. objX = self.pos[0]
  74. objX += self.margin.left
  75. if self.center_vertical:
  76. objZ = zeroZPx - (objHeight / 2)
  77. else:
  78. objZ = self.pos[1]
  79. objZ += self.margin.top
  80. oneUnitPerPixelX = ratio / zeroXPx
  81. oneUnitPerPixelZ = 1 / zeroZPx
  82. left = relLeft + (oneUnitPerPixelX * objX)
  83. top = relTop - (oneUnitPerPixelZ * objZ)
  84. right = left + (oneUnitPerPixelX * objWidth)
  85. bottom = top - (oneUnitPerPixelZ * objHeight)
  86. if (mousePos[0] <= right # Right
  87. and mousePos[0] >= left # Left
  88. and mousePos[1] >= bottom # Bottom
  89. and mousePos[1] <= top): # Top
  90. return True
  91. return False
  92. return False