Quit, Rest-Knopf Funktionieren
authorJoachim Breitner <mail@joachim-breitner.de>
Wed, 23 May 2007 09:49:17 +0000 (09:49 +0000)
committerJoachim Breitner <mail@joachim-breitner.de>
Wed, 23 May 2007 09:49:17 +0000 (09:49 +0000)
cairoboard.py
ozeanien.py

index 729591c..8a4d49d 100644 (file)
@@ -30,6 +30,9 @@ class CairoBoard(gtk.DrawingArea):
         self.fabric = fabric
 
         self.base_surface = None
+
+        self.quit_rect = None
+        self.reset_rect = None
         
         self.highlighted_slot = None
 
@@ -99,6 +102,20 @@ class CairoBoard(gtk.DrawingArea):
         else:
             return 0
 
+    def event2quit(self, event):
+        if self.quit_rect:
+            (x,y,w,h) = self.quit_rect
+            return (x <= event.x <= x + w) and (y <= event.y <= y + h)
+        else:
+            return False
+
+    def event2reset(self, event):
+        if self.reset_rect:
+            (x,y,w,h) = self.reset_rect
+            return (x <= event.x <= x + w) and (y <= event.y <= y + h)
+        else:
+            return False
+
     def event2slot(self, event):
         x = int((event.x - GRIDPAD) // GRIDWIDTH)
         y = int((event.y - GRIDPAD) // GRIDWIDTH)
index 56ff1cb..da9bbad 100644 (file)
@@ -24,6 +24,23 @@ class Game:
         gtk.main_quit()
 
     def __init__(self):
+        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
+        self.window.set_title("Ozeanien")
+        self.window.set_border_width(2)
+        self.window.set_resizable(False)
+        self.window.connect("destroy", self.destroy)
+        self.window.set_icon(gtk.gdk.pixbuf_new_from_file('icon.png'))
+
+        self.reset()
+
+        self.window.show_all()
+
+    def reset(self):
+        if self.window.get_child():
+            w = self.window.get_child()
+            self.window.remove(self.window.get_child())
+            w.destroy()
+
         self.board = Board()
         self.drag_from = None
 
@@ -35,15 +52,9 @@ class Game:
         self.boarddraw.connect("button-press-event",   self.press)
         self.boarddraw.connect("button-release-event", self.release)
 
-        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
-        self.window.set_title("Ozeanien")
-        self.window.set_border_width(2)
-        self.window.set_resizable(False)
-        self.window.connect("destroy", self.destroy)
-        self.window.set_icon(gtk.gdk.pixbuf_new_from_file('icon.png'))
-        self.window.add(self.boarddraw)
+        self.boarddraw.show()
 
-        self.window.show_all()
+        self.window.add(self.boarddraw)
 
     def press(self, widget, event):
         assert not self.drag_from
@@ -52,6 +63,14 @@ class Game:
         if event.type != gtk.gdk.BUTTON_PRESS:
             return
 
+        if self.boarddraw.event2quit(event):
+            self.window.destroy()
+            return
+
+        if self.boarddraw.event2reset(event):
+            self.reset()
+            return
+
         slot = self.boarddraw.event2slot(event)
 
         if self.board.get_state() == SHIPPING: