1 """<title>Menus, Toolboxes, a full Application</title>
2 Most all widgets are used in this example. A full custom widget
3 is included. A number of connections are used to make the application
7 from pygame.locals import *
9 # the following line is not needed if pgu is installed
10 import sys; sys.path.insert(0, "..")
14 from gui6 import AboutDialog
15 from gui7 import ColorDialog
16 from gui8 import NewDialog
18 class HelpDialog(gui.Dialog):
19 def __init__(self,**params):
20 title = gui.Label("Help")
22 doc = gui.Document(width=400)
24 space = title.style.font.size(" ")
27 doc.add(gui.Image("cuzco.png"),align=1)
28 for word in """Cuzco's Paint is a revolutionary new paint program it has all the awesome features that you need to paint really great pictures.""".split(" "):
29 doc.add(gui.Label(word))
34 for word in """This help isn't really here for any other reason than to have a help dialog.""".split(" "):
35 doc.add(gui.Label(word))
38 gui.Dialog.__init__(self,title,doc)
40 class QuitDialog(gui.Dialog):
41 def __init__(self,**params):
42 title = gui.Label("Quit")
47 t.add(gui.Label("Are you sure you want to quit?"),colspan=2)
50 e = gui.Button("Okay")
51 e.connect(gui.CLICK,self.send,gui.QUIT)
54 e = gui.Button("Cancel")
55 e.connect(gui.CLICK,self.close,None)
58 gui.Dialog.__init__(self,title,t)
60 class WelcomeDialog(gui.Dialog):
61 def __init__(self,**params):
62 title = gui.Label("Welcome")
64 doc = gui.Document(width=400)
66 space = title.style.font.size(" ")
69 doc.add(gui.Image("cuzco.png"),align=1)
70 for word in """Welcome to Cuzco's Paint. Cuzco's Paint is a demo of the features of Phil's Pygame GUI. Cuzco's Paint only supports saving in the .TGA format.""".split(" "):
71 doc.add(gui.Label(word))
74 gui.Dialog.__init__(self,title,doc)
76 class OpenDialog(gui.Dialog):
77 def __init__(self,**params):
78 title = gui.Label("Open Picture")
82 self.value = gui.Form()
85 t.td(gui.Label("Open: "))
86 t.td(gui.Input(name="fname"),colspan=3)
89 e = gui.Button("Okay")
90 e.connect(gui.CLICK,self.send,gui.CHANGE)
93 e = gui.Button("Cancel")
94 e.connect(gui.CLICK,self.close,None)
97 gui.Dialog.__init__(self,title,t)
99 class SaveDialog(gui.Dialog):
100 def __init__(self,**params):
101 title = gui.Label("Save As...")
105 self.value = gui.Form()
108 t.td(gui.Label("Save: "))
109 t.td(gui.Input(name="fname"),colspan=3)
112 e = gui.Button("Okay")
113 e.connect(gui.CLICK,self.send,gui.CHANGE)
116 e = gui.Button("Cancel")
117 e.connect(gui.CLICK,self.close,None)
120 gui.Dialog.__init__(self,title,t)
123 class Painter(gui.Widget):
124 def __init__(self,**params):
125 gui.Widget.__init__(self,**params)
129 self.cuzco = pygame.image.load("cuzco.png")
132 self.surface = pygame.Surface((int(v['width']),int(v['height'])))
134 if v['color'] == 'custom':
136 else: color = pygame.Color(color)
137 self.surface.fill(color)
138 self.overlay = pygame.Surface((int(v['width']),int(v['height'])),pygame.SRCALPHA,32)
142 if not self.surface: return
144 if e.type == gui.MOUSEBUTTONDOWN:
145 if hasattr(self,app.mode.value+"_down"):
146 action = getattr(self,app.mode.value+"_down")
148 if e.type == gui.MOUSEMOTION:
149 if hasattr(self,app.mode.value+"_motion"):
150 action = getattr(self,app.mode.value+"_motion")
152 if e.type is gui.MOUSEBUTTONUP:
153 if hasattr(self,app.mode.value+"_up"):
154 action = getattr(self,app.mode.value+"_up")
157 ##The Painter class has its own paint method to render the painting surface and overlay.
160 s.blit(self.surface,(0,0))
161 s.blit(self.overlay,(0,0))
164 def draw_down(self,e):
168 def draw_motion(self,e):
169 if self.state == 0: return
170 pygame.draw.line(self.surface,app.color.value,self.pos,e.pos,2)
176 def cuzco_down(self,e):
179 def cuzco_motion(self,e):
180 if self.state == 0: return
182 self.overlay.fill((0,0,0,0))
183 self.overlay.blit(img,(e.pos[0]-img.get_width()/2,e.pos[1]-img.get_height()/2))
185 def cuzco_up(self,e):
187 self.surface.blit(self.overlay,(0,0))
188 self.overlay.fill((0,0,0,0))
191 def box_down(self,e):
195 def box_motion(self,e):
196 if self.state == 0: return
197 self.overlay.fill((0,0,0,0))
198 pygame.draw.rect(self.overlay,app.color.value,(self.pos[0],self.pos[1],e.pos[0]-self.pos[0],e.pos[1]-self.pos[1]))
202 self.surface.blit(self.overlay,(0,0))
203 self.overlay.fill((0,0,0,0))
207 def circle_down(self,e):
210 self.circle_motion(e)
211 def circle_motion(self,e):
212 if self.state == 0: return
213 self.overlay.fill((0,0,0,0))
214 r = pygame.Rect(self.pos[0],self.pos[1],e.pos[0]-self.pos[0],e.pos[1]-self.pos[1])
220 pygame.draw.ellipse(self.overlay,app.color.value,r)
222 def circle_up(self,e):
224 self.surface.blit(self.overlay,(0,0))
225 self.overlay.fill((0,0,0,0))
230 class App(gui.Desktop):
231 def __init__(self,**params):
232 gui.Desktop.__init__(self,**params)
234 self.connect(gui.QUIT,self.quit,None)
236 c = gui.Container(width=640,height=480)
239 self.fname = 'untitled.tga'
241 self.new_d = NewDialog()
242 self.new_d.connect(gui.CHANGE,self.action_new,None)
243 self.open_d = OpenDialog()
244 self.open_d.connect(gui.CHANGE,self.action_open,None)
245 self.save_d = SaveDialog()
246 self.save_d.connect(gui.CHANGE,self.action_saveas,None)
247 self.quit_d = QuitDialog()
248 self.quit_d.connect(QUIT,self.quit,None)
250 self.help_d = HelpDialog()
251 self.about_d = AboutDialog()
253 ##Initializing the Menus, we connect to a number of Dialog.open methods for each of the dialogs.
256 ('File/New',self.new_d.open,None),
257 ('File/Open',self.open_d.open,None),
258 ('File/Save',self.action_save,None),
259 ('File/Save As',self.save_d.open,None),
260 ('File/Exit',self.quit_d.open,None),
261 ('Help/Help',self.help_d.open,None),
262 ('Help/About',self.about_d.open,None),
266 menus.rect.w,menus.rect.h = menus.resize()
267 #print 'menus',menus.rect
269 ##We utilize a Toolbox. The value of this widget determins how drawing is done in the Painter class.
271 self.mode = mode = gui.Toolbox([
276 ],cols=1,value='draw')
278 c.add(mode,0,menus.rect.bottom+spacer)
279 mode.rect.x,mode.rect.y = mode.style.x,mode.style.y
280 mode.rect.w,mode.rect.h = mode.resize()
284 self.color = color = gui.Color(default,width=mode.rect.w,height=mode.rect.w)
285 self.color_d = ColorDialog(default)
287 color.connect(gui.CLICK,self.color_d.open,None)
289 # Updates the toolbox color picker with the value in the color dialog box
290 def change_cb(*args):
291 self.color.value = self.color_d.value
292 self.color_d.connect(gui.CHANGE, change_cb)
294 c.add(self.color,0,mode.rect.bottom+spacer)
295 self.color.rect.w,self.color.rect.h = self.color.resize()
296 #self.color._resize()
298 self.painter = Painter(width=c.rect.w-mode.rect.w-spacer*2,height=c.rect.h-menus.rect.h-spacer*2,style={'border':1})
299 c.add(self.painter,mode.rect.w+spacer,menus.rect.h+spacer)
300 self.painter.init({'width':256,'height':256,'color':'#ffffff'})
301 self.painter.rect.w,self.painter.rect.h = self.painter.resize()
302 #self.painter._resize()
304 welcome_d = WelcomeDialog()
305 self.connect(gui.INIT,welcome_d.open,None)
309 def action_new(self,value):
311 self.fname = 'untitled.tga'
312 self.painter.init(self.new_d.value.results())
314 def action_save(self,value):
315 pygame.image.save(self.painter.surface,self.fname)
317 def action_saveas(self,value):
319 self.fname = self.save_d.value['fname'].value
320 pygame.image.save(self.painter.surface,self.fname)
322 def action_open(self,value):
324 self.fname = self.open_d.value['fname']
325 self.painter.surface = pygame.image.load(self.fname)
326 self.painter.repaint()