curvedraw

diff src/app.cc @ 13:4da693339d99

- distance from curve - hover/selection of curves directly on the curve
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Dec 2015 08:22:24 +0200
parents 84a647283237
children 37ab3a4c02f8
line diff
     1.1 --- a/src/app.cc	Sun Dec 20 07:21:32 2015 +0200
     1.2 +++ b/src/app.cc	Sun Dec 20 08:22:24 2015 +0200
     1.3 @@ -36,7 +36,8 @@
     1.4  static Curve *sel_curve;	// selected curve being edited
     1.5  static Curve *new_curve;	// new curve being entered
     1.6  static Curve *hover_curve;	// curve the mouse is hovering over (click to select)
     1.7 -static int sel_pidx = -1;	// selected point of the selected or hovered-over curve
     1.8 +static int sel_pidx = -1;	// selected point of the selected curve
     1.9 +static int hover_pidx = -1;	// hovered over point
    1.10  
    1.11  static Label *weight_label;	// floating label for the cp weight
    1.12  
    1.13 @@ -389,7 +390,7 @@
    1.14  		int pidx = curves[i]->nearest_point(pos);
    1.15  		if(pidx == -1) continue;
    1.16  
    1.17 -		Vector2 cp = curves[i]->get_point(pidx);
    1.18 +		Vector2 cp = curves[i]->get_point2(pidx);
    1.19  		if((cp - pos).length_sq() < thres * thres) {
    1.20  			*curveret = curves[i];
    1.21  			*pidxret = pidx;
    1.22 @@ -401,6 +402,28 @@
    1.23  	return false;
    1.24  }
    1.25  
    1.26 +static bool hit_test(const Vector2 &pos, Curve **curveret, int *pidxret)
    1.27 +{
    1.28 +	float thres = 0.02 / view_scale;
    1.29 +
    1.30 +	if(point_hit_test(pos, curveret, pidxret)) {
    1.31 +		return true;
    1.32 +	}
    1.33 +
    1.34 +	Vector3 pos3 = Vector3(pos.x, pos.y, 0.0f);
    1.35 +	for(size_t i=0; i<curves.size(); i++) {
    1.36 +		float x;
    1.37 +		if((x = curves[i]->distance_sq(pos3)) < thres * thres) {
    1.38 +			*curveret = curves[i];
    1.39 +			*pidxret = -1;
    1.40 +			return true;
    1.41 +		}
    1.42 +	}
    1.43 +	*curveret = 0;
    1.44 +	*pidxret = -1;
    1.45 +	return false;
    1.46 +}
    1.47 +
    1.48  static Vector2 snap(const Vector2 &p)
    1.49  {
    1.50  	switch(snap_mode) {
    1.51 @@ -427,7 +450,7 @@
    1.52  
    1.53  	Vector2 uv = pixel_to_uv(x, y);
    1.54  	mouse_pointer = uv;
    1.55 -	post_redisplay();
    1.56 +	//post_redisplay();
    1.57  
    1.58  	/* when entering a new curve, have the last (extra) point following
    1.59  	 * the mouse until it's entered by a click (see on_click).
    1.60 @@ -439,7 +462,10 @@
    1.61  
    1.62  	if(!new_curve && !bnstate) {
    1.63  		// not dragging, highlight curve under mouse
    1.64 -		point_hit_test(uv, &hover_curve, &sel_pidx);
    1.65 +		hit_test(uv, &hover_curve, &hover_pidx);
    1.66 +		if(hover_curve == sel_curve) {
    1.67 +			sel_pidx = hover_pidx;
    1.68 +		}
    1.69  		post_redisplay();
    1.70  
    1.71  	} else {
    1.72 @@ -499,6 +525,7 @@
    1.73  		if(hover_curve) {
    1.74  			// if we're hovering: click selects
    1.75  			sel_curve = hover_curve;
    1.76 +			sel_pidx = hover_pidx;
    1.77  			hover_curve = 0;
    1.78  		} else if(sel_curve) {
    1.79  			// if we have a selected curve: click adds point (enter new_curve mode)
    1.80 @@ -537,9 +564,11 @@
    1.81  			// in selected curve mode: delete control point or unselect
    1.82  			Curve *hit_curve;
    1.83  			int hit_pidx;
    1.84 -			if(point_hit_test(uv, &hit_curve, &hit_pidx) && hit_curve == sel_curve) {
    1.85 -				hit_curve->remove_point(hit_pidx);
    1.86 -				sel_pidx = -1;
    1.87 +			if(hit_test(uv, &hit_curve, &hit_pidx) && hit_curve == sel_curve) {
    1.88 +				if(hit_pidx != -1) {
    1.89 +					hit_curve->remove_point(hit_pidx);
    1.90 +					sel_pidx = -1;
    1.91 +				}
    1.92  			} else {
    1.93  				sel_curve = 0;
    1.94  				sel_pidx = -1;