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