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