chiark / gitweb /
Add initial support for i18n.
[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(),size.GetHeight(),0)
274                 # s = self._buttonSize * 1
275                 # glScale(s,s,s)
276                 # glTranslate(-1.2,-0.2,0)
277                 # glBegin(GL_QUADS)
278                 # glTexCoord2f(1, 0)
279                 # glVertex2f(0,-1)
280                 # glTexCoord2f(0, 0)
281                 # glVertex2f(-1,-1)
282                 # glTexCoord2f(0, 1)
283                 # glVertex2f(-1, 0)
284                 # glTexCoord2f(1, 1)
285                 # glVertex2f(0, 0)
286                 # glEnd()
287                 # glDisable(GL_TEXTURE_2D)
288                 glPopMatrix()
289
290         def _OnEraseBackground(self,event):
291                 #Workaround for windows background redraw flicker.
292                 pass
293
294         def _OnSize(self,e):
295                 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
296                 self._container.updateLayout()
297                 self.Refresh()
298
299         def OnMouseDown(self,e):
300                 pass
301         def OnMouseUp(self,e):
302                 pass
303         def OnMouseMotion(self, e):
304                 pass
305         def OnKeyChar(self, keyCode):
306                 pass
307         def OnPaint(self, e):
308                 pass
309         def OnKeyChar(self, keycode):
310                 pass
311
312         def QueueRefresh(self):
313                 wx.CallAfter(self._queueRefresh)
314
315         def _queueRefresh(self):
316                 if self._idleCalled:
317                         wx.CallAfter(self.Refresh)
318                 else:
319                         self._refreshQueued = True
320
321         def add(self, ctrl):
322                 if self._container is not None:
323                         self._container.add(ctrl)
324
325 class glGuiLayoutButtons(object):
326         def __init__(self, parent):
327                 self._parent = parent
328                 self._parent._layout = self
329
330         def update(self):
331                 bs = self._parent._base._buttonSize
332                 x0, y0, w, h = self._parent.getSize()
333                 gridSize = bs * 1.0
334                 for ctrl in self._parent._glGuiControlList:
335                         pos = ctrl._pos
336                         if pos[0] < 0:
337                                 x = w + pos[0] * gridSize - bs * 0.2
338                         else:
339                                 x = pos[0] * gridSize + bs * 0.2
340                         if pos[1] < 0:
341                                 y = h + pos[1] * gridSize * 1.2 - bs * 0.0
342                         else:
343                                 y = pos[1] * gridSize * 1.2 + bs * 0.2
344                         ctrl.setSize(x, y, gridSize, gridSize)
345
346         def getLayoutSize(self):
347                 _, _, w, h = self._parent.getSize()
348                 return w, h
349
350 class glGuiLayoutGrid(object):
351         def __init__(self, parent):
352                 self._parent = parent
353                 self._parent._layout = self
354                 self._size = 0,0
355                 self._alignBottom = True
356
357         def update(self):
358                 borderSize = self._parent._base._buttonSize * 0.2
359                 x0, y0, w, h = self._parent.getSize()
360                 x0 += borderSize
361                 y0 += borderSize
362                 widths = {}
363                 heights = {}
364                 for ctrl in self._parent._glGuiControlList:
365                         x, y = ctrl._pos
366                         w, h = ctrl.getMinSize()
367                         if not x in widths:
368                                 widths[x] = w
369                         else:
370                                 widths[x] = max(widths[x], w)
371                         if not y in heights:
372                                 heights[y] = h
373                         else:
374                                 heights[y] = max(heights[y], h)
375                 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
376                 if self._alignBottom:
377                         y0 -= self._size[1] - self._parent.getSize()[3]
378                         self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
379                 for ctrl in self._parent._glGuiControlList:
380                         x, y = ctrl._pos
381                         x1 = x0
382                         y1 = y0
383                         for n in xrange(0, x):
384                                 if not n in widths:
385                                         widths[n] = 3
386                                 x1 += widths[n]
387                         for n in xrange(0, y):
388                                 if not n in heights:
389                                         heights[n] = 3
390                                 y1 += heights[n]
391                         ctrl.setSize(x1, y1, widths[x], heights[y])
392
393         def getLayoutSize(self):
394                 return self._size
395
396 class glButton(glGuiControl):
397         def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
398                 self._buttonSize = size
399                 self._hidden = False
400                 super(glButton, self).__init__(parent, pos)
401                 self._tooltip = tooltip
402                 self._parent = parent
403                 self._imageID = imageID
404                 self._callback = callback
405                 self._selected = False
406                 self._focus = False
407                 self._disabled = False
408                 self._showExpandArrow = False
409                 self._progressBar = None
410                 self._altTooltip = ''
411
412         def setSelected(self, value):
413                 self._selected = value
414
415         def setExpandArrow(self, value):
416                 self._showExpandArrow = value
417
418         def setHidden(self, value):
419                 self._hidden = value
420
421         def setDisabled(self, value):
422                 self._disabled = value
423
424         def setProgressBar(self, value):
425                 self._progressBar = value
426
427         def getProgressBar(self):
428                 return self._progressBar
429
430         def setBottomText(self, value):
431                 self._altTooltip = value
432
433         def getSelected(self):
434                 return self._selected
435
436         def getMinSize(self):
437                 if self._hidden:
438                         return 0, 0
439                 if self._buttonSize is not None:
440                         return self._buttonSize, self._buttonSize
441                 return self._base._buttonSize, self._base._buttonSize
442
443         def _getPixelPos(self):
444                 x0, y0, w, h = self.getSize()
445                 return x0 + w / 2, y0 + h / 2
446
447         def draw(self):
448                 if self._hidden:
449                         return
450
451                 cx = (self._imageID % 4) / 4
452                 cy = int(self._imageID / 4) / 4
453                 bs = self.getMinSize()[0]
454                 pos = self._getPixelPos()
455
456                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
457                 scale = 0.8
458                 if self._selected:
459                         scale = 1.0
460                 elif self._focus:
461                         scale = 0.9
462                 if self._disabled:
463                         glColor4ub(128,128,128,128)
464                 else:
465                         glColor4ub(255,255,255,255)
466                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
467                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
468                 if self._showExpandArrow:
469                         if self._selected:
470                                 opengl.glDrawTexturedQuad(pos[0]+bs*scale/2-bs*scale/4*1.2, pos[1]-bs*scale/2*1.2, bs*scale/4, bs*scale/4, 1)
471                         else:
472                                 opengl.glDrawTexturedQuad(pos[0]+bs*scale/2-bs*scale/4*1.2, pos[1]-bs*scale/2*1.2, bs*scale/4, bs*scale/4, 1, 2)
473                 glPushMatrix()
474                 glTranslatef(pos[0], pos[1], 0)
475                 glDisable(GL_TEXTURE_2D)
476                 if self._focus:
477                         glTranslatef(0, -0.55*bs*scale, 0)
478
479                         glPushMatrix()
480                         glColor4ub(60,60,60,255)
481                         glTranslatef(-1, -1, 0)
482                         opengl.glDrawStringCenter(self._tooltip)
483                         glTranslatef(0, 2, 0)
484                         opengl.glDrawStringCenter(self._tooltip)
485                         glTranslatef(2, 0, 0)
486                         opengl.glDrawStringCenter(self._tooltip)
487                         glTranslatef(0, -2, 0)
488                         opengl.glDrawStringCenter(self._tooltip)
489                         glPopMatrix()
490
491                         glColor4ub(255,255,255,255)
492                         opengl.glDrawStringCenter(self._tooltip)
493                 glPopMatrix()
494                 progress = self._progressBar
495                 if progress is not None:
496                         glColor4ub(60,60,60,255)
497                         opengl.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
498                         glColor4ub(255,255,255,255)
499                         opengl.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
500                 elif len(self._altTooltip) > 0:
501                         glPushMatrix()
502                         glTranslatef(pos[0], pos[1], 0)
503                         glTranslatef(0, 0.6*bs, 0)
504                         glTranslatef(0, 6, 0)
505                         #glTranslatef(0.6*bs*scale, 0, 0)
506
507                         for line in self._altTooltip.split('\n'):
508                                 glPushMatrix()
509                                 glColor4ub(60,60,60,255)
510                                 glTranslatef(-1, -1, 0)
511                                 opengl.glDrawStringCenter(line)
512                                 glTranslatef(0, 2, 0)
513                                 opengl.glDrawStringCenter(line)
514                                 glTranslatef(2, 0, 0)
515                                 opengl.glDrawStringCenter(line)
516                                 glTranslatef(0, -2, 0)
517                                 opengl.glDrawStringCenter(line)
518                                 glPopMatrix()
519
520                                 glColor4ub(255,255,255,255)
521                                 opengl.glDrawStringCenter(line)
522                                 glTranslatef(0, 18, 0)
523                         glPopMatrix()
524
525         def _checkHit(self, x, y):
526                 if self._hidden or self._disabled:
527                         return False
528                 bs = self.getMinSize()[0]
529                 pos = self._getPixelPos()
530                 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
531
532         def OnMouseMotion(self, x, y):
533                 if self._checkHit(x, y):
534                         self._focus = True
535                         return True
536                 self._focus = False
537                 return False
538
539         def OnMouseDown(self, x, y, button):
540                 if self._checkHit(x, y):
541                         self._callback(button)
542                         return True
543                 return False
544
545 class glRadioButton(glButton):
546         def __init__(self, parent, imageID, tooltip, pos, group, callback):
547                 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
548                 self._group = group
549                 self._radioCallback = callback
550                 self._group.append(self)
551
552         def setSelected(self, value):
553                 self._selected = value
554
555         def _onRadioSelect(self, button):
556                 self._base._focus = None
557                 for ctrl in self._group:
558                         if ctrl != self:
559                                 ctrl.setSelected(False)
560                 if self.getSelected():
561                         self.setSelected(False)
562                 else:
563                         self.setSelected(True)
564                 self._radioCallback(button)
565
566 class glComboButton(glButton):
567         def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
568                 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
569                 self._imageIDs = imageIDs
570                 self._tooltips = tooltips
571                 self._comboCallback = callback
572                 self._selection = 0
573
574         def _onComboOpenSelect(self, button):
575                 if self.hasFocus():
576                         self._base._focus = None
577                 else:
578                         self._base._focus = self
579
580         def draw(self):
581                 if self._hidden:
582                         return
583                 self._selected = self.hasFocus()
584                 super(glComboButton, self).draw()
585
586                 bs = self._base._buttonSize / 2
587                 pos = self._getPixelPos()
588
589                 if not self._selected:
590                         return
591
592                 glPushMatrix()
593                 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
594                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
595                 for n in xrange(0, len(self._imageIDs)):
596                         glTranslatef(0, bs, 0)
597                         glColor4ub(255,255,255,255)
598                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
599                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
600                         glDisable(GL_TEXTURE_2D)
601
602                         glPushMatrix()
603                         glTranslatef(-0.55*bs, 0.1*bs, 0)
604
605                         glPushMatrix()
606                         glColor4ub(60,60,60,255)
607                         glTranslatef(-1, -1, 0)
608                         opengl.glDrawStringRight(self._tooltips[n])
609                         glTranslatef(0, 2, 0)
610                         opengl.glDrawStringRight(self._tooltips[n])
611                         glTranslatef(2, 0, 0)
612                         opengl.glDrawStringRight(self._tooltips[n])
613                         glTranslatef(0, -2, 0)
614                         opengl.glDrawStringRight(self._tooltips[n])
615                         glPopMatrix()
616
617                         glColor4ub(255,255,255,255)
618                         opengl.glDrawStringRight(self._tooltips[n])
619                         glPopMatrix()
620                 glPopMatrix()
621
622         def getValue(self):
623                 return self._selection
624
625         def setValue(self, value):
626                 self._selection = value
627                 self._imageID = self._imageIDs[self._selection]
628                 self._comboCallback()
629
630         def OnMouseDown(self, x, y, button):
631                 if self._hidden or self._disabled:
632                         return False
633                 if self.hasFocus():
634                         bs = self._base._buttonSize / 2
635                         pos = self._getPixelPos()
636                         if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
637                                 self._selection = int((y - pos[1] - bs) / bs)
638                                 self._imageID = self._imageIDs[self._selection]
639                                 self._base._focus = None
640                                 self._comboCallback()
641                                 return True
642                 return super(glComboButton, self).OnMouseDown(x, y, button)
643
644 class glFrame(glGuiContainer):
645         def __init__(self, parent, pos):
646                 super(glFrame, self).__init__(parent, pos)
647                 self._selected = False
648                 self._focus = False
649                 self._hidden = False
650
651         def setSelected(self, value):
652                 self._selected = value
653
654         def setHidden(self, value):
655                 self._hidden = value
656                 for child in self._glGuiControlList:
657                         if self._base._focus == child:
658                                 self._base._focus = None
659
660         def getSelected(self):
661                 return self._selected
662
663         def getMinSize(self):
664                 return self._base._buttonSize, self._base._buttonSize
665
666         def _getPixelPos(self):
667                 x0, y0, w, h = self.getSize()
668                 return x0, y0
669
670         def draw(self):
671                 if self._hidden:
672                         return
673
674                 bs = self._parent._buttonSize
675                 pos = self._getPixelPos()
676
677                 size = self._layout.getLayoutSize()
678                 glColor4ub(255,255,255,255)
679                 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
680                 #Draw the controls on the frame
681                 super(glFrame, self).draw()
682
683         def _checkHit(self, x, y):
684                 if self._hidden:
685                         return False
686                 pos = self._getPixelPos()
687                 w, h = self._layout.getLayoutSize()
688                 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
689
690         def OnMouseMotion(self, x, y):
691                 super(glFrame, self).OnMouseMotion(x, y)
692                 if self._checkHit(x, y):
693                         self._focus = True
694                         return True
695                 self._focus = False
696                 return False
697
698         def OnMouseDown(self, x, y, button):
699                 if self._checkHit(x, y):
700                         super(glFrame, self).OnMouseDown(x, y, button)
701                         return True
702                 return False
703
704 class glNotification(glFrame):
705         def __init__(self, parent, pos):
706                 self._anim = None
707                 super(glNotification, self).__init__(parent, pos)
708                 glGuiLayoutGrid(self)._alignBottom = False
709                 self._label = glLabel(self, "Notification", (0, 0))
710                 self._buttonEject = glButton(self, 31, "Eject", (1, 0), self.onEject, 25)
711                 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
712                 self._padding = glLabel(self, "", (0, 1))
713                 self.setHidden(True)
714
715         def setSize(self, x, y, w, h):
716                 w, h = self._layout.getLayoutSize()
717                 baseSize = self._base.GetSizeTuple()
718                 if self._anim is not None:
719                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
720                 else:
721                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
722
723         def draw(self):
724                 self.setSize(0,0,0,0)
725                 self.updateLayout()
726                 super(glNotification, self).draw()
727
728         def message(self, text, ejectCallback = None):
729                 self._anim = animation(self._base, -20, 25, 1)
730                 self.setHidden(False)
731                 self._label.setLabel(text)
732                 self._buttonEject.setHidden(ejectCallback is None)
733                 self._ejectCallback = ejectCallback
734                 self._base._queueRefresh()
735                 self.updateLayout()
736
737         def onEject(self, button):
738                 self.onClose(button)
739                 self._ejectCallback()
740
741         def onClose(self, button):
742                 if self._anim is not None:
743                         self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
744                 else:
745                         self._anim = animation(self._base, 25, -20, 1)
746
747 class glLabel(glGuiControl):
748         def __init__(self, parent, label, pos):
749                 self._label = label
750                 super(glLabel, self).__init__(parent, pos)
751
752         def setLabel(self, label):
753                 self._label = label
754
755         def getMinSize(self):
756                 w, h = opengl.glGetStringSize(self._label)
757                 return w + 10, h + 4
758
759         def _getPixelPos(self):
760                 x0, y0, w, h = self.getSize()
761                 return x0, y0
762
763         def draw(self):
764                 x, y, w, h = self.getSize()
765
766                 glPushMatrix()
767                 glTranslatef(x, y, 0)
768
769 #               glColor4ub(255,255,255,128)
770 #               glBegin(GL_QUADS)
771 #               glTexCoord2f(1, 0)
772 #               glVertex2f( w, 0)
773 #               glTexCoord2f(0, 0)
774 #               glVertex2f( 0, 0)
775 #               glTexCoord2f(0, 1)
776 #               glVertex2f( 0, h)
777 #               glTexCoord2f(1, 1)
778 #               glVertex2f( w, h)
779 #               glEnd()
780
781                 glTranslate(5, h - 5, 0)
782                 glColor4ub(255,255,255,255)
783                 opengl.glDrawStringLeft(self._label)
784                 glPopMatrix()
785
786         def _checkHit(self, x, y):
787                 return False
788
789         def OnMouseMotion(self, x, y):
790                 return False
791
792         def OnMouseDown(self, x, y, button):
793                 return False
794
795 class glNumberCtrl(glGuiControl):
796         def __init__(self, parent, value, pos, callback):
797                 self._callback = callback
798                 self._value = str(value)
799                 self._selectPos = 0
800                 self._maxLen = 6
801                 self._inCallback = False
802                 super(glNumberCtrl, self).__init__(parent, pos)
803
804         def setValue(self, value):
805                 if self._inCallback:
806                         return
807                 self._value = str(value)
808
809         def getMinSize(self):
810                 w, h = opengl.glGetStringSize("VALUES")
811                 return w + 10, h + 4
812
813         def _getPixelPos(self):
814                 x0, y0, w, h = self.getSize()
815                 return x0, y0
816
817         def draw(self):
818                 x, y, w, h = self.getSize()
819
820                 glPushMatrix()
821                 glTranslatef(x, y, 0)
822
823                 if self.hasFocus():
824                         glColor4ub(255,255,255,255)
825                 else:
826                         glColor4ub(255,255,255,192)
827                 glBegin(GL_QUADS)
828                 glTexCoord2f(1, 0)
829                 glVertex2f( w, 0)
830                 glTexCoord2f(0, 0)
831                 glVertex2f( 0, 0)
832                 glTexCoord2f(0, 1)
833                 glVertex2f( 0, h-1)
834                 glTexCoord2f(1, 1)
835                 glVertex2f( w, h-1)
836                 glEnd()
837
838                 glTranslate(5, h - 5, 0)
839                 glColor4ub(0,0,0,255)
840                 opengl.glDrawStringLeft(self._value)
841                 if self.hasFocus():
842                         glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
843                         opengl.glDrawStringLeft('|')
844                 glPopMatrix()
845
846         def _checkHit(self, x, y):
847                 x1, y1, w, h = self.getSize()
848                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
849
850         def OnMouseMotion(self, x, y):
851                 return False
852
853         def OnMouseDown(self, x, y, button):
854                 if self._checkHit(x, y):
855                         self.setFocus()
856                         return True
857                 return False
858
859         def OnKeyChar(self, c):
860                 self._inCallback = True
861                 if c == wx.WXK_LEFT:
862                         self._selectPos -= 1
863                         self._selectPos = max(0, self._selectPos)
864                 if c == wx.WXK_RIGHT:
865                         self._selectPos += 1
866                         self._selectPos = min(self._selectPos, len(self._value))
867                 if c == wx.WXK_UP:
868                         try:
869                                 value = float(self._value)
870                         except:
871                                 pass
872                         else:
873                                 value += 0.1
874                                 self._value = str(value)
875                                 self._callback(self._value)
876                 if c == wx.WXK_DOWN:
877                         try:
878                                 value = float(self._value)
879                         except:
880                                 pass
881                         else:
882                                 value -= 0.1
883                                 if value > 0:
884                                         self._value = str(value)
885                                         self._callback(self._value)
886                 if c == wx.WXK_BACK and self._selectPos > 0:
887                         self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
888                         self._selectPos -= 1
889                         self._callback(self._value)
890                 if c == wx.WXK_DELETE:
891                         self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
892                         self._callback(self._value)
893                 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
894                         if wx.GetKeyState(wx.WXK_SHIFT):
895                                 self.focusPrevious()
896                         else:
897                                 self.focusNext()
898                 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
899                         self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
900                         self._selectPos += 1
901                         self._callback(self._value)
902                 self._inCallback = False
903
904         def setFocus(self):
905                 self._base._focus = self
906                 self._selectPos = len(self._value)
907                 return True
908
909 class glCheckbox(glGuiControl):
910         def __init__(self, parent, value, pos, callback):
911                 self._callback = callback
912                 self._value = value
913                 self._selectPos = 0
914                 self._maxLen = 6
915                 self._inCallback = False
916                 super(glCheckbox, self).__init__(parent, pos)
917
918         def setValue(self, value):
919                 if self._inCallback:
920                         return
921                 self._value = str(value)
922
923         def getValue(self):
924                 return self._value
925
926         def getMinSize(self):
927                 return 20, 20
928
929         def _getPixelPos(self):
930                 x0, y0, w, h = self.getSize()
931                 return x0, y0
932
933         def draw(self):
934                 x, y, w, h = self.getSize()
935
936                 glPushMatrix()
937                 glTranslatef(x, y, 0)
938
939                 glColor3ub(255,255,255)
940                 if self._value:
941                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
942                 else:
943                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
944
945                 glPopMatrix()
946
947         def _checkHit(self, x, y):
948                 x1, y1, w, h = self.getSize()
949                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
950
951         def OnMouseMotion(self, x, y):
952                 return False
953
954         def OnMouseDown(self, x, y, button):
955                 if self._checkHit(x, y):
956                         self._value = not self._value
957                         return True
958                 return False
959
960 class glSlider(glGuiControl):
961         def __init__(self, parent, value, minValue, maxValue, pos, callback):
962                 super(glSlider, self).__init__(parent, pos)
963                 self._callback = callback
964                 self._focus = False
965                 self._hidden = False
966                 self._value = value
967                 self._minValue = minValue
968                 self._maxValue = maxValue
969
970         def setValue(self, value):
971                 self._value = value
972
973         def getValue(self):
974                 if self._value < self._minValue:
975                         return self._minValue
976                 if self._value > self._maxValue:
977                         return self._maxValue
978                 return self._value
979
980         def setRange(self, minValue, maxValue):
981                 if maxValue < minValue:
982                         maxValue = minValue
983                 self._minValue = minValue
984                 self._maxValue = maxValue
985
986         def getMinValue(self):
987                 return self._minValue
988
989         def getMaxValue(self):
990                 return self._maxValue
991
992         def setHidden(self, value):
993                 self._hidden = value
994
995         def getMinSize(self):
996                 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
997
998         def _getPixelPos(self):
999                 x0, y0, w, h = self.getSize()
1000                 minSize = self.getMinSize()
1001                 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
1002
1003         def draw(self):
1004                 if self._hidden:
1005                         return
1006
1007                 w, h = self.getMinSize()
1008                 pos = self._getPixelPos()
1009
1010                 glPushMatrix()
1011                 glTranslatef(pos[0], pos[1], 0)
1012                 glDisable(GL_TEXTURE_2D)
1013                 if self.hasFocus():
1014                         glColor4ub(60,60,60,255)
1015                 else:
1016                         glColor4ub(60,60,60,192)
1017                 glBegin(GL_QUADS)
1018                 glVertex2f( w/2,-h/2)
1019                 glVertex2f(-w/2,-h/2)
1020                 glVertex2f(-w/2, h/2)
1021                 glVertex2f( w/2, h/2)
1022                 glEnd()
1023                 scrollLength = h - w
1024                 if self._maxValue-self._minValue != 0:
1025                         valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1026                 else:
1027                         valueNormalized = 0
1028                 glTranslate(0.0,scrollLength/2,0)
1029                 if self._focus:
1030                         glColor4ub(0,0,0,255)
1031                         glPushMatrix()
1032                         glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1033                         opengl.glDrawStringRight(str(self._minValue))
1034                         glTranslate(0,-scrollLength,0)
1035                         opengl.glDrawStringRight(str(self._maxValue))
1036                         glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1037                         opengl.glDrawStringLeft(str(self.getValue()))
1038                         glPopMatrix()
1039                 glColor4ub(255,255,255,240)
1040                 glTranslate(0.0,-scrollLength*valueNormalized,0)
1041                 glBegin(GL_QUADS)
1042                 glVertex2f( w/2,-w/2)
1043                 glVertex2f(-w/2,-w/2)
1044                 glVertex2f(-w/2, w/2)
1045                 glVertex2f( w/2, w/2)
1046                 glEnd()
1047                 glPopMatrix()
1048
1049         def _checkHit(self, x, y):
1050                 if self._hidden:
1051                         return False
1052                 pos = self._getPixelPos()
1053                 w, h = self.getMinSize()
1054                 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1055
1056         def setFocus(self):
1057                 self._base._focus = self
1058                 return True
1059
1060         def OnMouseMotion(self, x, y):
1061                 if self.hasFocus():
1062                         w, h = self.getMinSize()
1063                         scrollLength = h - w
1064                         pos = self._getPixelPos()
1065                         self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1066                         self._callback()
1067                         return True
1068                 if self._checkHit(x, y):
1069                         self._focus = True
1070                         return True
1071                 self._focus = False
1072                 return False
1073
1074         def OnMouseDown(self, x, y, button):
1075                 if self._checkHit(x, y):
1076                         self.setFocus()
1077                         self.OnMouseMotion(x, y)
1078                         return True
1079                 return False
1080
1081         def OnMouseUp(self, x, y):
1082                 if self.hasFocus():
1083                         self._base._focus = None
1084                         return True
1085                 return False