Refactoring: use Match to handle interactions.
[kaya.git] / lib / board / table.rb
blobe1c8602ef1e287d0e6dd3db77ebfef7c2b3c75a4
1 class Table < Qt::GraphicsView
2   attr_reader :elements
4   Theme = Struct.new(:pieces, :board, :layout)
6   def initialize(scene, loader, parent)
7     super(@scene = scene, parent)
8     @loader = loader
9   end
10   
11   def reset(game)
12     # destroy old elements
13     if @elements
14       @scene.remove_element(@elements[:board])
15       @elements[:pools].each do |col, pool|
16         @scene.remove_element(pool)
17       end
18       @elements[:clocks].each do |col, clock|
19         @scene.remove_element(clock)
20       end
21     end
22     
23     # load theme
24     @theme = Theme.new
25     @theme.pieces = @loader.
26       get_matching((game.keywords || []) + %w(pieces)).
27       new(:game => game, :shadow => true)
28     @theme.board = @loader.
29       get_matching(%w(board), game.keywords || []).
30       new(:game => game)
31     @theme.layout = @loader.
32       get_matching(%w(layout), game.keywords || []).
33       new(game)
35     # recreate elements
36     @elements = { }
37     @elements[:board] = Board.new(@scene, @theme, game)
38     @elements[:pools] = if game.respond_to? :pool
39       game.players.inject({}) do |res, player|
40         res[player] = Pool.new(@scene, @theme, game)
41         res
42       end
43     else
44       {}
45     end
46     clock_class = @loader.get_matching(%w(clock))
47     @elements[:clocks] = game.players.inject({}) do |res, player|
48       res[player] = clock_class.new(scene)
49       res
50     end
51     
52     # relayout elements
53     unless @scene.scene_rect.isNull
54       @theme.layout.layout(@scene.scene_rect, @elements)
55     end
56   end
57   
58   def resizeEvent(e)
59     r = Qt::RectF.new(0, 0, e.size.width, e.size.height)
60     @scene.scene_rect = r
61     if @elements
62       @theme.layout.layout(r, @elements)
63     end
64   end
65 end