Elements.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. """
  2. OUTDATED
  3. Do not use anymore.
  4. """
  5. #from panda3d.lui import *
  6. #from panda3d.core import Point2
  7. #from functools import partial
  8. #from LUILayouts import *
  9. #import math
  10. import colorsys
  11. from LUICallback import LUICallback
  12. from LUILabel import LUILabel
  13. from LUIFrame import LUIFrame
  14. from LUIButton import LUIButton
  15. from LUIObject import LUIObject
  16. from LUISlider import LUISlider
  17. from LUISprite import LUISprite
  18. from LUIVerticalLayout import LUIVerticalLayout
  19. class LUISliderWithLabel(LUIObject, LUICallback):
  20. def __init__(self, parent=None, width=100.0, filled=False, min_value=0, max_value=1.0, precision=2, value=None):
  21. LUIObject.__init__(self, x=0, y=0, w=width, h=0)
  22. LUICallback.__init__(self)
  23. max_numbers_before = max(len(str(int(max_value))), len(str(int(min_value))))
  24. number_space_required = max_numbers_before
  25. if precision > 0:
  26. number_space_required += 1 + precision
  27. pixels_per_number = 7
  28. self.precision = precision
  29. self.slider = LUISlider(self, width=width - pixels_per_number * number_space_required - 5, filled=filled, min_value=min_value, max_value=max_value, value=value)
  30. self.label = LUILabel(parent=self, shadow=True, text=u"1.23")
  31. self.label.right = 0
  32. self.label.top = self.label.height - self.slider.height
  33. self.label.color = (1,1,1,0.5)
  34. self.slider.add_change_callback(self._on_slider_changed)
  35. self.slider.add_change_callback(self._trigger_callback)
  36. self._on_slider_changed(self.slider, self.slider.get_value())
  37. if parent is not None:
  38. self.parent = parent
  39. self.fit_to_children()
  40. def get_value(self):
  41. return self.slider.get_value()
  42. def set_value(self, val):
  43. self.slider.set_value(val)
  44. def _on_slider_changed(self, obj, value):
  45. self.label.text = ("{:." + str(self.precision) + "f}").format(value)
  46. class LUIKeyMarker(LUIObject):
  47. def __init__(self, parent=None, key=u"A"):
  48. LUIObject.__init__(self)
  49. self.bgLeft = LUISprite(self, "Keymarker_Left", "skin")
  50. self.bgMid = LUISprite(self, "Keymarker", "skin")
  51. self.bgRight = LUISprite(self, "Keymarker_Right", "skin")
  52. self.label = LUILabel(parent=self, text=key, shadow=True)
  53. self.label.centered = (True, True)
  54. self.label.margin = (-3, 0, 0, -1)
  55. self.margin = (-1, 0, 0, -1)
  56. self.set_key(key)
  57. if parent is not None:
  58. self.parent = parent
  59. self.fit_to_children()
  60. def set_key(self, key):
  61. self.label.set_text(key)
  62. self.width = self.label.width + self.bgLeft.width + self.bgRight.width + 7
  63. self.bgMid.width = self.width - self.bgLeft.width - self.bgRight.width
  64. self.bgMid.left = self.bgLeft.width
  65. self.bgRight.left = self.bgMid.width + self.bgMid.left
  66. self.fit_to_children()
  67. class LUIKeyInstruction(LUIObject):
  68. def __init__(self, parent=None, key=u"A", instruction=u"Instruction"):
  69. LUIObject.__init__(self)
  70. self.marker = LUIKeyMarker(parent=self, key=key)
  71. self.instructionLabel = LUILabel(parent=self, text=instruction, shadow=True)
  72. self.instructionLabel.centered = (False, True)
  73. self.instructionLabel.margin.top = -4
  74. self.set_key(key)
  75. def set_key(self, key):
  76. self.marker.set_key(key)
  77. self.instructionLabel.left = self.marker.width + 5
  78. self.fit_to_children()
  79. class LUIColorpicker(LUIObject):
  80. def __init__(self, parent=None, color=None):
  81. LUIObject.__init__(self, x=0, y=0, w=27, h=27)
  82. self.previewBg = LUISprite(self, "ColorpickerPreviewBg", "skin")
  83. self.filler = LUISprite(self, "blank", "skin")
  84. self.filler.width = 21
  85. self.filler.height = 21
  86. self.filler.pos = (5, 5)
  87. self.filler.color = (0.2,0.6,1.0,1.0)
  88. self.overlay = LUISprite(self, "ColorpickerPreviewOverlay", "skin")
  89. self.overlay.pos = (2, 2)
  90. self.overlay.bind("click", self._open_dialog)
  91. self.fit_to_children()
  92. self.popup = LUIColorpickerPopup(self)
  93. self.popup.hide()
  94. if color is not None:
  95. self.colorValue = color
  96. else:
  97. # My favourite color
  98. self.colorValue = (0.2, 0.6, 1.0)
  99. self.set_color_value(self.colorValue)
  100. self.popup.add_change_callback(self._on_popup_color_changed)
  101. if parent is not None:
  102. self.parent = parent
  103. def _open_dialog(self, event):
  104. if self.has_focus():
  105. self.blur()
  106. else:
  107. self.request_focus()
  108. def on_focus(self, event):
  109. self.popup._load_rgb(self.colorValue)
  110. self.popup.open_at(self, 14.0)
  111. def set_color_value(self, rgb):
  112. self.colorValue = rgb
  113. self.filler.color = rgb
  114. def get_color_value(self):
  115. return self.colorValue
  116. def on_tick(self, event):
  117. self.popup._update(event)
  118. def on_blur(self, event):
  119. self.popup.close()
  120. def _on_popup_color_changed(self, popup, rgb):
  121. self.set_color_value(rgb)
  122. def _on_popup_closed(self):
  123. self.blur()
  124. class LUIPopup(LUIFrame):
  125. def __init__(self, parent=None, width=200, height=200):
  126. LUIFrame.__init__(self, parent=parent, width=width, height=height, padding=10, innerPadding=0)
  127. self.topmost = True
  128. self.borderSize = 33
  129. self.content.bind("click", self._on_content_click)
  130. def open_at(self, targetElement, distance):
  131. self.show()
  132. targetPos = targetElement.get_abs_pos()+ targetElement.get_size() / 2
  133. showAbove = targetPos.y > self.height - self.borderSize
  134. showLeft = targetPos.x > self.width - self.borderSize
  135. relative = self.get_relative_pos(targetPos)
  136. self.pos += relative
  137. if showLeft:
  138. self.left -= self.width - self.borderSize
  139. self.left += 25
  140. else:
  141. self.left -= self.borderSize
  142. self.left -= 25
  143. if showAbove:
  144. self.top -= distance
  145. self.top -= self.height - self.borderSize
  146. else:
  147. self.top += distance
  148. self.top -= self.borderSize
  149. def _on_content_click(self, event):
  150. pass
  151. def close(self):
  152. self.hide()
  153. class LUIColorpickerPopup(LUIPopup, LUICallback):
  154. def __init__(self, parent=None):
  155. LUIPopup.__init__(self, parent=parent, width=240, height=146)
  156. LUICallback.__init__(self)
  157. self.field = LUIObject(self.content, x=0, y=0, w=128, h=128)
  158. self.fieldBG = LUISprite(self.field, "blank", "skin")
  159. self.fieldBG.size = (128, 128)
  160. self.fieldBG.color = (0.2,0.6,1.0)
  161. self.fieldFG = LUISprite(self.field, "ColorpickerFieldOverlay", "skin")
  162. self.fieldFG.pos = (-2, 0)
  163. self.fieldBG.bind("mousedown", self._start_field_dragging)
  164. self.fieldBG.bind("mouseup", self._stop_field_dragging)
  165. self.fieldHandle = LUISprite(self.field, "ColorpickerFieldHandle", "skin")
  166. self.fieldHandle.bind("mousedown", self._start_field_dragging)
  167. self.fieldHandle.bind("mouseup", self._stop_field_dragging)
  168. self.fieldDragging = False
  169. self.hueSlider = LUIObject(self.content, x=140, y=0, w=40, h=128)
  170. self.hueSliderFG = LUISprite(self.hueSlider, "ColorpickerHueSlider", "skin")
  171. self.hueHandle = LUISprite(self.hueSlider, "ColorpickerHueHandle", "skin")
  172. self.hueHandle.left = (self.hueSliderFG.width - self.hueHandle.width) / 2.0
  173. self.hueHandle.top = 50
  174. self.hueDragging = False
  175. self.hueSlider.bind("mousedown", self._start_hue_dragging)
  176. self.hueSlider.bind("mouseup", self._stop_hue_dragging)
  177. self.labels = LUIVerticalLayout(self.content, width=40)
  178. self.labels.pos = (177, 42)
  179. colors = [u"R", u"G", u"B"]
  180. self.colorLabels = []
  181. for color in colors:
  182. label = LUILabel(text=color, shadow=True)
  183. label.color = (1,1,1,0.3)
  184. valueLabel = LUILabel(text=u"255", shadow=True)
  185. valueLabel.right = 0
  186. self.labels.add(label, valueLabel)
  187. self.colorLabels.append(valueLabel)
  188. self.activeColor = LUIObject(self.content, x=177, y=0)
  189. self.activeColorBG = LUISprite(self.activeColor, "blank", "skin")
  190. self.activeColorFG = LUISprite(self.activeColor, "ColorpickerActiveColorOverlay", "skin")
  191. self.activeColorBG.size = (40, 40)
  192. self.activeColorBG.pos = (2, 0)
  193. self.activeColorBG.color = (0.2,0.6,1.0,1.0)
  194. self.closeButton = LUIButton(parent=self.content, text=u"Done", width=45, template="ButtonGreen")
  195. self.closeButton.left = 177
  196. self.closeButton.top = 98
  197. self.closeButton.bind("click", self._close_popup)
  198. self._set_hue(0.5)
  199. self._set_sat_val(0.5, 0.5)
  200. self.widget = parent
  201. def _load_rgb(self, rgb):
  202. hsv = colorsys.rgb_to_hsv(*rgb)
  203. self._set_hue(hsv[0])
  204. self._set_sat_val(hsv[1], hsv[2])
  205. def _close_popup(self, event):
  206. self.widget._on_popup_closed()
  207. self.close()
  208. def _update(self, event):
  209. if self.hueDragging:
  210. offset = event.coordinates.y - self.hueSliderFG.abs_pos.y
  211. offset /= 128.0
  212. offset = 1.0 - max(0.0, min(1.0, offset))
  213. self._set_hue(offset)
  214. if self.fieldDragging:
  215. offset = event.coordinates - self.fieldBG.abs_pos
  216. saturation = max(0.0, min(1.0, offset.x / 128.0))
  217. value = 1.0 - max(0.0, min(1.0, offset.y / 128.0))
  218. self._set_sat_val(saturation, value)
  219. self._update_color()
  220. def _set_sat_val(self, sat, val):
  221. self.saturation = sat
  222. self.valueValue = val
  223. self.fieldHandle.top = (1.0 - self.valueValue) * 128.0 - self.fieldHandle.height / 2.0
  224. self.fieldHandle.left = self.saturation * 128.0 - self.fieldHandle.width / 2.0
  225. def _set_hue(self, hue):
  226. self.hueValue = min(0.999, hue)
  227. self.hueHandle.top = (1.0-hue) * 128.0 - self.hueHandle.height / 2
  228. self.fieldBG.color = colorsys.hsv_to_rgb(self.hueValue, 1, 1)
  229. def _update_color(self):
  230. rgb = colorsys.hsv_to_rgb(self.hueValue, self.saturation, self.valueValue)
  231. self.activeColorBG.color = rgb
  232. self.colorLabels[0].set_text(str(int(rgb[0]*255.0)).encode('utf-8'))
  233. self.colorLabels[1].set_text(str(int(rgb[1]*255.0)).encode('utf-8'))
  234. self.colorLabels[2].set_text(str(int(rgb[2]*255.0)).encode('utf-8'))
  235. self._trigger_callback(rgb)
  236. def _start_field_dragging(self, event):
  237. if not self.fieldDragging:
  238. self.fieldDragging = True
  239. def _stop_field_dragging(self, event):
  240. if self.fieldDragging:
  241. self.fieldDragging = False
  242. def _start_hue_dragging(self, event):
  243. if not self.hueDragging:
  244. self.hueDragging = True
  245. def _stop_hue_dragging(self, event):
  246. if self.hueDragging:
  247. self.hueDragging = False