From 470a513366a824c90fece566ae3b561325404894 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Mon, 22 Jun 2009 16:50:28 +0200
Subject: [PATCH] Close drag-drop in destination is a click.
Dragging and dropping on the same empty square is now considered equivalent
to a click on that square.
---
lib/board/board.rb | 8 ++------
lib/board/scene.rb | 24 +++++++++++++++++++-----
2 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/lib/board/board.rb b/lib/board/board.rb
index fd08d94..7fce280 100644
--- a/lib/board/board.rb
+++ b/lib/board/board.rb
@@ -109,13 +109,9 @@ class Board < Qt::GraphicsItemGroup
create_item p, @theme.pieces.pixmap(piece, @unit), opts
end
- def on_click(pos, press_pos)
+ def on_click(pos)
p = to_logical(pos)
- p2 = to_logical(press_pos)
-
- if p == p2
- fire :click => p
- end
+ fire :click => p
end
def on_drag(pos)
diff --git a/lib/board/scene.rb b/lib/board/scene.rb
index b98e178..390e4ba 100644
--- a/lib/board/scene.rb
+++ b/lib/board/scene.rb
@@ -46,13 +46,22 @@ class Scene < Qt::GraphicsScene
src = if element_src == element_dst
old_pos
end
- notify(element_dst, :drop, [src, pos], data)
+ # if the drag and drop is close and there's no
+ # dragged item, notify a click instead
+ if src and
+ same_square(element_dst, src, pos) and
+ (not data[:item])
+ notify(element_dst, :click, [pos])
+ else
+ notify(element_dst, :drop, [src, pos], data)
+ end
end
elsif element_src == element_dst
- # close drag and drop == click
- # the element will decide how to handle it based on the distance
- # between the coordinates
- notify(element_dst, :click, [old_pos, pos])
+ # close drag and drop == click, unless
+ # old_pos and pos fall on different squares
+ if same_square(element, old_pos, pos)
+ notify(element_dst, :click, [pos])
+ end
else
# a rapid drag and drop between different elements
# is never considered a click
@@ -103,6 +112,11 @@ class Scene < Qt::GraphicsScene
end
end
+ def same_square(element, pos1, pos2)
+ element.to_logical(rel(element, pos1)) ==
+ element.to_logical(rel(element, pos2))
+ end
+
# invoked by the controller when one of the elements
# accepts a drag
def on_drag(data)
--
2.11.4.GIT