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
144 self._idleCalled = False
146 wx.EVT_PAINT(self, self._OnGuiPaint)
147 wx.EVT_SIZE(self, self._OnSize)
148 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
149 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseDown)
150 wx.EVT_LEFT_DCLICK(self, self._OnGuiMouseDown)
151 wx.EVT_LEFT_UP(self, self._OnGuiMouseUp)
152 wx.EVT_RIGHT_DOWN(self, self._OnGuiMouseDown)
153 wx.EVT_RIGHT_DCLICK(self, self._OnGuiMouseDown)
154 wx.EVT_RIGHT_UP(self, self._OnGuiMouseUp)
155 wx.EVT_MIDDLE_DOWN(self, self._OnGuiMouseDown)
156 wx.EVT_MIDDLE_DCLICK(self, self._OnGuiMouseDown)
157 wx.EVT_MIDDLE_UP(self, self._OnGuiMouseUp)
158 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
159 wx.EVT_CHAR(self, self._OnGuiKeyChar)
160 wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
161 wx.EVT_IDLE(self, self._OnIdle)
163 def _OnIdle(self, e):
164 self._idleCalled = True
165 if len(self._animationList) > 0 or self._refreshQueued:
166 self._refreshQueued = False
167 for anim in self._animationList:
169 self._animationList.remove(anim)
172 def _OnGuiKeyChar(self, e):
173 if self._focus is not None:
174 self._focus.OnKeyChar(e.GetKeyCode())
177 self.OnKeyChar(e.GetKeyCode())
179 def OnFocusLost(self, e):
183 def _OnGuiMouseDown(self,e):
185 if self._container.OnMouseDown(e.GetX(), e.GetY(), e.GetButton()):
190 def _OnGuiMouseUp(self, e):
191 if self._container.OnMouseUp(e.GetX(), e.GetY()):
196 def _OnGuiMouseMotion(self,e):
198 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
199 self.OnMouseMotion(e)
201 def _OnGuiPaint(self, e):
202 self._idleCalled = False
203 h = self.GetSize().GetHeight()
204 w = self.GetSize().GetWidth()
205 oldButtonSize = self._buttonSize
209 self._buttonSize = 32
211 self._buttonSize = 48
213 self._buttonSize = 64
215 self._buttonSize = 80
217 self._buttonSize = 96
218 if self._buttonSize != oldButtonSize:
219 self._container.updateLayout()
221 dc = wx.PaintDC(self)
223 self.SetCurrent(self._context)
224 for obj in self.glReleaseList:
226 del self.glReleaseList[:]
227 renderStartTime = time.time()
231 if version.isDevVersion():
232 renderTime = time.time() - renderStartTime
234 glTranslate(10, self.GetSize().GetHeight() - 30, -1)
235 glColor4f(0.2,0.2,0.2,0.5)
236 opengl.glDrawStringLeft("fps:%d" % (1 / renderTime))
239 errStr = 'An error has occurred during the 3D view drawing.'
240 tb = traceback.extract_tb(sys.exc_info()[2])
241 errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
242 for n in xrange(len(tb)-1, -1, -1):
244 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
245 if not self._shownError:
246 wx.CallAfter(wx.MessageBox, errStr, '3D window error', wx.OK | wx.ICON_EXCLAMATION)
247 self._shownError = True
250 if self._glButtonsTexture is None:
251 self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
252 self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
254 glDisable(GL_DEPTH_TEST)
256 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
257 glDisable(GL_LIGHTING)
258 glColor4ub(255,255,255,255)
260 glMatrixMode(GL_PROJECTION)
262 size = self.GetSize()
263 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
264 glMatrixMode(GL_MODELVIEW)
267 self._container.draw()
269 # glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
270 # glEnable(GL_TEXTURE_2D)
273 # glTranslate(size.GetWidth(),size.GetHeight(),0)
274 # s = self._buttonSize * 1
276 # glTranslate(-1.2,-0.2,0)
287 # glDisable(GL_TEXTURE_2D)
290 def _OnEraseBackground(self,event):
291 #Workaround for windows background redraw flicker.
295 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
296 self._container.updateLayout()
299 def OnMouseDown(self,e):
301 def OnMouseUp(self,e):
303 def OnMouseMotion(self, e):
305 def OnKeyChar(self, keyCode):
307 def OnPaint(self, e):
309 def OnKeyChar(self, keycode):
312 def QueueRefresh(self):
313 wx.CallAfter(self._queueRefresh)
315 def _queueRefresh(self):
317 wx.CallAfter(self.Refresh)
319 self._refreshQueued = True
322 if self._container is not None:
323 self._container.add(ctrl)
325 class glGuiLayoutButtons(object):
326 def __init__(self, parent):
327 self._parent = parent
328 self._parent._layout = self
331 bs = self._parent._base._buttonSize
332 x0, y0, w, h = self._parent.getSize()
334 for ctrl in self._parent._glGuiControlList:
337 x = w + pos[0] * gridSize - bs * 0.2
339 x = pos[0] * gridSize + bs * 0.2
341 y = h + pos[1] * gridSize * 1.2 - bs * 0.0
343 y = pos[1] * gridSize * 1.2 + bs * 0.2
344 ctrl.setSize(x, y, gridSize, gridSize)
346 def getLayoutSize(self):
347 _, _, w, h = self._parent.getSize()
350 class glGuiLayoutGrid(object):
351 def __init__(self, parent):
352 self._parent = parent
353 self._parent._layout = self
355 self._alignBottom = True
358 borderSize = self._parent._base._buttonSize * 0.2
359 x0, y0, w, h = self._parent.getSize()
364 for ctrl in self._parent._glGuiControlList:
366 w, h = ctrl.getMinSize()
370 widths[x] = max(widths[x], w)
374 heights[y] = max(heights[y], h)
375 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
376 if self._alignBottom:
377 y0 -= self._size[1] - self._parent.getSize()[3]
378 self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
379 for ctrl in self._parent._glGuiControlList:
383 for n in xrange(0, x):
387 for n in xrange(0, y):
391 ctrl.setSize(x1, y1, widths[x], heights[y])
393 def getLayoutSize(self):
396 class glButton(glGuiControl):
397 def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
398 self._buttonSize = size
400 super(glButton, self).__init__(parent, pos)
401 self._tooltip = tooltip
402 self._parent = parent
403 self._imageID = imageID
404 self._callback = callback
405 self._selected = False
407 self._disabled = False
408 self._showExpandArrow = False
409 self._progressBar = None
410 self._altTooltip = ''
412 def setSelected(self, value):
413 self._selected = value
415 def setExpandArrow(self, value):
416 self._showExpandArrow = value
418 def setHidden(self, value):
421 def setDisabled(self, value):
422 self._disabled = value
424 def setProgressBar(self, value):
425 self._progressBar = value
427 def getProgressBar(self):
428 return self._progressBar
430 def setBottomText(self, value):
431 self._altTooltip = value
433 def getSelected(self):
434 return self._selected
436 def getMinSize(self):
439 if self._buttonSize is not None:
440 return self._buttonSize, self._buttonSize
441 return self._base._buttonSize, self._base._buttonSize
443 def _getPixelPos(self):
444 x0, y0, w, h = self.getSize()
445 return x0 + w / 2, y0 + h / 2
451 cx = (self._imageID % 4) / 4
452 cy = int(self._imageID / 4) / 4
453 bs = self.getMinSize()[0]
454 pos = self._getPixelPos()
456 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
463 glColor4ub(128,128,128,128)
465 glColor4ub(255,255,255,255)
466 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
467 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
468 if self._showExpandArrow:
470 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)
472 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)
474 glTranslatef(pos[0], pos[1], 0)
475 glDisable(GL_TEXTURE_2D)
477 glTranslatef(0, -0.55*bs*scale, 0)
480 glColor4ub(60,60,60,255)
481 glTranslatef(-1, -1, 0)
482 opengl.glDrawStringCenter(self._tooltip)
483 glTranslatef(0, 2, 0)
484 opengl.glDrawStringCenter(self._tooltip)
485 glTranslatef(2, 0, 0)
486 opengl.glDrawStringCenter(self._tooltip)
487 glTranslatef(0, -2, 0)
488 opengl.glDrawStringCenter(self._tooltip)
491 glColor4ub(255,255,255,255)
492 opengl.glDrawStringCenter(self._tooltip)
494 progress = self._progressBar
495 if progress is not None:
496 glColor4ub(60,60,60,255)
497 opengl.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
498 glColor4ub(255,255,255,255)
499 opengl.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
500 elif len(self._altTooltip) > 0:
502 glTranslatef(pos[0], pos[1], 0)
503 glTranslatef(0.6*bs*scale, 0, 0)
506 glColor4ub(60,60,60,255)
507 glTranslatef(-1, -1, 0)
508 opengl.glDrawStringLeft(self._altTooltip)
509 glTranslatef(0, 2, 0)
510 opengl.glDrawStringLeft(self._altTooltip)
511 glTranslatef(2, 0, 0)
512 opengl.glDrawStringLeft(self._altTooltip)
513 glTranslatef(0, -2, 0)
514 opengl.glDrawStringLeft(self._altTooltip)
517 glColor4ub(255,255,255,255)
518 opengl.glDrawStringLeft(self._altTooltip)
521 def _checkHit(self, x, y):
522 if self._hidden or self._disabled:
524 bs = self.getMinSize()[0]
525 pos = self._getPixelPos()
526 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
528 def OnMouseMotion(self, x, y):
529 if self._checkHit(x, y):
535 def OnMouseDown(self, x, y, button):
536 if self._checkHit(x, y):
537 self._callback(button)
541 class glRadioButton(glButton):
542 def __init__(self, parent, imageID, tooltip, pos, group, callback):
543 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
545 self._radioCallback = callback
546 self._group.append(self)
548 def setSelected(self, value):
549 self._selected = value
551 def _onRadioSelect(self, button):
552 self._base._focus = None
553 for ctrl in self._group:
555 ctrl.setSelected(False)
556 if self.getSelected():
557 self.setSelected(False)
559 self.setSelected(True)
560 self._radioCallback(button)
562 class glComboButton(glButton):
563 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
564 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
565 self._imageIDs = imageIDs
566 self._tooltips = tooltips
567 self._comboCallback = callback
570 def _onComboOpenSelect(self, button):
572 self._base._focus = None
574 self._base._focus = self
579 self._selected = self.hasFocus()
580 super(glComboButton, self).draw()
582 bs = self._base._buttonSize / 2
583 pos = self._getPixelPos()
585 if not self._selected:
589 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
590 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
591 for n in xrange(0, len(self._imageIDs)):
592 glTranslatef(0, bs, 0)
593 glColor4ub(255,255,255,255)
594 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
595 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
596 glDisable(GL_TEXTURE_2D)
599 glTranslatef(-0.55*bs, 0.1*bs, 0)
602 glColor4ub(60,60,60,255)
603 glTranslatef(-1, -1, 0)
604 opengl.glDrawStringRight(self._tooltips[n])
605 glTranslatef(0, 2, 0)
606 opengl.glDrawStringRight(self._tooltips[n])
607 glTranslatef(2, 0, 0)
608 opengl.glDrawStringRight(self._tooltips[n])
609 glTranslatef(0, -2, 0)
610 opengl.glDrawStringRight(self._tooltips[n])
613 glColor4ub(255,255,255,255)
614 opengl.glDrawStringRight(self._tooltips[n])
619 return self._selection
621 def setValue(self, value):
622 self._selection = value
623 self._imageID = self._imageIDs[self._selection]
624 self._comboCallback()
626 def OnMouseDown(self, x, y, button):
627 if self._hidden or self._disabled:
630 bs = self._base._buttonSize / 2
631 pos = self._getPixelPos()
632 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
633 self._selection = int((y - pos[1] - bs) / bs)
634 self._imageID = self._imageIDs[self._selection]
635 self._base._focus = None
636 self._comboCallback()
638 return super(glComboButton, self).OnMouseDown(x, y, button)
640 class glFrame(glGuiContainer):
641 def __init__(self, parent, pos):
642 super(glFrame, self).__init__(parent, pos)
643 self._selected = False
647 def setSelected(self, value):
648 self._selected = value
650 def setHidden(self, value):
652 for child in self._glGuiControlList:
653 if self._base._focus == child:
654 self._base._focus = None
656 def getSelected(self):
657 return self._selected
659 def getMinSize(self):
660 return self._base._buttonSize, self._base._buttonSize
662 def _getPixelPos(self):
663 x0, y0, w, h = self.getSize()
670 bs = self._parent._buttonSize
671 pos = self._getPixelPos()
673 size = self._layout.getLayoutSize()
674 glColor4ub(255,255,255,255)
675 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
676 #Draw the controls on the frame
677 super(glFrame, self).draw()
679 def _checkHit(self, x, y):
682 pos = self._getPixelPos()
683 w, h = self._layout.getLayoutSize()
684 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
686 def OnMouseMotion(self, x, y):
687 super(glFrame, self).OnMouseMotion(x, y)
688 if self._checkHit(x, y):
694 def OnMouseDown(self, x, y, button):
695 if self._checkHit(x, y):
696 super(glFrame, self).OnMouseDown(x, y, button)
700 class glNotification(glFrame):
701 def __init__(self, parent, pos):
703 super(glNotification, self).__init__(parent, pos)
704 glGuiLayoutGrid(self)._alignBottom = False
705 self._label = glLabel(self, "Notification", (0, 0))
706 self._buttonEject = glButton(self, 31, "Eject", (1, 0), self.onEject, 25)
707 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
708 self._padding = glLabel(self, "", (0, 1))
711 def setSize(self, x, y, w, h):
712 w, h = self._layout.getLayoutSize()
713 baseSize = self._base.GetSizeTuple()
714 if self._anim is not None:
715 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
717 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
720 self.setSize(0,0,0,0)
722 super(glNotification, self).draw()
724 def message(self, text, ejectCallback = None):
725 self._anim = animation(self._base, -20, 25, 1)
726 self.setHidden(False)
727 self._label.setLabel(text)
728 self._buttonEject.setHidden(ejectCallback is None)
729 self._ejectCallback = ejectCallback
730 self._base._queueRefresh()
733 def onEject(self, button):
735 self._ejectCallback()
737 def onClose(self, button):
738 if self._anim is not None:
739 self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
741 self._anim = animation(self._base, 25, -20, 1)
743 class glLabel(glGuiControl):
744 def __init__(self, parent, label, pos):
746 super(glLabel, self).__init__(parent, pos)
748 def setLabel(self, label):
751 def getMinSize(self):
752 w, h = opengl.glGetStringSize(self._label)
755 def _getPixelPos(self):
756 x0, y0, w, h = self.getSize()
760 x, y, w, h = self.getSize()
763 glTranslatef(x, y, 0)
765 # glColor4ub(255,255,255,128)
777 glTranslate(5, h - 5, 0)
778 glColor4ub(255,255,255,255)
779 opengl.glDrawStringLeft(self._label)
782 def _checkHit(self, x, y):
785 def OnMouseMotion(self, x, y):
788 def OnMouseDown(self, x, y, button):
791 class glNumberCtrl(glGuiControl):
792 def __init__(self, parent, value, pos, callback):
793 self._callback = callback
794 self._value = str(value)
797 self._inCallback = False
798 super(glNumberCtrl, self).__init__(parent, pos)
800 def setValue(self, value):
803 self._value = str(value)
805 def getMinSize(self):
806 w, h = opengl.glGetStringSize("VALUES")
809 def _getPixelPos(self):
810 x0, y0, w, h = self.getSize()
814 x, y, w, h = self.getSize()
817 glTranslatef(x, y, 0)
820 glColor4ub(255,255,255,255)
822 glColor4ub(255,255,255,192)
834 glTranslate(5, h - 5, 0)
835 glColor4ub(0,0,0,255)
836 opengl.glDrawStringLeft(self._value)
838 glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
839 opengl.glDrawStringLeft('|')
842 def _checkHit(self, x, y):
843 x1, y1, w, h = self.getSize()
844 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
846 def OnMouseMotion(self, x, y):
849 def OnMouseDown(self, x, y, button):
850 if self._checkHit(x, y):
855 def OnKeyChar(self, c):
856 self._inCallback = True
859 self._selectPos = max(0, self._selectPos)
860 if c == wx.WXK_RIGHT:
862 self._selectPos = min(self._selectPos, len(self._value))
865 value = float(self._value)
870 self._value = str(value)
871 self._callback(self._value)
874 value = float(self._value)
880 self._value = str(value)
881 self._callback(self._value)
882 if c == wx.WXK_BACK and self._selectPos > 0:
883 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
885 self._callback(self._value)
886 if c == wx.WXK_DELETE:
887 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
888 self._callback(self._value)
889 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
890 if wx.GetKeyState(wx.WXK_SHIFT):
894 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
895 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
897 self._callback(self._value)
898 self._inCallback = False
901 self._base._focus = self
902 self._selectPos = len(self._value)
905 class glCheckbox(glGuiControl):
906 def __init__(self, parent, value, pos, callback):
907 self._callback = callback
911 self._inCallback = False
912 super(glCheckbox, self).__init__(parent, pos)
914 def setValue(self, value):
917 self._value = str(value)
922 def getMinSize(self):
925 def _getPixelPos(self):
926 x0, y0, w, h = self.getSize()
930 x, y, w, h = self.getSize()
933 glTranslatef(x, y, 0)
935 glColor3ub(255,255,255)
937 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
939 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
943 def _checkHit(self, x, y):
944 x1, y1, w, h = self.getSize()
945 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
947 def OnMouseMotion(self, x, y):
950 def OnMouseDown(self, x, y, button):
951 if self._checkHit(x, y):
952 self._value = not self._value
956 class glSlider(glGuiControl):
957 def __init__(self, parent, value, minValue, maxValue, pos, callback):
958 super(glSlider, self).__init__(parent, pos)
959 self._callback = callback
963 self._minValue = minValue
964 self._maxValue = maxValue
966 def setValue(self, value):
970 if self._value < self._minValue:
971 return self._minValue
972 if self._value > self._maxValue:
973 return self._maxValue
976 def setRange(self, minValue, maxValue):
977 if maxValue < minValue:
979 self._minValue = minValue
980 self._maxValue = maxValue
982 def getMinValue(self):
983 return self._minValue
985 def getMaxValue(self):
986 return self._maxValue
988 def setHidden(self, value):
991 def getMinSize(self):
992 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
994 def _getPixelPos(self):
995 x0, y0, w, h = self.getSize()
996 minSize = self.getMinSize()
997 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
1003 w, h = self.getMinSize()
1004 pos = self._getPixelPos()
1007 glTranslatef(pos[0], pos[1], 0)
1008 glDisable(GL_TEXTURE_2D)
1010 glColor4ub(60,60,60,255)
1012 glColor4ub(60,60,60,192)
1014 glVertex2f( w/2,-h/2)
1015 glVertex2f(-w/2,-h/2)
1016 glVertex2f(-w/2, h/2)
1017 glVertex2f( w/2, h/2)
1019 scrollLength = h - w
1020 if self._maxValue-self._minValue != 0:
1021 valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1024 glTranslate(0.0,scrollLength/2,0)
1026 glColor4ub(0,0,0,255)
1028 glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1029 opengl.glDrawStringRight(str(self._minValue))
1030 glTranslate(0,-scrollLength,0)
1031 opengl.glDrawStringRight(str(self._maxValue))
1032 glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1033 opengl.glDrawStringLeft(str(self.getValue()))
1035 glColor4ub(255,255,255,240)
1036 glTranslate(0.0,-scrollLength*valueNormalized,0)
1038 glVertex2f( w/2,-w/2)
1039 glVertex2f(-w/2,-w/2)
1040 glVertex2f(-w/2, w/2)
1041 glVertex2f( w/2, w/2)
1045 def _checkHit(self, x, y):
1048 pos = self._getPixelPos()
1049 w, h = self.getMinSize()
1050 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1053 self._base._focus = self
1056 def OnMouseMotion(self, x, y):
1058 w, h = self.getMinSize()
1059 scrollLength = h - w
1060 pos = self._getPixelPos()
1061 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1064 if self._checkHit(x, y):
1070 def OnMouseDown(self, x, y, button):
1071 if self._checkHit(x, y):
1073 self.OnMouseMotion(x, y)
1077 def OnMouseUp(self, x, y):
1079 self._base._focus = None