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 errStr = _("An error has occurred during the 3D view drawing.")
241 tb = traceback.extract_tb(sys.exc_info()[2])
242 errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
243 for n in xrange(len(tb)-1, -1, -1):
245 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
246 if not self._shownError:
247 traceback.print_exc()
248 wx.CallAfter(wx.MessageBox, errStr, _("3D window error"), wx.OK | wx.ICON_EXCLAMATION)
249 self._shownError = True
252 if self._glButtonsTexture is None:
253 self._glButtonsTexture = openglHelpers.loadGLTexture('glButtons.png')
254 self._glRobotTexture = openglHelpers.loadGLTexture('UltimakerRobot.png')
256 glDisable(GL_DEPTH_TEST)
258 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
259 glDisable(GL_LIGHTING)
260 glColor4ub(255,255,255,255)
262 glMatrixMode(GL_PROJECTION)
264 size = self.GetSize()
265 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
266 glMatrixMode(GL_MODELVIEW)
269 self._container.draw()
271 # glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
272 # glEnable(GL_TEXTURE_2D)
275 # glTranslate(size.GetWidth(),size.GetHeight(),0)
276 # s = self._buttonSize * 1
278 # glTranslate(-1.2,-0.2,0)
289 # glDisable(GL_TEXTURE_2D)
292 def _OnEraseBackground(self,event):
293 #Workaround for windows background redraw flicker.
297 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
298 self._container.updateLayout()
301 def OnMouseDown(self,e):
303 def OnMouseUp(self,e):
305 def OnMouseMotion(self, e):
307 def OnKeyChar(self, keyCode):
309 def OnPaint(self, e):
311 def OnKeyChar(self, keycode):
314 def QueueRefresh(self):
315 wx.CallAfter(self._queueRefresh)
317 def _queueRefresh(self):
319 wx.CallAfter(self.Refresh)
321 self._refreshQueued = True
324 if self._container is not None:
325 self._container.add(ctrl)
327 class glGuiLayoutButtons(object):
328 def __init__(self, parent):
329 self._parent = parent
330 self._parent._layout = self
333 bs = self._parent._base._buttonSize
334 x0, y0, w, h = self._parent.getSize()
336 for ctrl in self._parent._glGuiControlList:
339 x = w + pos[0] * gridSize - bs * 0.2
341 x = pos[0] * gridSize + bs * 0.2
343 y = h + pos[1] * gridSize * 1.2 - bs * 0.0
345 y = pos[1] * gridSize * 1.2 + bs * 0.2
346 ctrl.setSize(x, y, gridSize, gridSize)
348 def getLayoutSize(self):
349 _, _, w, h = self._parent.getSize()
352 class glGuiLayoutGrid(object):
353 def __init__(self, parent):
354 self._parent = parent
355 self._parent._layout = self
357 self._alignBottom = True
360 borderSize = self._parent._base._buttonSize * 0.2
361 x0, y0, w, h = self._parent.getSize()
366 for ctrl in self._parent._glGuiControlList:
368 w, h = ctrl.getMinSize()
372 widths[x] = max(widths[x], w)
376 heights[y] = max(heights[y], h)
377 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
378 if self._alignBottom:
379 y0 -= self._size[1] - self._parent.getSize()[3]
380 self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
381 for ctrl in self._parent._glGuiControlList:
385 for n in xrange(0, x):
389 for n in xrange(0, y):
393 ctrl.setSize(x1, y1, widths[x], heights[y])
395 def getLayoutSize(self):
398 class glButton(glGuiControl):
399 def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
400 self._buttonSize = size
402 super(glButton, self).__init__(parent, pos)
403 self._tooltip = tooltip
404 self._parent = parent
405 self._imageID = imageID
406 self._callback = callback
407 self._selected = False
409 self._disabled = False
410 self._showExpandArrow = False
411 self._progressBar = None
412 self._altTooltip = ''
414 def setSelected(self, value):
415 self._selected = value
417 def setExpandArrow(self, value):
418 self._showExpandArrow = value
420 def setHidden(self, value):
423 def setDisabled(self, value):
424 self._disabled = value
426 def setProgressBar(self, value):
427 self._progressBar = value
429 def getProgressBar(self):
430 return self._progressBar
432 def setBottomText(self, value):
433 self._altTooltip = value
435 def getSelected(self):
436 return self._selected
438 def getMinSize(self):
441 if self._buttonSize is not None:
442 return self._buttonSize, self._buttonSize
443 return self._base._buttonSize, self._base._buttonSize
445 def _getPixelPos(self):
446 x0, y0, w, h = self.getSize()
447 return x0 + w / 2, y0 + h / 2
453 cx = (self._imageID % 4) / 4
454 cy = int(self._imageID / 4) / 4
455 bs = self.getMinSize()[0]
456 pos = self._getPixelPos()
458 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
465 glColor4ub(128,128,128,128)
467 glColor4ub(255,255,255,255)
468 openglHelpers.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
469 openglHelpers.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
470 if self._showExpandArrow:
472 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)
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, 2)
476 glTranslatef(pos[0], pos[1], 0)
477 glDisable(GL_TEXTURE_2D)
479 glTranslatef(0, -0.55*bs*scale, 0)
482 glColor4ub(60,60,60,255)
483 glTranslatef(-1, -1, 0)
484 openglHelpers.glDrawStringCenter(self._tooltip)
485 glTranslatef(0, 2, 0)
486 openglHelpers.glDrawStringCenter(self._tooltip)
487 glTranslatef(2, 0, 0)
488 openglHelpers.glDrawStringCenter(self._tooltip)
489 glTranslatef(0, -2, 0)
490 openglHelpers.glDrawStringCenter(self._tooltip)
493 glColor4ub(255,255,255,255)
494 openglHelpers.glDrawStringCenter(self._tooltip)
496 progress = self._progressBar
497 if progress is not None:
498 glColor4ub(60,60,60,255)
499 openglHelpers.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
500 glColor4ub(255,255,255,255)
501 openglHelpers.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
502 elif len(self._altTooltip) > 0:
504 glTranslatef(pos[0], pos[1], 0)
505 glTranslatef(0, 0.6*bs, 0)
506 glTranslatef(0, 6, 0)
507 #glTranslatef(0.6*bs*scale, 0, 0)
509 for line in self._altTooltip.split('\n'):
511 glColor4ub(60,60,60,255)
512 glTranslatef(-1, -1, 0)
513 openglHelpers.glDrawStringCenter(line)
514 glTranslatef(0, 2, 0)
515 openglHelpers.glDrawStringCenter(line)
516 glTranslatef(2, 0, 0)
517 openglHelpers.glDrawStringCenter(line)
518 glTranslatef(0, -2, 0)
519 openglHelpers.glDrawStringCenter(line)
522 glColor4ub(255,255,255,255)
523 openglHelpers.glDrawStringCenter(line)
524 glTranslatef(0, 18, 0)
527 def _checkHit(self, x, y):
528 if self._hidden or self._disabled:
530 bs = self.getMinSize()[0]
531 pos = self._getPixelPos()
532 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
534 def OnMouseMotion(self, x, y):
535 if self._checkHit(x, y):
541 def OnMouseDown(self, x, y, button):
542 if self._checkHit(x, y):
543 self._callback(button)
547 class glRadioButton(glButton):
548 def __init__(self, parent, imageID, tooltip, pos, group, callback):
549 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
551 self._radioCallback = callback
552 self._group.append(self)
554 def setSelected(self, value):
555 self._selected = value
557 def _onRadioSelect(self, button):
558 self._base._focus = None
559 for ctrl in self._group:
561 ctrl.setSelected(False)
562 if self.getSelected():
563 self.setSelected(False)
565 self.setSelected(True)
566 self._radioCallback(button)
568 class glComboButton(glButton):
569 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
570 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
571 self._imageIDs = imageIDs
572 self._tooltips = tooltips
573 self._comboCallback = callback
576 def _onComboOpenSelect(self, button):
578 self._base._focus = None
580 self._base._focus = self
585 self._selected = self.hasFocus()
586 super(glComboButton, self).draw()
588 bs = self._base._buttonSize / 2
589 pos = self._getPixelPos()
591 if not self._selected:
595 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
596 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
597 for n in xrange(0, len(self._imageIDs)):
598 glTranslatef(0, bs, 0)
599 glColor4ub(255,255,255,255)
600 openglHelpers.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
601 openglHelpers.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
602 glDisable(GL_TEXTURE_2D)
605 glTranslatef(-0.55*bs, 0.1*bs, 0)
608 glColor4ub(60,60,60,255)
609 glTranslatef(-1, -1, 0)
610 openglHelpers.glDrawStringRight(self._tooltips[n])
611 glTranslatef(0, 2, 0)
612 openglHelpers.glDrawStringRight(self._tooltips[n])
613 glTranslatef(2, 0, 0)
614 openglHelpers.glDrawStringRight(self._tooltips[n])
615 glTranslatef(0, -2, 0)
616 openglHelpers.glDrawStringRight(self._tooltips[n])
619 glColor4ub(255,255,255,255)
620 openglHelpers.glDrawStringRight(self._tooltips[n])
625 return self._selection
627 def setValue(self, value):
628 self._selection = value
629 self._imageID = self._imageIDs[self._selection]
630 self._comboCallback()
632 def OnMouseDown(self, x, y, button):
633 if self._hidden or self._disabled:
636 bs = self._base._buttonSize / 2
637 pos = self._getPixelPos()
638 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
639 self._selection = int((y - pos[1] - bs) / bs)
640 self._imageID = self._imageIDs[self._selection]
641 self._base._focus = None
642 self._comboCallback()
644 return super(glComboButton, self).OnMouseDown(x, y, button)
646 class glFrame(glGuiContainer):
647 def __init__(self, parent, pos):
648 super(glFrame, self).__init__(parent, pos)
649 self._selected = False
653 def setSelected(self, value):
654 self._selected = value
656 def setHidden(self, value):
658 for child in self._glGuiControlList:
659 if self._base._focus == child:
660 self._base._focus = None
662 def getSelected(self):
663 return self._selected
665 def getMinSize(self):
666 return self._base._buttonSize, self._base._buttonSize
668 def _getPixelPos(self):
669 x0, y0, w, h = self.getSize()
676 bs = self._parent._buttonSize
677 pos = self._getPixelPos()
679 size = self._layout.getLayoutSize()
680 glColor4ub(255,255,255,255)
681 openglHelpers.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
682 #Draw the controls on the frame
683 super(glFrame, self).draw()
685 def _checkHit(self, x, y):
688 pos = self._getPixelPos()
689 w, h = self._layout.getLayoutSize()
690 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
692 def OnMouseMotion(self, x, y):
693 super(glFrame, self).OnMouseMotion(x, y)
694 if self._checkHit(x, y):
700 def OnMouseDown(self, x, y, button):
701 if self._checkHit(x, y):
702 super(glFrame, self).OnMouseDown(x, y, button)
706 class glNotification(glFrame):
707 def __init__(self, parent, pos):
709 super(glNotification, self).__init__(parent, pos)
710 glGuiLayoutGrid(self)._alignBottom = False
711 self._label = glLabel(self, "Notification", (0, 0))
712 self._buttonExtra = glButton(self, 31, "???", (1, 0), self.onExtraButton, 25)
713 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
714 self._padding = glLabel(self, "", (0, 1))
717 def setSize(self, x, y, w, h):
718 w, h = self._layout.getLayoutSize()
719 baseSize = self._base.GetSizeTuple()
720 if self._anim is not None:
721 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
723 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
726 self.setSize(0,0,0,0)
728 super(glNotification, self).draw()
730 def message(self, text, extraButtonCallback = None, extraButtonIcon = None, extraButtonTooltip = None):
731 self._anim = animation(self._base, -20, 25, 1)
732 self.setHidden(False)
733 self._label.setLabel(text)
734 self._buttonExtra.setHidden(extraButtonCallback is None)
735 self._buttonExtra._imageID = extraButtonIcon
736 self._buttonExtra._tooltip = extraButtonTooltip
737 self._extraButtonCallback = extraButtonCallback
738 self._base._queueRefresh()
741 def onExtraButton(self, button):
743 self._extraButtonCallback()
745 def onClose(self, button):
746 if self._anim is not None:
747 self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
749 self._anim = animation(self._base, 25, -20, 1)
751 class glLabel(glGuiControl):
752 def __init__(self, parent, label, pos):
754 super(glLabel, self).__init__(parent, pos)
756 def setLabel(self, label):
759 def getMinSize(self):
760 w, h = openglHelpers.glGetStringSize(self._label)
763 def _getPixelPos(self):
764 x0, y0, w, h = self.getSize()
768 x, y, w, h = self.getSize()
771 glTranslatef(x, y, 0)
773 # glColor4ub(255,255,255,128)
785 glTranslate(5, h - 5, 0)
786 glColor4ub(255,255,255,255)
787 openglHelpers.glDrawStringLeft(self._label)
790 def _checkHit(self, x, y):
793 def OnMouseMotion(self, x, y):
796 def OnMouseDown(self, x, y, button):
799 class glNumberCtrl(glGuiControl):
800 def __init__(self, parent, value, pos, callback):
801 self._callback = callback
802 self._value = str(value)
805 self._inCallback = False
806 super(glNumberCtrl, self).__init__(parent, pos)
808 def setValue(self, value):
811 self._value = str(value)
813 def getMinSize(self):
814 w, h = openglHelpers.glGetStringSize("VALUES")
817 def _getPixelPos(self):
818 x0, y0, w, h = self.getSize()
822 x, y, w, h = self.getSize()
825 glTranslatef(x, y, 0)
828 glColor4ub(255,255,255,255)
830 glColor4ub(255,255,255,192)
842 glTranslate(5, h - 5, 0)
843 glColor4ub(0,0,0,255)
844 openglHelpers.glDrawStringLeft(self._value)
846 glTranslate(openglHelpers.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
847 openglHelpers.glDrawStringLeft('|')
850 def _checkHit(self, x, y):
851 x1, y1, w, h = self.getSize()
852 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
854 def OnMouseMotion(self, x, y):
857 def OnMouseDown(self, x, y, button):
858 if self._checkHit(x, y):
863 def OnKeyChar(self, c):
864 self._inCallback = True
867 self._selectPos = max(0, self._selectPos)
868 if c == wx.WXK_RIGHT:
870 self._selectPos = min(self._selectPos, len(self._value))
873 value = float(self._value)
878 self._value = str(value)
879 self._callback(self._value)
882 value = float(self._value)
888 self._value = str(value)
889 self._callback(self._value)
890 if c == wx.WXK_BACK and self._selectPos > 0:
891 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
893 self._callback(self._value)
894 if c == wx.WXK_DELETE:
895 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
896 self._callback(self._value)
897 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
898 if wx.GetKeyState(wx.WXK_SHIFT):
902 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
903 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
905 self._callback(self._value)
906 self._inCallback = False
909 self._base._focus = self
910 self._selectPos = len(self._value)
913 class glCheckbox(glGuiControl):
914 def __init__(self, parent, value, pos, callback):
915 self._callback = callback
919 self._inCallback = False
920 super(glCheckbox, self).__init__(parent, pos)
922 def setValue(self, value):
925 self._value = str(value)
930 def getMinSize(self):
933 def _getPixelPos(self):
934 x0, y0, w, h = self.getSize()
938 x, y, w, h = self.getSize()
941 glTranslatef(x, y, 0)
943 glColor3ub(255,255,255)
945 openglHelpers.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
947 openglHelpers.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
951 def _checkHit(self, x, y):
952 x1, y1, w, h = self.getSize()
953 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
955 def OnMouseMotion(self, x, y):
958 def OnMouseDown(self, x, y, button):
959 if self._checkHit(x, y):
960 self._value = not self._value
964 class glSlider(glGuiControl):
965 def __init__(self, parent, value, minValue, maxValue, pos, callback):
966 super(glSlider, self).__init__(parent, pos)
967 self._callback = callback
971 self._minValue = minValue
972 self._maxValue = maxValue
974 def setValue(self, value):
978 if self._value < self._minValue:
979 return self._minValue
980 if self._value > self._maxValue:
981 return self._maxValue
984 def setRange(self, minValue, maxValue):
985 if maxValue < minValue:
987 self._minValue = minValue
988 self._maxValue = maxValue
990 def getMinValue(self):
991 return self._minValue
993 def getMaxValue(self):
994 return self._maxValue
996 def setHidden(self, value):
999 def getMinSize(self):
1000 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
1002 def _getPixelPos(self):
1003 x0, y0, w, h = self.getSize()
1004 minSize = self.getMinSize()
1005 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
1011 w, h = self.getMinSize()
1012 pos = self._getPixelPos()
1015 glTranslatef(pos[0], pos[1], 0)
1016 glDisable(GL_TEXTURE_2D)
1018 glColor4ub(60,60,60,255)
1020 glColor4ub(60,60,60,192)
1022 glVertex2f( w/2,-h/2)
1023 glVertex2f(-w/2,-h/2)
1024 glVertex2f(-w/2, h/2)
1025 glVertex2f( w/2, h/2)
1027 scrollLength = h - w
1028 if self._maxValue-self._minValue != 0:
1029 valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1032 glTranslate(0.0,scrollLength/2,0)
1033 if True: # self._focus:
1034 glColor4ub(0,0,0,255)
1036 glTranslate(-w/2,openglHelpers.glGetStringSize(str(self._minValue))[1]/2,0)
1037 openglHelpers.glDrawStringRight(str(self._minValue))
1038 glTranslate(0,-scrollLength,0)
1039 openglHelpers.glDrawStringRight(str(self._maxValue))
1040 glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1041 openglHelpers.glDrawStringLeft(str(self.getValue()))
1043 glColor4ub(255,255,255,240)
1044 glTranslate(0.0,-scrollLength*valueNormalized,0)
1046 glVertex2f( w/2,-w/2)
1047 glVertex2f(-w/2,-w/2)
1048 glVertex2f(-w/2, w/2)
1049 glVertex2f( w/2, w/2)
1053 def _checkHit(self, x, y):
1056 pos = self._getPixelPos()
1057 w, h = self.getMinSize()
1058 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1061 self._base._focus = self
1064 def OnMouseMotion(self, x, y):
1066 w, h = self.getMinSize()
1067 scrollLength = h - w
1068 pos = self._getPixelPos()
1069 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1072 if self._checkHit(x, y):
1078 def OnMouseDown(self, x, y, button):
1079 if self._checkHit(x, y):
1081 self.OnMouseMotion(x, y)
1085 def OnMouseUp(self, x, y):
1087 self._base._focus = None