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.
9 require 'board/redrawable'
11 class Pool < Qt::GraphicsItemGroup
12 BACKGROUND_ZVALUE = -10
16 include TaggableSquares
19 attr_reader :rect, :scene, :items, :theme
20 attr_reader :animator, :unit
21 square_tag :premove_src, :premove, :target => :extra
23 def initialize(scene, theme, game)
26 @scene.add_clickable_element(self)
32 @extra = ExtraItemContainer.new(self)
33 @size = Point.new(3, 5)
35 @type_values = Hash.new(-1)
36 if @game.respond_to? :types
37 @game.types.each_with_index do |type, index|
38 @type_values[type] = index
42 @animator = PoolAnimator.new(self)
46 def square_tag_container
59 @items.each_with_index do |item, index|
66 def set_geometry(rect)
69 self.pos = @rect.top_left.to_f
71 side = (@rect.width / @size.x).floor
72 @unit = Qt::Point.new(side, side)
76 def add_piece(index, piece, opts = {})
77 opts = opts.merge :name => piece,
78 :reloader => piece_reloader(piece)
79 item = create_item(opts)
80 items.insert(index, item)
81 item.reload(index) if opts.fetch(:load, true)
85 def remove_item(index, *args)
86 item = items.delete_at(index)
87 unless item.nil? or args.include?(:keep)
98 index = to_logical(pos)
101 fire :drag => { :index => index,
106 def on_drop(old_pos, pos, data)
117 result = Point.new((p.x.to_f / @unit.x).floor,
120 x = y % 2 == 0 ? result.x : @size.x - result.x - 1
127 x = @size.x - x - 1 if y % 2 == 1
131 rect.height - (y + 1) * @unit.y
136 Qt::PointF.new(rx, ry)
139 def compare(piece1, piece2)
140 [piece1.color.to_s, @type_values[piece1.type], piece1.type.to_s] <=>
141 [piece2.color.to_s, @type_values[piece2.type], piece2.type.to_s]
144 class ExtraItemContainer
156 @items.each do |key, item|