chiark / gitweb /
Add instant slicing.
[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                 self._progressBar = 0.0
351
352         def setSelected(self, value):
353                 self._selected = value
354
355         def setExpandArrow(self, value):
356                 self._showExpandArrow = value
357
358         def setHidden(self, value):
359                 self._hidden = value
360
361         def setDisabled(self, value):
362                 self._disabled = value
363
364         def setProgressBar(self, value):
365                 self._progressBar = value
366
367         def getSelected(self):
368                 return self._selected
369
370         def getMinSize(self):
371                 return self._base._buttonSize, self._base._buttonSize
372
373         def _getPixelPos(self):
374                 x0, y0, w, h = self.getSize()
375                 return x0 + w / 2, y0 + h / 2
376
377         def draw(self):
378                 if self._hidden:
379                         return
380
381                 cx = (self._imageID % 4) / 4
382                 cy = int(self._imageID / 4) / 4
383                 bs = self._base._buttonSize
384                 pos = self._getPixelPos()
385
386                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
387                 scale = 0.8
388                 if self._selected:
389                         scale = 1.0
390                 elif self._focus:
391                         scale = 0.9
392                 if self._disabled:
393                         glColor4ub(128,128,128,128)
394                 else:
395                         glColor4ub(255,255,255,255)
396                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
397                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
398                 if self._showExpandArrow:
399                         if self._selected:
400                                 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)
401                         else:
402                                 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)
403                 glPushMatrix()
404                 glTranslatef(pos[0], pos[1], 0)
405                 glDisable(GL_TEXTURE_2D)
406                 if self._focus:
407                         glTranslatef(0, -0.55*bs*scale, 0)
408
409                         glPushMatrix()
410                         glColor4ub(60,60,60,255)
411                         glTranslatef(-1, -1, 0)
412                         opengl.glDrawStringCenter(self._tooltip)
413                         glTranslatef(0, 2, 0)
414                         opengl.glDrawStringCenter(self._tooltip)
415                         glTranslatef(2, 0, 0)
416                         opengl.glDrawStringCenter(self._tooltip)
417                         glTranslatef(0, -2, 0)
418                         opengl.glDrawStringCenter(self._tooltip)
419                         glPopMatrix()
420
421                         glColor4ub(255,255,255,255)
422                         opengl.glDrawStringCenter(self._tooltip)
423                 glPopMatrix()
424                 if 0.0 < self._progressBar < 1.0:
425                         glColor4ub(255,255,255,192)
426                         glPushMatrix()
427                         opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4, 0)
428                         opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs * self._progressBar, bs / 4, 0)
429                         glPopMatrix()
430
431         def _checkHit(self, x, y):
432                 if self._hidden or self._disabled:
433                         return False
434                 bs = self.getMinSize()[0]
435                 pos = self._getPixelPos()
436                 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
437
438         def OnMouseMotion(self, x, y):
439                 if self._checkHit(x, y):
440                         self._focus = True
441                         return True
442                 self._focus = False
443                 return False
444
445         def OnMouseDown(self, x, y):
446                 if self._checkHit(x, y):
447                         self._callback()
448                         return True
449                 return False
450
451 class glRadioButton(glButton):
452         def __init__(self, parent, imageID, tooltip, pos, group, callback):
453                 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
454                 self._group = group
455                 self._radioCallback = callback
456                 self._group.append(self)
457
458         def setSelected(self, value):
459                 self._selected = value
460
461         def _onRadioSelect(self):
462                 self._base._focus = None
463                 for ctrl in self._group:
464                         if ctrl != self:
465                                 ctrl.setSelected(False)
466                 if self.getSelected():
467                         self.setSelected(False)
468                 else:
469                         self.setSelected(True)
470                 self._radioCallback()
471
472 class glComboButton(glButton):
473         def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
474                 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
475                 self._imageIDs = imageIDs
476                 self._tooltips = tooltips
477                 self._comboCallback = callback
478                 self._selection = 0
479
480         def _onComboOpenSelect(self):
481                 if self.hasFocus():
482                         self._base._focus = None
483                 else:
484                         self._base._focus = self
485
486         def draw(self):
487                 if self._hidden:
488                         return
489                 self._selected = self.hasFocus()
490                 super(glComboButton, self).draw()
491
492                 bs = self._base._buttonSize / 2
493                 pos = self._getPixelPos()
494
495                 if not self._selected:
496                         return
497
498                 glPushMatrix()
499                 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
500                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
501                 for n in xrange(0, len(self._imageIDs)):
502                         glTranslatef(0, bs, 0)
503                         glColor4ub(255,255,255,255)
504                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
505                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
506                         glDisable(GL_TEXTURE_2D)
507
508                         glPushMatrix()
509                         glTranslatef(-0.55*bs, 0.1*bs, 0)
510
511                         glPushMatrix()
512                         glColor4ub(60,60,60,255)
513                         glTranslatef(-1, -1, 0)
514                         opengl.glDrawStringRight(self._tooltips[n])
515                         glTranslatef(0, 2, 0)
516                         opengl.glDrawStringRight(self._tooltips[n])
517                         glTranslatef(2, 0, 0)
518                         opengl.glDrawStringRight(self._tooltips[n])
519                         glTranslatef(0, -2, 0)
520                         opengl.glDrawStringRight(self._tooltips[n])
521                         glPopMatrix()
522
523                         glColor4ub(255,255,255,255)
524                         opengl.glDrawStringRight(self._tooltips[n])
525                         glPopMatrix()
526                 glPopMatrix()
527
528         def getValue(self):
529                 return self._selection
530
531         def setValue(self, value):
532                 self._selection = value
533                 self._imageID = self._imageIDs[self._selection]
534                 self._comboCallback()
535
536         def OnMouseDown(self, x, y):
537                 if self._hidden or self._disabled:
538                         return False
539                 if self.hasFocus():
540                         bs = self._base._buttonSize / 2
541                         pos = self._getPixelPos()
542                         if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
543                                 self._selection = int((y - pos[1] - bs) / bs)
544                                 self._imageID = self._imageIDs[self._selection]
545                                 self._base._focus = None
546                                 self._comboCallback()
547                                 return True
548                 return super(glComboButton, self).OnMouseDown(x, y)
549
550 class glFrame(glGuiContainer):
551         def __init__(self, parent, pos):
552                 super(glFrame, self).__init__(parent, pos)
553                 self._selected = False
554                 self._focus = False
555                 self._hidden = False
556
557         def setSelected(self, value):
558                 self._selected = value
559
560         def setHidden(self, value):
561                 self._hidden = value
562
563         def getSelected(self):
564                 return self._selected
565
566         def getMinSize(self):
567                 return self._base._buttonSize, self._base._buttonSize
568
569         def _getPixelPos(self):
570                 x0, y0, w, h = self.getSize()
571                 return x0, y0
572
573         def draw(self):
574                 if self._hidden:
575                         return
576
577                 bs = self._parent._buttonSize
578                 pos = self._getPixelPos()
579
580                 glPushMatrix()
581                 glTranslatef(pos[0], pos[1], 0)
582                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
583                 glEnable(GL_TEXTURE_2D)
584
585                 size = self._layout.getLayoutSize()
586                 glColor4ub(255,255,255,255)
587                 glBegin(GL_QUADS)
588                 bs /= 2
589                 tc = 1 / 4 / 2
590
591 #               glTexCoord2f(1, 0)
592 #               glVertex2f( size[0], 0)
593 #               glTexCoord2f(0, 0)
594 #               glVertex2f( 0, 0)
595 #               glTexCoord2f(0, 1)
596 #               glVertex2f( 0, size[1])
597 #               glTexCoord2f(1, 1)
598 #               glVertex2f( size[0], size[1])
599                 #TopLeft
600                 glTexCoord2f(tc, 0)
601                 glVertex2f( bs, 0)
602                 glTexCoord2f(0, 0)
603                 glVertex2f( 0, 0)
604                 glTexCoord2f(0, tc/2)
605                 glVertex2f( 0, bs)
606                 glTexCoord2f(tc, tc/2)
607                 glVertex2f( bs, bs)
608                 #TopRight
609                 glTexCoord2f(tc+tc, 0)
610                 glVertex2f( size[0], 0)
611                 glTexCoord2f(tc, 0)
612                 glVertex2f( size[0] - bs, 0)
613                 glTexCoord2f(tc, tc/2)
614                 glVertex2f( size[0] - bs, bs)
615                 glTexCoord2f(tc+tc, tc/2)
616                 glVertex2f( size[0], bs)
617                 #BottomLeft
618                 glTexCoord2f(tc, tc/2)
619                 glVertex2f( bs, size[1] - bs)
620                 glTexCoord2f(0, tc/2)
621                 glVertex2f( 0, size[1] - bs)
622                 glTexCoord2f(0, tc/2+tc/2)
623                 glVertex2f( 0, size[1])
624                 glTexCoord2f(tc, tc/2+tc/2)
625                 glVertex2f( bs, size[1])
626                 #BottomRight
627                 glTexCoord2f(tc+tc, tc/2)
628                 glVertex2f( size[0], size[1] - bs)
629                 glTexCoord2f(tc, tc/2)
630                 glVertex2f( size[0] - bs, size[1] - bs)
631                 glTexCoord2f(tc, tc/2+tc/2)
632                 glVertex2f( size[0] - bs, size[1])
633                 glTexCoord2f(tc+tc, tc/2+tc/2)
634                 glVertex2f( size[0], size[1])
635
636                 #Center
637                 glTexCoord2f(tc, tc/2)
638                 glVertex2f( size[0]-bs, bs)
639                 glTexCoord2f(tc, tc/2)
640                 glVertex2f( bs, bs)
641                 glTexCoord2f(tc, tc/2)
642                 glVertex2f( bs, size[1]-bs)
643                 glTexCoord2f(tc, tc/2)
644                 glVertex2f( size[0]-bs, size[1]-bs)
645
646                 #Right
647                 glTexCoord2f(tc+tc, tc/2)
648                 glVertex2f( size[0], bs)
649                 glTexCoord2f(tc, tc/2)
650                 glVertex2f( size[0]-bs, bs)
651                 glTexCoord2f(tc, tc/2)
652                 glVertex2f( size[0]-bs, size[1]-bs)
653                 glTexCoord2f(tc+tc, tc/2)
654                 glVertex2f( size[0], size[1]-bs)
655
656                 #Left
657                 glTexCoord2f(tc, tc/2)
658                 glVertex2f( bs, bs)
659                 glTexCoord2f(0, tc/2)
660                 glVertex2f( 0, bs)
661                 glTexCoord2f(0, tc/2)
662                 glVertex2f( 0, size[1]-bs)
663                 glTexCoord2f(tc, tc/2)
664                 glVertex2f( bs, size[1]-bs)
665
666                 #Top
667                 glTexCoord2f(tc, 0)
668                 glVertex2f( size[0]-bs, 0)
669                 glTexCoord2f(tc, 0)
670                 glVertex2f( bs, 0)
671                 glTexCoord2f(tc, tc/2)
672                 glVertex2f( bs, bs)
673                 glTexCoord2f(tc, tc/2)
674                 glVertex2f( size[0]-bs, bs)
675
676                 #Bottom
677                 glTexCoord2f(tc, tc/2)
678                 glVertex2f( size[0]-bs, size[1]-bs)
679                 glTexCoord2f(tc, tc/2)
680                 glVertex2f( bs, size[1]-bs)
681                 glTexCoord2f(tc, tc/2+tc/2)
682                 glVertex2f( bs, size[1])
683                 glTexCoord2f(tc, tc/2+tc/2)
684                 glVertex2f( size[0]-bs, size[1])
685
686                 glEnd()
687                 glDisable(GL_TEXTURE_2D)
688                 glPopMatrix()
689                 #Draw the controls on the frame
690                 super(glFrame, self).draw()
691
692         def _checkHit(self, x, y):
693                 if self._hidden:
694                         return False
695                 pos = self._getPixelPos()
696                 w, h = self._layout.getLayoutSize()
697                 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
698
699         def OnMouseMotion(self, x, y):
700                 super(glFrame, self).OnMouseMotion(x, y)
701                 if self._checkHit(x, y):
702                         self._focus = True
703                         return True
704                 self._focus = False
705                 return False
706
707         def OnMouseDown(self, x, y):
708                 if self._checkHit(x, y):
709                         super(glFrame, self).OnMouseDown(x, y)
710                         return True
711                 return False
712
713 class glLabel(glGuiControl):
714         def __init__(self, parent, label, pos):
715                 self._label = label
716                 super(glLabel, self).__init__(parent, pos)
717
718         def getMinSize(self):
719                 w, h = opengl.glGetStringSize(self._label)
720                 return w + 10, h + 4
721
722         def _getPixelPos(self):
723                 x0, y0, w, h = self.getSize()
724                 return x0, y0
725
726         def draw(self):
727                 x, y, w, h = self.getSize()
728
729                 glPushMatrix()
730                 glTranslatef(x, y, 0)
731
732 #               glColor4ub(255,255,255,128)
733 #               glBegin(GL_QUADS)
734 #               glTexCoord2f(1, 0)
735 #               glVertex2f( w, 0)
736 #               glTexCoord2f(0, 0)
737 #               glVertex2f( 0, 0)
738 #               glTexCoord2f(0, 1)
739 #               glVertex2f( 0, h)
740 #               glTexCoord2f(1, 1)
741 #               glVertex2f( w, h)
742 #               glEnd()
743
744                 glTranslate(5, h - 5, 0)
745                 glColor4ub(255,255,255,255)
746                 opengl.glDrawStringLeft(self._label)
747                 glPopMatrix()
748
749         def _checkHit(self, x, y):
750                 return False
751
752         def OnMouseMotion(self, x, y):
753                 return False
754
755         def OnMouseDown(self, x, y):
756                 return False
757
758 class glNumberCtrl(glGuiControl):
759         def __init__(self, parent, value, pos, callback):
760                 self._callback = callback
761                 self._value = str(value)
762                 self._selectPos = 0
763                 self._maxLen = 6
764                 self._inCallback = False
765                 super(glNumberCtrl, self).__init__(parent, pos)
766
767         def setValue(self, value):
768                 if self._inCallback:
769                         return
770                 self._value = str(value)
771
772         def getMinSize(self):
773                 w, h = opengl.glGetStringSize("VALUES")
774                 return w + 10, h + 4
775
776         def _getPixelPos(self):
777                 x0, y0, w, h = self.getSize()
778                 return x0, y0
779
780         def draw(self):
781                 x, y, w, h = self.getSize()
782
783                 glPushMatrix()
784                 glTranslatef(x, y, 0)
785
786                 if self.hasFocus():
787                         glColor4ub(255,255,255,255)
788                 else:
789                         glColor4ub(255,255,255,192)
790                 glBegin(GL_QUADS)
791                 glTexCoord2f(1, 0)
792                 glVertex2f( w, 0)
793                 glTexCoord2f(0, 0)
794                 glVertex2f( 0, 0)
795                 glTexCoord2f(0, 1)
796                 glVertex2f( 0, h-1)
797                 glTexCoord2f(1, 1)
798                 glVertex2f( w, h-1)
799                 glEnd()
800
801                 glTranslate(5, h - 5, 0)
802                 glColor4ub(0,0,0,255)
803                 opengl.glDrawStringLeft(self._value)
804                 if self.hasFocus():
805                         glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
806                         opengl.glDrawStringLeft('|')
807                 glPopMatrix()
808
809         def _checkHit(self, x, y):
810                 x1, y1, w, h = self.getSize()
811                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
812
813         def OnMouseMotion(self, x, y):
814                 return False
815
816         def OnMouseDown(self, x, y):
817                 if self._checkHit(x, y):
818                         self.setFocus()
819                         return True
820                 return False
821
822         def OnKeyChar(self, c):
823                 self._inCallback = True
824                 if c == wx.WXK_LEFT:
825                         self._selectPos -= 1
826                         self._selectPos = max(0, self._selectPos)
827                 if c == wx.WXK_RIGHT:
828                         self._selectPos += 1
829                         self._selectPos = min(self._selectPos, len(self._value))
830                 if c == wx.WXK_UP:
831                         try:
832                                 value = float(self._value)
833                         except:
834                                 pass
835                         else:
836                                 value += 0.1
837                                 self._value = str(value)
838                                 self._callback(self._value)
839                 if c == wx.WXK_DOWN:
840                         try:
841                                 value = float(self._value)
842                         except:
843                                 pass
844                         else:
845                                 value -= 0.1
846                                 if value > 0:
847                                         self._value = str(value)
848                                         self._callback(self._value)
849                 if c == wx.WXK_BACK and self._selectPos > 0:
850                         self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
851                         self._selectPos -= 1
852                         self._callback(self._value)
853                 if c == wx.WXK_DELETE:
854                         self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
855                         self._callback(self._value)
856                 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
857                         if wx.GetKeyState(wx.WXK_SHIFT):
858                                 self.focusPrevious()
859                         else:
860                                 self.focusNext()
861                 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
862                         self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
863                         self._selectPos += 1
864                         self._callback(self._value)
865                 self._inCallback = False
866
867         def setFocus(self):
868                 self._base._focus = self
869                 self._selectPos = len(self._value)
870                 return True
871
872 class glCheckbox(glGuiControl):
873         def __init__(self, parent, value, pos, callback):
874                 self._callback = callback
875                 self._value = value
876                 self._selectPos = 0
877                 self._maxLen = 6
878                 self._inCallback = False
879                 super(glCheckbox, self).__init__(parent, pos)
880
881         def setValue(self, value):
882                 if self._inCallback:
883                         return
884                 self._value = str(value)
885
886         def getValue(self):
887                 return self._value
888
889         def getMinSize(self):
890                 return 20, 20
891
892         def _getPixelPos(self):
893                 x0, y0, w, h = self.getSize()
894                 return x0, y0
895
896         def draw(self):
897                 x, y, w, h = self.getSize()
898
899                 glPushMatrix()
900                 glTranslatef(x, y, 0)
901
902                 glColor3ub(255,255,255)
903                 if self._value:
904                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
905                 else:
906                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
907
908                 glPopMatrix()
909
910         def _checkHit(self, x, y):
911                 x1, y1, w, h = self.getSize()
912                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
913
914         def OnMouseMotion(self, x, y):
915                 return False
916
917         def OnMouseDown(self, x, y):
918                 if self._checkHit(x, y):
919                         self._value = not self._value
920                         return True
921                 return False
922
923 class glSlider(glGuiControl):
924         def __init__(self, parent, value, minValue, maxValue, pos, callback):
925                 super(glSlider, self).__init__(parent, pos)
926                 self._callback = callback
927                 self._focus = False
928                 self._hidden = False
929                 self._value = value
930                 self._minValue = minValue
931                 self._maxValue = maxValue
932
933         def setValue(self, value):
934                 self._value = value
935                 self._value = max(self._minValue, self._value)
936                 self._value = min(self._maxValue, self._value)
937
938         def getValue(self):
939                 return self._value
940
941         def setRange(self, minValue, maxValue):
942                 if maxValue < minValue:
943                         maxValue = minValue
944                 self._minValue = minValue
945                 self._maxValue = maxValue
946                 self._value = max(minValue, self._value)
947                 self._value = min(maxValue, self._value)
948
949         def getMinValue(self):
950                 return self._minValue
951
952         def getMaxValue(self):
953                 return self._maxValue
954
955         def setHidden(self, value):
956                 self._hidden = value
957
958         def getMinSize(self):
959                 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
960
961         def _getPixelPos(self):
962                 x0, y0, w, h = self.getSize()
963                 minSize = self.getMinSize()
964                 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
965
966         def draw(self):
967                 if self._hidden:
968                         return
969
970                 w, h = self.getMinSize()
971                 pos = self._getPixelPos()
972
973                 glPushMatrix()
974                 glTranslatef(pos[0], pos[1], 0)
975                 glDisable(GL_TEXTURE_2D)
976                 if self.hasFocus():
977                         glColor4ub(60,60,60,255)
978                 else:
979                         glColor4ub(60,60,60,192)
980                 glBegin(GL_QUADS)
981                 glVertex2f( w/2,-h/2)
982                 glVertex2f(-w/2,-h/2)
983                 glVertex2f(-w/2, h/2)
984                 glVertex2f( w/2, h/2)
985                 glEnd()
986                 scrollLength = h - w
987                 glTranslate(0.0,scrollLength/2,0)
988                 if self._focus:
989                         glColor4ub(0,0,0,255)
990                         glPushMatrix()
991                         glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
992                         opengl.glDrawStringRight(str(self._minValue))
993                         glTranslate(0,-scrollLength,0)
994                         opengl.glDrawStringRight(str(self._maxValue))
995                         if self._maxValue-self._minValue > 0:
996                                 glTranslate(w,scrollLength-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
997                         opengl.glDrawStringLeft(str(self._value))
998                         glPopMatrix()
999                 glColor4ub(255,255,255,240)
1000                 if self._maxValue - self._minValue != 0:
1001                         glTranslate(0.0,-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
1002                 glBegin(GL_QUADS)
1003                 glVertex2f( w/2,-w/2)
1004                 glVertex2f(-w/2,-w/2)
1005                 glVertex2f(-w/2, w/2)
1006                 glVertex2f( w/2, w/2)
1007                 glEnd()
1008                 glPopMatrix()
1009
1010         def _checkHit(self, x, y):
1011                 if self._hidden:
1012                         return False
1013                 pos = self._getPixelPos()
1014                 w, h = self.getMinSize()
1015                 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1016
1017         def setFocus(self):
1018                 self._base._focus = self
1019                 return True
1020
1021         def OnMouseMotion(self, x, y):
1022                 if self.hasFocus():
1023                         w, h = self.getMinSize()
1024                         scrollLength = h - w
1025                         pos = self._getPixelPos()
1026                         self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1027                         self._callback()
1028                         return True
1029                 if self._checkHit(x, y):
1030                         self._focus = True
1031                         return True
1032                 self._focus = False
1033                 return False
1034
1035         def OnMouseDown(self, x, y):
1036                 if self._checkHit(x, y):
1037                         self.setFocus()
1038                         self.OnMouseMotion(x, y)
1039                         return True
1040                 return False
1041
1042         def OnMouseUp(self, x, y):
1043                 if self.hasFocus():
1044                         self._base._focus = None
1045                         return True
1046                 return False