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