Skip to content

Commit

Permalink
Merge pull request #51 from sapoturge/bugfix-check-selected-first
Browse files Browse the repository at this point in the history
Improve click detection
  • Loading branch information
sapoturge authored Oct 21, 2024
2 parents 6bcc053 + e928d95 commit 23ad432
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 50 deletions.
4 changes: 0 additions & 4 deletions src/data/Circle.vala
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ public class Circle : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

segment = null;
if ((Math.sqrt ((x - this.x) * (x - this.x) + (y - this.y) * (y - this.y)) - r).abs () <= tolerance) {
element = this;
Expand Down
5 changes: 4 additions & 1 deletion src/data/Container.vala
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,17 @@ public interface Container : Undoable, Updatable, Transformed {
selected_child.transform.update_point (x, y, out new_x, out new_y);
selected_child.transform.update_distance (tolerance, out new_tolerance);
selected_child.check_controls (new_x, new_y, new_tolerance, out inner_handle);
selected_child.clicked_anywhere (new_x, new_y, new_tolerance, out element, out segment);
if (inner_handle != null) {
selected_child.clicked (new_x, new_y, new_tolerance, out element, out segment); // Just for if a segment was also clicked
if (element == null) {
element = selected_child;
}

handle = new TransformedHandle (element.title, inner_handle, element.transform);
return true;
} else if (element != null) {
handle = null;
return true;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/data/Element.vala
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public abstract class Element : Object, Undoable, Updatable, Transformed {

public abstract bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment);

public bool check_standard_clicks (double x, double y, double tolerance, out Element? element, out Segment? segment) {
public bool clicked_anywhere (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (fill.clicked (x, y, tolerance, out segment)) {
element = this;
return true;
Expand All @@ -138,8 +138,6 @@ public abstract class Element : Object, Undoable, Updatable, Transformed {
return true;
}

element = null;
segment = null;
return false;
return clicked (x, y, tolerance, out element, out segment);
}
}
4 changes: 0 additions & 4 deletions src/data/Ellipse.vala
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,6 @@ public class Ellipse : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

segment = null;
var surf = new Cairo.ImageSurface (Cairo.Format.ARGB32, 1, 1);
var cr = new Cairo.Context (surf);
Expand Down
4 changes: 0 additions & 4 deletions src/data/Line.vala
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ public class Line : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

var dot = (x - start.x) * (end.x - start.x) + (y - start.y) * (end.y - start.y);
var len_squared = (end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y);
var scale = dot / len_squared;
Expand Down
4 changes: 0 additions & 4 deletions src/data/Path.vala
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,6 @@ public class Path : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

var current_segment = root_segment;
var first = true;
while (first || current_segment != root_segment) {
Expand Down
4 changes: 0 additions & 4 deletions src/data/Polygon.vala
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ public class Polygon : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

segment = null;

var first = true;
Expand Down
4 changes: 0 additions & 4 deletions src/data/Polyline.vala
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ public class Polyline : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

for (var lsegment = root_segment; lsegment != null; lsegment = lsegment.next) {
if (lsegment.clicked (x, y, tolerance)) {
element = this;
Expand Down
4 changes: 0 additions & 4 deletions src/data/Rectangle.vala
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,6 @@ public class Rectangle : Element {
}

public override bool clicked (double x, double y, double tolerance, out Element? element, out Segment? segment) {
if (check_standard_clicks (x, y, tolerance, out element, out segment)) {
return true;
}

segment = null;
var in_x = this.x - tolerance < x && x < this.x + width + tolerance;
var in_y = this.y - tolerance < y && y < this.y + height + tolerance;
Expand Down
40 changes: 23 additions & 17 deletions src/widgets/Viewport.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Viewport : Gtk.DrawingArea, Gtk.Scrollable {
private int width = 0;
private int height = 0;
private Point base_point;
private uint cancel_drag_id;

private bool scrolling = false;

Expand Down Expand Up @@ -309,10 +310,7 @@ public class Viewport : Gtk.DrawingArea, Gtk.Scrollable {

drag_controller.drag_end.connect ((event) => {
// Stop scrolling, dragging, etc.
if (point_binding != null) {
unbind_point ();
}

unbind_point ();
scrolling = false;
});

Expand Down Expand Up @@ -378,23 +376,31 @@ public class Viewport : Gtk.DrawingArea, Gtk.Scrollable {
}

private void bind_point (Undoable obj, string name) {
if (tutorial != null && tutorial.step == DRAG) {
tutorial.next_step ();
}
cancel_drag_id = Timeout.add (100, () => {
if (tutorial != null && tutorial.step == DRAG) {
tutorial.next_step ();
}

bound_obj = obj;
bound_prop = name;
obj.begin (name);
point_binding = bind_property ("control-point", obj, name);
base_point = control_point;
queue_draw ();
bound_obj = obj;
bound_prop = name;
obj.begin (name);
point_binding = bind_property ("control-point", obj, name);
base_point = control_point;
queue_draw ();
cancel_drag_id = 0;
return false;
});
}

private void unbind_point () {
bound_obj.finish (bound_prop);
point_binding.unbind ();
point_binding = null;
queue_draw ();
if (cancel_drag_id != 0) {
Source.remove (cancel_drag_id);
} else if (point_binding != null) {
bound_obj.finish (bound_prop);
point_binding.unbind ();
point_binding = null;
queue_draw ();
}
}

private void position_tutorial () {
Expand Down

0 comments on commit 23ad432

Please sign in to comment.