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