Fix crash during clock updates.
[kaya.git] / lib / newgame.rb
blob6b17e57d165f818f67cf00e3860d7ffd81ef48ca
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 'qtutils'
10 class NewGame < KDE::Dialog
11   include Observable
12   
13   def initialize(parent, 
14                  engine_loader, 
15                  current_game)
16     super(parent)
17     self.caption = KDE.i18n("New game")
18     self.buttons = KDE::Dialog::Ok | KDE::Dialog::Cancel
19     
20     @widget = Qt::Widget.new(self)
21     
22     @engine_loader = engine_loader
23     label = Qt::Label.new(KDE.i18n("&Game:"), @widget)
24     @games = Game.new_combo(@widget)
25     
26     label.buddy = @games
27     hlayout = Qt::HBoxLayout.new
28     hlayout.add_widget(label)
29     hlayout.add_widget(@games)
30     @players = { }
32     @layout = Qt::VBoxLayout.new(@widget)
33     @layout.add_layout(hlayout)
35     current = (0...@games.count).
36       map{|i| @games.item_data(i).toString }.
37       index(current_game.class.data(:id).to_s)
38     @games.current_index = current if current
39     @games.on('currentIndexChanged(int)') do |index|
40       update_players(index)
41     end
42     
43     update_players(@games.current_index)
44     
45     self.main_widget = @widget
46     
47     on(:okClicked) do
48       engines = { }
49       humans = []
50       g = game
51       g.players.each do |player|
52         # iterate over the player array to preserve order
53         combo = @players[player]
54         data = combo.item_data(combo.current_index).to_a
55         if data.first == 'engine'
56           engine_name = data[1]
57           engines[player] = engine_loader[engine_name]
58         else
59           humans << player
60         end
61       end
62       fire :ok => {
63         :game => game,
64         :engines => engines,
65         :humans => humans }
66     end
67   end
68   
69   def update_players(index)
70     @player_widget.dispose if @player_widget
71     @player_widget = Qt::Widget.new(@widget)
72     layout = Qt::VBoxLayout.new(@player_widget)
73     layout.margin = 0
74     @layout.add_widget(@player_widget)
75     
76     @players = { }
77     g = game(index)
78     engines = @engine_loader.find_by_game(g)
79     g.players.each do |player|
80       label = Qt::Label.new(player.to_s.capitalize + ':', @player_widget)
81       combo = KDE::ComboBox.new(@player_widget) do
82         self.editable = false
83         add_item(KDE.i18n('Human'), Qt::Variant.new(['human']))
84       end
85       engines.each do |id, engine|
86         combo.add_item(engine.name, Qt::Variant.new(['engine', engine.name]))
87       end
88       @players[player] = combo
89       
90       hlayout = Qt::HBoxLayout.new
91       hlayout.add_widget(label)
92       hlayout.add_widget(combo)
93       layout.add_layout(hlayout)
94     end
95   end
96   
97   def game(index = nil)
98     index = @games.current_index if index.nil?
99     game_id = @games.item_data(index).toString.to_sym
100     Game.get(game_id)
101   end