1 from __future__ import absolute_import
2 from __future__ import division
5 from wx import glcanvas
7 OpenGL.ERROR_CHECKING = False
8 from OpenGL.GL import *
10 from Cura.gui.util import opengl
12 glButtonsTexture = None
14 class glGuiPanel(glcanvas.GLCanvas):
15 def __init__(self, parent):
16 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8)
17 glcanvas.GLCanvas.__init__(self, parent, attribList = attribList)
19 self._context = glcanvas.GLContext(self)
20 self._glGuiControlList = []
21 self._glButtonsTexture = None
24 wx.EVT_PAINT(self, self._OnGuiPaint)
25 wx.EVT_SIZE(self, self._OnSize)
26 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
27 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseLeftDown)
28 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
30 def _OnGuiMouseLeftDown(self,e):
31 for ctrl in self._glGuiControlList:
32 if ctrl.OnMouseDown(e.GetX(), e.GetY()):
34 self.OnMouseLeftDown(e)
36 def _OnGuiMouseMotion(self,e):
38 for ctrl in self._glGuiControlList:
39 if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
43 def _OnGuiPaint(self, e):
45 self.SetCurrent(self._context)
52 glDisable(GL_DEPTH_TEST)
54 glDisable(GL_LIGHTING)
55 glColor4ub(255,255,255,255)
57 glMatrixMode(GL_PROJECTION)
60 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
61 glMatrixMode(GL_MODELVIEW)
64 for glButton in self._glGuiControlList:
67 def _OnEraseBackground(self,event):
68 #Workaround for windows background redraw flicker.
74 def OnMouseLeftDown(self,e):
76 def OnMouseMotion(self, e):
81 class glButton(object):
82 def __init__(self, parent, imageID, tooltip, x, y, callback):
83 self._tooltip = tooltip
85 self._imageID = imageID
88 self._callback = callback
89 self._parent._glGuiControlList.append(self)
90 self._selected = False
94 def setSelected(self, value):
95 self._selected = value
97 def setHidden(self, value):
100 def getSelected(self):
101 return self._selected
104 return self._parent._buttonSize
106 def _getPixelPos(self):
108 x = self._x * bs * 1.3 + bs * 0.8
109 y = self._y * bs * 1.3 + bs * 0.8
111 x = self._parent.GetSize().GetWidth() + x - bs * 0.2
117 if self._parent._glButtonsTexture is None:
118 self._parent._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
120 cx = (self._imageID % 4) / 4
121 cy = int(self._imageID / 4) / 4
122 bs = self._parent._buttonSize
123 pos = self._getPixelPos()
126 glTranslatef(pos[0], pos[1], 0)
127 glBindTexture(GL_TEXTURE_2D, self._parent._glButtonsTexture)
128 glEnable(GL_TEXTURE_2D)
134 glScalef(bs * scale, bs * scale, bs * scale)
135 glColor4ub(255,255,255,255)
137 glTexCoord2f(cx+0.25, cy)
138 glVertex2f( 0.5,-0.5)
140 glVertex2f(-0.5,-0.5)
141 glTexCoord2f(cx, cy+0.25)
142 glVertex2f(-0.5, 0.5)
143 glTexCoord2f(cx+0.25, cy+0.25)
144 glVertex2f( 0.5, 0.5)
146 glDisable(GL_TEXTURE_2D)
148 glColor4ub(0,0,0,255)
149 glTranslatef(0, -0.55, 0)
150 opengl.glDrawStringCenter(self._tooltip)
153 def _checkHit(self, x, y):
157 pos = self._getPixelPos()
158 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
160 def OnMouseMotion(self, x, y):
161 if self._checkHit(x, y):
167 def OnMouseDown(self, x, y):
168 if self._checkHit(x, y):