Commit a0577521 authored by Florent D'Halluin's avatar Florent D'Halluin
Browse files

[yavgui] Fix transition hover detection.

Give transitions a meaningful position (close to the center of the
drawn arrow), instead of (0, 0).

Properly update the scene index when their geometry changes.

Improve their geometric shape (used for collision detection).

* yavgui/src/g_transition.cc: Fix hover detection.
* yavgui/src/g_state.cc: Delete a whitespace.
parent a09eafb7
2009-08-05 d-halluin <d-halluin@lrde.epita.fr>
[yavgui] Fix transition hover detection.
Give transitions a meaningful position (close to the center of the
drawn arrow), instead of (0, 0).
Properly update the scene index when their geometry changes.
Improve their geometric shape (used for collision detection).
* yavgui/src/g_transition.cc: Fix hover detection.
* yavgui/src/g_state.cc: Delete a whitespace.
2009-08-04 d-halluin <d-halluin@lrde.epita.fr>
 
[yavgui] Get rid of double pointers where applicable.
......
......@@ -137,7 +137,7 @@ namespace gui
path.moveTo (-radius_, 0);
path.lineTo (c2);
path.lineTo (c3);
path.lineTo (- radius_, 0);
path.lineTo (-radius_, 0);
painter->setBrush (QColor("black"));
painter->drawPath (path);
......
......@@ -109,6 +109,14 @@ namespace gui
{
BENCH_TASK_SCOPED("GTransition::paint ()");
// Debug collision detection
//painter->setPen(QColor("blue"));
//painter->drawPath(shape_);
//painter->setPen(QColor("green"));
//painter->drawRect(boundingRect());
//painter->setPen(QColor("yellow"));
//painter->drawRect(QRect(-5, -5, 10, 10));
painter->setPen (color_);
painter->drawPath (curve_);
......@@ -120,157 +128,202 @@ namespace gui
LABEL_MAX_SIZE, LABEL_MAX_SIZE,
Qt::AlignCenter, label_, &label_rect_);
// Update shape and bounding rect with the new
// label coordinates.
QPainterPath label;
label.addRect (label_rect_);
shape_ = label.united (curve_shape_);
// Update shape and bounding rect with the new
// label coordinates.
// FIXME: try to move this into refresh();
// Do not call prepareGeometryChange() here: kills perfs.
bounding_rect_ = shape_.boundingRect ();
QPainterPath label;
label.addRect (label_rect_);
shape_ = label.united (curve_shape_);
bounding_rect_ = shape_.boundingRect ();
}
void
GTransition::refresh ()
{
BENCH_TASK_SCOPED("GTransition::refresh ()");
// Ensure proper indexing.
prepareGeometryChange();
// Hooks
if (&state_to_ == &state_from_)
{
double qpx = cos (angle_);
double qpy = sin (angle_);
double d = state_from_.radius () * 5;
// Set temporary reference points
QPointF from = state_from_.scenePos();
QPointF to = state_to_.scenePos();
// Compute quad point
quad_point_.setX (from.x () + d * qpx
* quad_point_distance_);
quad_point_.setY (from.y () - d * qpy
* quad_point_distance_);
// Position
setPos(((state_to_.scenePos().x() + state_from_.scenePos().x()) / 2
+ quad_point_.x()) / 2,
((state_to_.scenePos().y() + state_from_.scenePos().y()) / 2
+ quad_point_.y()) / 2);
quad_point_ -= pos();
// Update temporary reference points.
from = state_from_.scenePos() - pos();
to = state_to_.scenePos() - pos();
label_point_.setX (from.x () + d * qpx
* label_point_distance_
* quad_point_distance_);
label_point_.setY (from.y () - d * qpy
* label_point_distance_
* quad_point_distance_);
double hd = state_from_.radius ();
double hfx = cos (angle_ + PI / 6);
double hfy = sin (angle_ + PI / 6);
double htx = cos (angle_ - PI / 6);
double hty = sin (angle_ - PI / 6);
hook_from_.setX (from.x ()
+ hd * hfx);
hook_from_.setY (from.y ()
- hd * hfy);
hook_to_.setX (to.x ()
+ hd * htx);
hook_to_.setY (to.y ()
- hd * hty);
}
else
{
float quad_point_distance_ = this->quad_point_distance_
* (transition_index_ * 0.5 + 1);
// Set temporary reference points
QPointF from = state_from_.scenePos();
QPointF to = state_to_.scenePos();
qreal state_dx = to.x () - from.x ();
qreal state_dy = to.y () - from.y ();
// Compute quad point
quad_point_.setX ((to.x () + from.x ()) / 2
+ (state_dy) / 2 * quad_point_distance_);
quad_point_.setY ((to.y () + from.y ()) / 2
- (state_dx) / 2 * quad_point_distance_);
// Position
setPos(((state_to_.scenePos().x() + state_from_.scenePos().x()) / 2
+ quad_point_.x()) / 2,
((state_to_.scenePos().y() + state_from_.scenePos().y()) / 2
+ quad_point_.y()) / 2);
quad_point_ -= pos();
// Update temporary reference points.
from = state_from_.scenePos() - pos();
to = state_to_.scenePos() - pos();
qreal quad_dx = to.x () - quad_point_.x ();
qreal quad_dy = to.y () - quad_point_.y ();
qreal quad_distance = std::sqrt ((double) ((quad_dx * quad_dx)
+ quad_dy * quad_dy));
hook_from_.setX (from.x ()
- state_from_.radius ()
* (from.x () - quad_point_.x ())
/ quad_distance);
hook_from_.setY (from.y ()
- state_from_.radius ()
* (from.y () - quad_point_.y ())
/ quad_distance);
hook_to_.setX (to.x ()
- state_to_.radius () * quad_dx / quad_distance);
hook_to_.setY (to.y ()
- state_to_.radius () * quad_dy / quad_distance);
label_point_.setX ((to.x () + from.x ()) / 2
+ (state_dy) / 2 * quad_point_distance_
* label_point_distance_);
label_point_.setY ((to.y () + from.y ()) / 2
- (state_dx) / 2 * quad_point_distance_
* label_point_distance_);
}
// ! Hooks
// Paint paths
QPainterPath curve;
curve.moveTo (hook_from_);
curve.quadTo (quad_point_, hook_to_);
curve_ = curve;
qreal dx = hook_to_.x () - quad_point_.x ();
qreal dy = hook_to_.y () - quad_point_.y ();
qreal d = std::sqrt ((double) (dx * dx + dy * dy));
QPainterPath arrow;
if (d != 0)
{
QPointF c2 (hook_to_.x () - 8 * dx / d - 4 * dy / d,
hook_to_.y () - 8 * dy / d + 4 * dx / d);
QPointF c3 (hook_to_.x () - 8 * dx / d + 4 * dy / d,
hook_to_.y () - 8 * dy / d - 4 * dx / d);
arrow.moveTo (hook_to_);
arrow.lineTo (c2);
arrow.lineTo (c3);
arrow.lineTo (hook_to_);
// Hooks
if (&state_to_ == &state_from_)
{
double qpx = cos (angle_);
double qpy = sin (angle_);
double d = state_from_.radius () * 5;
quad_point_.setX (state_from_.x () + d * qpx
* quad_point_distance_);
quad_point_.setY (state_from_.y () - d * qpy
* quad_point_distance_);
label_point_.setX (state_from_.x () + d * qpx
* label_point_distance_
* quad_point_distance_);
label_point_.setY (state_from_.y () - d * qpy
* label_point_distance_
* quad_point_distance_);
double hd = state_from_.radius ();
double hfx = cos (angle_ + PI / 6);
double hfy = sin (angle_ + PI / 6);
double htx = cos (angle_ - PI / 6);
double hty = sin (angle_ - PI / 6);
hook_from_.setX (state_from_.x ()
+ hd * hfx);
hook_from_.setY (state_from_.y ()
- hd * hfy);
hook_to_.setX (state_to_.x ()
+ hd * htx);
hook_to_.setY (state_to_.y ()
- hd * hty);
}
else
{
float quad_point_distance_ = this->quad_point_distance_
* (transition_index_ * 0.5 + 1);
qreal state_dx = state_to_.x () - state_from_.x ();
qreal state_dy = state_to_.y () - state_from_.y ();
quad_point_.setX ((state_to_.x () + state_from_.x ()) / 2
+ (state_dy) / 2 * quad_point_distance_);
quad_point_.setY ((state_to_.y () + state_from_.y ()) / 2
- (state_dx) / 2 * quad_point_distance_);
qreal quad_dx = state_to_.x () - quad_point_.x ();
qreal quad_dy = state_to_.y () - quad_point_.y ();
qreal quad_distance = std::sqrt ((double) ((quad_dx * quad_dx)
+ quad_dy * quad_dy));
hook_from_.setX (state_from_.x ()
- state_from_.radius ()
* (state_from_.x () - quad_point_.x ())
/ quad_distance);
hook_from_.setY (state_from_.y ()
- state_from_.radius ()
* (state_from_.y () - quad_point_.y ())
/ quad_distance);
hook_to_.setX (state_to_.x ()
- state_to_.radius () * quad_dx / quad_distance);
hook_to_.setY (state_to_.y ()
- state_to_.radius () * quad_dy / quad_distance);
label_point_.setX ((state_to_.x () + state_from_.x ()) / 2
+ (state_dy) / 2 * quad_point_distance_
* label_point_distance_);
label_point_.setY ((state_to_.y () + state_from_.y ()) / 2
- (state_dx) / 2 * quad_point_distance_
* label_point_distance_);
}
// ! Hooks
// Paint paths
QPainterPath curve;
curve.moveTo (hook_from_);
curve.quadTo (quad_point_, hook_to_);
curve_ = curve;
qreal dx = hook_to_.x () - quad_point_.x ();
qreal dy = hook_to_.y () - quad_point_.y ();
qreal d = std::sqrt ((double) (dx * dx + dy * dy));
QPainterPath arrow;
if (d != 0)
{
QPointF c2 (hook_to_.x () - 8 * dx / d - 4 * dy / d,
hook_to_.y () - 8 * dy / d + 4 * dx / d);
QPointF c3 (hook_to_.x () - 8 * dx / d + 4 * dy / d,
hook_to_.y () - 8 * dy / d - 4 * dx / d);
arrow.moveTo (hook_to_);
arrow.lineTo (c2);
arrow.lineTo (c3);
arrow.lineTo (hook_to_);
}
arrow_ = arrow;
// !Paint paths
// Shape paths
QPainterPath path;
path.moveTo (hook_from_.x (), hook_from_.y ()
- TRANSITION_GRAB_DISTANCE);
path.quadTo (QPointF (quad_point_.x (),quad_point_.y ()
- TRANSITION_GRAB_DISTANCE),
QPointF (hook_to_.x (), hook_to_.y ()
- TRANSITION_GRAB_DISTANCE));
path.lineTo (hook_to_.x (), hook_to_.y ()
+ TRANSITION_GRAB_DISTANCE);
path.quadTo (QPointF (quad_point_.x (),quad_point_.y ()
+ TRANSITION_GRAB_DISTANCE),
QPointF (hook_from_.x (), hook_from_.y ()
+ TRANSITION_GRAB_DISTANCE));
path.lineTo (hook_from_.x (), hook_from_.y ()
+ TRANSITION_GRAB_DISTANCE);
curve_shape_ = path;
bounding_rect_ = curve_shape_.boundingRect ();
// !Shape paths
update ();
}
arrow_ = arrow;
// !Paint paths
// Shape paths
dx = hook_to_.x() - hook_from_.x();
dy = hook_to_.y() - hook_from_.y();
d = std::sqrt((double) (dx * dx + dy * dy));
dx = dx / d * TRANSITION_GRAB_DISTANCE;
dy = dy / d * TRANSITION_GRAB_DISTANCE;
QPainterPath path;
path.moveTo (hook_from_.x() + dy,
hook_from_.y() - dx);
path.quadTo (QPointF(quad_point_.x() + dy,
quad_point_.y() - dx),
QPointF(hook_to_.x() + dy,
hook_to_.y() - dx));
path.lineTo (hook_to_.x() - dy,
hook_to_.y() + dx);
path.quadTo (QPointF(quad_point_.x() - dy,
quad_point_.y() + dx),
QPointF(hook_from_.x() - dy,
hook_from_.y() + dx));
path.lineTo (hook_from_.x() + dy,
hook_from_.y() - dx);
curve_shape_ = path;
bounding_rect_ = curve_shape_.boundingRect ();
// !Shape paths
update ();
}
void
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment