Update connect/disconnect action states.
[kaya.git] / lib / newgame.rb
blobe7527416963360bd6f690441863735303208e400
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'toolkit'
10 class NewGame < KDE::Dialog
11   include Observable
12   
13   def initialize(parent, engine_loader, current_game)
14     super(parent)
15     @engine_loader = engine_loader
16     @players = { }
17     
18     gui = KDE::autogui(:new_game, :caption => KDE::i18n("New game")) do |g|
19       g.layout(:type => :vertical) do |l|
20         l.check_box(:new_tab, :text => KDE::i18n("Open in new &tab"))
21         
22         l.layout(:type => :horizontal) do |h|
23           h.label(:text => KDE::i18n("&Game:"), :buddy => :games)
24           h.widget(:games, :factory => Factory.new{|p| Game.new_combo(p)})
25         end
26         
27         l.widget(:player_widget, :factory => Qt::Widget)
28       end
29     end
30     setGUI(gui)
31     
32     if current_game
33       current = (0...games.count).
34         map{|i| games.item_data(i).toString }.
35         index(current_game.class.data(:id).to_s)
36     end
37     games.current_index = current if current
38     games.on(:current_index_changed, ["int"]) do |index|
39       update_players(index)
40     end
41     
42     update_players(games.current_index)
43     
44     on(:ok_clicked) do
45       engines = { }
46       humans = []
47       g = game
48       g.players.each do |player|
49         # iterate over the player array to preserve order
50         combo = @players[player]
51         data = combo.item_data(combo.current_index).to_a
52         if data.first == 'engine'
53           engine_name = data[1]
54           engines[player] = engine_loader[engine_name]
55         else
56           humans << player
57         end
58       end
59       fire :ok => {
60         :game => game,
61         :engines => engines,
62         :humans => humans,
63         :new_tab => new_tab.checked? }
64     end
65   end
66   
67   def update_players(index)
68     parent = player_widget.parent
69     player_widget.dispose
70     player_widget = Qt::Widget.new(parent)
71     main_layout.add_widget(player_widget)
72     
73     @players = { }
74     g = game(index)
75     engines = @engine_loader.find_by_game(g)
76     
77     gui = KDE::autogui(:player_widget) do |b|
78       b.layout(:type => :vertical, :margin => 0) do |l|
79         g.players.each do |player|
80           l.layout(:type => :horizontal) do |h|
81             h.label(:text => "&#{g.translate(player)}:",
82                     :buddy => player)
83             h.combo_box(player)
84           end
85         end
86       end
87     end
88     player_widget.setGUI(gui)
89     parent.owner.add_accessor(:player_widget, player_widget)
90     
91     g.players.each do |player|
92       combo = player_widget.send(player)
93       combo.editable = false
94       combo.add_item(KDE.i18n('Human'), Qt::Variant.new(['human']))
95       
96       engines.each do |id, engine|
97         combo.add_item(engine.name, Qt::Variant.new(['engine', engine.name]))
98       end
99       @players[player] = combo
100     end
101   end
102   
103   def game(index = nil)
104     index = games.current_index if index.nil?
105     game_id = games.item_data(index).toString.to_sym
106     Game.get(game_id)
107   end