Quit, Rest-Knopf Funktionieren
[darcs-mirror-ozeanien.git] / ozeanien.py
1 #!/usr/bin/python
2 # encoding:utf8
3
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8 import sys
9
10 from engine import * 
11 from seatheme import SeaFabric
12 from grasstheme import GrassFabric
13
14
15 DEBUG = 1
16
17 def dbg(txt):
18     if DEBUG:
19         print txt
20
21
22 class Game:
23     def destroy(self, widget, data=None):
24         gtk.main_quit()
25
26     def __init__(self):
27         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
28         self.window.set_title("Ozeanien")
29         self.window.set_border_width(2)
30         self.window.set_resizable(False)
31         self.window.connect("destroy", self.destroy)
32         self.window.set_icon(gtk.gdk.pixbuf_new_from_file('icon.png'))
33
34         self.reset()
35
36         self.window.show_all()
37
38     def reset(self):
39         if self.window.get_child():
40             w = self.window.get_child()
41             self.window.remove(self.window.get_child())
42             w.destroy()
43
44         self.board = Board()
45         self.drag_from = None
46
47         if len(sys.argv) > 1 and sys.argv[1] == "sea": 
48             self.boarddraw = SeaFabric().get_widget(self.board)
49         else:
50             self.boarddraw = GrassFabric().get_widget(self.board)
51
52         self.boarddraw.connect("button-press-event",   self.press)
53         self.boarddraw.connect("button-release-event", self.release)
54
55         self.boarddraw.show()
56
57         self.window.add(self.boarddraw)
58
59     def press(self, widget, event):
60         assert not self.drag_from
61
62         # Doppelclick ignorieren
63         if event.type != gtk.gdk.BUTTON_PRESS:
64             return
65
66         if self.boarddraw.event2quit(event):
67             self.window.destroy()
68             return
69
70         if self.boarddraw.event2reset(event):
71             self.reset()
72             return
73
74         slot = self.boarddraw.event2slot(event)
75
76         if self.board.get_state() == SHIPPING:
77             if slot:
78                 if slot.ship:
79                     self.drag_from = slot
80                     self.board.hand.move_ship_from(slot)
81             elif self.boarddraw.pileclick(event):
82                 if self.board.pile.can_flip_card():
83                     self.board.pile.flip_card()
84
85         elif self.board.get_state() == PLACING:
86             if slot:
87                 if slot.card and not slot.card.placed:
88                     self.drag_from = slot
89                     self.board.hand.set_card(slot.card)
90                     slot.card = None
91
92         rot = self.boarddraw.pilerotclick(event)
93         if rot == 1:
94             self.board.pile.rotate_right()
95         elif rot == -1:
96             self.board.pile.rotate_left()
97
98         self.boarddraw.queue_draw()
99
100
101     def release(self, widget, event):
102         slot = self.boarddraw.event2slot(event)
103         if self.board.hand.ship:
104             if slot and slot.can_drop_ship():
105                 slot.move_ship_from(self.board.hand)
106                 self.drag_from = None
107             else:
108                 self.drag_from.move_ship_from(self.board.hand)
109                 self.drag_from = None
110
111         if self.board.hand.card:
112             if slot and slot.can_drop_card(self.board.hand.card):
113                 slot.set_card(self.board.hand.card)
114                 self.board.hand.card = None
115                 self.drag_from = None
116             else:
117                 self.drag_from.set_card(self.board.hand.card)
118                 self.board.hand.card = None
119                 self.drag_from = None
120
121         self.boarddraw.queue_draw()
122         self.board.calc_points()
123
124     def main(self):
125         gtk.main()
126
127 if __name__ == "__main__":
128     game = Game()
129     game.main()
130
131
132 # vim:ts=4:sw=4:sts=4:et