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 = []
22 self._allowDrag = False
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_MOUSE_EVENTS(self, self._OnGuiMouseEvents)
28 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
30 def _OnGuiMouseEvents(self,e):
33 for ctrl in self._glGuiControlList:
34 if ctrl.OnMouseDown(e.GetX(), e.GetY()):
36 self._allowDrag = True
39 if not e.LeftIsDown() and not e.RightIsDown():
40 self._allowDrag = False
43 def _OnGuiMouseMotion(self,e):
45 for ctrl in self._glGuiControlList:
46 if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
50 def _OnGuiPaint(self, e):
52 self.SetCurrent(self._context)
59 glDisable(GL_DEPTH_TEST)
61 glDisable(GL_LIGHTING)
62 glColor4ub(255,255,255,255)
64 glMatrixMode(GL_PROJECTION)
67 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
68 glMatrixMode(GL_MODELVIEW)
71 for glButton in self._glGuiControlList:
74 def _OnEraseBackground(self,event):
75 #Workaround for windows background redraw flicker.
81 def OnMouseEvents(self,e):
84 def OnMouseMotion(self, e):
89 class glButton(object):
90 def __init__(self, parent, imageID, tooltip, x, y, callback):
91 self._tooltip = tooltip
93 self._imageID = imageID
96 self._callback = callback
97 self._parent._glGuiControlList.append(self)
98 self._selected = False
102 def setSelected(self, value):
103 self._selected = value
105 def setHidden(self, value):
108 def getSelected(self):
109 return self._selected
112 return self._parent._buttonSize
114 def _getPixelPos(self):
116 x = self._x * bs * 1.3 + bs * 0.8
117 y = self._y * bs * 1.3 + bs * 0.8
119 x = self._parent.GetSize().GetWidth() + x - bs * 0.2
123 global glButtonsTexture
126 if glButtonsTexture is None:
127 glButtonsTexture = opengl.loadGLTexture('glButtons.png')
129 cx = (self._imageID % 4) / 4
130 cy = int(self._imageID / 4) / 4
131 bs = self._parent._buttonSize
132 pos = self._getPixelPos()
135 glTranslatef(pos[0], pos[1], 0)
136 glBindTexture(GL_TEXTURE_2D, glButtonsTexture)
137 glEnable(GL_TEXTURE_2D)
143 glScalef(bs * scale, bs * scale, bs * scale)
144 glColor4ub(255,255,255,255)
146 glTexCoord2f(cx+0.25, cy)
147 glVertex2f( 0.5,-0.5)
149 glVertex2f(-0.5,-0.5)
150 glTexCoord2f(cx, cy+0.25)
151 glVertex2f(-0.5, 0.5)
152 glTexCoord2f(cx+0.25, cy+0.25)
153 glVertex2f( 0.5, 0.5)
155 glDisable(GL_TEXTURE_2D)
157 glColor4ub(0,0,0,255)
158 glTranslatef(0, -0.55, 0)
159 opengl.glDrawStringCenter(self._tooltip)
162 def _checkHit(self, x, y):
166 pos = self._getPixelPos()
167 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
169 def OnMouseMotion(self, x, y):
170 if self._checkHit(x, y):
176 def OnMouseDown(self, x, y):
177 if self._checkHit(x, y):