1 from __future__ import absolute_import
2 from __future__ import division
10 from wx import glcanvas
12 OpenGL.ERROR_CHECKING = False
13 from OpenGL.GL import *
15 from Cura.gui.util import opengl
17 class animation(object):
18 def __init__(self, gui, start, end, runTime):
21 self._startTime = time.time()
22 self._runTime = runTime
23 gui._animationList.append(self)
26 return time.time() > self._startTime + self._runTime
28 def getPosition(self):
31 f = (time.time() - self._startTime) / self._runTime
34 #f = 6*tc*ts + -15*ts*ts + 10*tc
36 return self._start + (self._end - self._start) * f
38 class glGuiControl(object):
39 def __init__(self, parent, pos):
41 self._base = parent._base
43 self._size = (0,0, 1, 1)
44 self._parent.add(self)
46 def setSize(self, x, y, w, h):
47 self._size = (x, y, w, h)
55 def updateLayout(self):
59 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
60 if self._parent._glGuiControlList[n].setFocus():
62 for n in xrange(0, self._parent._glGuiControlList.index(self)):
63 if self._parent._glGuiControlList[n].setFocus():
66 def focusPrevious(self):
67 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
68 if self._parent._glGuiControlList[n].setFocus():
70 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
71 if self._parent._glGuiControlList[n].setFocus():
78 return self._base._focus == self
80 def OnMouseUp(self, x, y):
83 def OnKeyChar(self, key):
86 class glGuiContainer(glGuiControl):
87 def __init__(self, parent, pos):
88 self._glGuiControlList = []
89 glGuiLayoutButtons(self)
90 super(glGuiContainer, self).__init__(parent, pos)
93 self._glGuiControlList.append(ctrl)
96 def OnMouseDown(self, x, y, button):
97 for ctrl in self._glGuiControlList:
98 if ctrl.OnMouseDown(x, y, button):
102 def OnMouseUp(self, x, y):
103 for ctrl in self._glGuiControlList:
104 if ctrl.OnMouseUp(x, y):
108 def OnMouseMotion(self, x, y):
110 for ctrl in self._glGuiControlList:
111 if ctrl.OnMouseMotion(x, y):
116 for ctrl in self._glGuiControlList:
119 def updateLayout(self):
120 self._layout.update()
121 for ctrl in self._glGuiControlList:
124 class glGuiPanel(glcanvas.GLCanvas):
125 def __init__(self, parent):
126 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 32, glcanvas.WX_GL_STENCIL_SIZE, 8)
127 glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList)
130 self._container = None
131 self._container = glGuiContainer(self, (0,0))
132 self._shownError = False
134 self._context = glcanvas.GLContext(self)
135 self._glButtonsTexture = None
136 self._glRobotTexture = None
137 self._buttonSize = 64
139 self._animationList = []
140 self.glReleaseList = []
141 self._refreshQueued = False
143 wx.EVT_PAINT(self, self._OnGuiPaint)
144 wx.EVT_SIZE(self, self._OnSize)
145 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
146 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseDown)
147 wx.EVT_LEFT_DCLICK(self, self._OnGuiMouseDown)
148 wx.EVT_LEFT_UP(self, self._OnGuiMouseUp)
149 wx.EVT_RIGHT_DOWN(self, self._OnGuiMouseDown)
150 wx.EVT_RIGHT_DCLICK(self, self._OnGuiMouseDown)
151 wx.EVT_RIGHT_UP(self, self._OnGuiMouseUp)
152 wx.EVT_MIDDLE_DOWN(self, self._OnGuiMouseDown)
153 wx.EVT_MIDDLE_DCLICK(self, self._OnGuiMouseDown)
154 wx.EVT_MIDDLE_UP(self, self._OnGuiMouseUp)
155 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
156 wx.EVT_CHAR(self, self._OnGuiKeyChar)
157 wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
158 wx.EVT_IDLE(self, self._OnIdle)
160 def _OnIdle(self, e):
161 if len(self._animationList) > 0 or self._refreshQueued:
162 self._refreshQueued = False
163 for anim in self._animationList:
165 self._animationList.remove(anim)
168 def _OnGuiKeyChar(self, e):
169 if self._focus is not None:
170 self._focus.OnKeyChar(e.GetKeyCode())
173 self.OnKeyChar(e.GetKeyCode())
175 def OnFocusLost(self, e):
179 def _OnGuiMouseDown(self,e):
181 if self._container.OnMouseDown(e.GetX(), e.GetY(), e.GetButton()):
186 def _OnGuiMouseUp(self, e):
187 if self._container.OnMouseUp(e.GetX(), e.GetY()):
192 def _OnGuiMouseMotion(self,e):
194 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
195 self.OnMouseMotion(e)
197 def _OnGuiPaint(self, e):
198 h = self.GetSize().GetHeight()
199 w = self.GetSize().GetWidth()
200 oldButtonSize = self._buttonSize
204 self._buttonSize = 32
206 self._buttonSize = 48
208 self._buttonSize = 64
210 self._buttonSize = 80
212 self._buttonSize = 96
213 if self._buttonSize != oldButtonSize:
214 self._container.updateLayout()
216 dc = wx.PaintDC(self)
218 self.SetCurrent(self._context)
219 for obj in self.glReleaseList:
221 del self.glReleaseList[:]
227 errStr = 'An error has occurred during the 3D view drawing.'
228 tb = traceback.extract_tb(sys.exc_info()[2])
229 errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
230 for n in xrange(len(tb)-1, -1, -1):
232 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
233 if not self._shownError:
234 wx.CallAfter(wx.MessageBox, errStr, '3D window error', wx.OK | wx.ICON_EXCLAMATION)
235 self._shownError = True
238 if self._glButtonsTexture is None:
239 self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
240 self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
242 glDisable(GL_DEPTH_TEST)
244 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
245 glDisable(GL_LIGHTING)
246 glColor4ub(255,255,255,255)
248 glMatrixMode(GL_PROJECTION)
250 size = self.GetSize()
251 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
252 glMatrixMode(GL_MODELVIEW)
255 self._container.draw()
257 glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
258 glEnable(GL_TEXTURE_2D)
261 glTranslate(size.GetWidth() - 8,size.GetHeight() - 16,0)
262 s = self._buttonSize * 1.4
274 glDisable(GL_TEXTURE_2D)
277 def _OnEraseBackground(self,event):
278 #Workaround for windows background redraw flicker.
282 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
283 self._container.updateLayout()
286 def OnMouseDown(self,e):
288 def OnMouseUp(self,e):
290 def OnMouseMotion(self, e):
292 def OnKeyChar(self, keyCode):
294 def OnPaint(self, e):
296 def OnKeyChar(self, keycode):
299 def QueueRefresh(self):
300 wx.CallAfter(self._queueRefresh)
302 def _queueRefresh(self):
303 self._refreshQueued = True
306 if self._container is not None:
307 self._container.add(ctrl)
309 class glGuiLayoutButtons(object):
310 def __init__(self, parent):
311 self._parent = parent
312 self._parent._layout = self
315 bs = self._parent._base._buttonSize
316 x0, y0, w, h = self._parent.getSize()
318 for ctrl in self._parent._glGuiControlList:
321 x = w + pos[0] * gridSize - bs * 0.2
323 x = pos[0] * gridSize + bs * 0.2
325 y = h + pos[1] * gridSize * 1.2 - bs * 0.2
327 y = pos[1] * gridSize * 1.2 + bs * 0.2
328 ctrl.setSize(x, y, gridSize, gridSize)
330 def getLayoutSize(self):
331 _, _, w, h = self._parent.getSize()
334 class glGuiLayoutGrid(object):
335 def __init__(self, parent):
336 self._parent = parent
337 self._parent._layout = self
339 self._alignBottom = True
342 borderSize = self._parent._base._buttonSize * 0.2
343 x0, y0, w, h = self._parent.getSize()
348 for ctrl in self._parent._glGuiControlList:
350 w, h = ctrl.getMinSize()
354 widths[x] = max(widths[x], w)
358 heights[y] = max(heights[y], h)
359 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
360 if self._alignBottom:
361 y0 -= self._size[1] - self._parent.getSize()[3]
362 self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
363 for ctrl in self._parent._glGuiControlList:
367 for n in xrange(0, x):
371 for n in xrange(0, y):
375 ctrl.setSize(x1, y1, widths[x], heights[y])
377 def getLayoutSize(self):
380 class glButton(glGuiControl):
381 def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
382 self._buttonSize = size
384 super(glButton, self).__init__(parent, pos)
385 self._tooltip = tooltip
386 self._parent = parent
387 self._imageID = imageID
388 self._callback = callback
389 self._selected = False
391 self._disabled = False
392 self._showExpandArrow = False
393 self._progressBar = None
394 self._altTooltip = ''
396 def setSelected(self, value):
397 self._selected = value
399 def setExpandArrow(self, value):
400 self._showExpandArrow = value
402 def setHidden(self, value):
405 def setDisabled(self, value):
406 self._disabled = value
408 def setProgressBar(self, value):
409 self._progressBar = value
411 def setBottomText(self, value):
412 self._altTooltip = value
414 def getSelected(self):
415 return self._selected
417 def getMinSize(self):
420 if self._buttonSize is not None:
421 return self._buttonSize, self._buttonSize
422 return self._base._buttonSize, self._base._buttonSize
424 def _getPixelPos(self):
425 x0, y0, w, h = self.getSize()
426 return x0 + w / 2, y0 + h / 2
432 cx = (self._imageID % 4) / 4
433 cy = int(self._imageID / 4) / 4
434 bs = self.getMinSize()[0]
435 pos = self._getPixelPos()
437 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
444 glColor4ub(128,128,128,128)
446 glColor4ub(255,255,255,255)
447 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
448 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
449 if self._showExpandArrow:
451 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)
453 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)
455 glTranslatef(pos[0], pos[1], 0)
456 glDisable(GL_TEXTURE_2D)
458 glTranslatef(0, -0.55*bs*scale, 0)
461 glColor4ub(60,60,60,255)
462 glTranslatef(-1, -1, 0)
463 opengl.glDrawStringCenter(self._tooltip)
464 glTranslatef(0, 2, 0)
465 opengl.glDrawStringCenter(self._tooltip)
466 glTranslatef(2, 0, 0)
467 opengl.glDrawStringCenter(self._tooltip)
468 glTranslatef(0, -2, 0)
469 opengl.glDrawStringCenter(self._tooltip)
472 glColor4ub(255,255,255,255)
473 opengl.glDrawStringCenter(self._tooltip)
475 progress = self._progressBar
476 if progress is not None:
477 glColor4ub(255,255,255,192)
478 opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4, 0)
479 opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs * progress, bs / 4, 0)
480 elif len(self._altTooltip) > 0:
482 glTranslatef(pos[0], pos[1], 0)
483 glTranslatef(0.6*bs*scale, 0, 0)
486 glColor4ub(60,60,60,255)
487 glTranslatef(-1, -1, 0)
488 opengl.glDrawStringLeft(self._altTooltip)
489 glTranslatef(0, 2, 0)
490 opengl.glDrawStringLeft(self._altTooltip)
491 glTranslatef(2, 0, 0)
492 opengl.glDrawStringLeft(self._altTooltip)
493 glTranslatef(0, -2, 0)
494 opengl.glDrawStringLeft(self._altTooltip)
497 glColor4ub(255,255,255,255)
498 opengl.glDrawStringLeft(self._altTooltip)
501 def _checkHit(self, x, y):
502 if self._hidden or self._disabled:
504 bs = self.getMinSize()[0]
505 pos = self._getPixelPos()
506 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
508 def OnMouseMotion(self, x, y):
509 if self._checkHit(x, y):
515 def OnMouseDown(self, x, y, button):
516 if self._checkHit(x, y):
517 self._callback(button)
521 class glRadioButton(glButton):
522 def __init__(self, parent, imageID, tooltip, pos, group, callback):
523 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
525 self._radioCallback = callback
526 self._group.append(self)
528 def setSelected(self, value):
529 self._selected = value
531 def _onRadioSelect(self, button):
532 self._base._focus = None
533 for ctrl in self._group:
535 ctrl.setSelected(False)
536 if self.getSelected():
537 self.setSelected(False)
539 self.setSelected(True)
540 self._radioCallback(button)
542 class glComboButton(glButton):
543 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
544 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
545 self._imageIDs = imageIDs
546 self._tooltips = tooltips
547 self._comboCallback = callback
550 def _onComboOpenSelect(self, button):
552 self._base._focus = None
554 self._base._focus = self
559 self._selected = self.hasFocus()
560 super(glComboButton, self).draw()
562 bs = self._base._buttonSize / 2
563 pos = self._getPixelPos()
565 if not self._selected:
569 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
570 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
571 for n in xrange(0, len(self._imageIDs)):
572 glTranslatef(0, bs, 0)
573 glColor4ub(255,255,255,255)
574 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
575 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
576 glDisable(GL_TEXTURE_2D)
579 glTranslatef(-0.55*bs, 0.1*bs, 0)
582 glColor4ub(60,60,60,255)
583 glTranslatef(-1, -1, 0)
584 opengl.glDrawStringRight(self._tooltips[n])
585 glTranslatef(0, 2, 0)
586 opengl.glDrawStringRight(self._tooltips[n])
587 glTranslatef(2, 0, 0)
588 opengl.glDrawStringRight(self._tooltips[n])
589 glTranslatef(0, -2, 0)
590 opengl.glDrawStringRight(self._tooltips[n])
593 glColor4ub(255,255,255,255)
594 opengl.glDrawStringRight(self._tooltips[n])
599 return self._selection
601 def setValue(self, value):
602 self._selection = value
603 self._imageID = self._imageIDs[self._selection]
604 self._comboCallback()
606 def OnMouseDown(self, x, y, button):
607 if self._hidden or self._disabled:
610 bs = self._base._buttonSize / 2
611 pos = self._getPixelPos()
612 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
613 self._selection = int((y - pos[1] - bs) / bs)
614 self._imageID = self._imageIDs[self._selection]
615 self._base._focus = None
616 self._comboCallback()
618 return super(glComboButton, self).OnMouseDown(x, y, button)
620 class glFrame(glGuiContainer):
621 def __init__(self, parent, pos):
622 super(glFrame, self).__init__(parent, pos)
623 self._selected = False
627 def setSelected(self, value):
628 self._selected = value
630 def setHidden(self, value):
632 for child in self._glGuiControlList:
633 if self._base._focus == child:
634 self._base._focus = None
636 def getSelected(self):
637 return self._selected
639 def getMinSize(self):
640 return self._base._buttonSize, self._base._buttonSize
642 def _getPixelPos(self):
643 x0, y0, w, h = self.getSize()
650 bs = self._parent._buttonSize
651 pos = self._getPixelPos()
653 size = self._layout.getLayoutSize()
654 glColor4ub(255,255,255,255)
655 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
656 #Draw the controls on the frame
657 super(glFrame, self).draw()
659 def _checkHit(self, x, y):
662 pos = self._getPixelPos()
663 w, h = self._layout.getLayoutSize()
664 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
666 def OnMouseMotion(self, x, y):
667 super(glFrame, self).OnMouseMotion(x, y)
668 if self._checkHit(x, y):
674 def OnMouseDown(self, x, y, button):
675 if self._checkHit(x, y):
676 super(glFrame, self).OnMouseDown(x, y, button)
680 class glNotification(glFrame):
681 def __init__(self, parent, pos):
683 super(glNotification, self).__init__(parent, pos)
684 glGuiLayoutGrid(self)._alignBottom = False
685 self._label = glLabel(self, "Notification", (0, 0))
686 self._buttonEject = glButton(self, 31, "Eject", (1, 0), self.onEject, 25)
687 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
688 self._padding = glLabel(self, "", (0, 1))
691 def setSize(self, x, y, w, h):
692 w, h = self._layout.getLayoutSize()
693 baseSize = self._base.GetSizeTuple()
694 if self._anim is not None:
695 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
697 super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
700 self.setSize(0,0,0,0)
702 super(glNotification, self).draw()
704 def message(self, text, ejectCallback = None):
705 if self._anim is not None:
706 self._anim = animation(self._base, self._anim.getPosition(), 25, 1)
708 self._anim = animation(self._base, -20, 25, 1)
709 self.setHidden(False)
710 self._label.setLabel(text)
711 self._buttonEject.setHidden(ejectCallback is None)
712 self._ejectCallback = ejectCallback
715 def onEject(self, button):
717 self._ejectCallback()
719 def onClose(self, button):
720 if self._anim is not None:
721 self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
723 self._anim = animation(self._base, 25, -20, 1)
725 class glLabel(glGuiControl):
726 def __init__(self, parent, label, pos):
728 super(glLabel, self).__init__(parent, pos)
730 def setLabel(self, label):
733 def getMinSize(self):
734 w, h = opengl.glGetStringSize(self._label)
737 def _getPixelPos(self):
738 x0, y0, w, h = self.getSize()
742 x, y, w, h = self.getSize()
745 glTranslatef(x, y, 0)
747 # glColor4ub(255,255,255,128)
759 glTranslate(5, h - 5, 0)
760 glColor4ub(255,255,255,255)
761 opengl.glDrawStringLeft(self._label)
764 def _checkHit(self, x, y):
767 def OnMouseMotion(self, x, y):
770 def OnMouseDown(self, x, y, button):
773 class glNumberCtrl(glGuiControl):
774 def __init__(self, parent, value, pos, callback):
775 self._callback = callback
776 self._value = str(value)
779 self._inCallback = False
780 super(glNumberCtrl, self).__init__(parent, pos)
782 def setValue(self, value):
785 self._value = str(value)
787 def getMinSize(self):
788 w, h = opengl.glGetStringSize("VALUES")
791 def _getPixelPos(self):
792 x0, y0, w, h = self.getSize()
796 x, y, w, h = self.getSize()
799 glTranslatef(x, y, 0)
802 glColor4ub(255,255,255,255)
804 glColor4ub(255,255,255,192)
816 glTranslate(5, h - 5, 0)
817 glColor4ub(0,0,0,255)
818 opengl.glDrawStringLeft(self._value)
820 glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
821 opengl.glDrawStringLeft('|')
824 def _checkHit(self, x, y):
825 x1, y1, w, h = self.getSize()
826 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
828 def OnMouseMotion(self, x, y):
831 def OnMouseDown(self, x, y, button):
832 if self._checkHit(x, y):
837 def OnKeyChar(self, c):
838 self._inCallback = True
841 self._selectPos = max(0, self._selectPos)
842 if c == wx.WXK_RIGHT:
844 self._selectPos = min(self._selectPos, len(self._value))
847 value = float(self._value)
852 self._value = str(value)
853 self._callback(self._value)
856 value = float(self._value)
862 self._value = str(value)
863 self._callback(self._value)
864 if c == wx.WXK_BACK and self._selectPos > 0:
865 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
867 self._callback(self._value)
868 if c == wx.WXK_DELETE:
869 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
870 self._callback(self._value)
871 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
872 if wx.GetKeyState(wx.WXK_SHIFT):
876 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
877 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
879 self._callback(self._value)
880 self._inCallback = False
883 self._base._focus = self
884 self._selectPos = len(self._value)
887 class glCheckbox(glGuiControl):
888 def __init__(self, parent, value, pos, callback):
889 self._callback = callback
893 self._inCallback = False
894 super(glCheckbox, self).__init__(parent, pos)
896 def setValue(self, value):
899 self._value = str(value)
904 def getMinSize(self):
907 def _getPixelPos(self):
908 x0, y0, w, h = self.getSize()
912 x, y, w, h = self.getSize()
915 glTranslatef(x, y, 0)
917 glColor3ub(255,255,255)
919 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
921 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
925 def _checkHit(self, x, y):
926 x1, y1, w, h = self.getSize()
927 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
929 def OnMouseMotion(self, x, y):
932 def OnMouseDown(self, x, y, button):
933 if self._checkHit(x, y):
934 self._value = not self._value
938 class glSlider(glGuiControl):
939 def __init__(self, parent, value, minValue, maxValue, pos, callback):
940 super(glSlider, self).__init__(parent, pos)
941 self._callback = callback
945 self._minValue = minValue
946 self._maxValue = maxValue
948 def setValue(self, value):
950 self._value = max(self._minValue, self._value)
951 self._value = min(self._maxValue, self._value)
956 def setRange(self, minValue, maxValue):
957 if maxValue < minValue:
959 self._minValue = minValue
960 self._maxValue = maxValue
961 self._value = max(minValue, self._value)
962 self._value = min(maxValue, self._value)
964 def getMinValue(self):
965 return self._minValue
967 def getMaxValue(self):
968 return self._maxValue
970 def setHidden(self, value):
973 def getMinSize(self):
974 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
976 def _getPixelPos(self):
977 x0, y0, w, h = self.getSize()
978 minSize = self.getMinSize()
979 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
985 w, h = self.getMinSize()
986 pos = self._getPixelPos()
989 glTranslatef(pos[0], pos[1], 0)
990 glDisable(GL_TEXTURE_2D)
992 glColor4ub(60,60,60,255)
994 glColor4ub(60,60,60,192)
996 glVertex2f( w/2,-h/2)
997 glVertex2f(-w/2,-h/2)
998 glVertex2f(-w/2, h/2)
999 glVertex2f( w/2, h/2)
1001 scrollLength = h - w
1002 glTranslate(0.0,scrollLength/2,0)
1004 glColor4ub(0,0,0,255)
1006 glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1007 opengl.glDrawStringRight(str(self._minValue))
1008 glTranslate(0,-scrollLength,0)
1009 opengl.glDrawStringRight(str(self._maxValue))
1010 if self._maxValue-self._minValue > 0:
1011 glTranslate(w,scrollLength-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
1012 opengl.glDrawStringLeft(str(self._value))
1014 glColor4ub(255,255,255,240)
1015 if self._maxValue - self._minValue != 0:
1016 glTranslate(0.0,-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
1018 glVertex2f( w/2,-w/2)
1019 glVertex2f(-w/2,-w/2)
1020 glVertex2f(-w/2, w/2)
1021 glVertex2f( w/2, w/2)
1025 def _checkHit(self, x, y):
1028 pos = self._getPixelPos()
1029 w, h = self.getMinSize()
1030 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1033 self._base._focus = self
1036 def OnMouseMotion(self, x, y):
1038 w, h = self.getMinSize()
1039 scrollLength = h - w
1040 pos = self._getPixelPos()
1041 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1044 if self._checkHit(x, y):
1050 def OnMouseDown(self, x, y, button):
1051 if self._checkHit(x, y):
1053 self.OnMouseMotion(x, y)
1057 def OnMouseUp(self, x, y):
1059 self._base._focus = None