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

问题描述:

我想循环所有当前按下的鼠标按钮,这样我就可以实现一个按钮拖动系统。在LibGDX中有没有办法做到这一点?如何获取在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; 
} 

您可以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)