15 #ifndef FPLBASE_INPUT_SYSTEM_H
16 #define FPLBASE_INPUT_SYSTEM_H
24 #include "fplbase/config.h"
27 #include "mathfu/constants.h"
28 #include "mathfu/glsl_mappings.h"
34 #if !defined(ANDROID_GAMEPAD) && defined(__ANDROID__) && \
35 !defined(FPLBASE_BACKEND_STDLIB)
39 #define ANDROID_GAMEPAD 1
41 #endif // !defined(ANDROID_GAMEPAD) && defined(__ANDROID__)
49 typedef uint64_t FingerId;
50 typedef void *JoystickData;
51 typedef uint64_t JoystickId;
54 typedef void *TouchFingerEvent;
57 typedef int AndroidInputDeviceId;
159 float GetAxis(
size_t axis_index);
168 mathfu::vec2
GetHat(
size_t hat_index);
175 void SetAxis(
size_t axis_index,
float axis);
182 void SetHat(
size_t hat_index,
const mathfu::vec2 &hat);
222 JoystickData joystick_data_;
223 std::vector<float> axis_list_;
224 std::vector<Button> button_list_;
225 std::vector<mathfu::vec2> hat_list_;
264 Gamepad() { button_list_.resize(Gamepad::kControlCount); }
302 AndroidInputDeviceId controller_id_;
303 std::vector<Button> button_list_;
307 const float kGamepadHatThreshold = 0.5f;
321 int control_code_,
float x_,
float y_)
338 #endif // ANDROID_GAMEPAD
340 #if FPLBASE_ANDROID_VR
348 class HeadMountedDisplayInput {
351 HeadMountedDisplayInput()
353 left_eye_transform_(),
354 right_eye_transform_(),
355 is_in_head_mounted_display_(
false),
357 pending_trigger_(
false),
358 use_device_orientation_correction_(
false),
359 device_orientation_(0) {}
364 bool is_in_head_mounted_display()
const {
365 return is_in_head_mounted_display_;
371 void set_is_in_head_mounted_display(
bool is_in_head_mounted_display) {
372 is_in_head_mounted_display_ = is_in_head_mounted_display;
378 bool triggered()
const {
return triggered_; }
382 const mathfu::mat4 &head_transform()
const {
return head_transform_; }
387 const mathfu::mat4 &left_eye_transform()
const {
return left_eye_transform_; }
392 const mathfu::mat4 &right_eye_transform()
const {
393 return right_eye_transform_;
398 mathfu::vec3 right()
const {
399 return (mathfu::kAxisX4f * head_transform_).xyz();
403 mathfu::vec3 up()
const {
return (mathfu::kAxisY4f * head_transform_).xyz(); }
408 mathfu::vec3 forward()
const {
409 return (-mathfu::kAxisZ4f * head_transform_).xyz();
414 mathfu::vec3 left_eye_translation()
const {
415 return (left_eye_transform_ * mathfu::kAxisW4f).xyz();
420 mathfu::vec3 right_eye_translation()
const {
421 return (right_eye_transform_ * mathfu::kAxisW4f).xyz();
428 mathfu::vec3 left_eye_rotated_translation()
const {
429 return (mathfu::vec4(left_eye_translation(), 0) * left_eye_transform_)
437 mathfu::vec3 right_eye_rotated_translation()
const {
438 return (mathfu::vec4(right_eye_translation(), 0) * right_eye_transform_)
446 void OnTrigger() { pending_trigger_ =
true; }
449 void ResetHeadTracker();
454 void EnableDeviceOrientationCorrection();
458 void set_device_orientation(
int rotation) { device_orientation_ = rotation; }
463 int device_orientation() {
return device_orientation_; }
465 #if FPLBASE_ANDROID_VR
466 void InitHMDJNIReference();
467 void ClearHMDJNIReference();
471 void UpdateTransforms();
473 mathfu::mat4 head_transform_;
474 mathfu::mat4 left_eye_transform_;
475 mathfu::mat4 right_eye_transform_;
476 bool is_in_head_mounted_display_;
478 bool pending_trigger_;
480 bool use_device_orientation_correction_;
483 int device_orientation_;
485 int device_orientation_at_reset_;
487 #endif // FPLBASE_ANDROID_VR
492 enum TextInputEventType {
493 kTextInputEventTypeEdit = 0,
494 kTextInputEventTypeText = 1,
495 kTextInputEventTypeKey = 2,
536 int32_t symbol, int32_t modifier);
540 TextInputEvent(TextInputEventType t,
const char *str, int32_t start,
553 static const int kMaxSimultanuousPointers = 10;
593 void Delay(
double seconds)
const;
624 return joystick_map_;
640 const std::map<AndroidInputDeviceId, Gamepad> &
GamepadMap()
const {
647 int control_code,
float x,
float y);
651 #endif // ANDROID_GAMEPAD
653 #if FPLBASE_ANDROID_VR
657 HeadMountedDisplayInput &head_mounted_display_input() {
658 return head_mounted_display_input_;
664 const HeadMountedDisplayInput &head_mounted_display_input()
const {
665 return head_mounted_display_input_;
667 #endif // FPLBASE_ANDROID_VR
674 return GetButton(static_cast<int>(pointer + K_POINTER1));
698 return app_event_callbacks_;
725 record_text_input_ = b;
726 if (!b) text_input_events_.clear();
764 const std::vector<InputPointer> &
get_pointers()
const {
return pointers_; }
765 std::vector<InputPointer> &
get_pointers() {
return pointers_; }
779 void ResetInputState();
781 static const int kMillisecondsPerSecond = 1000;
783 static int HandleAppEvents(
void *userdata,
void *event);
786 void UpdateEvents(mathfu::vec2i *window_size);
788 bool exit_requested_;
790 std::vector<InputPointer> pointers_;
791 std::vector<JoystickData> open_joystick_list;
792 size_t FindPointer(FingerId
id);
793 size_t UpdateDragPosition(TouchFingerEvent e, uint32_t event_type,
794 const mathfu::vec2i &window_size);
795 void RemovePointer(
size_t i);
796 mathfu::vec2 ConvertHatToVector(uint32_t hat_enum)
const;
797 std::vector<AppEventCallback> app_event_callbacks_;
798 std::map<int, Button> button_map_;
799 std::map<JoystickId, Joystick> joystick_map_;
802 std::map<AndroidInputDeviceId, Gamepad> gamepad_map_;
803 static pthread_mutex_t android_event_mutex;
804 static std::queue<AndroidInputEvent> unhandled_java_input_events_;
805 #endif // ANDROID_GAMEPAD
807 #if FPLBASE_ANDROID_VR
809 HeadMountedDisplayInput head_mounted_display_input_;
810 #endif // FPLBASE_ANDROID_VR
816 double elapsed_time_;
819 uint64_t start_time_;
829 int minimized_frame_;
832 mathfu::vec2i mousewheel_delta_;
835 std::vector<TextInputEvent> text_input_events_;
838 bool record_text_input_;
846 bool relative_mouse_mode_;
849 double last_android_keypress_;
850 #endif // __ANDROID__
856 #endif // FPLBASE_INPUT_SYSTEM_H
mathfu::vec2 GetHat(size_t hat_index)
Get the current state of a joystick hat control.
void set_joystick_data(JoystickData joy)
Sets the pointer to the raw joystick data.
Definition: input.h:202
void SetHat(size_t hat_index, const mathfu::vec2 &hat)
Sets the position of a hat.
int32_t start
A start index of a focus region in the text.
Definition: input.h:503
bool state
key state, true:pressed, false:released.
Definition: input.h:513
static int GetGamepadCodeFromJavaKeyCode(int java_keycode)
Internal function for translating android input.
FPL_Keymod modifier
Modifier key state, refer keyboard_keycodes.h for details.
Definition: input.h:518
TextInputEventType type
Type of the event.
Definition: input.h:524
int GetNumButtons() const
Returns the number of buttons available on the joystick.
FPL_Keycode symbol
Key symbol, refer keyboard_keycodes.h for details.
Definition: input.h:516
void set_controller_id(AndroidInputDeviceId controller_id)
Set the controller ID.
Definition: input.h:294
void AdvanceFrame()
Updates the internal state of the joystick by one frame.
GamepadInputButton
Enum describing all possible button inputs on a gamepad.
Definition: input.h:238
TextInputEventEdit edit
An event parameter for a text edit in IME (Input Method Editor).
Definition: input.h:529
void SetAxis(size_t axis_index, float axis)
Sets the value of an axis.
JoystickId GetJoystickId() const
Gets the ID of the joystick.
TextInputEvent(TextInputEventType t)
Constructor for TextInputEvent.
Button & GetButton(GamepadInputButton i)
Get the Button from a given enum of all possible input buttons.
FPL_Keymod
Enumeration of valid key mods (possibly OR'd together).
Definition: keyboard_keycodes.h:300
An event parameter for a text edit in IME (Input Method Editor). The information passed in the event ...
Definition: input.h:502
const Button & GetButton(GamepadInputButton i) const
Get the Button from a given enum of all possible input buttons.
Definition: input.h:281
int32_t length
A length of a focus region in the text.
Definition: input.h:504
An event parameter for a keyboard input.
Definition: input.h:512
bool repeat
A flag indicates if the key is repeated input.
Definition: input.h:514
void AdvanceFrame()
Advances the internal state by one frame.
int GetNumHats() const
Returns the number of hats available on the joystick.
Represents the state of a Joystick.
Definition: input.h:148
Represents the state of a connected gamepad.
Definition: input.h:235
JoystickData joystick_data()
Returns the pointer to the raw joystick data.
Definition: input.h:195
Gamepad()
Default constructor for a Gamepad.
Definition: input.h:264
float GetAxis(size_t axis_index)
Get the current state of a joystick axis control.
std::string text
Input string.
Definition: input.h:525
TextInputEventKey key
An event parameter for a keyboard input.
Definition: input.h:528
AndroidInputDeviceId controller_id()
Returns the Android controller_id of the gamepad.
Definition: input.h:289
int GetNumAxes() const
Returns the number of control axes available on the joystick.
Holds text input events.
Definition: input.h:523
Button & GetButton(size_t button_index)
Get the current state of a button.