Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing and storing a user's touch path in libgdx

Tags:

android

libgdx

I'm struggling with something I would expect to be straight forward with libgdx.

In short this is a "finger paint" app where I want to draw a path of a certain width where the user touches the screen.

I've earlier done this by using a plain Android android.view.View. I had a android.graphics.Path in which I stored the coordinates of the user's current touch. In the onDraw() method of the view I drew the path to the android.graphics.Canvas. Whenever the user released a finger I drew the path to an offline canvas/android.graphics.Bitmap which also was drawn in the onDraw() method. Plain and simple.

How can that be done using libgdx?

I have tried using a com.badlogic.gdx.graphics.Pixmap that I can draw a line to whenever the user moves a finger. This works well except the fact that I'm unable to control the witdh of the line using Gdx.gl.glLineWidth(). I know I can draw a rectangle instead of a line to set the width, but Pixmap doesn't seem to have any means of rotating, so I don't see how this can be done.

I can use a com.badlogic.gdx.graphics.glutils.ShapeRenderer for drawing lines (or rectangles) in com.badlogic.gdx.Screen.render(). As far as I can see I then need to store every single touch point of the current touch, and then draw all lines on render. Whenever the user relases a finger I guess I can store the screen as-is with something like com.badlogic.gdx.utils.ScreenUtils.getFrameBufferPixmap(). Hopefully there is a easier way to achieve what I want.

like image 891
Roy Solberg Avatar asked Nov 11 '22 12:11

Roy Solberg


1 Answers

I ended up drawing circles on a pixmap where the line should be:

  • One circle on touchDown
  • Several circles from last touch point to next touch point reported to touchDragged

I'm not very happy with this solution, but it kinda works.

like image 139
Roy Solberg Avatar answered Nov 14 '22 23:11

Roy Solberg