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