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