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