Implemented layouting.
[kaya.git] / lib / mainwindow.rb
blobd8c374ec0ec04076b56d87d4aee83a1473e9651e
1 require 'qtutils'
2 require 'board/board'
3 require 'board/pool'
4 require 'board/table'
5 require 'history'
6 require 'controller'
8 class MainWindow < KDE::XmlGuiWindow
9   include ActionHandler
10   
11   Theme = Struct.new(:pieces, :board, :layout)
12   
13   def initialize(loader, game)
14     super nil
15     
16     @loader = loader
17     
18     load_board(game)
19     
20     setup_actions
21     setupGUI
22   end
24 private
26   def setup_actions
27     std_action :open_new do
28       puts "new game"
29     end
30     std_action :quit, :slot => :close
31     regular_action :back, :icon => 'go-previous', 
32                           :text => KDE.i18n("&Back") do
33       @board.fire :back
34     end
35     regular_action :forward, :icon => 'go-next', 
36                              :text => KDE.i18n("&Forward") do
37       @board.fire :forward
38     end
39                   
40   end
41   
42   def load_board(game)
43     config = KDE::Global.config.group('themes')
44     
45     theme = Theme.new
46     theme.pieces = @loader.get_matching('Celtic', game,
47       (game.keywords || []) + ['pieces'], [], :shadow => true)
48     theme.board = @loader.get_matching(nil, game,
49       ['board'], game.keywords || [])
50     theme.layout = @loader.get_matching(nil, game,
51       ['layout'], game.keywords || [])
52     
53     scene = Qt::GraphicsScene.new
54     
55     state = game.state.new.tap {|s| s.setup }
56     
57     field = AnimationField.new(20)
58     @board = Board.new(scene, theme, game, state, field)
59     if game.respond_to? :pool
60       @pools = game.players.inject({}) do |res, player|
61         res[player] = Pool.new(scene, theme, game, state.pool(player), field)
62         res
63       end
64     end
65     
66     table = Table.new scene, theme, self,
67       :board => @board,
68       :pools => @pools
69       
70     self.central_widget = table
72     history = History.new(state)
73     controller = Controller.new(@board, history)
74   end
75 end