1 require 'board/square_tag.rb'
3 require 'board/point_converter.rb'
5 require 'board/item_bag'
7 class Board < Qt::GraphicsItemGroup
8 include TaggableSquares
10 include PointConverter
14 attr_reader :scene, :items, :unit, :rect, :theme
15 attr_accessor :movable
17 square_tag :last_move_src, :highlight
18 square_tag :last_move_dst, :highlight
20 def initialize(scene, theme, game)
23 @scene.add_element(self)
30 @movable = lambda { true }
37 def flip!(value = nil)
44 redraw if old != @flipped
47 def redraw(names = nil)
49 names = @items.inject({}) do |res, data|
51 res[p] = item.name if item
56 @items.each {|item| remove_item(item) }
58 names.each do |key, name|
59 reload_item(key, name)
64 pieces = board.to_enum(:each_square).inject({}) do |res, p|
65 res[p] = board[p] if board[p]
68 pieces[:background] = nil
73 @pieces_to_draw = pieces
77 def reload_item(key, name)
81 @theme.pieces.pixmap(name, @unit),
84 when :background # background
86 @theme.board.pixmap(@unit),
87 :z => BACKGROUND_ZVALUE
89 # force redraw by setting tag again
90 set_tag(key, tag(key))
94 def set_geometry(rect)
96 side = [@rect.width / @game.size.x, @rect.height / @game.size.y].min.floor
97 @unit = Qt::Point.new(side, side)
98 base = Qt::Point.new(((@rect.width - side * @game.size.x) / 2.0).to_i,
99 ((@rect.height - side * @game.size.y) / 2.0).to_i)
101 self.pos = (base + @rect.top_left).to_f
103 redraw @pieces_to_draw
104 @pieces_to_draw = nil
107 def add_piece(p, piece, opts = {})
108 opts = opts.merge :pos => to_real(p),
110 add_item p, @theme.pieces.pixmap(piece, @unit), opts
113 def create_piece(piece, opts = {})
114 opts = opts.merge :name => piece
115 create_item p, @theme.pieces.pixmap(piece, @unit), opts
118 def on_click(pos, press_pos)
120 p2 = to_logical(press_pos)
131 fire :drag => { :src => p,
137 def on_drop(old_pos, pos, data)
145 fire :drop => data.merge(:src => src, :dst => dst)
151 self.last_move_src = move.src
152 self.last_move_dst = move.dst
154 self.last_move_src = nil
155 self.last_move_dst = nil