chiark / gitweb /
Merge pull request #347 from smorloc/master
[cura.git] / Cura / gui / util / openglGui.py
1 from __future__ import absolute_import
2 from __future__ import division
3
4 import wx
5 from wx import glcanvas
6 import OpenGL
7 OpenGL.ERROR_CHECKING = False
8 from OpenGL.GL import *
9
10 from Cura.gui.util import opengl
11
12 glButtonsTexture = None
13
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)
18                 self._parent = parent
19                 self._context = glcanvas.GLContext(self)
20                 self._glGuiControlList = []
21                 self._buttonSize = 64
22                 self._allowDrag = False
23
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)
29
30         def _OnGuiMouseEvents(self,e):
31                 if e.ButtonDown():
32                         if e.LeftIsDown():
33                                 for ctrl in self._glGuiControlList:
34                                         if ctrl.OnMouseDown(e.GetX(), e.GetY()):
35                                                 return
36                         self._allowDrag = True
37                         print 1
38                 if e.ButtonUp():
39                         if not e.LeftIsDown() and not e.RightIsDown():
40                                 self._allowDrag = False
41                                 print 0
42
43         def _OnGuiMouseMotion(self,e):
44                 self.Refresh()
45                 for ctrl in self._glGuiControlList:
46                         if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
47                                 return
48                 self.OnMouseMotion(e)
49
50         def _OnGuiPaint(self, e):
51                 dc = wx.PaintDC(self)
52                 self.SetCurrent(self._context)
53                 self.OnPaint(e)
54                 self._drawGui()
55                 glFlush()
56                 self.SwapBuffers()
57
58         def _drawGui(self):
59                 glDisable(GL_DEPTH_TEST)
60                 glEnable(GL_BLEND)
61                 glDisable(GL_LIGHTING)
62                 glColor4ub(255,255,255,255)
63
64                 glMatrixMode(GL_PROJECTION)
65                 glLoadIdentity()
66                 size = self.GetSize()
67                 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
68                 glMatrixMode(GL_MODELVIEW)
69                 glLoadIdentity()
70
71                 for glButton in self._glGuiControlList:
72                         glButton.draw()
73
74         def _OnEraseBackground(self,event):
75                 #Workaround for windows background redraw flicker.
76                 pass
77
78         def _OnSize(self,e):
79                 self.Refresh()
80
81         def OnMouseEvents(self,e):
82                 pass
83
84         def OnMouseMotion(self, e):
85                 pass
86         def OnPaint(self, e):
87                 pass
88
89 class glButton(object):
90         def __init__(self, parent, imageID, tooltip, x, y, callback):
91                 self._tooltip = tooltip
92                 self._parent = parent
93                 self._imageID = imageID
94                 self._x = x
95                 self._y = y
96                 self._callback = callback
97                 self._parent._glGuiControlList.append(self)
98                 self._selected = False
99                 self._focus = False
100                 self._hidden = False
101
102         def setSelected(self, value):
103                 self._selected = value
104
105         def setHidden(self, value):
106                 self._hidden = value
107
108         def getSelected(self):
109                 return self._selected
110
111         def _getSize(self):
112                 return self._parent._buttonSize
113
114         def _getPixelPos(self):
115                 bs = self._getSize()
116                 x = self._x * bs * 1.3 + bs * 0.8
117                 y = self._y * bs * 1.3 + bs * 0.8
118                 if self._x < 0:
119                         x = self._parent.GetSize().GetWidth() + x - bs * 0.2
120                 return x, y
121
122         def draw(self):
123                 global glButtonsTexture
124                 if self._hidden:
125                         return
126                 if glButtonsTexture is None:
127                         glButtonsTexture = opengl.loadGLTexture('glButtons.png')
128
129                 cx = (self._imageID % 4) / 4
130                 cy = int(self._imageID / 4) / 4
131                 bs = self._parent._buttonSize
132                 pos = self._getPixelPos()
133
134                 glPushMatrix()
135                 glTranslatef(pos[0], pos[1], 0)
136                 glBindTexture(GL_TEXTURE_2D, glButtonsTexture)
137                 glEnable(GL_TEXTURE_2D)
138                 scale = 0.8
139                 if self._selected:
140                         scale = 1.0
141                 elif self._focus:
142                         scale = 0.9
143                 glScalef(bs * scale, bs * scale, bs * scale)
144                 glColor4ub(255,255,255,255)
145                 glBegin(GL_QUADS)
146                 glTexCoord2f(cx+0.25, cy)
147                 glVertex2f( 0.5,-0.5)
148                 glTexCoord2f(cx, cy)
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)
154                 glEnd()
155                 glDisable(GL_TEXTURE_2D)
156                 if self._focus:
157                         glColor4ub(0,0,0,255)
158                         glTranslatef(0, -0.55, 0)
159                         opengl.glDrawStringCenter(self._tooltip)
160                 glPopMatrix()
161
162         def _checkHit(self, x, y):
163                 if self._hidden:
164                         return False
165                 bs = self._getSize()
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
168
169         def OnMouseMotion(self, x, y):
170                 if self._checkHit(x, y):
171                         self._focus = True
172                         return True
173                 self._focus = False
174                 return False
175
176         def OnMouseDown(self, x, y):
177                 if self._checkHit(x, y):
178                         self._callback()
179                         return True
180                 return False