Skip to content

Commit

Permalink
Propagate input even return values in more places. Handle repeats bet…
Browse files Browse the repository at this point in the history
…ter.
  • Loading branch information
hrydgard committed Jun 15, 2014
1 parent 6739fc5 commit a3e23f0
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 77 deletions.
6 changes: 5 additions & 1 deletion android/app-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,15 @@ extern "C" jboolean JNICALL Java_com_henrikrydgard_libnative_NativeApp_touch
return retval;
}

extern "C" jboolean Java_com_henrikrydgard_libnative_NativeApp_keyDown(JNIEnv *, jclass, jint deviceId, jint key) {
extern "C" jboolean Java_com_henrikrydgard_libnative_NativeApp_keyDown(JNIEnv *, jclass, jint deviceId, jint key, jboolean isRepeat) {
KeyInput keyInput;
keyInput.deviceId = deviceId;
keyInput.keyCode = key;
keyInput.flags = KEY_DOWN;
if (isRepeat) {
ILOG("Is repeat! %i", key);
keyInput.flags |= KEY_IS_REPEAT;
}
return NativeKey(keyInput);
}

Expand Down
18 changes: 7 additions & 11 deletions android/src/com/henrikrydgard/libnative/InputDeviceState.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class InputDeviceState {

// DEVICE_ID_PAD_0 from the cpp code. TODO: allocate these sequentially if we get more controllers.
private static int deviceId = 10;

private InputDevice mDevice;
private int[] mAxes;

InputDevice getDevice() { return mDevice; }

@TargetApi(19)
void logAdvanced(InputDevice device) {
Log.i(TAG, "Vendor ID:" + device.getVendorId() + " productId: " + device.getProductId());
Expand Down Expand Up @@ -68,17 +68,13 @@ public static float ProcessAxis(InputDevice.MotionRange range, float axisvalue)

public boolean onKeyDown(KeyEvent event) {
int keyCode = event.getKeyCode();
if (event.getRepeatCount() == 0) {
NativeApp.keyDown(deviceId, keyCode);
return true;
}
return false;
boolean repeat = event.getRepeatCount() > 0;
return NativeApp.keyDown(deviceId, keyCode, repeat);
}

public boolean onKeyUp(KeyEvent event) {
int keyCode = event.getKeyCode();
NativeApp.keyUp(deviceId, keyCode);
return true;
int keyCode = event.getKeyCode();
return NativeApp.keyUp(deviceId, keyCode);
}

public boolean onJoystickMotion(MotionEvent event) {
Expand Down
24 changes: 7 additions & 17 deletions android/src/com/henrikrydgard/libnative/NativeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,6 @@ public boolean dispatchKeyEvent(KeyEvent event) {
boolean passThrough = false;
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_MUTE:
case KeyEvent.KEYCODE_MENU:
passThrough = true;
break;
Expand Down Expand Up @@ -608,25 +605,22 @@ public boolean onGenericMotionEvent(MotionEvent event) {
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Eat these keys, to avoid accidental exits / other screwups.
// Maybe there's even more we need to eat on tablets?
boolean repeat = event.getRepeatCount() > 0;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (event.isAltPressed()) {
NativeApp.keyDown(0, 1004); // special custom keycode
NativeApp.keyDown(0, 1004, repeat); // special custom keycode
} else if (NativeApp.isAtTopLevel()) {
Log.i(TAG, "IsAtTopLevel returned true.");
return super.onKeyDown(keyCode, event);
} else {
NativeApp.keyDown(0, keyCode);
NativeApp.keyDown(0, keyCode, repeat);
}
return true;
case KeyEvent.KEYCODE_MENU:
case KeyEvent.KEYCODE_SEARCH:
NativeApp.keyDown(0, keyCode);
NativeApp.keyDown(0, keyCode, repeat);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
// NativeApp should ignore these.
return super.onKeyDown(keyCode, event);

case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
Expand All @@ -641,8 +635,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
// send the rest of the keys through.
// TODO: get rid of the three special cases above by adjusting the native side of the code.
// Log.d(TAG, "Key down: " + keyCode + ", KeyEvent: " + event);
NativeApp.keyDown(0, keyCode);
return true;
return NativeApp.keyDown(0, keyCode, repeat);
}
}

Expand All @@ -665,9 +658,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
// Search probably should also be ignored. We send it to the app.
NativeApp.keyUp(0, keyCode);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
return super.onKeyUp(keyCode, event);

case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_LEFT:
Expand All @@ -681,8 +672,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
// send the rest of the keys through.
// TODO: get rid of the three special cases above by adjusting the native side of the code.
// Log.d(TAG, "Key down: " + keyCode + ", KeyEvent: " + event);
NativeApp.keyUp(0, keyCode);
return true;
return NativeApp.keyUp(0, keyCode);
}
}

Expand Down
2 changes: 1 addition & 1 deletion android/src/com/henrikrydgard/libnative/NativeApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NativeApp {
// There's not really any reason to ever call shutdown as we can recover from a killed activity.
public static native void shutdown();

public static native boolean keyDown(int deviceId, int key);
public static native boolean keyDown(int deviceId, int key, boolean isRepeat);
public static native boolean keyUp(int deviceId, int key);

public static native void beginJoystickEvent();
Expand Down
11 changes: 7 additions & 4 deletions android/src/com/henrikrydgard/libnative/NativeGLView.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ private int getToolType(final MotionEvent ev, int pointer) {

public boolean onTouchEvent(final MotionEvent ev) {
boolean canReadToolType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

boolean retval = false;
for (int i = 0; i < ev.getPointerCount(); i++) {
int pid = ev.getPointerId(i);
int code = 0;
Expand Down Expand Up @@ -101,11 +103,11 @@ public boolean onTouchEvent(final MotionEvent ev) {
int tool = getToolType(ev, i);
code |= tool << 10; // We use the Android tool type codes
}
NativeApp.touch(ev.getX(i), ev.getY(i), code, pid);
retval = retval || NativeApp.touch(ev.getX(i), ev.getY(i), code, pid);
}
}
return true;
}
return retval;
}

// Sensor management
public void onAccuracyChanged(Sensor sensor, int arg1) {
Expand Down Expand Up @@ -165,9 +167,10 @@ public void onKeyEvent(KeyEvent event) {
}
}

boolean repeat = false; // Moga has no repeats?
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
NativeApp.keyDown(NativeApp.DEVICE_ID_PAD_0, event.getKeyCode());
NativeApp.keyDown(NativeApp.DEVICE_ID_PAD_0, event.getKeyCode(), repeat);
break;
case KeyEvent.ACTION_UP:
NativeApp.keyUp(NativeApp.DEVICE_ID_PAD_0, event.getKeyCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ public void run() {
}
});
}
}
}
1 change: 1 addition & 0 deletions input/input_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ enum {
KEY_DOWN = 1 << 0,
KEY_UP = 1 << 1,
KEY_HASWHEELDELTA = 1 << 2,
KEY_IS_REPEAT = 1 << 3,
};

struct KeyInput {
Expand Down
27 changes: 18 additions & 9 deletions ui/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,28 @@ void ScreenManager::switchToNext() {
UI::SetFocusedView(0);
}

void ScreenManager::touch(const TouchInput &touch) {
if (!stack_.empty())
stack_.back().screen->touch(touch);
bool ScreenManager::touch(const TouchInput &touch) {
if (!stack_.empty()) {
return stack_.back().screen->touch(touch);
} else {
return false;
}
}

void ScreenManager::key(const KeyInput &key) {
if (!stack_.empty())
stack_.back().screen->key(key);
bool ScreenManager::key(const KeyInput &key) {
if (!stack_.empty()) {
return stack_.back().screen->key(key);
} else {
return false;
}
}

void ScreenManager::axis(const AxisInput &axis) {
if (!stack_.empty())
stack_.back().screen->axis(axis);
bool ScreenManager::axis(const AxisInput &axis) {
if (!stack_.empty()) {
return stack_.back().screen->axis(axis);
} else {
return false;
}
}

void ScreenManager::resized() {
Expand Down
12 changes: 6 additions & 6 deletions ui/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class Screen {
virtual void deviceLost() {}
virtual void resized() {}
virtual void dialogFinished(const Screen *dialog, DialogResult result) {}
virtual void touch(const TouchInput &touch) {}
virtual void key(const KeyInput &key) {}
virtual void axis(const AxisInput &touch) {}
virtual bool touch(const TouchInput &touch) { return false; }
virtual bool key(const KeyInput &key) { return false; }
virtual bool axis(const AxisInput &touch) { return false; }
virtual void sendMessage(const char *msg, const char *value) {}

virtual void RecreateViews() {}
Expand Down Expand Up @@ -109,9 +109,9 @@ class ScreenManager {
void finishDialog(Screen *dialog, DialogResult result = DR_OK);

// Instant touch, separate from the update() mechanism.
void touch(const TouchInput &touch);
void key(const KeyInput &key);
void axis(const AxisInput &touch);
bool touch(const TouchInput &touch);
bool key(const KeyInput &key);
bool axis(const AxisInput &touch);

// Generic facility for gross hacks :P
void sendMessage(const char *msg, const char *value);
Expand Down
25 changes: 15 additions & 10 deletions ui/ui_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,36 @@ void UIScreen::render() {
}
}

void UIScreen::touch(const TouchInput &touch) {
bool UIScreen::touch(const TouchInput &touch) {
if (root_) {
UI::TouchEvent(touch, root_);
return true;
}
return false;
}

void UIScreen::key(const KeyInput &key) {
bool UIScreen::key(const KeyInput &key) {
if (root_) {
UI::KeyEvent(key, root_);
return UI::KeyEvent(key, root_);
}
return false;
}

void UIDialogScreen::key(const KeyInput &key) {
bool UIDialogScreen::key(const KeyInput &key) {
if ((key.flags & KEY_DOWN) && UI::IsEscapeKeyCode(key.keyCode)) {
if (finished_) {
ELOG("Screen already finished");
} else {
finished_ = true;
screenManager()->finishDialog(this, DR_BACK);
}
return true;
} else {
UIScreen::key(key);
return UIScreen::key(key);
}
}

void UIScreen::axis(const AxisInput &axis) {
bool UIScreen::axis(const AxisInput &axis) {
// Simple translation of hat to keys for Shield and other modern pads.
// TODO: Use some variant of keymap?
int flags = 0;
Expand Down Expand Up @@ -104,7 +108,9 @@ void UIScreen::axis(const AxisInput &axis) {
hatDown_ = flags;
if (root_) {
UI::AxisEvent(axis, root_);
return true;
}
return (pressed & (PAD_BUTTON_LEFT | PAD_BUTTON_RIGHT | PAD_BUTTON_UP | PAD_BUTTON_DOWN)) != 0;
}

UI::EventReturn UIScreen::OnBack(UI::EventParams &e) {
Expand All @@ -131,16 +137,15 @@ PopupScreen::PopupScreen(std::string title, std::string button1, std::string but
button2_ = d->T(button2.c_str());
}

void PopupScreen::touch(const TouchInput &touch) {
bool PopupScreen::touch(const TouchInput &touch) {
if (!box_ || (touch.flags & TOUCH_DOWN) == 0 || touch.id != 0) {
UIDialogScreen::touch(touch);
return;
return UIDialogScreen::touch(touch);
}

if (!box_->GetBounds().Contains(touch.x, touch.y))
screenManager()->finishDialog(this, DR_BACK);

UIDialogScreen::touch(touch);
return UIDialogScreen::touch(touch);
}

void PopupScreen::CreateViews() {
Expand Down
19 changes: 10 additions & 9 deletions ui/ui_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class UIScreen : public Screen {
UIScreen();
~UIScreen();

virtual void update(InputState &input);
virtual void render();
virtual void touch(const TouchInput &touch);
virtual void key(const KeyInput &touch);
virtual void axis(const AxisInput &touch);
virtual void update(InputState &input) override;
virtual void render() override;

virtual bool touch(const TouchInput &touch) override;
virtual bool key(const KeyInput &touch) override;
virtual bool axis(const AxisInput &touch) override;

// Some useful default event handlers
UI::EventReturn OnOK(UI::EventParams &e);
Expand All @@ -39,7 +40,7 @@ class UIScreen : public Screen {
class UIDialogScreen : public UIScreen {
public:
UIDialogScreen() : UIScreen(), finished_(false) {}
virtual void key(const KeyInput &key);
virtual bool key(const KeyInput &key) override;

private:
bool finished_;
Expand All @@ -51,9 +52,9 @@ class PopupScreen : public UIDialogScreen {
PopupScreen(std::string title, std::string button1 = "", std::string button2 = "");

virtual void CreatePopupContents(UI::ViewGroup *parent) = 0;
virtual void CreateViews();
virtual bool isTransparent() const { return true; }
virtual void touch(const TouchInput &touch);
virtual void CreateViews() override;
virtual bool isTransparent() const override { return true; }
virtual bool touch(const TouchInput &touch) override;

protected:
virtual bool FillVertical() const { return false; }
Expand Down
Loading

0 comments on commit a3e23f0

Please sign in to comment.