56ff1cb808c879611016aa441bf1ad89121b2470
[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.board = Board()
28         self.drag_from = None
29
30         if len(sys.argv) > 1 and sys.argv[1] == "sea": 
31             self.boarddraw = SeaFabric().get_widget(self.board)
32         else:
33             self.boarddraw = GrassFabric().get_widget(self.board)
34
35         self.boarddraw.connect("button-press-event",   self.press)
36         self.boarddraw.connect("button-release-event", self.release)
37
38         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
39         self.window.set_title("Ozeanien")
40         self.window.set_border_width(2)
41         self.window.set_resizable(False)
42         self.window.connect("destroy", self.destroy)
43         self.window.set_icon(gtk.gdk.pixbuf_new_from_file('icon.png'))
44         self.window.add(self.boarddraw)
45
46         self.window.show_all()
47
48     def press(self, widget, event):
49         assert not self.drag_from
50
51         # Doppelclick ignorieren
52         if event.type != gtk.gdk.BUTTON_PRESS:
53             return
54
55         slot = self.boarddraw.event2slot(event)
56
57         if self.board.get_state() == SHIPPING:
58             if slot:
59                 if slot.ship:
60                     self.drag_from = slot
61                     self.board.hand.move_ship_from(slot)
62             elif self.boarddraw.pileclick(event):
63                 if self.board.pile.can_flip_card():
64                     self.board.pile.flip_card()
65
66         elif self.board.get_state() == PLACING:
67             if slot:
68                 if slot.card and not slot.card.placed:
69                     self.drag_from = slot
70                     self.board.hand.set_card(slot.card)
71                     slot.card = None
72
73         rot = self.boarddraw.pilerotclick(event)
74         if rot == 1:
75             self.board.pile.rotate_right()
76         elif rot == -1:
77             self.board.pile.rotate_left()
78
79         self.boarddraw.queue_draw()
80
81
82     def release(self, widget, event):
83         slot = self.boarddraw.event2slot(event)
84         if self.board.hand.ship:
85             if slot and slot.can_drop_ship():
86                 slot.move_ship_from(self.board.hand)
87                 self.drag_from = None
88             else:
89                 self.drag_from.move_ship_from(self.board.hand)
90                 self.drag_from = None
91
92         if self.board.hand.card:
93             if slot and slot.can_drop_card(self.board.hand.card):
94                 slot.set_card(self.board.hand.card)
95                 self.board.hand.card = None
96                 self.drag_from = None
97             else:
98                 self.drag_from.set_card(self.board.hand.card)
99                 self.board.hand.card = None
100                 self.drag_from = None
101
102         self.boarddraw.queue_draw()
103         self.board.calc_points()
104
105     def main(self):
106         gtk.main()
107
108 if __name__ == "__main__":
109     game = Game()
110     game.main()
111
112
113 # vim:ts=4:sw=4:sts=4:et