Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make actor clip child Image

Tags:

libgdx

I want an actor that draws a drawable, but clips it to the size of the actor. I'm deriving this class from Widget, and using some hard-coded values as a simple test:

public class MyWidget extends Widget {

    public MyWidget(Drawable drawable) {
        setDrawable(drawable);
        setSize(100, 100);
    }

    public void draw(Batch batch, float parentAlpha) {
        clipBegin(getX(), getY(), 20, 20);

        drawable.draw(batch, getX(), getY(), 500, 500);

        clipEnd();
    }
}

No clipping is performed though, the drawable spills out of the bounds of the actor. This actor is part of a Table, if it matters. I believe I am using the clipBegin() / clipEnd() methods incorrectly. What is the right way to do this?

Thanks

like image 747
user3203425 Avatar asked Apr 04 '15 15:04

user3203425


1 Answers

This is what I found based on the comments and my own experimentation. It's important to flush the batch before starting the clip and after drawing to ensure the proper drawing gets clipped.

public void draw(Batch batch, float parentAlpha) {
    batch.flush();
    if (clipBegin(getX(), getY(), 20.0f, 20.0f)) {
        //do your drawing here
        drawable.draw(batch, getX(), getY(), 500, 500);
        batch.flush();
        clipEnd();
    }
}
like image 181
Raeleus Avatar answered Sep 30 '22 18:09

Raeleus