chiark / gitweb /
Show a plane in the 3D window during printing to show the printing process.
[cura.git] / Cura / gui / util / openglGui.py
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"
4
5 import wx
6 import traceback
7 import sys
8 import os
9 import time
10
11 from wx import glcanvas
12 import OpenGL
13 OpenGL.ERROR_CHECKING = False
14 from OpenGL.GL import *
15
16 from Cura.util import version
17 from Cura.gui.util import opengl
18
19 class animation(object):
20         def __init__(self, gui, start, end, runTime):
21                 self._start = start
22                 self._end = end
23                 self._startTime = time.time()
24                 self._runTime = runTime
25                 gui._animationList.append(self)
26
27         def isDone(self):
28                 return time.time() > self._startTime + self._runTime
29
30         def getPosition(self):
31                 if self.isDone():
32                         return self._end
33                 f = (time.time() - self._startTime) / self._runTime
34                 ts = f*f
35                 tc = f*f*f
36                 #f = 6*tc*ts + -15*ts*ts + 10*tc
37                 f = tc + -3*ts + 3*f
38                 return self._start + (self._end - self._start) * f
39
40 class glGuiControl(object):
41         def __init__(self, parent, pos):
42                 self._parent = parent
43                 self._base = parent._base
44                 self._pos = pos
45                 self._size = (0,0, 1, 1)
46                 self._parent.add(self)
47
48         def setSize(self, x, y, w, h):
49                 self._size = (x, y, w, h)
50
51         def getSize(self):
52                 return self._size
53
54         def getMinSize(self):
55                 return 1, 1
56
57         def updateLayout(self):
58                 pass
59
60         def focusNext(self):
61                 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
62                         if self._parent._glGuiControlList[n].setFocus():
63                                 return
64                 for n in xrange(0, self._parent._glGuiControlList.index(self)):
65                         if self._parent._glGuiControlList[n].setFocus():
66                                 return
67
68         def focusPrevious(self):
69                 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
70                         if self._parent._glGuiControlList[n].setFocus():
71                                 return
72                 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
73                         if self._parent._glGuiControlList[n].setFocus():
74                                 return
75
76         def setFocus(self):
77                 return False
78
79         def hasFocus(self):
80                 return self._base._focus == self
81
82         def OnMouseUp(self, x, y):
83                 pass
84
85         def OnKeyChar(self, key):
86                 pass
87
88 class glGuiContainer(glGuiControl):
89         def __init__(self, parent, pos):
90                 self._glGuiControlList = []
91                 glGuiLayoutButtons(self)
92                 super(glGuiContainer, self).__init__(parent, pos)
93
94         def add(self, ctrl):
95                 self._glGuiControlList.append(ctrl)
96                 self.updateLayout()
97
98         def OnMouseDown(self, x, y, button):
99                 for ctrl in self._glGuiControlList:
100                         if ctrl.OnMouseDown(x, y, button):
101                                 return True
102                 return False
103
104         def OnMouseUp(self, x, y):
105                 for ctrl in self._glGuiControlList:
106                         if ctrl.OnMouseUp(x, y):
107                                 return True
108                 return False
109
110         def OnMouseMotion(self, x, y):
111                 handled = False
112                 for ctrl in self._glGuiControlList:
113                         if ctrl.OnMouseMotion(x, y):
114                                 handled = True
115                 return handled
116
117         def draw(self):
118                 for ctrl in self._glGuiControlList:
119                         ctrl.draw()
120
121         def updateLayout(self):
122                 self._layout.update()
123                 for ctrl in self._glGuiControlList:
124                         ctrl.updateLayout()
125
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)
130                 self._base = self
131                 self._focus = None
132                 self._container = None
133                 self._container = glGuiContainer(self, (0,0))
134                 self._shownError = False
135
136                 self._context = glcanvas.GLContext(self)
137                 self._glButtonsTexture = None
138                 self._glRobotTexture = None
139                 self._buttonSize = 64
140
141                 self._animationList = []
142                 self.glReleaseList = []
143                 self._refreshQueued = False
144                 self._idleCalled = False
145
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)
162
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:
168                                 if anim.isDone():
169                                         self._animationList.remove(anim)
170                         self.Refresh()
171
172         def _OnGuiKeyChar(self, e):
173                 if self._focus is not None:
174                         self._focus.OnKeyChar(e.GetKeyCode())
175                         self.Refresh()
176                 else:
177                         self.OnKeyChar(e.GetKeyCode())
178
179         def OnFocusLost(self, e):
180                 self._focus = None
181                 self.Refresh()
182
183         def _OnGuiMouseDown(self,e):
184                 self.SetFocus()
185                 if self._container.OnMouseDown(e.GetX(), e.GetY(), e.GetButton()):
186                         self.Refresh()
187                         return
188                 self.OnMouseDown(e)
189
190         def _OnGuiMouseUp(self, e):
191                 if self._container.OnMouseUp(e.GetX(), e.GetY()):
192                         self.Refresh()
193                         return
194                 self.OnMouseUp(e)
195
196         def _OnGuiMouseMotion(self,e):
197                 self.Refresh()
198                 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
199                         self.OnMouseMotion(e)
200
201         def _OnGuiPaint(self, e):
202                 self._idleCalled = False
203                 h = self.GetSize().GetHeight()
204                 w = self.GetSize().GetWidth()
205                 oldButtonSize = self._buttonSize
206                 if h / 3 < w / 4:
207                         w = h * 4 / 3
208                 if w < 64 * 8:
209                         self._buttonSize = 32
210                 elif w < 64 * 10:
211                         self._buttonSize = 48
212                 elif w < 64 * 15:
213                         self._buttonSize = 64
214                 elif w < 64 * 20:
215                         self._buttonSize = 80
216                 else:
217                         self._buttonSize = 96
218                 if self._buttonSize != oldButtonSize:
219                         self._container.updateLayout()
220
221                 dc = wx.PaintDC(self)
222                 try:
223                         self.SetCurrent(self._context)
224                         for obj in self.glReleaseList:
225                                 obj.release()
226                         del self.glReleaseList[:]
227                         renderStartTime = time.time()
228                         self.OnPaint(e)
229                         self._drawGui()
230                         glFlush()
231                         if version.isDevVersion():
232                                 renderTime = time.time() - renderStartTime
233                                 glLoadIdentity()
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))
237                         self.SwapBuffers()
238                 except:
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):
243                                 locationInfo = tb[n]
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
248
249         def _drawGui(self):
250                 if self._glButtonsTexture is None:
251                         self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
252                         self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
253
254                 glDisable(GL_DEPTH_TEST)
255                 glEnable(GL_BLEND)
256                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
257                 glDisable(GL_LIGHTING)
258                 glColor4ub(255,255,255,255)
259
260                 glMatrixMode(GL_PROJECTION)
261                 glLoadIdentity()
262                 size = self.GetSize()
263                 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
264                 glMatrixMode(GL_MODELVIEW)
265                 glLoadIdentity()
266
267                 self._container.draw()
268
269                 glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
270                 glEnable(GL_TEXTURE_2D)
271                 glPushMatrix()
272                 glColor4f(1,1,1,1)
273                 glTranslate(size.GetWidth() - 8,size.GetHeight() - 32,0)
274                 s = self._buttonSize * 1.4
275                 glScale(s,s,s)
276                 glBegin(GL_QUADS)
277                 glTexCoord2f(1, 0)
278                 glVertex2f(0,-1)
279                 glTexCoord2f(0, 0)
280                 glVertex2f(-1,-1)
281                 glTexCoord2f(0, 1)
282                 glVertex2f(-1, 0)
283                 glTexCoord2f(1, 1)
284                 glVertex2f(0, 0)
285                 glEnd()
286                 glDisable(GL_TEXTURE_2D)
287                 glPopMatrix()
288
289         def _OnEraseBackground(self,event):
290                 #Workaround for windows background redraw flicker.
291                 pass
292
293         def _OnSize(self,e):
294                 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
295                 self._container.updateLayout()
296                 self.Refresh()
297
298         def OnMouseDown(self,e):
299                 pass
300         def OnMouseUp(self,e):
301                 pass
302         def OnMouseMotion(self, e):
303                 pass
304         def OnKeyChar(self, keyCode):
305                 pass
306         def OnPaint(self, e):
307                 pass
308         def OnKeyChar(self, keycode):
309                 pass
310
311         def QueueRefresh(self):
312                 wx.CallAfter(self._queueRefresh)
313
314         def _queueRefresh(self):
315                 if self._idleCalled:
316                         wx.CallAfter(self.Refresh)
317                 else:
318                         self._refreshQueued = True
319
320         def add(self, ctrl):
321                 if self._container is not None:
322                         self._container.add(ctrl)
323
324 class glGuiLayoutButtons(object):
325         def __init__(self, parent):
326                 self._parent = parent
327                 self._parent._layout = self
328
329         def update(self):
330                 bs = self._parent._base._buttonSize
331                 x0, y0, w, h = self._parent.getSize()
332                 gridSize = bs * 1.0
333                 for ctrl in self._parent._glGuiControlList:
334                         pos = ctrl._pos
335                         if pos[0] < 0:
336                                 x = w + pos[0] * gridSize - bs * 0.2
337                         else:
338                                 x = pos[0] * gridSize + bs * 0.2
339                         if pos[1] < 0:
340                                 y = h + pos[1] * gridSize * 1.2 - bs * 0.2
341                         else:
342                                 y = pos[1] * gridSize * 1.2 + bs * 0.2
343                         ctrl.setSize(x, y, gridSize, gridSize)
344
345         def getLayoutSize(self):
346                 _, _, w, h = self._parent.getSize()
347                 return w, h
348
349 class glGuiLayoutGrid(object):
350         def __init__(self, parent):
351                 self._parent = parent
352                 self._parent._layout = self
353                 self._size = 0,0
354                 self._alignBottom = True
355
356         def update(self):
357                 borderSize = self._parent._base._buttonSize * 0.2
358                 x0, y0, w, h = self._parent.getSize()
359                 x0 += borderSize
360                 y0 += borderSize
361                 widths = {}
362                 heights = {}
363                 for ctrl in self._parent._glGuiControlList:
364                         x, y = ctrl._pos
365                         w, h = ctrl.getMinSize()
366                         if not x in widths:
367                                 widths[x] = w
368                         else:
369                                 widths[x] = max(widths[x], w)
370                         if not y in heights:
371                                 heights[y] = h
372                         else:
373                                 heights[y] = max(heights[y], h)
374                 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
375                 if self._alignBottom:
376                         y0 -= self._size[1] - self._parent.getSize()[3]
377                         self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
378                 for ctrl in self._parent._glGuiControlList:
379                         x, y = ctrl._pos
380                         x1 = x0
381                         y1 = y0
382                         for n in xrange(0, x):
383                                 if not n in widths:
384                                         widths[n] = 3
385                                 x1 += widths[n]
386                         for n in xrange(0, y):
387                                 if not n in heights:
388                                         heights[n] = 3
389                                 y1 += heights[n]
390                         ctrl.setSize(x1, y1, widths[x], heights[y])
391
392         def getLayoutSize(self):
393                 return self._size
394
395 class glButton(glGuiControl):
396         def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
397                 self._buttonSize = size
398                 self._hidden = False
399                 super(glButton, self).__init__(parent, pos)
400                 self._tooltip = tooltip
401                 self._parent = parent
402                 self._imageID = imageID
403                 self._callback = callback
404                 self._selected = False
405                 self._focus = False
406                 self._disabled = False
407                 self._showExpandArrow = False
408                 self._progressBar = None
409                 self._altTooltip = ''
410
411         def setSelected(self, value):
412                 self._selected = value
413
414         def setExpandArrow(self, value):
415                 self._showExpandArrow = value
416
417         def setHidden(self, value):
418                 self._hidden = value
419
420         def setDisabled(self, value):
421                 self._disabled = value
422
423         def setProgressBar(self, value):
424                 self._progressBar = value
425
426         def getProgressBar(self):
427                 return self._progressBar
428
429         def setBottomText(self, value):
430                 self._altTooltip = value
431
432         def getSelected(self):
433                 return self._selected
434
435         def getMinSize(self):
436                 if self._hidden:
437                         return 0, 0
438                 if self._buttonSize is not None:
439                         return self._buttonSize, self._buttonSize
440                 return self._base._buttonSize, self._base._buttonSize
441
442         def _getPixelPos(self):
443                 x0, y0, w, h = self.getSize()
444                 return x0 + w / 2, y0 + h / 2
445
446         def draw(self):
447                 if self._hidden:
448                         return
449
450                 cx = (self._imageID % 4) / 4
451                 cy = int(self._imageID / 4) / 4
452                 bs = self.getMinSize()[0]
453                 pos = self._getPixelPos()
454
455                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
456                 scale = 0.8
457                 if self._selected:
458                         scale = 1.0
459                 elif self._focus:
460                         scale = 0.9
461                 if self._disabled:
462                         glColor4ub(128,128,128,128)
463                 else:
464                         glColor4ub(255,255,255,255)
465                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
466                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
467                 if self._showExpandArrow:
468                         if self._selected:
469                                 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)
470                         else:
471                                 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)
472                 glPushMatrix()
473                 glTranslatef(pos[0], pos[1], 0)
474                 glDisable(GL_TEXTURE_2D)
475                 if self._focus:
476                         glTranslatef(0, -0.55*bs*scale, 0)
477
478                         glPushMatrix()
479                         glColor4ub(60,60,60,255)
480                         glTranslatef(-1, -1, 0)
481                         opengl.glDrawStringCenter(self._tooltip)
482                         glTranslatef(0, 2, 0)
483                         opengl.glDrawStringCenter(self._tooltip)
484                         glTranslatef(2, 0, 0)
485                         opengl.glDrawStringCenter(self._tooltip)
486                         glTranslatef(0, -2, 0)
487                         opengl.glDrawStringCenter(self._tooltip)
488                         glPopMatrix()
489
490                         glColor4ub(255,255,255,255)
491                         opengl.glDrawStringCenter(self._tooltip)
492                 glPopMatrix()
493                 progress = self._progressBar
494                 if progress is not None:
495                         glColor4ub(60,60,60,255)
496                         opengl.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
497                         glColor4ub(255,255,255,255)
498                         opengl.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
499                 elif len(self._altTooltip) > 0:
500                         glPushMatrix()
501                         glTranslatef(pos[0], pos[1], 0)
502                         glTranslatef(0.6*bs*scale, 0, 0)
503
504                         glPushMatrix()
505                         glColor4ub(60,60,60,255)
506                         glTranslatef(-1, -1, 0)
507                         opengl.glDrawStringLeft(self._altTooltip)
508                         glTranslatef(0, 2, 0)
509                         opengl.glDrawStringLeft(self._altTooltip)
510                         glTranslatef(2, 0, 0)
511                         opengl.glDrawStringLeft(self._altTooltip)
512                         glTranslatef(0, -2, 0)
513                         opengl.glDrawStringLeft(self._altTooltip)
514                         glPopMatrix()
515
516                         glColor4ub(255,255,255,255)
517                         opengl.glDrawStringLeft(self._altTooltip)
518                         glPopMatrix()
519
520         def _checkHit(self, x, y):
521                 if self._hidden or self._disabled:
522                         return False
523                 bs = self.getMinSize()[0]
524                 pos = self._getPixelPos()
525                 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
526
527         def OnMouseMotion(self, x, y):
528                 if self._checkHit(x, y):
529                         self._focus = True
530                         return True
531                 self._focus = False
532                 return False
533
534         def OnMouseDown(self, x, y, button):
535                 if self._checkHit(x, y):
536                         self._callback(button)
537                         return True
538                 return False
539
540 class glRadioButton(glButton):
541         def __init__(self, parent, imageID, tooltip, pos, group, callback):
542                 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
543                 self._group = group
544                 self._radioCallback = callback
545                 self._group.append(self)
546
547         def setSelected(self, value):
548                 self._selected = value
549
550         def _onRadioSelect(self, button):
551                 self._base._focus = None
552                 for ctrl in self._group:
553                         if ctrl != self:
554                                 ctrl.setSelected(False)
555                 if self.getSelected():
556                         self.setSelected(False)
557                 else:
558                         self.setSelected(True)
559                 self._radioCallback(button)
560
561 class glComboButton(glButton):
562         def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
563                 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
564                 self._imageIDs = imageIDs
565                 self._tooltips = tooltips
566                 self._comboCallback = callback
567                 self._selection = 0
568
569         def _onComboOpenSelect(self, button):
570                 if self.hasFocus():
571                         self._base._focus = None
572                 else:
573                         self._base._focus = self
574
575         def draw(self):
576                 if self._hidden:
577                         return
578                 self._selected = self.hasFocus()
579                 super(glComboButton, self).draw()
580
581                 bs = self._base._buttonSize / 2
582                 pos = self._getPixelPos()
583
584                 if not self._selected:
585                         return
586
587                 glPushMatrix()
588                 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
589                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
590                 for n in xrange(0, len(self._imageIDs)):
591                         glTranslatef(0, bs, 0)
592                         glColor4ub(255,255,255,255)
593                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
594                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
595                         glDisable(GL_TEXTURE_2D)
596
597                         glPushMatrix()
598                         glTranslatef(-0.55*bs, 0.1*bs, 0)
599
600                         glPushMatrix()
601                         glColor4ub(60,60,60,255)
602                         glTranslatef(-1, -1, 0)
603                         opengl.glDrawStringRight(self._tooltips[n])
604                         glTranslatef(0, 2, 0)
605                         opengl.glDrawStringRight(self._tooltips[n])
606                         glTranslatef(2, 0, 0)
607                         opengl.glDrawStringRight(self._tooltips[n])
608                         glTranslatef(0, -2, 0)
609                         opengl.glDrawStringRight(self._tooltips[n])
610                         glPopMatrix()
611
612                         glColor4ub(255,255,255,255)
613                         opengl.glDrawStringRight(self._tooltips[n])
614                         glPopMatrix()
615                 glPopMatrix()
616
617         def getValue(self):
618                 return self._selection
619
620         def setValue(self, value):
621                 self._selection = value
622                 self._imageID = self._imageIDs[self._selection]
623                 self._comboCallback()
624
625         def OnMouseDown(self, x, y, button):
626                 if self._hidden or self._disabled:
627                         return False
628                 if self.hasFocus():
629                         bs = self._base._buttonSize / 2
630                         pos = self._getPixelPos()
631                         if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
632                                 self._selection = int((y - pos[1] - bs) / bs)
633                                 self._imageID = self._imageIDs[self._selection]
634                                 self._base._focus = None
635                                 self._comboCallback()
636                                 return True
637                 return super(glComboButton, self).OnMouseDown(x, y, button)
638
639 class glFrame(glGuiContainer):
640         def __init__(self, parent, pos):
641                 super(glFrame, self).__init__(parent, pos)
642                 self._selected = False
643                 self._focus = False
644                 self._hidden = False
645
646         def setSelected(self, value):
647                 self._selected = value
648
649         def setHidden(self, value):
650                 self._hidden = value
651                 for child in self._glGuiControlList:
652                         if self._base._focus == child:
653                                 self._base._focus = None
654
655         def getSelected(self):
656                 return self._selected
657
658         def getMinSize(self):
659                 return self._base._buttonSize, self._base._buttonSize
660
661         def _getPixelPos(self):
662                 x0, y0, w, h = self.getSize()
663                 return x0, y0
664
665         def draw(self):
666                 if self._hidden:
667                         return
668
669                 bs = self._parent._buttonSize
670                 pos = self._getPixelPos()
671
672                 size = self._layout.getLayoutSize()
673                 glColor4ub(255,255,255,255)
674                 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
675                 #Draw the controls on the frame
676                 super(glFrame, self).draw()
677
678         def _checkHit(self, x, y):
679                 if self._hidden:
680                         return False
681                 pos = self._getPixelPos()
682                 w, h = self._layout.getLayoutSize()
683                 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
684
685         def OnMouseMotion(self, x, y):
686                 super(glFrame, self).OnMouseMotion(x, y)
687                 if self._checkHit(x, y):
688                         self._focus = True
689                         return True
690                 self._focus = False
691                 return False
692
693         def OnMouseDown(self, x, y, button):
694                 if self._checkHit(x, y):
695                         super(glFrame, self).OnMouseDown(x, y, button)
696                         return True
697                 return False
698
699 class glNotification(glFrame):
700         def __init__(self, parent, pos):
701                 self._anim = None
702                 super(glNotification, self).__init__(parent, pos)
703                 glGuiLayoutGrid(self)._alignBottom = False
704                 self._label = glLabel(self, "Notification", (0, 0))
705                 self._buttonEject = glButton(self, 31, "Eject", (1, 0), self.onEject, 25)
706                 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
707                 self._padding = glLabel(self, "", (0, 1))
708                 self.setHidden(True)
709
710         def setSize(self, x, y, w, h):
711                 w, h = self._layout.getLayoutSize()
712                 baseSize = self._base.GetSizeTuple()
713                 if self._anim is not None:
714                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
715                 else:
716                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
717
718         def draw(self):
719                 self.setSize(0,0,0,0)
720                 self.updateLayout()
721                 super(glNotification, self).draw()
722
723         def message(self, text, ejectCallback = None):
724                 self._anim = animation(self._base, -20, 25, 1)
725                 self.setHidden(False)
726                 self._label.setLabel(text)
727                 self._buttonEject.setHidden(ejectCallback is None)
728                 self._ejectCallback = ejectCallback
729                 self._base._queueRefresh()
730                 self.updateLayout()
731
732         def onEject(self, button):
733                 self.onClose(button)
734                 self._ejectCallback()
735
736         def onClose(self, button):
737                 if self._anim is not None:
738                         self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
739                 else:
740                         self._anim = animation(self._base, 25, -20, 1)
741
742 class glLabel(glGuiControl):
743         def __init__(self, parent, label, pos):
744                 self._label = label
745                 super(glLabel, self).__init__(parent, pos)
746
747         def setLabel(self, label):
748                 self._label = label
749
750         def getMinSize(self):
751                 w, h = opengl.glGetStringSize(self._label)
752                 return w + 10, h + 4
753
754         def _getPixelPos(self):
755                 x0, y0, w, h = self.getSize()
756                 return x0, y0
757
758         def draw(self):
759                 x, y, w, h = self.getSize()
760
761                 glPushMatrix()
762                 glTranslatef(x, y, 0)
763
764 #               glColor4ub(255,255,255,128)
765 #               glBegin(GL_QUADS)
766 #               glTexCoord2f(1, 0)
767 #               glVertex2f( w, 0)
768 #               glTexCoord2f(0, 0)
769 #               glVertex2f( 0, 0)
770 #               glTexCoord2f(0, 1)
771 #               glVertex2f( 0, h)
772 #               glTexCoord2f(1, 1)
773 #               glVertex2f( w, h)
774 #               glEnd()
775
776                 glTranslate(5, h - 5, 0)
777                 glColor4ub(255,255,255,255)
778                 opengl.glDrawStringLeft(self._label)
779                 glPopMatrix()
780
781         def _checkHit(self, x, y):
782                 return False
783
784         def OnMouseMotion(self, x, y):
785                 return False
786
787         def OnMouseDown(self, x, y, button):
788                 return False
789
790 class glNumberCtrl(glGuiControl):
791         def __init__(self, parent, value, pos, callback):
792                 self._callback = callback
793                 self._value = str(value)
794                 self._selectPos = 0
795                 self._maxLen = 6
796                 self._inCallback = False
797                 super(glNumberCtrl, self).__init__(parent, pos)
798
799         def setValue(self, value):
800                 if self._inCallback:
801                         return
802                 self._value = str(value)
803
804         def getMinSize(self):
805                 w, h = opengl.glGetStringSize("VALUES")
806                 return w + 10, h + 4
807
808         def _getPixelPos(self):
809                 x0, y0, w, h = self.getSize()
810                 return x0, y0
811
812         def draw(self):
813                 x, y, w, h = self.getSize()
814
815                 glPushMatrix()
816                 glTranslatef(x, y, 0)
817
818                 if self.hasFocus():
819                         glColor4ub(255,255,255,255)
820                 else:
821                         glColor4ub(255,255,255,192)
822                 glBegin(GL_QUADS)
823                 glTexCoord2f(1, 0)
824                 glVertex2f( w, 0)
825                 glTexCoord2f(0, 0)
826                 glVertex2f( 0, 0)
827                 glTexCoord2f(0, 1)
828                 glVertex2f( 0, h-1)
829                 glTexCoord2f(1, 1)
830                 glVertex2f( w, h-1)
831                 glEnd()
832
833                 glTranslate(5, h - 5, 0)
834                 glColor4ub(0,0,0,255)
835                 opengl.glDrawStringLeft(self._value)
836                 if self.hasFocus():
837                         glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
838                         opengl.glDrawStringLeft('|')
839                 glPopMatrix()
840
841         def _checkHit(self, x, y):
842                 x1, y1, w, h = self.getSize()
843                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
844
845         def OnMouseMotion(self, x, y):
846                 return False
847
848         def OnMouseDown(self, x, y, button):
849                 if self._checkHit(x, y):
850                         self.setFocus()
851                         return True
852                 return False
853
854         def OnKeyChar(self, c):
855                 self._inCallback = True
856                 if c == wx.WXK_LEFT:
857                         self._selectPos -= 1
858                         self._selectPos = max(0, self._selectPos)
859                 if c == wx.WXK_RIGHT:
860                         self._selectPos += 1
861                         self._selectPos = min(self._selectPos, len(self._value))
862                 if c == wx.WXK_UP:
863                         try:
864                                 value = float(self._value)
865                         except:
866                                 pass
867                         else:
868                                 value += 0.1
869                                 self._value = str(value)
870                                 self._callback(self._value)
871                 if c == wx.WXK_DOWN:
872                         try:
873                                 value = float(self._value)
874                         except:
875                                 pass
876                         else:
877                                 value -= 0.1
878                                 if value > 0:
879                                         self._value = str(value)
880                                         self._callback(self._value)
881                 if c == wx.WXK_BACK and self._selectPos > 0:
882                         self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
883                         self._selectPos -= 1
884                         self._callback(self._value)
885                 if c == wx.WXK_DELETE:
886                         self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
887                         self._callback(self._value)
888                 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
889                         if wx.GetKeyState(wx.WXK_SHIFT):
890                                 self.focusPrevious()
891                         else:
892                                 self.focusNext()
893                 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
894                         self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
895                         self._selectPos += 1
896                         self._callback(self._value)
897                 self._inCallback = False
898
899         def setFocus(self):
900                 self._base._focus = self
901                 self._selectPos = len(self._value)
902                 return True
903
904 class glCheckbox(glGuiControl):
905         def __init__(self, parent, value, pos, callback):
906                 self._callback = callback
907                 self._value = value
908                 self._selectPos = 0
909                 self._maxLen = 6
910                 self._inCallback = False
911                 super(glCheckbox, self).__init__(parent, pos)
912
913         def setValue(self, value):
914                 if self._inCallback:
915                         return
916                 self._value = str(value)
917
918         def getValue(self):
919                 return self._value
920
921         def getMinSize(self):
922                 return 20, 20
923
924         def _getPixelPos(self):
925                 x0, y0, w, h = self.getSize()
926                 return x0, y0
927
928         def draw(self):
929                 x, y, w, h = self.getSize()
930
931                 glPushMatrix()
932                 glTranslatef(x, y, 0)
933
934                 glColor3ub(255,255,255)
935                 if self._value:
936                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
937                 else:
938                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
939
940                 glPopMatrix()
941
942         def _checkHit(self, x, y):
943                 x1, y1, w, h = self.getSize()
944                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
945
946         def OnMouseMotion(self, x, y):
947                 return False
948
949         def OnMouseDown(self, x, y, button):
950                 if self._checkHit(x, y):
951                         self._value = not self._value
952                         return True
953                 return False
954
955 class glSlider(glGuiControl):
956         def __init__(self, parent, value, minValue, maxValue, pos, callback):
957                 super(glSlider, self).__init__(parent, pos)
958                 self._callback = callback
959                 self._focus = False
960                 self._hidden = False
961                 self._value = value
962                 self._minValue = minValue
963                 self._maxValue = maxValue
964
965         def setValue(self, value):
966                 self._value = value
967
968         def getValue(self):
969                 if self._value < self._minValue:
970                         return self._minValue
971                 if self._value > self._maxValue:
972                         return self._maxValue
973                 return self._value
974
975         def setRange(self, minValue, maxValue):
976                 if maxValue < minValue:
977                         maxValue = minValue
978                 self._minValue = minValue
979                 self._maxValue = maxValue
980
981         def getMinValue(self):
982                 return self._minValue
983
984         def getMaxValue(self):
985                 return self._maxValue
986
987         def setHidden(self, value):
988                 self._hidden = value
989
990         def getMinSize(self):
991                 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
992
993         def _getPixelPos(self):
994                 x0, y0, w, h = self.getSize()
995                 minSize = self.getMinSize()
996                 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
997
998         def draw(self):
999                 if self._hidden:
1000                         return
1001
1002                 w, h = self.getMinSize()
1003                 pos = self._getPixelPos()
1004
1005                 glPushMatrix()
1006                 glTranslatef(pos[0], pos[1], 0)
1007                 glDisable(GL_TEXTURE_2D)
1008                 if self.hasFocus():
1009                         glColor4ub(60,60,60,255)
1010                 else:
1011                         glColor4ub(60,60,60,192)
1012                 glBegin(GL_QUADS)
1013                 glVertex2f( w/2,-h/2)
1014                 glVertex2f(-w/2,-h/2)
1015                 glVertex2f(-w/2, h/2)
1016                 glVertex2f( w/2, h/2)
1017                 glEnd()
1018                 scrollLength = h - w
1019                 if self._maxValue-self._minValue != 0:
1020                         valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1021                 else:
1022                         valueNormalized = 0
1023                 glTranslate(0.0,scrollLength/2,0)
1024                 if self._focus:
1025                         glColor4ub(0,0,0,255)
1026                         glPushMatrix()
1027                         glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1028                         opengl.glDrawStringRight(str(self._minValue))
1029                         glTranslate(0,-scrollLength,0)
1030                         opengl.glDrawStringRight(str(self._maxValue))
1031                         glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1032                         opengl.glDrawStringLeft(str(self.getValue()))
1033                         glPopMatrix()
1034                 glColor4ub(255,255,255,240)
1035                 glTranslate(0.0,-scrollLength*valueNormalized,0)
1036                 glBegin(GL_QUADS)
1037                 glVertex2f( w/2,-w/2)
1038                 glVertex2f(-w/2,-w/2)
1039                 glVertex2f(-w/2, w/2)
1040                 glVertex2f( w/2, w/2)
1041                 glEnd()
1042                 glPopMatrix()
1043
1044         def _checkHit(self, x, y):
1045                 if self._hidden:
1046                         return False
1047                 pos = self._getPixelPos()
1048                 w, h = self.getMinSize()
1049                 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1050
1051         def setFocus(self):
1052                 self._base._focus = self
1053                 return True
1054
1055         def OnMouseMotion(self, x, y):
1056                 if self.hasFocus():
1057                         w, h = self.getMinSize()
1058                         scrollLength = h - w
1059                         pos = self._getPixelPos()
1060                         self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1061                         self._callback()
1062                         return True
1063                 if self._checkHit(x, y):
1064                         self._focus = True
1065                         return True
1066                 self._focus = False
1067                 return False
1068
1069         def OnMouseDown(self, x, y, button):
1070                 if self._checkHit(x, y):
1071                         self.setFocus()
1072                         self.OnMouseMotion(x, y)
1073                         return True
1074                 return False
1075
1076         def OnMouseUp(self, x, y):
1077                 if self.hasFocus():
1078                         self._base._focus = None
1079                         return True
1080                 return False