如何获取在LibGDX中按下的所有鼠标按钮?

时间:2017-01-04 15:03:25

标签: java input libgdx mouse

我想遍历当前按下的所有鼠标按钮,这样我就可以实现按钮拖动系统。有没有办法在LibGDX中做到这一点?

示例用例:

@Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
    Vector3 prev = obtain(Vector3.class);
    Vector3 cur = obtain(Vector3.class);

    prev.set(dragX, dragY, 0);
    cur.set(screenX, screenY, 0);

    screen.getCamera().unproject(prev); //unprojecting previous position
    screen.getCamera().unproject(cur); //unprojecting current position

    for(int button : getPressedButtons())
    {
        drag((int)cur.x, (int)cur.y, (int)(cur.x - prev.x), (int)(cur.y - prev.y), button); //calling my own drag method that support mouse buttons
    }

    free(prev);
    free(cur);
    dragX = screenX;
    dragY = screenY;
    return true;
}

1 个答案:

答案 0 :(得分:2)

您可以在Gdx.input上调用isButtonPressed()。如果你想避免装箱和数组分配,你将不得不迭代冗长的方式:

private final IntSet pressedButtons = new IntSet(5); 
// There are five possible buttons (See com.badlogic.gdx.Input.Buttons)

void updatePressedButtons (){
    pressedButtons.clear();
    for (int i=0; i<5; i++){
        if (Gdx.input.isButtonPressed(i)) pressedButtons.add(i);
}

IntSetIterator iterator = pressedButtons.iterator();
while (iterator.hasNext){
    drag((int)cur.x, (int)cur.y, (int)(cur.x - prev.x), (int)(cur.y - prev.y), iterator.next());
}

(或使用Java 8的PrimitiveIterator)