1 from __future__ import absolute_import
2 from __future__ import division
3 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 from wx import glcanvas
13 OpenGL.ERROR_CHECKING = False
14 from OpenGL.GL import *
16 from Cura.util import version
17 from Cura.gui.util import opengl
19 class animation(object):
20 def __init__(self, gui, start, end, runTime):
23 self._startTime = time.time()
24 self._runTime = runTime
25 gui._animationList.append(self)
28 return time.time() > self._startTime + self._runTime
30 def getPosition(self):
33 f = (time.time() - self._startTime) / self._runTime
36 #f = 6*tc*ts + -15*ts*ts + 10*tc
38 return self._start + (self._end - self._start) * f
40 class glGuiControl(object):
41 def __init__(self, parent, pos):
43 self._base = parent._base
45 self._size = (0,0, 1, 1)
46 self._parent.add(self)
48 def setSize(self, x, y, w, h):
49 self._size = (x, y, w, h)
57 def updateLayout(self):
61 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
62 if self._parent._glGuiControlList[n].setFocus():
64 for n in xrange(0, self._parent._glGuiControlList.index(self)):
65 if self._parent._glGuiControlList[n].setFocus():
68 def focusPrevious(self):
69 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
70 if self._parent._glGuiControlList[n].setFocus():
72 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
73 if self._parent._glGuiControlList[n].setFocus():
80 return self._base._focus == self
82 def OnMouseUp(self, x, y):
85 def OnKeyChar(self, key):
88 class glGuiContainer(glGuiControl):
89 def __init__(self, parent, pos):
90 self._glGuiControlList = []
91 glGuiLayoutButtons(self)
92 super(glGuiContainer, self).__init__(parent, pos)
95 self._glGuiControlList.append(ctrl)
98 def OnMouseDown(self, x, y, button):
99 for ctrl in self._glGuiControlList:
100 if ctrl.OnMouseDown(x, y, button):
104 def OnMouseUp(self, x, y):
105 for ctrl in self._glGuiControlList:
106 if ctrl.OnMouseUp(x, y):
110 def OnMouseMotion(self, x, y):
112 for ctrl in self._glGuiControlList:
113 if ctrl.OnMouseMotion(x, y):
118 for ctrl in self._glGuiControlList:
121 def updateLayout(self):
122 self._layout.update()
123 for ctrl in self._glGuiControlList:
126 class glGuiPanel(glcanvas.GLCanvas):
127 def __init__(self, parent):
128 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8, 0)
129 glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList)
132 self._container = None
133 self._container = glGuiContainer(self, (0,0))
134 self._shownError = False
136 self._context = glcanvas.GLContext(self)
137 self._glButtonsTexture = None
138 self._glRobotTexture = None
139 self._buttonSize = 64
141 self._animationList = []
142 self.glReleaseList = []
143 self._refreshQueued = 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 if len(self._animationList) > 0 or self._refreshQueued:
164 self._refreshQueued = False
165 for anim in self._animationList:
167 self._animationList.remove(anim)
170 def _OnGuiKeyChar(self, e):
171 if self._focus is not None:
172 self._focus.OnKeyChar(e.GetKeyCode())
175 self.OnKeyChar(e.GetKeyCode())
177 def OnFocusLost(self, e):
181 def _OnGuiMouseDown(self,e):
183 if self._container.OnMouseDown(e.GetX(), e.GetY(), e.GetButton()):
188 def _OnGuiMouseUp(self, e):
189 if self._container.OnMouseUp(e.GetX(), e.GetY()):
194 def _OnGuiMouseMotion(self,e):
196 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
197 self.OnMouseMotion(e)
199 def _OnGuiPaint(self, e):
200 h = self.GetSize().GetHeight()
201 w = self.GetSize().GetWidth()
202 oldButtonSize = self._buttonSize
206 self._buttonSize = 32
208 self._buttonSize = 48
210 self._buttonSize = 64
212 self._buttonSize = 80
214 self._buttonSize = 96
215 if self._buttonSize != oldButtonSize:
216 self._container.updateLayout()
218 dc = wx.PaintDC(self)
220 self.SetCurrent(self._context)
221 for obj in self.glReleaseList:
223 del self.glReleaseList[:]
224 renderStartTime = time.time()
228 if version.isDevVersion():
229 renderTime = time.time() - renderStartTime
231 glTranslate(10, self.GetSize().GetHeight() - 30, -1)
232 glColor4f(0.2,0.2,0.2,0.5)
233 opengl.glDrawStringLeft("fps:%d" % (1 / renderTime))
236 errStr = 'An error has occurred during the 3D view drawing.'
237 tb = traceback.extract_tb(sys.exc_info()[2])
238 errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
239 for n in xrange(len(tb)-1, -1, -1):
241 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
242 if not self._shownError:
243 wx.CallAfter(wx.MessageBox, errStr, '3D window error', wx.OK | wx.ICON_EXCLAMATION)
244 self._shownError = True
247 if self._glButtonsTexture is None:
248 self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
249 self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
251 glDisable(GL_DEPTH_TEST)
253 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
254 glDisable(GL_LIGHTING)
255 glColor4ub(255,255,255,255)
257 glMatrixMode(GL_PROJECTION)
259 size = self.GetSize()
260 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
261 glMatrixMode(GL_MODELVIEW)
264 self._container.draw()
266 glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
267 glEnable(GL_TEXTURE_2D)
270 glTranslate(size.GetWidth() - 8,size.GetHeight() - 32,0)
271 s = self._buttonSize * 1.4
283 glDisable(GL_TEXTURE_2D)
286 def _OnEraseBackground(self,event):
287 #Workaround for windows background redraw flicker.
291 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
292 self._container.updateLayout()
295 def OnMouseDown(self,e):
297 def OnMouseUp(self,e):
299 def OnMouseMotion(self, e):
301 def OnKeyChar(self, keyCode):
303 def OnPaint(self, e):
305 def OnKeyChar(self, keycode):
308 def QueueRefresh(self):
309 wx.CallAfter(self._queueRefresh)
311 def _queueRefresh(self):
312 self._refreshQueued = True
315 if self._container is not None:
316 self._container.add(ctrl)
318 class glGuiLayoutButtons(object):
319 def __init__(self, parent):
320 self._parent = parent
321 self._parent._layout = self
324 bs = self._parent._base._buttonSize
325 x0, y0, w, h = self._parent.getSize()
327 for ctrl in self._parent._glGuiControlList:
330 x = w + pos[0] * gridSize - bs * 0.2
332 x = pos[0] * gridSize + bs * 0.2
334 y = h + pos[1] * gridSize * 1.2 - bs * 0.2
336 y = pos[1] * gridSize * 1.2 + bs * 0.2
337 ctrl.setSize(x, y, gridSize, gridSize)
339 def getLayoutSize(self):
340 _, _, w, h = self._parent.getSize()
343 class glGuiLayoutGrid(object):
344 def __init__(self, parent):
345 self._parent = parent
346 self._parent._layout = self
348 self._alignBottom = True
351 borderSize = self._parent._base._buttonSize * 0.2
352 x0, y0, w, h = self._parent.getSize()
357 for ctrl in self._parent._glGuiControlList:
359 w, h = ctrl.getMinSize()
363 widths[x] = max(widths[x], w)
367 heights[y] = max(heights[y], h)
368 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
369 if self._alignBottom:
370 y0 -= self._size[1] - self._parent.getSize()[3]
371 self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
372 for ctrl in self._parent._glGuiControlList:
376 for n in xrange(0, x):
380 for n in xrange(0, y):
384 ctrl.setSize(x1, y1, widths[x], heights[y])
386 def getLayoutSize(self):
389 class glButton(glGuiControl):
390 def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
391 self._buttonSize = size
393 super(glButton, self).__init__(parent, pos)
394 self._tooltip = tooltip
395 self._parent = parent
396 self._imageID = imageID
397 self._callback = callback
398 self._selected = False
400 self._disabled = False
401 self._showExpandArrow = False
402 self._progressBar = None
403 self._altTooltip = ''
405 def setSelected(self, value):
406 self._selected = value
408 def setExpandArrow(self, value):
409 self._showExpandArrow = value
411 def setHidden(self, value):
414 def setDisabled(self, value):
415 self._disabled = value
417 def setProgressBar(self, value):
418 self._progressBar = value
420 def getProgressBar(self):
421 return self._progressBar
423 def setBottomText(self, value):
424 self._altTooltip = value
426 def getSelected(self):
427 return self._selected
429 def getMinSize(self):
432 if self._buttonSize is not None:
433 return self._buttonSize, self._buttonSize
434 return self._base._buttonSize, self._base._buttonSize
436 def _getPixelPos(self):
437 x0, y0, w, h = self.getSize()
438 return x0 + w / 2, y0 + h / 2
444 cx = (self._imageID % 4) / 4
445 cy = int(self._imageID / 4) / 4
446 bs = self.getMinSize()[0]
447 pos = self._getPixelPos()
449 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
456 glColor4ub(128,128,128,128)
458 glColor4ub(255,255,255,255)
459 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
460 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
461 if self._showExpandArrow:
463 opengl.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)
465 opengl.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)
467 glTranslatef(pos[0], pos[1], 0)
468 glDisable(GL_TEXTURE_2D)
470 glTranslatef(0, -0.55*bs*scale, 0)
473 glColor4ub(60,60,60,255)
474 glTranslatef(-1, -1, 0)
475 opengl.glDrawStringCenter(self._tooltip)
476 glTranslatef(0, 2, 0)
477 opengl.glDrawStringCenter(self._tooltip)
478 glTranslatef(2, 0, 0)
479 opengl.glDrawStringCenter(self._tooltip)
480 glTranslatef(0, -2, 0)
481 opengl.glDrawStringCenter(self._tooltip)
484 glColor4ub(255,255,255,255)
485 opengl.glDrawStringCenter(self._tooltip)
487 progress = self._progressBar
488 if progress is not None:
489 glColor4ub(60,60,60,255)
490 opengl.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
491 glColor4ub(255,255,255,255)
492 opengl.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
493 elif len(self._altTooltip) > 0:
495 glTranslatef(pos[0], pos[1], 0)
496 glTranslatef(0.6*bs*scale, 0, 0)
499 glColor4ub(60,60,60,255)
500 glTranslatef(-1, -1, 0)
501 opengl.glDrawStringLeft(self._altTooltip)
502 glTranslatef(0, 2, 0)
503 opengl.glDrawStringLeft(self._altTooltip)
504 glTranslatef(2, 0, 0)
505 opengl.glDrawStringLeft(self._altTooltip)
506 glTranslatef(0, -2, 0)
507 opengl.glDrawStringLeft(self._altTooltip)
510 glColor4ub(255,255,255,255)
511 opengl.glDrawStringLeft(self._altTooltip)
514 def _checkHit(self, x, y):
515 if self._hidden or self._disabled:
517 bs = self.getMinSize()[0]
518 pos = self._getPixelPos()
519 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
521 def OnMouseMotion(self, x, y):
522 if self._checkHit(x, y):
528 def OnMouseDown(self, x, y, button):
529 if self._checkHit(x, y):
530 self._callback(button)
534 class glRadioButton(glButton):
535 def __init__(self, parent, imageID, tooltip, pos, group, callback):
536 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
538 self._radioCallback = callback
539 self._group.append(self)
541 def setSelected(self, value):
542 self._selected = value
544 def _onRadioSelect(self, button):
545 self._base._focus = None
546 for ctrl in self._group:
548 ctrl.setSelected(False)
549 if self.getSelected():
550 self.setSelected(False)
552 self.setSelected(True)
553 self._radioCallback(button)
555 class glComboButton(glButton):
556 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
557 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
558 self._imageIDs = imageIDs
559 self._tooltips = tooltips
560 self._comboCallback = callback
563 def _onComboOpenSelect(self, button):
565 self._base._focus = None
567 self._base._focus = self
572 self._selected = self.hasFocus()
573 super(glComboButton, self).draw()
575 bs = self._base._buttonSize / 2
576 pos = self._getPixelPos()
578 if not self._selected:
582 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
583 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
584 for n in xrange(0, len(self._imageIDs)):
585 glTranslatef(0, bs, 0)
586 glColor4ub(255,255,255,255)
587 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
588 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
589 glDisable(GL_TEXTURE_2D)
592 glTranslatef(-0.55*bs, 0.1*bs, 0)
595 glColor4ub(60,60,60,255)
596 glTranslatef(-1, -1, 0)
597 opengl.glDrawStringRight(self._tooltips[n])
598 glTranslatef(0, 2, 0)
599 opengl.glDrawStringRight(self._tooltips[n])
600 glTranslatef(2, 0, 0)
601 opengl.glDrawStringRight(self._tooltips[n])
602 glTranslatef(0, -2, 0)
603 opengl.glDrawStringRight(self._tooltips[n])
606 glColor4ub(255,255,255,255)
607 opengl.glDrawStringRight(self._tooltips[n])
612 return self._selection
614 def setValue(self, value):
615 self._selection = value
616 self._imageID = self._imageIDs[self._selection]
617 self._comboCallback()
619 def OnMouseDown(self, x, y, button):
620 if self._hidden or self._disabled:
623 bs = self._base._buttonSize / 2
624 pos = self._getPixelPos()
625 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
626 self._selection = int((y - pos[1] - bs) / bs)
627 self._imageID = self._imageIDs[self._selection]
628 self._base._focus = None
629 self._comboCallback()
631 return super(glComboButton, self).OnMouseDown(x, y, button)
633 class glFrame(glGuiContainer):
634 def __init__(self, parent, pos):
635 super(glFrame, self).__init__(parent, pos)
636 self._selected = False
640 def setSelected(self, value):
641 self._selected = value
643 def setHidden(self, value):
645 for child in self._glGuiControlList:
646 if self._base._focus == child:
647 self._base._focus = None
649 def getSelected(self):
650 return self._selected
652 def getMinSize(self):
653 return self._base._buttonSize, self._base._buttonSize
655 def _getPixelPos(self):
656 x0, y0, w, h = self.getSize()
663 bs = self._parent._buttonSize
664 pos = self._getPixelPos()
666 size = self._layout.getLayoutSize()
667 glColor4ub(255,255,255,255)
668 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
669 #Draw the controls on the frame
670 super(glFrame, self).draw()
672 def _checkHit(self, x, y):
675 pos = self._getPixelPos()
676 w, h = self._layout.getLayoutSize()
677 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
679 def OnMouseMotion(self, x, y):
680 super(glFrame, self).OnMouseMotion(x, y)
681 if self._checkHit(x, y):
687 def OnMouseDown(self, x, y, button):
688 if self._checkHit(x, y):
689 super(glFrame, self).OnMouseDown(x, y, button)
693 class glNotification(glFrame):
694 def __init__(self, parent, pos):
696 super(glNotification, self).__init__(parent, pos)
697 glGuiLayoutGrid(self)._alignBottom = False
698 self._label = glLabel(self, "Notification", (0, 0))
699 self._buttonEject = glButton(self, 31, "Eject", (1, 0), self.onEject, 25)
700 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
701 self._padding = glLabel(self, "", (0, 1))
704 def setSize(self, x, y, w, h):
705 w, h = self._layout.getLayoutSize()
706 baseSize = self._base.GetSizeTuple()
707 if self._anim is not None:
708 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
710 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
713 self.setSize(0,0,0,0)
715 super(glNotification, self).draw()
717 def message(self, text, ejectCallback = None):
718 self._anim = animation(self._base, -20, 25, 1)
719 self.setHidden(False)
720 self._label.setLabel(text)
721 self._buttonEject.setHidden(ejectCallback is None)
722 self._ejectCallback = ejectCallback
723 self._base._queueRefresh()
726 def onEject(self, button):
728 self._ejectCallback()
730 def onClose(self, button):
731 if self._anim is not None:
732 self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
734 self._anim = animation(self._base, 25, -20, 1)
736 class glLabel(glGuiControl):
737 def __init__(self, parent, label, pos):
739 super(glLabel, self).__init__(parent, pos)
741 def setLabel(self, label):
744 def getMinSize(self):
745 w, h = opengl.glGetStringSize(self._label)
748 def _getPixelPos(self):
749 x0, y0, w, h = self.getSize()
753 x, y, w, h = self.getSize()
756 glTranslatef(x, y, 0)
758 # glColor4ub(255,255,255,128)
770 glTranslate(5, h - 5, 0)
771 glColor4ub(255,255,255,255)
772 opengl.glDrawStringLeft(self._label)
775 def _checkHit(self, x, y):
778 def OnMouseMotion(self, x, y):
781 def OnMouseDown(self, x, y, button):
784 class glNumberCtrl(glGuiControl):
785 def __init__(self, parent, value, pos, callback):
786 self._callback = callback
787 self._value = str(value)
790 self._inCallback = False
791 super(glNumberCtrl, self).__init__(parent, pos)
793 def setValue(self, value):
796 self._value = str(value)
798 def getMinSize(self):
799 w, h = opengl.glGetStringSize("VALUES")
802 def _getPixelPos(self):
803 x0, y0, w, h = self.getSize()
807 x, y, w, h = self.getSize()
810 glTranslatef(x, y, 0)
813 glColor4ub(255,255,255,255)
815 glColor4ub(255,255,255,192)
827 glTranslate(5, h - 5, 0)
828 glColor4ub(0,0,0,255)
829 opengl.glDrawStringLeft(self._value)
831 glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
832 opengl.glDrawStringLeft('|')
835 def _checkHit(self, x, y):
836 x1, y1, w, h = self.getSize()
837 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
839 def OnMouseMotion(self, x, y):
842 def OnMouseDown(self, x, y, button):
843 if self._checkHit(x, y):
848 def OnKeyChar(self, c):
849 self._inCallback = True
852 self._selectPos = max(0, self._selectPos)
853 if c == wx.WXK_RIGHT:
855 self._selectPos = min(self._selectPos, len(self._value))
858 value = float(self._value)
863 self._value = str(value)
864 self._callback(self._value)
867 value = float(self._value)
873 self._value = str(value)
874 self._callback(self._value)
875 if c == wx.WXK_BACK and self._selectPos > 0:
876 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
878 self._callback(self._value)
879 if c == wx.WXK_DELETE:
880 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
881 self._callback(self._value)
882 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
883 if wx.GetKeyState(wx.WXK_SHIFT):
887 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
888 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
890 self._callback(self._value)
891 self._inCallback = False
894 self._base._focus = self
895 self._selectPos = len(self._value)
898 class glCheckbox(glGuiControl):
899 def __init__(self, parent, value, pos, callback):
900 self._callback = callback
904 self._inCallback = False
905 super(glCheckbox, self).__init__(parent, pos)
907 def setValue(self, value):
910 self._value = str(value)
915 def getMinSize(self):
918 def _getPixelPos(self):
919 x0, y0, w, h = self.getSize()
923 x, y, w, h = self.getSize()
926 glTranslatef(x, y, 0)
928 glColor3ub(255,255,255)
930 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
932 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
936 def _checkHit(self, x, y):
937 x1, y1, w, h = self.getSize()
938 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
940 def OnMouseMotion(self, x, y):
943 def OnMouseDown(self, x, y, button):
944 if self._checkHit(x, y):
945 self._value = not self._value
949 class glSlider(glGuiControl):
950 def __init__(self, parent, value, minValue, maxValue, pos, callback):
951 super(glSlider, self).__init__(parent, pos)
952 self._callback = callback
956 self._minValue = minValue
957 self._maxValue = maxValue
959 def setValue(self, value):
963 if self._value < self._minValue:
964 return self._minValue
965 if self._value > self._maxValue:
966 return self._maxValue
969 def setRange(self, minValue, maxValue):
970 if maxValue < minValue:
972 self._minValue = minValue
973 self._maxValue = maxValue
975 def getMinValue(self):
976 return self._minValue
978 def getMaxValue(self):
979 return self._maxValue
981 def setHidden(self, value):
984 def getMinSize(self):
985 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
987 def _getPixelPos(self):
988 x0, y0, w, h = self.getSize()
989 minSize = self.getMinSize()
990 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
996 w, h = self.getMinSize()
997 pos = self._getPixelPos()
1000 glTranslatef(pos[0], pos[1], 0)
1001 glDisable(GL_TEXTURE_2D)
1003 glColor4ub(60,60,60,255)
1005 glColor4ub(60,60,60,192)
1007 glVertex2f( w/2,-h/2)
1008 glVertex2f(-w/2,-h/2)
1009 glVertex2f(-w/2, h/2)
1010 glVertex2f( w/2, h/2)
1012 scrollLength = h - w
1013 if self._maxValue-self._minValue != 0:
1014 valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1017 glTranslate(0.0,scrollLength/2,0)
1019 glColor4ub(0,0,0,255)
1021 glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1022 opengl.glDrawStringRight(str(self._minValue))
1023 glTranslate(0,-scrollLength,0)
1024 opengl.glDrawStringRight(str(self._maxValue))
1025 glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1026 opengl.glDrawStringLeft(str(self.getValue()))
1028 glColor4ub(255,255,255,240)
1029 glTranslate(0.0,-scrollLength*valueNormalized,0)
1031 glVertex2f( w/2,-w/2)
1032 glVertex2f(-w/2,-w/2)
1033 glVertex2f(-w/2, w/2)
1034 glVertex2f( w/2, w/2)
1038 def _checkHit(self, x, y):
1041 pos = self._getPixelPos()
1042 w, h = self.getMinSize()
1043 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1046 self._base._focus = self
1049 def OnMouseMotion(self, x, y):
1051 w, h = self.getMinSize()
1052 scrollLength = h - w
1053 pos = self._getPixelPos()
1054 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1057 if self._checkHit(x, y):
1063 def OnMouseDown(self, x, y, button):
1064 if self._checkHit(x, y):
1066 self.OnMouseMotion(x, y)
1070 def OnMouseUp(self, x, y):
1072 self._base._focus = None