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