From 423124887ea0ffac767f460590e95c45abb46ea6 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Sun, 21 Jun 2009 11:33:07 +0200
Subject: [PATCH] Use integer coordinates everywhere.
---
lib/board/board.rb | 8 ++++----
lib/board/pool.rb | 29 +++++++++++++++++++++++++++--
lib/board/scene.rb | 5 +++--
lib/item.rb | 4 ++++
lib/qtutils.rb | 8 ++++++++
lib/themes/layouts/cool/cool.rb | 6 +++---
lib/themes/shadow.rb | 8 ++++----
7 files changed, 53 insertions(+), 15 deletions(-)
diff --git a/lib/board/board.rb b/lib/board/board.rb
index 0b5e0af..d3b8a3c 100644
--- a/lib/board/board.rb
+++ b/lib/board/board.rb
@@ -73,11 +73,11 @@ class Board < Qt::GraphicsItemGroup
@rect = rect
board = @state.board
side = [@rect.width / board.size.x, @rect.height / board.size.y].min.floor
- @unit = Qt::PointF.new(side, side)
- base = Qt::PointF.new(((@rect.width - side * board.size.x) / 2.0).to_i,
- ((@rect.height - side * board.size.y) / 2.0).to_i)
+ @unit = Qt::Point.new(side, side)
+ base = Qt::Point.new(((@rect.width - side * board.size.x) / 2.0).to_i,
+ ((@rect.height - side * board.size.y) / 2.0).to_i)
- self.pos = base + @rect.top_left
+ self.pos = (base + @rect.top_left).to_f
redraw
end
diff --git a/lib/board/pool.rb b/lib/board/pool.rb
index 40ccf75..41f8de4 100644
--- a/lib/board/pool.rb
+++ b/lib/board/pool.rb
@@ -23,22 +23,47 @@ class Pool < Qt::GraphicsItemGroup
end
def redraw
+ pieces = @items.map do |item|
+ destroy_item(item)
+ item.name
+ end
+
+ pieces.each_with_index do |piece, index|
+ add_piece(index, piece)
+ end
end
def set_geometry(rect)
@rect = rect
- self.pos = @rect.top_left
+ self.pos = @rect.top_left.to_f
@unit = (@rect.width / @size.x).floor
redraw
end
- def add_piece(piece)
+ def add_piece(index, piece)
+ item = create_piece piece,
+ @theme.pieces.pixmap(piece, Qt::Point.new(@unit, @unit)),
+ :pos => to_real(index)
+ @items.insert(index, item)
+
+ # TODO shift the other items
+ item
+ end
+
+ def on_click(pos)
+ index = to_logical(pos)
+ puts "index = #{index}"
end
def to_logical(p)
+ result = Point.new((p.x.to_f / @unit).floor,
+ (p.y.to_f / @unit).floor)
+ y = result.y
+ x = y % 2 == 0 ? result.x : @size.x - result.x - 1
+ x + y * @size.x
end
def to_real(index)
diff --git a/lib/board/scene.rb b/lib/board/scene.rb
index 62065ce..29db2c2 100644
--- a/lib/board/scene.rb
+++ b/lib/board/scene.rb
@@ -11,9 +11,10 @@ class Scene < Qt::GraphicsScene
def mousePressEvent(e)
if e.button == Qt::LeftButton
+ pos = e.scene_pos.to_i
@elements.each do |element|
- if element.rect.contains(e.scene_pos)
- element.on_click(e.scene_pos - element.rect.top_left)
+ if element.rect.contains(pos)
+ element.on_click(pos - element.rect.top_left)
end
end
end
diff --git a/lib/item.rb b/lib/item.rb
index 2393f28..f39ab30 100644
--- a/lib/item.rb
+++ b/lib/item.rb
@@ -21,6 +21,10 @@ class Item < Qt::GraphicsPixmapItem
@opacity = value
update
end
+
+ def remove
+ scene.remove_item self
+ end
end
module ItemUtils
diff --git a/lib/qtutils.rb b/lib/qtutils.rb
index 98d2b25..f89b1cd 100644
--- a/lib/qtutils.rb
+++ b/lib/qtutils.rb
@@ -74,10 +74,18 @@ end
class Qt::Point
include PrintablePoint
+
+ def to_f
+ Qt::PointF.new(x, y)
+ end
end
class Qt::PointF
include PrintablePoint
+
+ def to_i
+ Qt::Point.new(x.to_i, y.to_i)
+ end
end
class Qt::Size
diff --git a/lib/themes/layouts/cool/cool.rb b/lib/themes/layouts/cool/cool.rb
index 209054d..c766f0e 100644
--- a/lib/themes/layouts/cool/cool.rb
+++ b/lib/themes/layouts/cool/cool.rb
@@ -23,10 +23,10 @@ class CoolLayout
margin = MARGIN * unit
clock_width = CLOCK_WIDTH * unit
- base = Qt::PointF.new((rect.width - xrel * unit) / 2,
+ base = Qt::Point.new((rect.width - xrel * unit) / 2,
(rect.height - yrel * unit) / 2)
- board_rect = Qt::RectF.new(
+ board_rect = Qt::Rect.new(
base.x + margin, base.y + margin,
@size.x * unit, @size.y * unit)
elements[:board].set_geometry(board_rect)
@@ -35,7 +35,7 @@ class CoolLayout
pool_height = (board_rect.height - margin) / @game.players.size
offy = base.y
pools_rect = @game.players.map do |player|
- r = Qt::RectF.new(
+ r = Qt::Rect.new(
board_rect.right + margin,
offy + margin,
clock_width,
diff --git a/lib/themes/shadow.rb b/lib/themes/shadow.rb
index 4dbdbb8..960a4cb 100644
--- a/lib/themes/shadow.rb
+++ b/lib/themes/shadow.rb
@@ -7,8 +7,8 @@ module Shadower
def with_shadow(loader, data = DEFAULT_SHADOW_DATA)
lambda do |piece, size|
- isz = size * 100 / (100 + data.grow) + Qt::PointF.new(0.5, 0.5)
- off = Qt::PointF.new(
+ isz = size * 100 / (100 + data.grow) + Qt::Point.new(0.5, 0.5)
+ off = Qt::Point.new(
data.offset.x * isz.x / 200.0,
data.offset.y * isz.x / 200.0)
img = loader[piece, isz]
@@ -18,10 +18,10 @@ module Shadower
off)
s = shadow(img, scaled_data)
Qt::Painter.new(s).paint do |p|
- dst = Qt::RectF.new((size.x - isz.x) / 2 - off.x,
+ dst = Qt::Rect.new((size.x - isz.x) / 2 - off.x,
(size.y - isz.y) / 2 - off.y,
isz.x, isz.y)
- p.draw_image(dst, img, Qt::RectF.new(Qt::PointF.new(0, 0), isz))
+ p.draw_image(dst, img, Qt::Rect.new(Qt::Point.new(0, 0), isz))
end
s
end
--
2.11.4.GIT