Avoid relayout for null flip
[kaya.git] / lib / board / table.rb
blob4b505af82c43dadf57ded95dc8594d323da3e8de
1 class Table < Qt::GraphicsView
2   include Observable
3   
4   attr_reader :elements
6   Theme = Struct.new(:pieces, :board, :layout)
8   def initialize(scene, loader, parent)
9     super(@scene = scene, parent)
10     @loader = loader
11   end
12   
13   def reset(match)
14     game = match.game
15     # destroy old elements
16     if @elements
17       @scene.remove_element(@elements[:board])
18       @elements[:pools].each do |col, pool|
19         @scene.remove_element(pool)
20       end
21       @elements[:clocks].each do |col, clock|
22         @scene.remove_element(clock)
23       end
24     end
25     
26     # load theme
27     @theme = Theme.new
28     @theme.pieces = @loader.
29       get_matching((game.keywords || []) + %w(pieces)).
30       new(:game => game, :shadow => true)
31     @theme.board = @loader.
32       get_matching(%w(board), game.keywords || []).
33       new(:game => game)
34     @theme.layout = @loader.
35       get_matching(%w(layout), game.keywords || []).
36       new(game)
38     # recreate elements
39     @elements = { }
40     @elements[:board] = Board.new(@scene, @theme, game)
41     @elements[:pools] = if game.respond_to? :pool
42       game.players.inject({}) do |res, player|
43         res[player] = Pool.new(@scene, @theme, game)
44         res
45       end
46     else
47       {}
48     end
49     clock_class = @loader.get_matching(%w(clock))
50     @elements[:clocks] = game.players.inject({}) do |res, player|
51       res[player] = clock_class.new(scene)
52       res
53     end
54     
55     relayout
56     fire :reset => match
57   end
59   def flip(value)
60     if flipped? != value
61       @theme.layout.flip(value)
62       @theme.board.flip(value)
63       @theme.pieces.flip(value)
64       relayout
65     end
66   end
68   def resizeEvent(e)
69     @initialized = true
70     r = Qt::RectF.new(0, 0, e.size.width, e.size.height)
71     @scene.scene_rect = r
72     relayout if @elements
73   end
74   
75   def relayout
76     if @initialized
77       @theme.layout.layout(@scene.scene_rect, @elements)
78     end
79   end
80   
81   def flipped?
82     @theme.layout.flipped?
83   end
84 end