A.K
A.K

Reputation: 41

Libgdx Draw Line between 2 mouse clicks

I am trying to create an app in which, when I click on a background texture image, I place a dot(it is an imagebutton with a dot image on it, and only to diplay). Now after clicking, when I move the mouse pointer away from that dot, I want that a thin line starts from that dot and ends where my current pointer is. As soon as I click second time on the background, that line becomes fixed with start and end points as the first and the second dot.

I am able to draw the dots, but that I am not able to draw the line:

1 . when I move the mouse, I am not able to draw a moving line originating from the first dot till the mouse pointer. (to help me place my line better)

2 . when I press the mouse second time, I am able to create a dot, but not a line.

I tried to create an actor (imagebutton having texture made from a line image) inside the mousemoved inputlistener, but an actor has only originating x,y points and width/height. I am not able to mention ending x/y co-ordinates:

 actor.addListener(new InputListener() {
           @Override
           public boolean mouseMoved(InputEvent event, float xSecond, float ySecond) {
            dot1.setPosition(xSecond, ySecond); //dot imagebutton actor
            line.setPosition(xFirst, yFirst); //Line starting points(
            stage.addActor(dot1);
            stage.addActor(line);
            return false;
           }
    });

I need a help on this, as I am stuck as this step. Please refer to the screenshot for this:Line between the dots 1 and 2

Upvotes: 1

Views: 681

Answers (1)

Hllink
Hllink

Reputation: 918

Use the ShapeRenderer to draw lines between 2 points:

 shapeRenderer.setProjectionMatrix(camera.combined);
 shapeRenderer.begin(ShapeType.Line);
 shapeRenderer.setColor(1, 1, 0, 1);
 shapeRenderer.line(x, y, x2, y2);
 shapeRenderer.end();

If you want to choose the thickness of the line you could use the ShapeRenderer rectLine method:

rectLine(float x1, float y1, float x2, float y2, float width)
Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.

Upvotes: 1

Related Questions