How to make texture not accept click through it in Libgdx android? - android

I created a transparent black layer using this code:
Pixmap pixmap = new Pixmap((int) stage.getWidth(), (int) stage.getHeight(), Pixmap.Format.LuminanceAlpha);
Pixmap.setBlending(Pixmap.Blending.None); // disable Blending
pixmap.setColor(0, 0, 0, 1f);
pixmap.fill();
pixmap.fillCircle(200, 200, 100);
pixmapTexture = new Texture(pixmap, Pixmap.Format.LuminanceAlpha, false);
#Override
public void render(float delta) {
super.render(delta);
stage.getBatch().begin();
stage.getBatch().draw(pixmapTexture, 0, 0);
stage.getBatch().end();
}
The problem that I can click through it.
For example if I have a button behind it I can click it.
I want to be able to click only specific actors that I will make for them a transparent circle in the black screen (texture) that I created.
Why I'm able to click through the texture? I want to be able to click only through a transparent circle that I will make.
P.S I'm trying to do something like this

Related

Draw a frame with textures in libgdx

I am trying to draw a frame with two textures within Libgdx. My texture is just a white square that i scale and draw with SpriteBatch to the screen. The color is set with batch.setColor(). Now I want to draw a black rectangle and a smaller opaque rectangle in the middle, so it looks like a frame.
batch.begin();
batch.setColor(new Color(0,0,0,1)); //Black
batch.draw(rectangle, 0,0,100,100);
batch.setColor(new Color(0,0,0,0)); //Transparent
batch.draw(rectangle, 25,25,50,50);
batch.end();
I am using this blend function:
Gdx.gl.glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
the problem now is when i draw this it just shows the black rectangle cause the transparent rectangle is drawn on top of it. I want to draw it that I can see through the second rectangle the stuff I drawed before so it acts like a frame.
My question is: How can i accomplish this?
Example Background
How it should look
How it looks
First of all, you should not create new instances of Color in your render() method, since it's called every frame it will lead to a memory leak. Create colors in create() for example. And for black color you could use Color.BLACK instead of creating your own instance.
Back to your question.
You can simply just use TextureRegion class. Here is a sample code:
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture whiteRectangle;
TextureRegion backgroundRegion;
#Override
public void create() {
//initializing batch, whiteTexture, etc...
//background, the one with a turtle from you example
Texture backgroundTexture = ...;
backgroundRegion = new TextureRegion(backgroundTexture);
//to set a region (part of the texture you want to draw),
//you can use normalized coordinates (from 0 to 1), or absolute values in pixels
backgroundRegion.setRegion(0.25f, 0.25f, 0.75f, 0.75f);
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
//black background
batch.setColor(Color.BLACK);
batch.draw(whiteRectangle, 0, 0, 100, 100);
//on top of that background draw the backgroundRegion
batch.setColor(Color.WHITE);
batch.draw(backgroundRegion, 25,25,50,50);
batch.end();
}
}

Resetting the viewport after creating/rendering a scene2d Stage

In my game I am drawing a scene2d Stage using a custom world coordinate system.
I then want to draw a debug UI with some text like FPS on top of that, but simply using screen coordinates, i.e. where the text is positioned in the upper right corner of the screen. My main render method looks something like this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.act(delta);
gameStage.draw();
debugInfo.render(delta);
}
The Stage sets a custom viewport:
public GameStage(GameDimensions dimensions, OrthographicCamera camera) {
// mapWith will be smaller than screen width
super(new FitViewport(dimensions.mapWidth, dimensions.mapHeight, camera));
...
}
The DebugInfo render code looks like this:
public DebugInfo() {
Matrix4 mat = new Matrix4();
mat.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(mat);
}
#Override
public void render(float delta) {
batch.begin();
font.setScale(2f);
drawText("FPS " + Gdx.graphics.getFramesPerSecond());
batch.end();
}
private void drawText(String text) {
final BitmapFont.TextBounds textBounds = font.getBounds(text);
textPos.x = Gdx.graphics.getWidth() - textBounds.width - MARGIN;
textPos.y = Gdx.graphics.getHeight() - MARGIN;
textPos.z = 0;
font.draw(batch, text, textPos.x, textPos.y);
}
The problem is that even though I make no reference whatsoever to the stage's world system or its camera, but strictly use pixel values and an according projection matrix, the text appears in the upper right corner of the stage's viewport, which is smaller than the screen. I want it to appear detached from the stage in the screen's corner instead.
I could pinpoint the problem down to the creation of the Stage instance itself; stopping to draw it is not enough. I actually have to remove the call to super(new FitViewport(...)) to prevent this from happening.
This leads me to believe that I need to somehow reset the viewport of the rendering pipeline before I render the UI overlay? How would I do that?
My first answer was wrong. Now, when I've looked into Viewport source code I can see that it uses OpenGL's glViewPort and indeed it affects all subsequent draw calls. So you have to call glViewport with appropriate dimensions before rendering your stuff:
#Override
public void render(float delta) {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.begin();
font.setScale(2f);
drawText("FPS " + Gdx.graphics.getFramesPerSecond());
batch.end();
}

how to make a background scroll according to user tap to right/left in live wallpaper app?

i'm trying to draw an image as a background in live wallpaper app, but i don't know how to make it scrolls according to user tap to right/left (just like you set a simple wallpaper, if you move the menu to Rightmost, the background also move to Rightmost), here is the code in a live wallpaper app:
private void drawFrame() {
SurfaceHolder holder=getSurfaceHolder();
Canvas c=holder.lockCanvas();
c.drawBitmap(bg, 0, 0, paint);
holder.unlockCanvasAndPost(c);
handler.removeCallbacks(drawThread);
if (visible) handler.postDelayed(drawThread,100);
}
The above code only draw the background to a canvas, but i want to make it as same as simple wallpaper. For example, a very wide picture, how come it only shows the part that fits on "current" screen. I see other live wallpaper where the picture scrolls as you move to a different workspace left or right...is that an easy option
or any other example i can follow?
here is the answer
private int _xOffset = 0;
private int _yOffset = 0;
public void onOffsetsChanged(float xOffset,float yOffset,
float xStep,float yStep,int xPixels,int yPixels) {
_xOffset = xPixels;
_yOffset = yPixels;
drawFrame();
}
in the drawFrame() function:
c.drawBitmap(bg, _xOffset, _yOffset, paint);

controll bitmap image manually

I,m taking my first step into movement of bitmaps. From bits on the internet i,ve created this simple code. The bitmap moves across the screen from top left to top right it goes off the screen and back on at 0,0. What i want to do is add a button or method to manually move the image. I,m only using this single class and have noticed it does not use the main_activity xml Or does it?? If someone could show me on this 1 direction i can duplicate for the other directions. If youd like to add code so doesnt go off screen would be a bonus
public class MainActivity extends Activity {
int x=0;
int y=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this)); }
private class myView extends View{
public myView(Context context) {
super(context); }
#Override
protected void onDraw(Canvas canvas) {
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.richinch);
if (x < canvas.getWidth()){x +=10;}
else {x=0;}
canvas.drawBitmap(myBitmap, x, y, null);
invalidate();
}}}
Ive added this to the code and read a little on OnTouch listener. How would i add that to the region or Rectangle this would be very helpfull so effectively i,m using the Bitmap as a button if was button id know with onclick, Basicall im trying to make 2 Bitmap buttons to move the image Left Right for now.Eventually all directions. Please use names im using unless creating summit eg int etc
Paint green = new Paint();
green.setColor(Color.RED);
green.setStyle(Paint.Style.FILL);
////creating the shape////
Rect rect= new Rect();
rect.set(0, 0,x+50, x+50);
canvas.drawRect(rect,green);
Region region = new Region(0, 950, 100, 1030);
I am not exactly sure what you want to achieve, but if you would like to make an animation, avoid using onDraw() and just let ObjectAnimator do the "moving" for you. Here's a detailed tutorial on it.
The minimum code you need:
ObjectAnimator animation = ObjectAnimator.ofFloat(yourObject, "x", xDest);
animation.setDuration(500); // milliseconds
animation.start();
You are not using any xml
this part here:
setContentView(new myView(this)); is where you would add your xml file setContentView(R.layout.mainxml)
If you want to move around a bitmap with the touch of your finger check out these tutorials. They do exactly this and you will learn to use a SurfaceView
http://www.eis4u.com/2012/02/13/playing-with-graphics-in-android-part-i/
http://www.eis4u.com/2012/02/13/playing-with-graphics-in-android-part-ii/
http://www.eis4u.com/2012/02/13/playing-with-graphics-in-android-part-iii/

ShapeRenderer colors stop working when rendering images with SpriteBatch

I've made a game that uses ShapeRenderer for making colorized lines. This worked fine, but when I start to import images the colored lines suddenly became black. Worst of all: When I'm using a background the lines doesn't show at all, and yes, i'm drawing it in the right order....
Code for importing and rendering the images:
Constructor(){
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.atlas"));
AtlasRegion region = atlas.findRegion("path");
Sprite sprite = new Sprite(region);
}
..........................................
#Override
public void render() {
Gdx.gl.glClearColor(255, 255, 255, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
background.draw(batch); // drawing the background
drawing.draw(); // drawing the lines
drawObjects(); // drawing some pictures
batch.end();
}
But when I remove the code for rnedering the background and the pictures the lines will show up and in the right color....
Please help!!
EDIT: Drawing with the ShapeRenderer looks something like this (Don't have to put everything in):
public void draw() {
shaperenderer.begin(ShapeType.Line);
shaperenderer.setColor(Color.RED);
shaperenderer.line(1, 1, 100, 100);
shaperenderer.end();
}
You cannot nest objects that depend on OpenGL context. Specifically, you are nesting a ShapeRenderer.begin() within a SpriteBatch.begin(). If you change render to look like this:
batch.begin();
background.draw(batch); // drawing the background
batch.end(); // end spritebatch context to let ShapeRenderer in
drawing.draw(); // drawing the lines (with ShapeRenderer)
batch.begin();
drawObjects(); // drawing some pictures
batch.end();

Categories

Resources