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