chiark / gitweb /
Move the OpenGL GUI base code to a seperate class for easier re-use.
[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
23                 wx.EVT_PAINT(self, self._OnGuiPaint)
24                 wx.EVT_SIZE(self, self._OnSize)
25                 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
26                 wx.EVT_MOUSE_EVENTS(self, self._OnGuiMouseEvents)
27                 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
28
29         def _OnGuiMouseEvents(self,e):
30                 if e.ButtonDown() and e.LeftIsDown():
31                         for ctrl in self._glGuiControlList:
32                                 if ctrl.OnMouseDown(e.GetX(), e.GetY()):
33                                         return
34
35         def _OnGuiMouseMotion(self,e):
36                 self.Refresh()
37                 for ctrl in self._glGuiControlList:
38                         if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
39                                 return
40                 self.OnMouseMotion(e)
41
42         def _OnGuiPaint(self, e):
43                 dc = wx.PaintDC(self)
44                 self.SetCurrent(self._context)
45                 self.OnPaint(e)
46                 self._drawGui()
47                 glFlush()
48                 self.SwapBuffers()
49
50         def _drawGui(self):
51                 glDisable(GL_DEPTH_TEST)
52                 glEnable(GL_BLEND)
53                 glDisable(GL_LIGHTING)
54                 glColor4ub(255,255,255,255)
55
56                 glMatrixMode(GL_PROJECTION)
57                 glLoadIdentity()
58                 size = self.GetSize()
59                 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
60                 glMatrixMode(GL_MODELVIEW)
61                 glLoadIdentity()
62
63                 for glButton in self._glGuiControlList:
64                         glButton.draw()
65
66         def _OnEraseBackground(self,event):
67                 #Workaround for windows background redraw flicker.
68                 pass
69
70         def _OnSize(self,e):
71                 self.Refresh()
72
73         def OnMouseEvents(self,e):
74                 pass
75
76         def OnMouseMotion(self, e):
77                 pass
78         def OnPaint(self, e):
79                 pass
80
81 class glButton(object):
82         def __init__(self, parent, imageID, tooltip, x, y, callback):
83                 self._tooltip = tooltip
84                 self._parent = parent
85                 self._imageID = imageID
86                 self._x = x
87                 self._y = y
88                 self._callback = callback
89                 self._parent._glGuiControlList.append(self)
90                 self._selected = False
91                 self._focus = False
92                 self._hidden = False
93
94         def setSelected(self, value):
95                 self._selected = value
96
97         def setHidden(self, value):
98                 self._hidden = value
99
100         def getSelected(self):
101                 return self._selected
102
103         def draw(self):
104                 global glButtonsTexture
105                 if self._hidden:
106                         return
107                 if glButtonsTexture is None:
108                         glButtonsTexture = opengl.loadGLTexture('glButtons.png')
109
110                 cx = (self._imageID % 4) / 4
111                 cy = int(self._imageID / 4) / 4
112                 bs = self._parent._buttonSize
113
114                 glPushMatrix()
115                 glTranslatef(self._x * bs * 1.3 + bs * 0.8, self._y * bs * 1.3 + bs * 0.8, 0)
116                 glBindTexture(GL_TEXTURE_2D, glButtonsTexture)
117                 glEnable(GL_TEXTURE_2D)
118                 scale = 0.8
119                 if self._selected:
120                         scale = 1.0
121                 elif self._focus:
122                         scale = 0.9
123                 glScalef(bs * scale, bs * scale, bs * scale)
124                 glColor4ub(255,255,255,255)
125                 glBegin(GL_QUADS)
126                 glTexCoord2f(cx+0.25, cy)
127                 glVertex2f( 0.5,-0.5)
128                 glTexCoord2f(cx, cy)
129                 glVertex2f(-0.5,-0.5)
130                 glTexCoord2f(cx, cy+0.25)
131                 glVertex2f(-0.5, 0.5)
132                 glTexCoord2f(cx+0.25, cy+0.25)
133                 glVertex2f( 0.5, 0.5)
134                 glEnd()
135                 glDisable(GL_TEXTURE_2D)
136                 if self._focus:
137                         glColor4ub(0,0,0,255)
138                         glTranslatef(0, -0.55, 0)
139                         opengl.glDrawStringCenter(self._tooltip)
140                 glPopMatrix()
141
142         def _checkHit(self, x, y):
143                 if self._hidden:
144                         return False
145                 bs = self._parent._buttonSize
146                 return -bs * 0.5 <= x - (self._x * bs * 1.3 + bs * 0.8) <= bs * 0.5 and -bs * 0.5 <= y - (self._y * bs * 1.3 + bs * 0.8) <= bs * 0.5
147
148         def OnMouseMotion(self, x, y):
149                 if self._checkHit(x, y):
150                         self._focus = True
151                         return True
152                 self._focus = False
153                 return False
154
155         def OnMouseDown(self, x, y):
156                 if self._checkHit(x, y):
157                         self._callback()