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