Dragable oblique line

Hello,
is it possible to make dragable oblique line? But need to make only line to be dragable, not the whole component rectangle .

I mean. If I want to draw line like:
g.drawLine(0.0f, 0.0f, 100, 100, 5);

Then I need to make component size 100x100. And then whole that squere is dragable, but I need to make only line to be dragable.

Should I configure in some way the:
ComponentBoundsConstrainer constrainer;
ComponentDragger myDragger;

Or what?

For any help thanks in advance.
Best Regards

For this you need to override the hitTest method of the component. See: https://docs.juce.com/master/classComponent.html#a2da9631236e1cbf1b340454209a6c9e0

If you want to use more complex paths I recommend storing it as a path object and using something like this:

bool hitTest(int x, int y) override {
  Point<float> pointToTest(x, y);
  Point<float> nearestOnPath;
  yourPath.getNearestPoint(pointToTest, nearestOnPath);
  return pointToTest.getDistanceFrom(nearestOnPath) < 20;
}

If you really only need a line you can just use the Line class. See: https://docs.juce.com/master/classLine.html

1 Like

Great thanks

Hello, thanks again but unfortunately it looks like I need more help please :slight_smile:
I just need to make simply line dragable, but want one end (let’s say start) to be constant. Something like lever, when I drag it close to that constant point (hinge) small movement turn the lever a lot. But when drag the end I can turn lever more precisely.

I created my own class inherited public from component. And in that class I created variable Line<float>. And in mouseDrag method I use something like that:

myLine.setStart(100, 100);
myLine.setEnd(e.position);

but it jumps like crazy. I tried also e.getEventRelativeTo( this ).position but also jump as crazy. It looks like it randomly change the relative position.

I also noticed, that hitTest doesn’t work as expect. I made it:

bool hitTest(int x, int y)
{
    Point<float> pointToTest(x, y);
    myLine.findNearestPointTo(pointToTest);
    return pointToTest.getDistanceFrom(myLine.getEnd()) < 100;
}

But I see 100 is measured not on the line from the end of it, but on the whole area around the line end.
Please could you give me more hints?

Now it works like that (on the gif file my mouse coursor disappeared, but I am drugging by the end of line):
lever

OK, I figured it out. It took me couple of hours, but finally it works.
My hitTest now looks like that:

bool hitTest(int x, int y)
{
	Point<float> pointToTest(x, y);
	Point<float> nearestOnPath = myLine.findNearestPointTo(pointToTest);
	return pointToTest.getDistanceFrom(nearestOnPath) < 10;
}

And start of line I hard coded. Then I take angle between my drag point to that hard coded start point by float angle = e.position.getAngleToPoint(startLine). And then I use:
myLine = Line<float>::fromStartAndAngle(startLine, 100.0f, angle);

And everything would be great. But still my line jumps. Not as crazy as on attached gif, but still sometimes jumps strange.
I use setInterceptsMouseClicks(false, true); for all parent components, and other components that can be found under my drag coordinates. But it does not help.

What’s wrong?