1 from __future__ import absolute_import
2 from __future__ import division
7 from wx import glcanvas
9 OpenGL.ERROR_CHECKING = False
10 from OpenGL.GL import *
12 from Cura.gui.util import opengl
14 class glGuiControl(object):
15 def __init__(self, parent, pos):
17 self._base = parent._base
19 self._size = (0,0, 1, 1)
20 self._parent.add(self)
22 def setSize(self, x, y, w, h):
23 self._size = (x, y, w, h)
31 def updateLayout(self):
35 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
36 if self._parent._glGuiControlList[n].setFocus():
38 for n in xrange(0, self._parent._glGuiControlList.index(self)):
39 if self._parent._glGuiControlList[n].setFocus():
42 def focusPrevious(self):
43 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
44 if self._parent._glGuiControlList[n].setFocus():
46 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
47 if self._parent._glGuiControlList[n].setFocus():
54 return self._base._focus == self
56 def OnMouseUp(self, x, y):
59 def OnKeyChar(self, key):
62 class glGuiContainer(glGuiControl):
63 def __init__(self, parent, pos):
64 self._glGuiControlList = []
65 glGuiLayoutButtons(self)
66 super(glGuiContainer, self).__init__(parent, pos)
69 self._glGuiControlList.append(ctrl)
72 def OnMouseDown(self, x, y):
73 for ctrl in self._glGuiControlList:
74 if ctrl.OnMouseDown(x, y):
78 def OnMouseUp(self, x, y):
79 for ctrl in self._glGuiControlList:
80 if ctrl.OnMouseUp(x, y):
84 def OnMouseMotion(self, x, y):
86 for ctrl in self._glGuiControlList:
87 if ctrl.OnMouseMotion(x, y):
92 for ctrl in self._glGuiControlList:
95 def updateLayout(self):
97 for ctrl in self._glGuiControlList:
100 class glGuiPanel(glcanvas.GLCanvas):
101 def __init__(self, parent):
102 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8)
103 glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList)
106 self._container = None
107 self._container = glGuiContainer(self, (0,0))
109 self._context = glcanvas.GLContext(self)
110 self._glButtonsTexture = None
111 self._buttonSize = 64
113 wx.EVT_PAINT(self, self._OnGuiPaint)
114 wx.EVT_SIZE(self, self._OnSize)
115 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
116 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseLeftDown)
117 wx.EVT_LEFT_UP(self, self._OnGuiMouseLeftUp)
118 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
119 wx.EVT_CHAR(self, self.OnKeyChar)
120 wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
122 def OnKeyChar(self, e):
123 if self._focus is not None:
124 self._focus.OnKeyChar(e.GetKeyCode())
127 def OnFocusLost(self, e):
131 def _OnGuiMouseLeftDown(self,e):
133 if self._container.OnMouseDown(e.GetX(), e.GetY()):
136 self.OnMouseLeftDown(e)
137 def _OnGuiMouseLeftUp(self, e):
138 if self._container.OnMouseUp(e.GetX(), e.GetY()):
141 self.OnMouseLeftUp(e)
143 def _OnGuiMouseMotion(self,e):
145 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
146 self.OnMouseMotion(e)
148 def _OnGuiPaint(self, e):
149 h = self.GetSize().GetHeight()
150 w = self.GetSize().GetWidth()
151 oldButtonSize = self._buttonSize
155 self._buttonSize = 32
157 self._buttonSize = 48
159 self._buttonSize = 64
161 self._buttonSize = 80
163 self._buttonSize = 96
164 if self._buttonSize != oldButtonSize:
165 self._container.updateLayout()
167 dc = wx.PaintDC(self)
168 self.SetCurrent(self._context)
175 if self._glButtonsTexture is None:
176 self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
178 glDisable(GL_DEPTH_TEST)
180 glDisable(GL_LIGHTING)
181 glColor4ub(255,255,255,255)
183 glMatrixMode(GL_PROJECTION)
185 size = self.GetSize()
186 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
187 glMatrixMode(GL_MODELVIEW)
190 self._container.draw()
192 def _OnEraseBackground(self,event):
193 #Workaround for windows background redraw flicker.
197 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
198 self._container.updateLayout()
201 def OnMouseLeftDown(self,e):
203 def OnMouseLeftUp(self,e):
205 def OnMouseMotion(self, e):
207 def OnPaint(self, e):
211 if self._container is not None:
212 self._container.add(ctrl)
214 class glGuiLayoutButtons(object):
215 def __init__(self, parent):
216 self._parent = parent
217 self._parent._layout = self
220 bs = self._parent._base._buttonSize
221 x0, y0, w, h = self._parent.getSize()
223 for ctrl in self._parent._glGuiControlList:
226 x = w + pos[0] * gridSize - bs * 0.2
228 x = pos[0] * gridSize + bs * 0.2
230 y = h + pos[1] * gridSize - bs * 0.2
232 y = pos[1] * gridSize + bs * 0.2
233 ctrl.setSize(x, y, gridSize, gridSize)
235 def getLayoutSize(self):
236 _, _, w, h = self._parent.getSize()
239 class glGuiLayoutGrid(object):
240 def __init__(self, parent):
241 self._parent = parent
242 self._parent._layout = self
246 borderSize = self._parent._base._buttonSize * 0.2
247 x0, y0, w, h = self._parent.getSize()
252 for ctrl in self._parent._glGuiControlList:
254 w, h = ctrl.getMinSize()
258 widths[x] = max(widths[x], w)
262 heights[y] = max(heights[y], h)
263 for ctrl in self._parent._glGuiControlList:
267 for n in xrange(0, x):
271 for n in xrange(0, y):
275 ctrl.setSize(x1, y1, widths[x], heights[y])
276 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
278 def getLayoutSize(self):
281 class glButton(glGuiControl):
282 def __init__(self, parent, imageID, tooltip, pos, callback):
283 super(glButton, self).__init__(parent, pos)
284 self._tooltip = tooltip
285 self._parent = parent
286 self._imageID = imageID
287 self._callback = callback
288 self._selected = False
291 self._disabled = False
293 def setSelected(self, value):
294 self._selected = value
296 def setHidden(self, value):
299 def setDisabled(self, value):
300 self._disabled = value
302 def getSelected(self):
303 return self._selected
305 def getMinSize(self):
306 return self._base._buttonSize, self._base._buttonSize
308 def _getPixelPos(self):
309 x0, y0, w, h = self.getSize()
310 return x0 + w / 2, y0 + h / 2
316 cx = (self._imageID % 4) / 4
317 cy = int(self._imageID / 4) / 4
318 bs = self._base._buttonSize
319 pos = self._getPixelPos()
322 glTranslatef(pos[0], pos[1], 0)
323 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
324 glEnable(GL_TEXTURE_2D)
330 glScalef(bs * scale, bs * scale, bs * scale)
332 glColor4ub(128,128,128,128)
334 glColor4ub(255,255,255,255)
336 glTexCoord2f(cx+0.25, cy)
337 glVertex2f( 0.5,-0.5)
339 glVertex2f(-0.5,-0.5)
340 glTexCoord2f(cx, cy+0.25)
341 glVertex2f(-0.5, 0.5)
342 glTexCoord2f(cx+0.25, cy+0.25)
343 glVertex2f( 0.5, 0.5)
345 glDisable(GL_TEXTURE_2D)
347 glColor4ub(0,0,0,255)
348 glTranslatef(0, -0.55, 0)
349 opengl.glDrawStringCenter(self._tooltip)
352 def _checkHit(self, x, y):
353 if self._hidden or self._disabled:
355 bs = self.getMinSize()[0]
356 pos = self._getPixelPos()
357 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
359 def OnMouseMotion(self, x, y):
360 if self._checkHit(x, y):
366 def OnMouseDown(self, x, y):
367 if self._checkHit(x, y):
372 class glRadioButton(glButton):
373 def __init__(self, parent, imageID, tooltip, pos, group, callback):
374 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
376 self._radioCallback = callback
377 self._group.append(self)
379 def setSelected(self, value):
380 self._selected = value
382 def _onRadioSelect(self):
383 self._base._focus = None
384 for ctrl in self._group:
386 ctrl.setSelected(False)
387 if self.getSelected():
388 self.setSelected(False)
390 self.setSelected(True)
391 self._radioCallback()
393 class glComboButton(glButton):
394 def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
395 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
396 self._imageIDs = imageIDs
397 self._tooltips = tooltips
398 self._comboCallback = callback
401 def _onComboOpenSelect(self):
402 self._base._focus = self
407 self._selected = self.hasFocus()
408 super(glComboButton, self).draw()
409 if not self._selected:
412 bs = self._base._buttonSize / 2
413 pos = self._getPixelPos()
416 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
417 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
419 for n in xrange(0, len(self._imageIDs)):
420 cx = (self._imageIDs[n] % 4) / 4
421 cy = int(self._imageIDs[n] / 4) / 4
422 glEnable(GL_TEXTURE_2D)
423 glTranslatef(0, 1, 0)
425 glColor4ub(128,128,128,128)
427 glColor4ub(255,255,255,255)
429 glTexCoord2f(cx+0.25, cy)
430 glVertex2f( 0.5,-0.5)
432 glVertex2f(-0.5,-0.5)
433 glTexCoord2f(cx, cy+0.25)
434 glVertex2f(-0.5, 0.5)
435 glTexCoord2f(cx+0.25, cy+0.25)
436 glVertex2f( 0.5, 0.5)
438 glDisable(GL_TEXTURE_2D)
441 glColor4ub(0,0,0,255)
442 glTranslatef(-0.55, 0.1, 0)
443 opengl.glDrawStringRight(self._tooltips[n])
448 return self._selection
450 def setValue(self, value):
451 self._selection = value
452 self._comboCallback()
454 def OnMouseDown(self, x, y):
455 if self._hidden or self._disabled:
458 bs = self._base._buttonSize / 2
459 pos = self._getPixelPos()
460 if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
461 self._selection = int((y - pos[1] - bs) / bs)
462 self._imageID = self._imageIDs[self._selection]
463 self._base._focus = None
464 self._comboCallback()
466 return super(glComboButton, self).OnMouseDown(x, y)
468 class glFrame(glGuiContainer):
469 def __init__(self, parent, pos):
470 super(glFrame, self).__init__(parent, pos)
471 self._selected = False
475 def setSelected(self, value):
476 self._selected = value
478 def setHidden(self, value):
481 def getSelected(self):
482 return self._selected
484 def getMinSize(self):
485 return self._base._buttonSize, self._base._buttonSize
487 def _getPixelPos(self):
488 x0, y0, w, h = self.getSize()
495 bs = self._parent._buttonSize
496 pos = self._getPixelPos()
499 glTranslatef(pos[0], pos[1], 0)
500 glBindTexture(GL_TEXTURE_2D, self._parent._glButtonsTexture)
501 glEnable(GL_TEXTURE_2D)
503 size = self._layout.getLayoutSize()
504 glColor4ub(255,255,255,128)
507 glVertex2f( size[0], 0)
511 glVertex2f( 0, size[1])
513 glVertex2f( size[0], size[1])
515 glDisable(GL_TEXTURE_2D)
517 #Draw the controls on the frame
518 super(glFrame, self).draw()
520 def _checkHit(self, x, y):
523 pos = self._getPixelPos()
524 w, h = self._layout.getLayoutSize()
525 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
527 def OnMouseMotion(self, x, y):
528 super(glFrame, self).OnMouseMotion(x, y)
529 if self._checkHit(x, y):
535 def OnMouseDown(self, x, y):
536 if self._checkHit(x, y):
537 super(glFrame, self).OnMouseDown(x, y)
541 class glLabel(glGuiControl):
542 def __init__(self, parent, label, pos):
544 super(glLabel, self).__init__(parent, pos)
546 def getMinSize(self):
547 w, h = opengl.glGetStringSize(self._label)
550 def _getPixelPos(self):
551 x0, y0, w, h = self.getSize()
555 x, y, w, h = self.getSize()
558 glTranslatef(x, y, 0)
560 glColor4ub(255,255,255,128)
572 glTranslate(5, h - 5, 0)
573 glColor4ub(0,0,0,255)
574 opengl.glDrawStringLeft(self._label)
577 def _checkHit(self, x, y):
580 def OnMouseMotion(self, x, y):
583 def OnMouseDown(self, x, y):
586 class glNumberCtrl(glGuiControl):
587 def __init__(self, parent, value, pos, callback):
588 self._callback = callback
589 self._value = str(value)
592 self._inCallback = False
593 super(glNumberCtrl, self).__init__(parent, pos)
595 def setValue(self, value):
598 self._value = str(value)
600 def getMinSize(self):
601 w, h = opengl.glGetStringSize("VALUES")
604 def _getPixelPos(self):
605 x0, y0, w, h = self.getSize()
609 x, y, w, h = self.getSize()
612 glTranslatef(x, y, 0)
615 glColor4ub(255,255,255,255)
617 glColor4ub(255,255,255,192)
629 glTranslate(5, h - 5, 0)
630 glColor4ub(0,0,0,255)
631 opengl.glDrawStringLeft(self._value)
633 glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
634 opengl.glDrawStringLeft('|')
637 def _checkHit(self, x, y):
638 x1, y1, w, h = self.getSize()
639 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
641 def OnMouseMotion(self, x, y):
644 def OnMouseDown(self, x, y):
645 if self._checkHit(x, y):
650 def OnKeyChar(self, c):
651 self._inCallback = True
654 self._selectPos = max(0, self._selectPos)
655 if c == wx.WXK_RIGHT:
657 self._selectPos = min(self._selectPos, len(self._value))
660 value = float(self._value)
665 self._value = str(value)
666 self._callback(self._value)
669 value = float(self._value)
675 self._value = str(value)
676 self._callback(self._value)
677 if c == wx.WXK_BACK and self._selectPos > 0:
678 self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
680 self._callback(self._value)
681 if c == wx.WXK_DELETE:
682 self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
683 self._callback(self._value)
685 if wx.GetKeyState(wx.WXK_SHIFT):
689 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
690 self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
692 self._callback(self._value)
693 self._inCallback = False
696 self._base._focus = self
697 self._selectPos = len(self._value)
700 class glCheckbox(glGuiControl):
701 def __init__(self, parent, value, pos, callback):
702 self._callback = callback
706 self._inCallback = False
707 super(glCheckbox, self).__init__(parent, pos)
709 def setValue(self, value):
712 self._value = str(value)
717 def getMinSize(self):
720 def _getPixelPos(self):
721 x0, y0, w, h = self.getSize()
725 x, y, w, h = self.getSize()
728 glTranslatef(x, y, 0)
731 glColor4ub(0,255,0,255)
733 glColor4ub(255,0,0,255)
747 def _checkHit(self, x, y):
748 x1, y1, w, h = self.getSize()
749 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
751 def OnMouseMotion(self, x, y):
754 def OnMouseDown(self, x, y):
755 if self._checkHit(x, y):
756 self._value = not self._value
760 class glSlider(glGuiControl):
761 def __init__(self, parent, value, minValue, maxValue, pos, callback):
762 super(glSlider, self).__init__(parent, pos)
763 self._callback = callback
767 self._minValue = minValue
768 self._maxValue = maxValue
770 def setValue(self, value):
772 self._value = max(self._minValue, self._value)
773 self._value = min(self._maxValue, self._value)
778 def setRange(self, minValue, maxValue):
779 self._minValue = minValue
780 self._maxValue = maxValue
781 self._value = max(minValue, self._value)
782 self._value = min(maxValue, self._value)
784 def getMinValue(self):
785 return self._minValue
787 def getMaxValue(self):
788 return self._maxValue
790 def setHidden(self, value):
793 def getMinSize(self):
794 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
796 def _getPixelPos(self):
797 x0, y0, w, h = self.getSize()
798 minSize = self.getMinSize()
799 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
805 w, h = self.getMinSize()
806 pos = self._getPixelPos()
809 glTranslatef(pos[0], pos[1], 0)
810 glDisable(GL_TEXTURE_2D)
812 glColor4ub(32,32,32,255)
814 glColor4ub(32,32,32,192)
816 glVertex2f( w/2,-h/2)
817 glVertex2f(-w/2,-h/2)
818 glVertex2f(-w/2, h/2)
819 glVertex2f( w/2, h/2)
822 glTranslate(0.0,scrollLength/2,0)
824 glColor4ub(0,0,0,255)
826 glTranslate(-w/2,0,0)
827 opengl.glDrawStringRight(str(self._minValue))
828 glTranslate(0,-scrollLength,0)
829 opengl.glDrawStringRight(str(self._maxValue))
830 glTranslate(w,scrollLength-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
831 opengl.glDrawStringLeft(str(self._value))
833 glColor4ub(255,255,255,240)
834 glTranslate(0.0,-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
836 glVertex2f( w/2,-w/2)
837 glVertex2f(-w/2,-w/2)
838 glVertex2f(-w/2, w/2)
839 glVertex2f( w/2, w/2)
843 def _checkHit(self, x, y):
846 pos = self._getPixelPos()
847 w, h = self.getMinSize()
848 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
851 self._base._focus = self
854 def OnMouseMotion(self, x, y):
856 w, h = self.getMinSize()
858 pos = self._getPixelPos()
859 self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
862 if self._checkHit(x, y):
868 def OnMouseDown(self, x, y):
869 if self._checkHit(x, y):
871 self.OnMouseMotion(x, y)
875 def OnMouseUp(self, x, y):
877 self._base._focus = None