1 from __future__ import division
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
10 from wx import glcanvas
12 OpenGL.ERROR_CHECKING = False
13 from OpenGL.GL import *
15 from Cura.util import version
16 from Cura.gui.util import openglHelpers
18 class animation(object):
19 def __init__(self, gui, start, end, runTime):
22 self._startTime = time.time()
23 self._runTime = runTime
24 gui._animationList.append(self)
27 return time.time() > self._startTime + self._runTime
29 def getPosition(self):
32 f = (time.time() - self._startTime) / self._runTime
35 #f = 6*tc*ts + -15*ts*ts + 10*tc
37 return self._start + (self._end - self._start) * f
39 class glGuiControl(object):
40 def __init__(self, parent, pos):
42 self._base = parent._base
44 self._size = (0,0, 1, 1)
45 self._parent.add(self)
47 def setSize(self, x, y, w, h):
48 self._size = (x, y, w, h)
56 def updateLayout(self):
60 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
61 if self._parent._glGuiControlList[n].setFocus():
63 for n in xrange(0, self._parent._glGuiControlList.index(self)):
64 if self._parent._glGuiControlList[n].setFocus():
67 def focusPrevious(self):
68 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
69 if self._parent._glGuiControlList[n].setFocus():
71 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
72 if self._parent._glGuiControlList[n].setFocus():
79 return self._base._focus == self
81 def OnMouseUp(self, x, y):
84 def OnKeyChar(self, key):
87 class glGuiContainer(glGuiControl):
88 def __init__(self, parent, pos):
89 self._glGuiControlList = []
90 glGuiLayoutButtons(self)
91 super(glGuiContainer, self).__init__(parent, pos)
94 self._glGuiControlList.append(ctrl)
97 def OnMouseDown(self, x, y, button):
98 for ctrl in self._glGuiControlList:
99 if ctrl.OnMouseDown(x, y, button):
103 def OnMouseUp(self, x, y):
104 for ctrl in self._glGuiControlList:
105 if ctrl.OnMouseUp(x, y):
109 def OnMouseMotion(self, x, y):
111 for ctrl in self._glGuiControlList:
112 if ctrl.OnMouseMotion(x, y):
117 for ctrl in self._glGuiControlList:
120 def updateLayout(self):
121 self._layout.update()
122 for ctrl in self._glGuiControlList:
125 class glGuiPanel(glcanvas.GLCanvas):
126 def __init__(self, parent):
127 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8, 0)
128 glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList)
131 self._container = None
132 self._container = glGuiContainer(self, (0,0))
133 self._shownError = False
135 self._context = glcanvas.GLContext(self)
136 self._glButtonsTexture = None
137 self._glRobotTexture = None
138 self._buttonSize = 64
140 self._animationList = []
141 self.glReleaseList = []
142 self._refreshQueued = False
143 self._idleCalled = False
145 wx.EVT_PAINT(self, self._OnGuiPaint)
146 wx.EVT_SIZE(self, self._OnSize)
147 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
148 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseDown)
149 wx.EVT_LEFT_DCLICK(self, self._OnGuiMouseDown)
150 wx.EVT_LEFT_UP(self, self._OnGuiMouseUp)
151 wx.EVT_RIGHT_DOWN(self, self._OnGuiMouseDown)
152 wx.EVT_RIGHT_DCLICK(self, self._OnGuiMouseDown)
153 wx.EVT_RIGHT_UP(self, self._OnGuiMouseUp)
154 wx.EVT_MIDDLE_DOWN(self, self._OnGuiMouseDown)
155 wx.EVT_MIDDLE_DCLICK(self, self._OnGuiMouseDown)
156 wx.EVT_MIDDLE_UP(self, self._OnGuiMouseUp)
157 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
158 wx.EVT_CHAR(self, self._OnGuiKeyChar)
159 wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
160 wx.EVT_IDLE(self, self._OnIdle)
162 def _OnIdle(self, e):
163 self._idleCalled = True
164 if len(self._animationList) > 0 or self._refreshQueued:
165 self._refreshQueued = False
166 for anim in self._animationList:
168 self._animationList.remove(anim)
171 def _OnGuiKeyChar(self, e):
172 if self._focus is not None:
173 self._focus.OnKeyChar(e.GetKeyCode())
176 self.OnKeyChar(e.GetKeyCode())
178 def OnFocusLost(self, e):
182 def _OnGuiMouseDown(self,e):
184 if self._container.OnMouseDown(e.GetX(), e.GetY(), e.GetButton()):
189 def _OnGuiMouseUp(self, e):
190 if self._container.OnMouseUp(e.GetX(), e.GetY()):
195 def _OnGuiMouseMotion(self,e):
197 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
198 self.OnMouseMotion(e)
200 def _OnGuiPaint(self, e):
201 self._idleCalled = False
202 h = self.GetSize().GetHeight()
203 w = self.GetSize().GetWidth()
204 oldButtonSize = self._buttonSize
208 self._buttonSize = 32
210 self._buttonSize = 48
212 self._buttonSize = 64
214 self._buttonSize = 80
216 self._buttonSize = 96
217 if self._buttonSize != oldButtonSize:
218 self._container.updateLayout()
220 dc = wx.PaintDC(self)
222 self.SetCurrent(self._context)
223 for obj in self.glReleaseList:
225 del self.glReleaseList[:]
226 renderStartTime = time.time()
230 if version.isDevVersion():
231 renderTime = time.time() - renderStartTime
235 glTranslate(10, self.GetSize().GetHeight() - 30, -1)
236 glColor4f(0.2,0.2,0.2,0.5)
237 openglHelpers.glDrawStringLeft("fps:%d" % (1 / renderTime))
240 # When an exception happens, catch it and show a message box. If the exception is not caught the draw function bugs out.
241 # Only show this exception once so we do not overload the user with popups.
242 errStr = _("An error has occurred during the 3D view drawing.")
243 tb = traceback.extract_tb(sys.exc_info()[2])
244 errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
245 for n in xrange(len(tb)-1, -1, -1):
247 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
248 if not self._shownError:
249 traceback.print_exc()
250 wx.CallAfter(wx.MessageBox, errStr, _("3D window error"), wx.OK | wx.ICON_EXCLAMATION)
251 self._shownError = True
254 if self._glButtonsTexture is None:
255 self._glButtonsTexture = openglHelpers.loadGLTexture('glButtons.png')
256 self._glRobotTexture = openglHelpers.loadGLTexture('UltimakerRobot.png')
258 glDisable(GL_DEPTH_TEST)
260 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
261 glDisable(GL_LIGHTING)
262 glColor4ub(255,255,255,255)
264 glMatrixMode(GL_PROJECTION)
266 size = self.GetSize()
267 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
268 glMatrixMode(GL_MODELVIEW)
271 self._container.draw()
273 # glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
274 # glEnable(GL_TEXTURE_2D)
277 # glTranslate(size.GetWidth(),size.GetHeight(),0)
278 # s = self._buttonSize * 1
280 # glTranslate(-1.2,-0.2,0)
291 # glDisable(GL_TEXTURE_2D)
294 def _OnEraseBackground(self,event):
295 #Workaround for windows background redraw flicker.
299 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
300 self._container.updateLayout()
303 def OnMouseDown(self,e):
305 def OnMouseUp(self,e):
307 def OnMouseMotion(self, e):
309 def OnKeyChar(self, keyCode):
311 def OnPaint(self, e):
313 def OnKeyChar(self, keycode):
316 def QueueRefresh(self):
317 wx.CallAfter(self._queueRefresh)
319 def _queueRefresh(self):
321 wx.CallAfter(self.Refresh)
323 self._refreshQueued = True
326 if self._container is not None:
327 self._container.add(ctrl)
329 class glGuiLayoutButtons(object):
330 def __init__(self, parent):
331 self._parent = parent
332 self._parent._layout = self
335 bs = self._parent._base._buttonSize
336 x0, y0, w, h = self._parent.getSize()
338 for ctrl in self._parent._glGuiControlList:
341 x = w + pos[0] * gridSize - bs * 0.2
343 x = pos[0] * gridSize + bs * 0.2
345 y = h + pos[1] * gridSize * 1.2 - bs * 0.0
347 y = pos[1] * gridSize * 1.2 + bs * 0.2
348 ctrl.setSize(x, y, gridSize, gridSize)
350 def getLayoutSize(self):
351 _, _, w, h = self._parent.getSize()
354 class glGuiLayoutGrid(object):
355 def __init__(self, parent):
356 self._parent = parent
357 self._parent._layout = self
359 self._alignBottom = True
362 borderSize = self._parent._base._buttonSize * 0.2
363 x0, y0, w, h = self._parent.getSize()
368 for ctrl in self._parent._glGuiControlList:
370 w, h = ctrl.getMinSize()
374 widths[x] = max(widths[x], w)
378 heights[y] = max(heights[y], h)
379 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
380 if self._alignBottom:
381 y0 -= self._size[1] - self._parent.getSize()[3]
382 self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
383 for ctrl in self._parent._glGuiControlList:
387 for n in xrange(0, x):
391 for n in xrange(0, y):
395 ctrl.setSize(x1, y1, widths[x], heights[y])
397 def getLayoutSize(self):
400 class glButton(glGuiControl):
401 def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
402 self._buttonSize = size
404 super(glButton, self).__init__(parent, pos)
405 self._tooltip = tooltip
406 self._parent = parent
407 self._imageID = imageID
408 self._callback = callback
409 self._selected = False
411 self._disabled = False
412 self._showExpandArrow = False
413 self._progressBar = None
414 self._altTooltip = ''
416 def setSelected(self, value):
417 self._selected = value
419 def setExpandArrow(self, value):
420 self._showExpandArrow = value
422 def setHidden(self, value):
425 def setDisabled(self, value):
426 self._disabled = value
428 def setProgressBar(self, value):
429 self._progressBar = value
431 def getProgressBar(self):
432 return self._progressBar
434 def setBottomText(self, value):
435 self._altTooltip = value
437 def getSelected(self):
438 return self._selected
440 def getMinSize(self):
443 if self._buttonSize is not None:
444 return self._buttonSize, self._buttonSize
445 return self._base._buttonSize, self._base._buttonSize
447 def _getPixelPos(self):
448 x0, y0, w, h = self.getSize()
449 return x0 + w / 2, y0 + h / 2
455 cx = (self._imageID % 4) / 4
456 cy = int(self._imageID / 4) / 4
457 bs = self.getMinSize()[0]
458 pos = self._getPixelPos()
460 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
467 glColor4ub(128,128,128,128)
469 glColor4ub(255,255,255,255)
470 openglHelpers.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
471 openglHelpers.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
472 if self._showExpandArrow:
474 openglHelpers.glDrawTexturedQuad(pos[0]+bs*scale/2-bs*scale/4*1.2, pos[1]-bs*scale/2*1.2, bs*scale/4, bs*scale/4, 1)
476 openglHelpers.glDrawTexturedQuad(pos[0]+bs*scale/2-bs*scale/4*1.2, pos[1]-bs*scale/2*1.2, bs*scale/4, bs*scale/4, 1, 2)
478 glTranslatef(pos[0], pos[1], 0)
479 glDisable(GL_TEXTURE_2D)
481 glTranslatef(0, -0.55*bs*scale, 0)
484 glColor4ub(60,60,60,255)
485 glTranslatef(-1, -1, 0)
486 openglHelpers.glDrawStringCenter(self._tooltip)
487 glTranslatef(0, 2, 0)
488 openglHelpers.glDrawStringCenter(self._tooltip)
489 glTranslatef(2, 0, 0)
490 openglHelpers.glDrawStringCenter(self._tooltip)
491 glTranslatef(0, -2, 0)
492 openglHelpers.glDrawStringCenter(self._tooltip)
495 glColor4ub(255,255,255,255)
496 openglHelpers.glDrawStringCenter(self._tooltip)
498 progress = self._progressBar
499 if progress is not None:
500 glColor4ub(60,60,60,255)
501 openglHelpers.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
502 glColor4ub(255,255,255,255)
503 openglHelpers.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
504 elif len(self._altTooltip) > 0:
506 glTranslatef(pos[0], pos[1], 0)
507 glTranslatef(0, 0.6*bs, 0)
508 glTranslatef(0, 6, 0)
509 #glTranslatef(0.6*bs*scale, 0, 0)
511 for line in self._altTooltip.split('\n'):
513 glColor4ub(60,60,60,255)
514 glTranslatef(-1, -1, 0)
515 openglHelpers.glDrawStringCenter(line)
516 glTranslatef(0, 2, 0)
517 openglHelpers.glDrawStringCenter(line)
518 glTranslatef(2, 0, 0)
519 openglHelpers.glDrawStringCenter(line)
520 glTranslatef(0, -2, 0)
521 openglHelpers.glDrawStringCenter(line)
524 glColor4ub(255,255,255,255)
525 openglHelpers.glDrawStringCenter(line)
526 glTranslatef(0, 18, 0)
529 def _checkHit(self, x, y):
530 if self._hidden or self._disabled:
532 bs = self.getMinSize()[0]
533 pos = self._getPixelPos()
534 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
536 def OnMouseMotion(self, x, y):
537 if self._checkHit(x, y):
543 def OnMouseDown(self, x, y, button):
544 if self._checkHit(x, y):
545 self._callback(button)
549 class glRadioButton(glButton):
550 def __init__(self, parent, imageID, tooltip, pos, group, callback):
551 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
553 self._radioCallback = callback
554 self._group.append(self)
556 def setSelected(self, value):
557 self._selected = value
559 def _onRadioSelect(self, button):
560 self._base._focus = None
561 for ctrl in self._group:
563 ctrl.setSelected(False)
564 if self.getSelected():
565 self.setSelected(False)
567 self.setSelected(True)
568 self._radioCallback(button)
570 class glComboButton(glButton):
571 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
572 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
573 self._imageIDs = imageIDs
574 self._tooltips = tooltips
575 self._comboCallback = callback
578 def _onComboOpenSelect(self, button):
580 self._base._focus = None
582 self._base._focus = self
587 self._selected = self.hasFocus()
588 super(glComboButton, self).draw()
590 bs = self._base._buttonSize / 2
591 pos = self._getPixelPos()
593 if not self._selected:
597 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
598 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
599 for n in xrange(0, len(self._imageIDs)):
600 glTranslatef(0, bs, 0)
601 glColor4ub(255,255,255,255)
602 openglHelpers.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
603 openglHelpers.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
604 glDisable(GL_TEXTURE_2D)
607 glTranslatef(-0.55*bs, 0.1*bs, 0)
610 glColor4ub(60,60,60,255)
611 glTranslatef(-1, -1, 0)
612 openglHelpers.glDrawStringRight(self._tooltips[n])
613 glTranslatef(0, 2, 0)
614 openglHelpers.glDrawStringRight(self._tooltips[n])
615 glTranslatef(2, 0, 0)
616 openglHelpers.glDrawStringRight(self._tooltips[n])
617 glTranslatef(0, -2, 0)
618 openglHelpers.glDrawStringRight(self._tooltips[n])
621 glColor4ub(255,255,255,255)
622 openglHelpers.glDrawStringRight(self._tooltips[n])
627 return self._selection
629 def setValue(self, value):
630 self._selection = value
631 self._imageID = self._imageIDs[self._selection]
632 self._comboCallback()
634 def OnMouseDown(self, x, y, button):
635 if self._hidden or self._disabled:
638 bs = self._base._buttonSize / 2
639 pos = self._getPixelPos()
640 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
641 self._selection = int((y - pos[1] - bs) / bs)
642 self._imageID = self._imageIDs[self._selection]
643 self._base._focus = None
644 self._comboCallback()
646 return super(glComboButton, self).OnMouseDown(x, y, button)
648 class glFrame(glGuiContainer):
649 def __init__(self, parent, pos):
650 super(glFrame, self).__init__(parent, pos)
651 self._selected = False
655 def setSelected(self, value):
656 self._selected = value
658 def setHidden(self, value):
660 for child in self._glGuiControlList:
661 if self._base._focus == child:
662 self._base._focus = None
664 def getSelected(self):
665 return self._selected
667 def getMinSize(self):
668 return self._base._buttonSize, self._base._buttonSize
670 def _getPixelPos(self):
671 x0, y0, w, h = self.getSize()
678 bs = self._parent._buttonSize
679 pos = self._getPixelPos()
681 size = self._layout.getLayoutSize()
682 glColor4ub(255,255,255,255)
683 openglHelpers.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
684 #Draw the controls on the frame
685 super(glFrame, self).draw()
687 def _checkHit(self, x, y):
690 pos = self._getPixelPos()
691 w, h = self._layout.getLayoutSize()
692 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
694 def OnMouseMotion(self, x, y):
695 super(glFrame, self).OnMouseMotion(x, y)
696 if self._checkHit(x, y):
702 def OnMouseDown(self, x, y, button):
703 if self._checkHit(x, y):
704 super(glFrame, self).OnMouseDown(x, y, button)
708 class glNotification(glFrame):
709 def __init__(self, parent, pos):
711 super(glNotification, self).__init__(parent, pos)
712 glGuiLayoutGrid(self)._alignBottom = False
713 self._label = glLabel(self, "Notification", (0, 0))
714 self._buttonExtra = glButton(self, 31, "???", (1, 0), self.onExtraButton, 25)
715 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
716 self._padding = glLabel(self, "", (0, 1))
719 def setSize(self, x, y, w, h):
720 w, h = self._layout.getLayoutSize()
721 baseSize = self._base.GetSizeTuple()
722 if self._anim is not None:
723 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
725 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
728 self.setSize(0,0,0,0)
730 super(glNotification, self).draw()
732 def message(self, text, extraButtonCallback = None, extraButtonIcon = None, extraButtonTooltip = None):
733 self._anim = animation(self._base, -20, 25, 1)
734 self.setHidden(False)
735 self._label.setLabel(text)
736 self._buttonExtra.setHidden(extraButtonCallback is None)
737 self._buttonExtra._imageID = extraButtonIcon
738 self._buttonExtra._tooltip = extraButtonTooltip
739 self._extraButtonCallback = extraButtonCallback
740 self._base._queueRefresh()
743 def onExtraButton(self, button):
745 self._extraButtonCallback()
747 def onClose(self, button):
748 if self._anim is not None:
749 self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
751 self._anim = animation(self._base, 25, -20, 1)
753 class glLabel(glGuiControl):
754 def __init__(self, parent, label, pos):
756 super(glLabel, self).__init__(parent, pos)
758 def setLabel(self, label):
761 def getMinSize(self):
762 w, h = openglHelpers.glGetStringSize(self._label)
765 def _getPixelPos(self):
766 x0, y0, w, h = self.getSize()
770 x, y, w, h = self.getSize()
773 glTranslatef(x, y, 0)
775 # glColor4ub(255,255,255,128)
787 glTranslate(5, h - 5, 0)
788 glColor4ub(255,255,255,255)
789 openglHelpers.glDrawStringLeft(self._label)
792 def _checkHit(self, x, y):
795 def OnMouseMotion(self, x, y):
798 def OnMouseDown(self, x, y, button):
801 class glNumberCtrl(glGuiControl):
802 def __init__(self, parent, value, pos, callback):
803 self._callback = callback
804 self._value = str(value)
807 self._inCallback = False
808 super(glNumberCtrl, self).__init__(parent, pos)
810 def setValue(self, value):
813 self._value = str(value)
815 def getMinSize(self):
816 w, h = openglHelpers.glGetStringSize("VALUES")
819 def _getPixelPos(self):
820 x0, y0, w, h = self.getSize()
824 x, y, w, h = self.getSize()
827 glTranslatef(x, y, 0)
830 glColor4ub(255,255,255,255)
832 glColor4ub(255,255,255,192)
844 glTranslate(5, h - 5, 0)
845 glColor4ub(0,0,0,255)
846 openglHelpers.glDrawStringLeft(self._value)
848 glTranslate(openglHelpers.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
849 openglHelpers.glDrawStringLeft('|')
852 def _checkHit(self, x, y):
853 x1, y1, w, h = self.getSize()
854 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
856 def OnMouseMotion(self, x, y):
859 def OnMouseDown(self, x, y, button):
860 if self._checkHit(x, y):
865 def OnKeyChar(self, c):
866 self._inCallback = True
869 self._selectPos = max(0, self._selectPos)
870 if c == wx.WXK_RIGHT:
872 self._selectPos = min(self._selectPos, len(self._value))
875 value = float(self._value)
880 self._value = str(value)
881 self._callback(self._value)
884 value = float(self._value)
890 self._value = str(value)
891 self._callback(self._value)
892 if c == wx.WXK_BACK and self._selectPos > 0:
893 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
895 self._callback(self._value)
896 if c == wx.WXK_DELETE:
897 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
898 self._callback(self._value)
899 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
900 if wx.GetKeyState(wx.WXK_SHIFT):
904 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
905 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
907 self._callback(self._value)
908 self._inCallback = False
911 self._base._focus = self
912 self._selectPos = len(self._value)
915 class glCheckbox(glGuiControl):
916 def __init__(self, parent, value, pos, callback):
917 self._callback = callback
921 self._inCallback = False
922 super(glCheckbox, self).__init__(parent, pos)
924 def setValue(self, value):
927 self._value = str(value)
932 def getMinSize(self):
935 def _getPixelPos(self):
936 x0, y0, w, h = self.getSize()
940 x, y, w, h = self.getSize()
943 glTranslatef(x, y, 0)
945 glColor3ub(255,255,255)
947 openglHelpers.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
949 openglHelpers.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
953 def _checkHit(self, x, y):
954 x1, y1, w, h = self.getSize()
955 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
957 def OnMouseMotion(self, x, y):
960 def OnMouseDown(self, x, y, button):
961 if self._checkHit(x, y):
962 self._value = not self._value
966 class glSlider(glGuiControl):
967 def __init__(self, parent, value, minValue, maxValue, pos, callback):
968 super(glSlider, self).__init__(parent, pos)
969 self._callback = callback
973 self._minValue = minValue
974 self._maxValue = maxValue
976 def setValue(self, value):
980 if self._value < self._minValue:
981 return self._minValue
982 if self._value > self._maxValue:
983 return self._maxValue
986 def setRange(self, minValue, maxValue):
987 if maxValue < minValue:
989 self._minValue = minValue
990 self._maxValue = maxValue
992 def getMinValue(self):
993 return self._minValue
995 def getMaxValue(self):
996 return self._maxValue
998 def setHidden(self, value):
1001 def getMinSize(self):
1002 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
1004 def _getPixelPos(self):
1005 x0, y0, w, h = self.getSize()
1006 minSize = self.getMinSize()
1007 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
1013 w, h = self.getMinSize()
1014 pos = self._getPixelPos()
1017 glTranslatef(pos[0], pos[1], 0)
1018 glDisable(GL_TEXTURE_2D)
1020 glColor4ub(60,60,60,255)
1022 glColor4ub(60,60,60,192)
1024 glVertex2f( w/2,-h/2)
1025 glVertex2f(-w/2,-h/2)
1026 glVertex2f(-w/2, h/2)
1027 glVertex2f( w/2, h/2)
1029 scrollLength = h - w
1030 if self._maxValue-self._minValue != 0:
1031 valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1034 glTranslate(0.0,scrollLength/2,0)
1035 if True: # self._focus:
1036 glColor4ub(0,0,0,255)
1038 glTranslate(-w/2,openglHelpers.glGetStringSize(str(self._minValue))[1]/2,0)
1039 openglHelpers.glDrawStringRight(str(self._minValue))
1040 glTranslate(0,-scrollLength,0)
1041 openglHelpers.glDrawStringRight(str(self._maxValue))
1042 glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1043 openglHelpers.glDrawStringLeft(str(self.getValue()))
1045 glColor4ub(255,255,255,240)
1046 glTranslate(0.0,-scrollLength*valueNormalized,0)
1048 glVertex2f( w/2,-w/2)
1049 glVertex2f(-w/2,-w/2)
1050 glVertex2f(-w/2, w/2)
1051 glVertex2f( w/2, w/2)
1055 def _checkHit(self, x, y):
1058 pos = self._getPixelPos()
1059 w, h = self.getMinSize()
1060 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1063 self._base._focus = self
1066 def OnMouseMotion(self, x, y):
1068 w, h = self.getMinSize()
1069 scrollLength = h - w
1070 pos = self._getPixelPos()
1071 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1074 if self._checkHit(x, y):
1080 def OnMouseDown(self, x, y, button):
1081 if self._checkHit(x, y):
1083 self.OnMouseMotion(x, y)
1087 def OnMouseUp(self, x, y):
1089 self._base._focus = None