3 class Pool < Qt::GraphicsItemGroup
4 BACKGROUND_ZVALUE = -10
9 attr_reader :rect, :scene, :items
12 def initialize(scene, theme, game)
15 @scene.add_element(self)
21 @size = Point.new(3, 5)
23 @type_values = Hash.new(-1)
24 if @game.respond_to? :types
25 @game.types.each_with_index do |type, index|
26 @type_values[type] = index
30 @animator = PoolAnimator.new(self)
43 pieces = @items.map do |item|
49 pieces.each_with_index do |piece, index|
50 add_piece(index, piece)
54 def set_geometry(rect)
57 self.pos = @rect.top_left.to_f
59 side = (@rect.width / @size.x).floor
60 @unit = Qt::Point.new(side, side)
64 def add_piece(index, piece, opts = {})
65 opts = opts.merge :pos => to_real(index),
67 item = create_item index, @theme.pieces.pixmap(piece, @unit), opts
68 items.insert(index, item)
72 def remove_item(index, *args)
73 item = items.delete_at(index)
74 unless item.nil? or args.include?(:keep)
80 def on_click(pos, press_pos)
85 index = to_logical(pos)
88 fire :drag => { :index => index,
93 def on_drop(old_pos, pos, data)
104 result = Point.new((p.x.to_f / @unit.x).floor,
107 x = y % 2 == 0 ? result.x : @size.x - result.x - 1
114 x = @size.x - x - 1 if y % 2 == 1
118 rect.height - (y + 1) * @unit.y
123 Qt::PointF.new(rx, ry)
126 def compare(piece1, piece2)
127 [piece1.color.to_s, @type_values[piece1.type], piece1.type.to_s] <=>
128 [piece2.color.to_s, @type_values[piece2.type], piece2.type.to_s]