1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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.
10 class Pool < Qt::GraphicsItemGroup
11 BACKGROUND_ZVALUE = -10
15 include TaggableSquares
17 attr_reader :rect, :scene, :items, :theme
18 attr_reader :animator, :unit
19 square_tag :premove_src, :premove, :target => :extra
21 def initialize(scene, theme, game)
24 @scene.add_clickable_element(self)
30 @extra = ExtraItemContainer.new(self)
31 @size = Point.new(3, 5)
33 @type_values = Hash.new(-1)
34 if @game.respond_to? :types
35 @game.types.each_with_index do |type, index|
36 @type_values[type] = index
40 @animator = PoolAnimator.new(self)
44 def square_tag_container
57 pieces = @items.map do |item|
63 pieces.each_with_index do |piece, index|
64 add_piece(index, piece)
70 def set_geometry(rect)
73 self.pos = @rect.top_left.to_f
75 side = (@rect.width / @size.x).floor
76 @unit = Qt::Point.new(side, side)
80 def add_piece(index, piece, opts = {})
81 opts = opts.merge :pos => to_real(index),
83 item = create_item index, @theme.pieces.pixmap(piece, @unit), opts
84 items.insert(index, item)
88 def remove_item(index, *args)
89 item = items.delete_at(index)
90 unless item.nil? or args.include?(:keep)
101 index = to_logical(pos)
104 fire :drag => { :index => index,
109 def on_drop(old_pos, pos, data)
120 result = Point.new((p.x.to_f / @unit.x).floor,
123 x = y % 2 == 0 ? result.x : @size.x - result.x - 1
130 x = @size.x - x - 1 if y % 2 == 1
134 rect.height - (y + 1) * @unit.y
139 Qt::PointF.new(rx, ry)
142 def compare(piece1, piece2)
143 [piece1.color.to_s, @type_values[piece1.type], piece1.type.to_s] <=>
144 [piece2.color.to_s, @type_values[piece2.type], piece2.type.to_s]
147 class ExtraItemContainer
159 @items.each do |key, item|
160 @pool.set_tag(key, @pool.tag(key))