Introduce the concept of plugin interface.
[kaya.git] / lib / plugins / layouts / cool / cool.rb
blob127dbd1792522a5d0711f9a03644a50ebc224e4d
1 require 'plugins/plugin'
3 class CoolLayout
4   include Plugin
5   
6   plugin :name => 'Layouts/Cool',
7          :interface => :layout
8         
9   # values relative to unit = 1
10   MARGIN = 0.2
11   CLOCK_WIDTH = 2.6
12   CLOCK_HEIGHT_RATIO = 0.4
13         
14   def initialize(game)
15     @game = game
16     @size = @game.size
17     @flipped = false
18   end
19         
20   def layout(rect, elements)
21     xrel = @size.x + MARGIN * 3 + CLOCK_WIDTH
22     yrel = @size.y + MARGIN * 2
23     unit = [rect.width / xrel, rect.height / yrel].min.floor
24     margin = MARGIN * unit
25     clock_width = CLOCK_WIDTH * unit
26     clock_height = clock_width * CLOCK_HEIGHT_RATIO
28     base = Qt::Point.new((rect.width - xrel * unit) / 2,
29                           (rect.height - yrel * unit) / 2)
30     
31     board_rect = Qt::Rect.new(
32       base.x + margin, base.y + margin,
33       @size.x * unit, @size.y * unit)
34     elements[:board].flip(@flipped)
35     elements[:board].set_geometry(board_rect)
37     pool_height = (board_rect.height - margin * (@game.players.size - 1)) / 
38                   @game.players.size
39     offy = base.y
40     flip = false
41     players = @game.players
42     players = players.reverse unless @flipped
43     players.each do |player|
44       r_pool, r_clock = if flip
45         [Qt::Rect.new(
46             board_rect.right + margin,
47             offy + margin,
48             clock_width,
49             pool_height - clock_height - margin),
50           Qt::Rect.new(
51             board_rect.right + margin,
52             offy + margin + pool_height - clock_height,
53             clock_width,
54             clock_height)]
55       else
56         [Qt::Rect.new(
57             board_rect.right + margin,
58             offy + margin * 2 + clock_height,
59             clock_width,
60             pool_height - clock_height - margin),
61           Qt::Rect.new(
62             board_rect.right + margin,
63             offy + margin,
64             clock_width,
65             clock_height)]
66       end
67       unless elements[:pools].empty?
68         elements[:pools][player].flip(flip)
69         elements[:pools][player].set_geometry(r_pool)
70       end
71       elements[:clocks][player].set_geometry(r_clock)
72       offy = offy + margin + pool_height
73       flip = !flip
74     end
75   end
76   
77   def flip(value)
78     @flipped = value
79   end
80   
81   def flipped?
82     @flipped
83   end
84 end