UnderwaterApps / overlap2d-runtime-libgdx

Overlap2D - UI and Level Editor libgdx runtime
http://overlap2d.com
Other
170 stars 96 forks source link

feature request: Custom DrawLogic #63

Closed artyss closed 8 years ago

artyss commented 8 years ago

Hi,

Could you please implement some kind of custom DrawLogic for a sprite item with a specific tag?

My end goal is to make custom player object which will be generated procedurally. Basically, it could be considered as an array of points connected with a line.

Adding such kind of item type into O2D editor and runtime could be time consuming (e.g. implement something similar to spine external extension). That's why, as a workaround, I want to use standard sprite (or other suitable) item with some specific tag but need a way to completely redefine its drawing logic to draw its points connected with a line using ShapeRenderer or BatchRenderer with custom shader.

Please, correct me if I misunderstood something and many thanks!

azakhary commented 8 years ago

We can probably add DrawLogic reference to say MainComponent, and make Rendering system to use it if it's not null instead of default. So that you can simple set it from your source if required. Will this work?

artyss commented 8 years ago

Awesome! Thanks!

azakhary commented 8 years ago

Okay, I wanted to start doing this but then figured we already have this :D Here is how:

If you need a custom draw logic: 1) Create your class that implements IExternalItemType and in it's getDrawable supply your own class that implements Drawable 2) Fetch your entity that needs custom logic, and change it's entityId to some kind of unique integer aim for a big number. (something like 999) 3) Retrieve Overlap2dRenderer system from the engine, and call it's addDrawableType method, add your custom draw logic class here with number 999.

This seems pretty easy to do, so I am not sure any other implementation is needed.

azakhary commented 8 years ago

Let me know if this solves it, so I can close the issue.

artyss commented 8 years ago

That is exactly what I need, thank you! I assume you've meant code like below:

class TestDrawable implements IExternalItemType {

@Override
public int getTypeId() {
        return 999;
}

... }

TestDrawable testDrawable = new TestDrawable();
Overlap2dRenderer o2dRenderSys = sl.engine.getSystem(Overlap2dRenderer.class); if(o2dRenderSys!=null) { o2dRenderSys.addDrawableType(testDrawable); }

<...> in create()

sl = new SceneLoader(); sl.loadScene("MainScene", viewport); ComponentMapper mainItemComponentMapper = ComponentMapper.getFor(MainItemComponent.class);

ImmutableArray entities = sl.engine.getEntities(); for(Entity entity: entities) { MainItemComponent mainItemComponent = mainItemComponentMapper.get(entity); if (mainItemComponent.tags.contains("customdraw")) { mainItemComponent.entityType = 999; } }

azakhary commented 8 years ago

Almost

azakhary commented 8 years ago
class MyDrawable implements Drawable
{
    @Override
     public void draw(Batch batch, Entity entity, float parentAlpha) {
         //TODO: your draw logic goes here   
     }
}

class MyExternalType implements IExternalItemType
{
    public MyDrawable drawable;
     public MyExternalType() {
          drawable = new MyDrawable();
     }

    @Override
    public int getTypeId() {
            return 999;
    }

    @Override
    public DrawablegetDrawable() {
            return drawable ;
    }
}
azakhary commented 8 years ago

I know this is like 2 classes instead of one, but that'll do it till I make it "simpler"

artyss commented 8 years ago

Thanks again! :)