1 from __future__ import absolute_import
2 from __future__ import division
9 from wx import glcanvas
11 OpenGL.ERROR_CHECKING = False
12 from OpenGL.GL import *
14 from Cura.gui.util import opengl
16 class glGuiControl(object):
17 def __init__(self, parent, pos):
19 self._base = parent._base
21 self._size = (0,0, 1, 1)
22 self._parent.add(self)
24 def setSize(self, x, y, w, h):
25 self._size = (x, y, w, h)
33 def updateLayout(self):
37 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
38 if self._parent._glGuiControlList[n].setFocus():
40 for n in xrange(0, self._parent._glGuiControlList.index(self)):
41 if self._parent._glGuiControlList[n].setFocus():
44 def focusPrevious(self):
45 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
46 if self._parent._glGuiControlList[n].setFocus():
48 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
49 if self._parent._glGuiControlList[n].setFocus():
56 return self._base._focus == self
58 def OnMouseUp(self, x, y):
61 def OnKeyChar(self, key):
64 class glGuiContainer(glGuiControl):
65 def __init__(self, parent, pos):
66 self._glGuiControlList = []
67 glGuiLayoutButtons(self)
68 super(glGuiContainer, self).__init__(parent, pos)
71 self._glGuiControlList.append(ctrl)
74 def OnMouseDown(self, x, y):
75 for ctrl in self._glGuiControlList:
76 if ctrl.OnMouseDown(x, y):
80 def OnMouseUp(self, x, y):
81 for ctrl in self._glGuiControlList:
82 if ctrl.OnMouseUp(x, y):
86 def OnMouseMotion(self, x, y):
88 for ctrl in self._glGuiControlList:
89 if ctrl.OnMouseMotion(x, y):
94 for ctrl in self._glGuiControlList:
97 def updateLayout(self):
99 for ctrl in self._glGuiControlList:
102 class glGuiPanel(glcanvas.GLCanvas):
103 def __init__(self, parent):
104 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8)
105 glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList)
108 self._container = None
109 self._container = glGuiContainer(self, (0,0))
110 self._shownError = False
112 self._context = glcanvas.GLContext(self)
113 self._glButtonsTexture = None
114 self._glRobotTexture = None
115 self._buttonSize = 64
117 self.glReleaseList = []
119 wx.EVT_PAINT(self, self._OnGuiPaint)
120 wx.EVT_SIZE(self, self._OnSize)
121 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
122 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseDown)
123 wx.EVT_LEFT_DCLICK(self, self._OnGuiMouseDown)
124 wx.EVT_LEFT_UP(self, self._OnGuiMouseUp)
125 wx.EVT_RIGHT_DOWN(self, self._OnGuiMouseDown)
126 wx.EVT_RIGHT_DCLICK(self, self._OnGuiMouseDown)
127 wx.EVT_RIGHT_UP(self, self._OnGuiMouseUp)
128 wx.EVT_MIDDLE_DOWN(self, self._OnGuiMouseDown)
129 wx.EVT_MIDDLE_DCLICK(self, self._OnGuiMouseDown)
130 wx.EVT_MIDDLE_UP(self, self._OnGuiMouseUp)
131 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
132 wx.EVT_CHAR(self, self._OnGuiKeyChar)
133 wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
135 def _OnGuiKeyChar(self, e):
136 if self._focus is not None:
137 self._focus.OnKeyChar(e.GetKeyCode())
140 self.OnKeyChar(e.GetKeyCode())
142 def OnFocusLost(self, e):
146 def _OnGuiMouseDown(self,e):
148 if self._container.OnMouseDown(e.GetX(), e.GetY()):
153 def _OnGuiMouseUp(self, e):
154 if self._container.OnMouseUp(e.GetX(), e.GetY()):
159 def _OnGuiMouseMotion(self,e):
161 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
162 self.OnMouseMotion(e)
164 def _OnGuiPaint(self, e):
165 h = self.GetSize().GetHeight()
166 w = self.GetSize().GetWidth()
167 oldButtonSize = self._buttonSize
171 self._buttonSize = 32
173 self._buttonSize = 48
175 self._buttonSize = 64
177 self._buttonSize = 80
179 self._buttonSize = 96
180 if self._buttonSize != oldButtonSize:
181 self._container.updateLayout()
183 dc = wx.PaintDC(self)
185 self.SetCurrent(self._context)
186 for obj in self.glReleaseList:
188 del self.glReleaseList[:]
194 errStr = 'An error has occurred during the 3D view drawing.'
195 tb = traceback.extract_tb(sys.exc_info()[2])
196 errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
197 for n in xrange(len(tb)-1, -1, -1):
199 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
200 if not self._shownError:
201 wx.CallAfter(wx.MessageBox, errStr, '3D window error', wx.OK | wx.ICON_EXCLAMATION)
202 self._shownError = True
205 if self._glButtonsTexture is None:
206 self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
207 self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
209 glDisable(GL_DEPTH_TEST)
211 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
212 glDisable(GL_LIGHTING)
213 glColor4ub(255,255,255,255)
215 glMatrixMode(GL_PROJECTION)
217 size = self.GetSize()
218 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
219 glMatrixMode(GL_MODELVIEW)
222 self._container.draw()
223 glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
224 glEnable(GL_TEXTURE_2D)
227 glTranslate(size.GetWidth() - 8,size.GetHeight() - 16,0)
228 s = self._buttonSize * 1.4
240 glDisable(GL_TEXTURE_2D)
243 def _OnEraseBackground(self,event):
244 #Workaround for windows background redraw flicker.
248 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
249 self._container.updateLayout()
252 def OnMouseDown(self,e):
254 def OnMouseUp(self,e):
256 def OnMouseMotion(self, e):
258 def OnKeyChar(self, keyCode):
260 def OnPaint(self, e):
264 if self._container is not None:
265 self._container.add(ctrl)
267 class glGuiLayoutButtons(object):
268 def __init__(self, parent):
269 self._parent = parent
270 self._parent._layout = self
273 bs = self._parent._base._buttonSize
274 x0, y0, w, h = self._parent.getSize()
276 for ctrl in self._parent._glGuiControlList:
279 x = w + pos[0] * gridSize - bs * 0.2
281 x = pos[0] * gridSize + bs * 0.2
283 y = h + pos[1] * gridSize * 1.2 - bs * 0.2
285 y = pos[1] * gridSize * 1.2 + bs * 0.2
286 ctrl.setSize(x, y, gridSize, gridSize)
288 def getLayoutSize(self):
289 _, _, w, h = self._parent.getSize()
292 class glGuiLayoutGrid(object):
293 def __init__(self, parent):
294 self._parent = parent
295 self._parent._layout = self
297 self._alignBottom = True
300 borderSize = self._parent._base._buttonSize * 0.2
301 x0, y0, w, h = self._parent.getSize()
306 for ctrl in self._parent._glGuiControlList:
308 w, h = ctrl.getMinSize()
312 widths[x] = max(widths[x], w)
316 heights[y] = max(heights[y], h)
317 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
318 if self._alignBottom:
319 y0 -= self._size[1] - self._parent.getSize()[3]
320 self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
321 for ctrl in self._parent._glGuiControlList:
325 for n in xrange(0, x):
329 for n in xrange(0, y):
333 ctrl.setSize(x1, y1, widths[x], heights[y])
335 def getLayoutSize(self):
338 class glButton(glGuiControl):
339 def __init__(self, parent, imageID, tooltip, pos, callback):
340 super(glButton, self).__init__(parent, pos)
341 self._tooltip = tooltip
342 self._parent = parent
343 self._imageID = imageID
344 self._callback = callback
345 self._selected = False
348 self._disabled = False
349 self._showExpandArrow = False
350 self._progressBar = 0.0
352 def setSelected(self, value):
353 self._selected = value
355 def setExpandArrow(self, value):
356 self._showExpandArrow = value
358 def setHidden(self, value):
361 def setDisabled(self, value):
362 self._disabled = value
364 def setProgressBar(self, value):
365 self._progressBar = value
367 def getSelected(self):
368 return self._selected
370 def getMinSize(self):
371 return self._base._buttonSize, self._base._buttonSize
373 def _getPixelPos(self):
374 x0, y0, w, h = self.getSize()
375 return x0 + w / 2, y0 + h / 2
381 cx = (self._imageID % 4) / 4
382 cy = int(self._imageID / 4) / 4
383 bs = self._base._buttonSize
384 pos = self._getPixelPos()
386 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
393 glColor4ub(128,128,128,128)
395 glColor4ub(255,255,255,255)
396 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
397 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
398 if self._showExpandArrow:
400 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)
402 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)
404 glTranslatef(pos[0], pos[1], 0)
405 glDisable(GL_TEXTURE_2D)
407 glTranslatef(0, -0.55*bs*scale, 0)
410 glColor4ub(60,60,60,255)
411 glTranslatef(-1, -1, 0)
412 opengl.glDrawStringCenter(self._tooltip)
413 glTranslatef(0, 2, 0)
414 opengl.glDrawStringCenter(self._tooltip)
415 glTranslatef(2, 0, 0)
416 opengl.glDrawStringCenter(self._tooltip)
417 glTranslatef(0, -2, 0)
418 opengl.glDrawStringCenter(self._tooltip)
421 glColor4ub(255,255,255,255)
422 opengl.glDrawStringCenter(self._tooltip)
424 if 0.0 < self._progressBar < 1.0:
425 glColor4ub(255,255,255,192)
427 opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4, 0)
428 opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs * self._progressBar, bs / 4, 0)
431 def _checkHit(self, x, y):
432 if self._hidden or self._disabled:
434 bs = self.getMinSize()[0]
435 pos = self._getPixelPos()
436 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
438 def OnMouseMotion(self, x, y):
439 if self._checkHit(x, y):
445 def OnMouseDown(self, x, y):
446 if self._checkHit(x, y):
451 class glRadioButton(glButton):
452 def __init__(self, parent, imageID, tooltip, pos, group, callback):
453 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
455 self._radioCallback = callback
456 self._group.append(self)
458 def setSelected(self, value):
459 self._selected = value
461 def _onRadioSelect(self):
462 self._base._focus = None
463 for ctrl in self._group:
465 ctrl.setSelected(False)
466 if self.getSelected():
467 self.setSelected(False)
469 self.setSelected(True)
470 self._radioCallback()
472 class glComboButton(glButton):
473 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
474 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
475 self._imageIDs = imageIDs
476 self._tooltips = tooltips
477 self._comboCallback = callback
480 def _onComboOpenSelect(self):
482 self._base._focus = None
484 self._base._focus = self
489 self._selected = self.hasFocus()
490 super(glComboButton, self).draw()
492 bs = self._base._buttonSize / 2
493 pos = self._getPixelPos()
495 if not self._selected:
499 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
500 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
501 for n in xrange(0, len(self._imageIDs)):
502 glTranslatef(0, bs, 0)
503 glColor4ub(255,255,255,255)
504 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
505 opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
506 glDisable(GL_TEXTURE_2D)
509 glTranslatef(-0.55*bs, 0.1*bs, 0)
512 glColor4ub(60,60,60,255)
513 glTranslatef(-1, -1, 0)
514 opengl.glDrawStringRight(self._tooltips[n])
515 glTranslatef(0, 2, 0)
516 opengl.glDrawStringRight(self._tooltips[n])
517 glTranslatef(2, 0, 0)
518 opengl.glDrawStringRight(self._tooltips[n])
519 glTranslatef(0, -2, 0)
520 opengl.glDrawStringRight(self._tooltips[n])
523 glColor4ub(255,255,255,255)
524 opengl.glDrawStringRight(self._tooltips[n])
529 return self._selection
531 def setValue(self, value):
532 self._selection = value
533 self._imageID = self._imageIDs[self._selection]
534 self._comboCallback()
536 def OnMouseDown(self, x, y):
537 if self._hidden or self._disabled:
540 bs = self._base._buttonSize / 2
541 pos = self._getPixelPos()
542 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
543 self._selection = int((y - pos[1] - bs) / bs)
544 self._imageID = self._imageIDs[self._selection]
545 self._base._focus = None
546 self._comboCallback()
548 return super(glComboButton, self).OnMouseDown(x, y)
550 class glFrame(glGuiContainer):
551 def __init__(self, parent, pos):
552 super(glFrame, self).__init__(parent, pos)
553 self._selected = False
557 def setSelected(self, value):
558 self._selected = value
560 def setHidden(self, value):
563 def getSelected(self):
564 return self._selected
566 def getMinSize(self):
567 return self._base._buttonSize, self._base._buttonSize
569 def _getPixelPos(self):
570 x0, y0, w, h = self.getSize()
577 bs = self._parent._buttonSize
578 pos = self._getPixelPos()
581 glTranslatef(pos[0], pos[1], 0)
582 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
583 glEnable(GL_TEXTURE_2D)
585 size = self._layout.getLayoutSize()
586 glColor4ub(255,255,255,255)
592 # glVertex2f( size[0], 0)
596 # glVertex2f( 0, size[1])
598 # glVertex2f( size[0], size[1])
604 glTexCoord2f(0, tc/2)
606 glTexCoord2f(tc, tc/2)
609 glTexCoord2f(tc+tc, 0)
610 glVertex2f( size[0], 0)
612 glVertex2f( size[0] - bs, 0)
613 glTexCoord2f(tc, tc/2)
614 glVertex2f( size[0] - bs, bs)
615 glTexCoord2f(tc+tc, tc/2)
616 glVertex2f( size[0], bs)
618 glTexCoord2f(tc, tc/2)
619 glVertex2f( bs, size[1] - bs)
620 glTexCoord2f(0, tc/2)
621 glVertex2f( 0, size[1] - bs)
622 glTexCoord2f(0, tc/2+tc/2)
623 glVertex2f( 0, size[1])
624 glTexCoord2f(tc, tc/2+tc/2)
625 glVertex2f( bs, size[1])
627 glTexCoord2f(tc+tc, tc/2)
628 glVertex2f( size[0], size[1] - bs)
629 glTexCoord2f(tc, tc/2)
630 glVertex2f( size[0] - bs, size[1] - bs)
631 glTexCoord2f(tc, tc/2+tc/2)
632 glVertex2f( size[0] - bs, size[1])
633 glTexCoord2f(tc+tc, tc/2+tc/2)
634 glVertex2f( size[0], size[1])
637 glTexCoord2f(tc, tc/2)
638 glVertex2f( size[0]-bs, bs)
639 glTexCoord2f(tc, tc/2)
641 glTexCoord2f(tc, tc/2)
642 glVertex2f( bs, size[1]-bs)
643 glTexCoord2f(tc, tc/2)
644 glVertex2f( size[0]-bs, size[1]-bs)
647 glTexCoord2f(tc+tc, tc/2)
648 glVertex2f( size[0], bs)
649 glTexCoord2f(tc, tc/2)
650 glVertex2f( size[0]-bs, bs)
651 glTexCoord2f(tc, tc/2)
652 glVertex2f( size[0]-bs, size[1]-bs)
653 glTexCoord2f(tc+tc, tc/2)
654 glVertex2f( size[0], size[1]-bs)
657 glTexCoord2f(tc, tc/2)
659 glTexCoord2f(0, tc/2)
661 glTexCoord2f(0, tc/2)
662 glVertex2f( 0, size[1]-bs)
663 glTexCoord2f(tc, tc/2)
664 glVertex2f( bs, size[1]-bs)
668 glVertex2f( size[0]-bs, 0)
671 glTexCoord2f(tc, tc/2)
673 glTexCoord2f(tc, tc/2)
674 glVertex2f( size[0]-bs, bs)
677 glTexCoord2f(tc, tc/2)
678 glVertex2f( size[0]-bs, size[1]-bs)
679 glTexCoord2f(tc, tc/2)
680 glVertex2f( bs, size[1]-bs)
681 glTexCoord2f(tc, tc/2+tc/2)
682 glVertex2f( bs, size[1])
683 glTexCoord2f(tc, tc/2+tc/2)
684 glVertex2f( size[0]-bs, size[1])
687 glDisable(GL_TEXTURE_2D)
689 #Draw the controls on the frame
690 super(glFrame, self).draw()
692 def _checkHit(self, x, y):
695 pos = self._getPixelPos()
696 w, h = self._layout.getLayoutSize()
697 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
699 def OnMouseMotion(self, x, y):
700 super(glFrame, self).OnMouseMotion(x, y)
701 if self._checkHit(x, y):
707 def OnMouseDown(self, x, y):
708 if self._checkHit(x, y):
709 super(glFrame, self).OnMouseDown(x, y)
713 class glLabel(glGuiControl):
714 def __init__(self, parent, label, pos):
716 super(glLabel, self).__init__(parent, pos)
718 def getMinSize(self):
719 w, h = opengl.glGetStringSize(self._label)
722 def _getPixelPos(self):
723 x0, y0, w, h = self.getSize()
727 x, y, w, h = self.getSize()
730 glTranslatef(x, y, 0)
732 # glColor4ub(255,255,255,128)
744 glTranslate(5, h - 5, 0)
745 glColor4ub(255,255,255,255)
746 opengl.glDrawStringLeft(self._label)
749 def _checkHit(self, x, y):
752 def OnMouseMotion(self, x, y):
755 def OnMouseDown(self, x, y):
758 class glNumberCtrl(glGuiControl):
759 def __init__(self, parent, value, pos, callback):
760 self._callback = callback
761 self._value = str(value)
764 self._inCallback = False
765 super(glNumberCtrl, self).__init__(parent, pos)
767 def setValue(self, value):
770 self._value = str(value)
772 def getMinSize(self):
773 w, h = opengl.glGetStringSize("VALUES")
776 def _getPixelPos(self):
777 x0, y0, w, h = self.getSize()
781 x, y, w, h = self.getSize()
784 glTranslatef(x, y, 0)
787 glColor4ub(255,255,255,255)
789 glColor4ub(255,255,255,192)
801 glTranslate(5, h - 5, 0)
802 glColor4ub(0,0,0,255)
803 opengl.glDrawStringLeft(self._value)
805 glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
806 opengl.glDrawStringLeft('|')
809 def _checkHit(self, x, y):
810 x1, y1, w, h = self.getSize()
811 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
813 def OnMouseMotion(self, x, y):
816 def OnMouseDown(self, x, y):
817 if self._checkHit(x, y):
822 def OnKeyChar(self, c):
823 self._inCallback = True
826 self._selectPos = max(0, self._selectPos)
827 if c == wx.WXK_RIGHT:
829 self._selectPos = min(self._selectPos, len(self._value))
832 value = float(self._value)
837 self._value = str(value)
838 self._callback(self._value)
841 value = float(self._value)
847 self._value = str(value)
848 self._callback(self._value)
849 if c == wx.WXK_BACK and self._selectPos > 0:
850 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
852 self._callback(self._value)
853 if c == wx.WXK_DELETE:
854 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
855 self._callback(self._value)
856 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
857 if wx.GetKeyState(wx.WXK_SHIFT):
861 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
862 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
864 self._callback(self._value)
865 self._inCallback = False
868 self._base._focus = self
869 self._selectPos = len(self._value)
872 class glCheckbox(glGuiControl):
873 def __init__(self, parent, value, pos, callback):
874 self._callback = callback
878 self._inCallback = False
879 super(glCheckbox, self).__init__(parent, pos)
881 def setValue(self, value):
884 self._value = str(value)
889 def getMinSize(self):
892 def _getPixelPos(self):
893 x0, y0, w, h = self.getSize()
897 x, y, w, h = self.getSize()
900 glTranslatef(x, y, 0)
902 glColor3ub(255,255,255)
904 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
906 opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
910 def _checkHit(self, x, y):
911 x1, y1, w, h = self.getSize()
912 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
914 def OnMouseMotion(self, x, y):
917 def OnMouseDown(self, x, y):
918 if self._checkHit(x, y):
919 self._value = not self._value
923 class glSlider(glGuiControl):
924 def __init__(self, parent, value, minValue, maxValue, pos, callback):
925 super(glSlider, self).__init__(parent, pos)
926 self._callback = callback
930 self._minValue = minValue
931 self._maxValue = maxValue
933 def setValue(self, value):
935 self._value = max(self._minValue, self._value)
936 self._value = min(self._maxValue, self._value)
941 def setRange(self, minValue, maxValue):
942 if maxValue < minValue:
944 self._minValue = minValue
945 self._maxValue = maxValue
946 self._value = max(minValue, self._value)
947 self._value = min(maxValue, self._value)
949 def getMinValue(self):
950 return self._minValue
952 def getMaxValue(self):
953 return self._maxValue
955 def setHidden(self, value):
958 def getMinSize(self):
959 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
961 def _getPixelPos(self):
962 x0, y0, w, h = self.getSize()
963 minSize = self.getMinSize()
964 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
970 w, h = self.getMinSize()
971 pos = self._getPixelPos()
974 glTranslatef(pos[0], pos[1], 0)
975 glDisable(GL_TEXTURE_2D)
977 glColor4ub(60,60,60,255)
979 glColor4ub(60,60,60,192)
981 glVertex2f( w/2,-h/2)
982 glVertex2f(-w/2,-h/2)
983 glVertex2f(-w/2, h/2)
984 glVertex2f( w/2, h/2)
987 glTranslate(0.0,scrollLength/2,0)
989 glColor4ub(0,0,0,255)
991 glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
992 opengl.glDrawStringRight(str(self._minValue))
993 glTranslate(0,-scrollLength,0)
994 opengl.glDrawStringRight(str(self._maxValue))
995 if self._maxValue-self._minValue > 0:
996 glTranslate(w,scrollLength-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
997 opengl.glDrawStringLeft(str(self._value))
999 glColor4ub(255,255,255,240)
1000 if self._maxValue - self._minValue != 0:
1001 glTranslate(0.0,-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
1003 glVertex2f( w/2,-w/2)
1004 glVertex2f(-w/2,-w/2)
1005 glVertex2f(-w/2, w/2)
1006 glVertex2f( w/2, w/2)
1010 def _checkHit(self, x, y):
1013 pos = self._getPixelPos()
1014 w, h = self.getMinSize()
1015 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1018 self._base._focus = self
1021 def OnMouseMotion(self, x, y):
1023 w, h = self.getMinSize()
1024 scrollLength = h - w
1025 pos = self._getPixelPos()
1026 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1029 if self._checkHit(x, y):
1035 def OnMouseDown(self, x, y):
1036 if self._checkHit(x, y):
1038 self.OnMouseMotion(x, y)
1042 def OnMouseUp(self, x, y):
1044 self._base._focus = None