LibGDX - 自定义单击侦听器?

时间:2014-06-14 10:53:49

标签: java android libgdx

我想创建自己的Button类,它将有一个Sprite,每当我触摸它时它会增长或旋转等,该类也会有一个Draw和Update函数。

要检查我是否触摸它,我可以检查精灵矩形是否包含屏幕类的触地和触摸方法中的触摸位置。

我不想那样做,我想制作像android按钮点击监听器之类的东西,这可能吗?

这样的东西
myCoolButton.setOnClickListener(new CoolButtonClassClickListener(

public void OnTouchDown()  {

}

public void OnTouchUp()   {
}
});

有可能吗?

2 个答案:

答案 0 :(得分:8)

当然你可以,这正是我所做的。

public interface FFListener
{
    public void onClick(FFListenerButton flb);
}

public class FFListenerButton extends FFButton
{
    private FFListener ffListener;

    public FFListenerButton(Rectangle bounds, CharSequence text, FFListener ffListener)
    {
        super(bounds, text);
        this.ffListener = ffListener;
    }

    @Override
    protected void action()
    {
        ffListener.onClick(this);
    }
}

public abstract class FFButton 
{
    private Rectangle bounds;
    private CharSequence text;
    private boolean selected;
    private boolean hidden;
    private boolean active;
    private boolean disabled;

    public FFButton(Rectangle bounds, CharSequence text)
    {
        this.bounds = bounds;
        this.text = text;
        this.hidden = false;
        this.active = false;
        this.disabled = false;
    }

    protected abstract void action();

    public void execute()
    {
        if(disabled == false)
        {
            action();
        }
    }


    public boolean contains(float x, float y)
    {
        return bounds.contains(x, y);
    }

    public float x()
    {
        return bounds.x;
    }

    public float y()
    {
        return bounds.y;
    }

    public float width()
    {
        return bounds.width;
    }

    public float height()
    {
        return bounds.height;
    }

    public void drawBounds(ShapeRenderer shapeRenderer)
    {
        if(hidden != true)
        {
        shapeRenderer.rect(x(), y(), width(), height(), 0, 0, 0);
        }
    }

    public CharSequence getText()
    {
        return text;
    }

    public FFButton setText(String text)
    {
        this.text = text;
        return this;
    }

    public void drawText(SpriteBatch batch)
    {
        if(hidden != true)
        {
            Resources.bitmapFont.draw(batch, getText(), x()+(width()/8), y()+height()*0.75f); //black magic, please adjust
        }
    }

    public boolean getSelected()
    {
        return selected;
    }

    public FFButton setSelected(boolean selected)
    {
        this.selected = selected;
        return this;
    }

    public boolean isActive()
    {
        return active;
    }

    public FFButton setActive(boolean active)
    { 
        this.active = active;
        return this;
    }

    public boolean isHidden()
    {
        return hidden;
    }

    public FFButton setHidden(boolean hidden)
    {
        this.hidden = hidden;
        return this;
    }

    public Rectangle getBounds()
    {
        return bounds;
    }

    public boolean isDisabled()
    {
        return disabled;
    }

    public void setDisabled(boolean disabled)
    {
        this.disabled = disabled;
    }
}

虽然从技术上讲它使用了一个Rectangle并使用ShapeRenderer来渲染它,但实际上并不难将它换成Sprite。之后,您可以询问包含是否包含click,并且如果有,则从外部调用execute()。

像这样创建:

backButton = new FFListenerButton(new Rectangle(400, 20, 60, 30), "Back", this);

并处理这样的事件:

@Override
public void onClick(FFListenerButton clb)
{
    if(clb == backButton)
    {
        backButtonPressed();
    }
    else if(clb == selectButton)
    {
        ...
    }
}

这使用了我的AbstractMenuScreen类,如果点击它们就将事件委托给按钮:

public abstract class AbstractMenuScreen extends BaseScreen
{
    protected List<FFButton> buttons;

    public AbstractMenuScreen(List<FFButton> buttons)
    {
        this.buttons = buttons; 
    }

    @Override
    public void render(float delta)
    {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        Resources.batch.setProjectionMatrix(Resources.normalProjection);

        Resources.batch.begin();
        for(int i = 0; i < buttons.size(); i++)
        {
            FFButton b = buttons.get(i);
            if(b.isHidden() != true)
            {
                b.drawText(Resources.batch);
            }
        }

        Resources.batch.end();

        Resources.shapeRenderer.setColor(Color.WHITE);
        Resources.shapeRenderer.begin(ShapeType.Line);
        for(int i = 0; i < buttons.size(); i++)
        {
            FFButton b = buttons.get(i);
            if(b.isHidden() != true)
            {
                b.drawBounds(Resources.shapeRenderer);
            }
        }
        Resources.shapeRenderer.end();

        Resources.shapeRenderer.setColor(Color.RED);
        Resources.shapeRenderer.begin(ShapeType.Line);
        for(int i = 0; i < buttons.size(); i++)
        {
            FFButton b = buttons.get(i);
            if(b.isHidden() != true)
            {
                if(b.isActive() == true)
                {
                    b.drawBounds(Resources.shapeRenderer);
                }
            }
        }
        Resources.shapeRenderer.end();

        Resources.shapeRenderer.setColor(Color.MAGENTA);
        Resources.shapeRenderer.begin(ShapeType.Line);
        for(int i = 0; i < buttons.size(); i++)
        {
            FFButton b = buttons.get(i);
            if(b.isHidden() != true)
            {
                if(b.getSelected() == true)
                {
                    b.drawBounds(Resources.shapeRenderer);
                }
            }
        }
        Resources.shapeRenderer.end();
    }

    @Override
    public void show()
    {
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button)
    {
        float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
        float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
        for(int i = 0; i < buttons.size(); i++)
        {
            if(buttons.get(i).contains(pointerX, pointerY))
            {
                if(buttons.get(i).isHidden() != true)
                {
                    buttons.get(i).setSelected(true);
                }
            }
        }
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button)
    {
        float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
        float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
        for(int i = 0; i < buttons.size(); i++)
        {
            if(buttons.get(i).contains(pointerX, pointerY) && buttons.get(i).getSelected())
            {
                buttons.get(i).execute();
            }
            buttons.get(i).setSelected(false);
        }
        return true;
    }
    ...

你是这样延伸的:

public class ServerClientPickScreen extends AbstractMenuScreen implements FFListener
{
    private FFButton backButton;
    private FFButton clientButton;
    private FFButton serverButton;

    public ServerClientPickScreen()
    {
        super(new ArrayList<FFButton>());
        backButton = new FFListenerButton(new Rectangle(400, 20, 60, 30), "Back", this);
        clientButton = new FFListenerButton(new Rectangle(260, 140, 80, 30), "Client", this);
        serverButton = new FFListenerButton(new Rectangle(140, 140, 80, 30), "Server", this);
        buttons.add(backButton);
        buttons.add(clientButton);
        buttons.add(serverButton);
 ....

答案 1 :(得分:2)

buttonUp = new TextButton("", styleUpButton); //** Button text and style **//
   buttonUp.setPosition(84, 40);
   buttonUp.setHeight(64); 
   buttonUp.setWidth(64); 
   buttonUp.addListener(new InputListener() {
   public boolean touchDown (InputEvent event, float x, float y, int pointer, int button)   {
HereYouHaveToCallMethodYouNeed();
   return true;
}
   public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
//  Here may be onemore method.
   }
});