openxr_action_map.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*************************************************************************/
  2. /* openxr_action_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "openxr_action_map.h"
  31. void OpenXRActionMap::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("set_action_sets", "action_sets"), &OpenXRActionMap::set_action_sets);
  33. ClassDB::bind_method(D_METHOD("get_action_sets"), &OpenXRActionMap::get_action_sets);
  34. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "action_sets", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRActionSet", PROPERTY_USAGE_NO_EDITOR), "set_action_sets", "get_action_sets");
  35. ClassDB::bind_method(D_METHOD("get_action_set_count"), &OpenXRActionMap::get_action_set_count);
  36. ClassDB::bind_method(D_METHOD("find_action_set", "name"), &OpenXRActionMap::find_action_set);
  37. ClassDB::bind_method(D_METHOD("get_action_set", "idx"), &OpenXRActionMap::get_action_set);
  38. ClassDB::bind_method(D_METHOD("add_action_set", "action_set"), &OpenXRActionMap::add_action_set);
  39. ClassDB::bind_method(D_METHOD("remove_action_set", "action_set"), &OpenXRActionMap::remove_action_set);
  40. ClassDB::bind_method(D_METHOD("set_interaction_profiles", "interaction_profiles"), &OpenXRActionMap::set_interaction_profiles);
  41. ClassDB::bind_method(D_METHOD("get_interaction_profiles"), &OpenXRActionMap::get_interaction_profiles);
  42. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "interaction_profiles", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRInteractionProfile", PROPERTY_USAGE_NO_EDITOR), "set_interaction_profiles", "get_interaction_profiles");
  43. ClassDB::bind_method(D_METHOD("get_interaction_profile_count"), &OpenXRActionMap::get_interaction_profile_count);
  44. ClassDB::bind_method(D_METHOD("find_interaction_profile", "name"), &OpenXRActionMap::find_interaction_profile);
  45. ClassDB::bind_method(D_METHOD("get_interaction_profile", "idx"), &OpenXRActionMap::get_interaction_profile);
  46. ClassDB::bind_method(D_METHOD("add_interaction_profile", "interaction_profile"), &OpenXRActionMap::add_interaction_profile);
  47. ClassDB::bind_method(D_METHOD("remove_interaction_profile", "interaction_profile"), &OpenXRActionMap::remove_interaction_profile);
  48. ClassDB::bind_method(D_METHOD("create_default_action_sets"), &OpenXRActionMap::create_default_action_sets);
  49. }
  50. void OpenXRActionMap::set_action_sets(Array p_action_sets) {
  51. action_sets.clear();
  52. for (int i = 0; i < p_action_sets.size(); i++) {
  53. Ref<OpenXRActionSet> action_set = p_action_sets[i];
  54. if (action_set.is_valid() && action_sets.find(action_set) == -1) {
  55. action_sets.push_back(action_set);
  56. }
  57. }
  58. }
  59. Array OpenXRActionMap::get_action_sets() const {
  60. return action_sets;
  61. }
  62. int OpenXRActionMap::get_action_set_count() const {
  63. return action_sets.size();
  64. }
  65. Ref<OpenXRActionSet> OpenXRActionMap::find_action_set(String p_name) const {
  66. for (int i = 0; i < action_sets.size(); i++) {
  67. Ref<OpenXRActionSet> action_set = action_sets[i];
  68. if (action_set->get_name() == p_name) {
  69. return action_set;
  70. }
  71. }
  72. return Ref<OpenXRActionSet>();
  73. }
  74. Ref<OpenXRActionSet> OpenXRActionMap::get_action_set(int p_idx) const {
  75. ERR_FAIL_INDEX_V(p_idx, action_sets.size(), Ref<OpenXRActionSet>());
  76. return action_sets[p_idx];
  77. }
  78. void OpenXRActionMap::add_action_set(Ref<OpenXRActionSet> p_action_set) {
  79. ERR_FAIL_COND(p_action_set.is_null());
  80. if (action_sets.find(p_action_set) == -1) {
  81. action_sets.push_back(p_action_set);
  82. }
  83. }
  84. void OpenXRActionMap::remove_action_set(Ref<OpenXRActionSet> p_action_set) {
  85. int idx = action_sets.find(p_action_set);
  86. if (idx != -1) {
  87. action_sets.remove_at(idx);
  88. }
  89. }
  90. void OpenXRActionMap::set_interaction_profiles(Array p_interaction_profiles) {
  91. interaction_profiles.clear();
  92. for (int i = 0; i < p_interaction_profiles.size(); i++) {
  93. Ref<OpenXRInteractionProfile> interaction_profile = p_interaction_profiles[i];
  94. if (interaction_profile.is_valid() && interaction_profiles.find(interaction_profile) == -1) {
  95. interaction_profiles.push_back(interaction_profile);
  96. }
  97. }
  98. }
  99. Array OpenXRActionMap::get_interaction_profiles() const {
  100. return interaction_profiles;
  101. }
  102. int OpenXRActionMap::get_interaction_profile_count() const {
  103. return interaction_profiles.size();
  104. }
  105. Ref<OpenXRInteractionProfile> OpenXRActionMap::find_interaction_profile(String p_path) const {
  106. for (int i = 0; i < interaction_profiles.size(); i++) {
  107. Ref<OpenXRInteractionProfile> interaction_profile = interaction_profiles[i];
  108. if (interaction_profile->get_interaction_profile_path() == p_path) {
  109. return interaction_profile;
  110. }
  111. }
  112. return Ref<OpenXRInteractionProfile>();
  113. }
  114. Ref<OpenXRInteractionProfile> OpenXRActionMap::get_interaction_profile(int p_idx) const {
  115. ERR_FAIL_INDEX_V(p_idx, interaction_profiles.size(), Ref<OpenXRInteractionProfile>());
  116. return interaction_profiles[p_idx];
  117. }
  118. void OpenXRActionMap::add_interaction_profile(Ref<OpenXRInteractionProfile> p_interaction_profile) {
  119. ERR_FAIL_COND(p_interaction_profile.is_null());
  120. if (interaction_profiles.find(p_interaction_profile) == -1) {
  121. interaction_profiles.push_back(p_interaction_profile);
  122. }
  123. }
  124. void OpenXRActionMap::remove_interaction_profile(Ref<OpenXRInteractionProfile> p_interaction_profile) {
  125. int idx = interaction_profiles.find(p_interaction_profile);
  126. if (idx != -1) {
  127. interaction_profiles.remove_at(idx);
  128. }
  129. }
  130. void OpenXRActionMap::create_default_action_sets() {
  131. // Note, if you make changes here make sure to delete your default_action_map.tres file of it will load an old version.
  132. // Create our Godot action set
  133. Ref<OpenXRActionSet> action_set = OpenXRActionSet::new_action_set("godot", "Godot action set");
  134. add_action_set(action_set);
  135. // Create our actions
  136. Ref<OpenXRAction> trigger = action_set->add_new_action("trigger", "Trigger", OpenXRAction::OPENXR_ACTION_FLOAT, "/user/hand/left,/user/hand/right");
  137. Ref<OpenXRAction> trigger_click = action_set->add_new_action("trigger_click", "Trigger click", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  138. Ref<OpenXRAction> trigger_touch = action_set->add_new_action("trigger_touch", "Trigger touching", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  139. Ref<OpenXRAction> grip = action_set->add_new_action("grip", "Grip", OpenXRAction::OPENXR_ACTION_FLOAT, "/user/hand/left,/user/hand/right");
  140. Ref<OpenXRAction> grip_click = action_set->add_new_action("grip_click", "Grip click", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  141. Ref<OpenXRAction> grip_touch = action_set->add_new_action("grip_touch", "Grip touching", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  142. Ref<OpenXRAction> primary = action_set->add_new_action("primary", "Primary joystick/thumbstick/trackpad", OpenXRAction::OPENXR_ACTION_VECTOR2, "/user/hand/left,/user/hand/right");
  143. Ref<OpenXRAction> primary_click = action_set->add_new_action("primary_click", "Primary joystick/thumbstick/trackpad click", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  144. Ref<OpenXRAction> primary_touch = action_set->add_new_action("primary_touch", "Primary joystick/thumbstick/trackpad touching", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  145. Ref<OpenXRAction> secondary = action_set->add_new_action("secondary", "Secondary joystick/thumbstick/trackpad", OpenXRAction::OPENXR_ACTION_VECTOR2, "/user/hand/left,/user/hand/right");
  146. Ref<OpenXRAction> secondary_click = action_set->add_new_action("secondary_click", "Secondary joystick/thumbstick/trackpad click", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  147. Ref<OpenXRAction> secondary_touch = action_set->add_new_action("secondary_touch", "Secondary joystick/thumbstick/trackpad touching", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  148. Ref<OpenXRAction> menu_button = action_set->add_new_action("menu_button", "Menu button", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  149. Ref<OpenXRAction> select_button = action_set->add_new_action("select_button", "Select button", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  150. Ref<OpenXRAction> ax_button = action_set->add_new_action("ax_button", "A/X button", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  151. Ref<OpenXRAction> ax_touch = action_set->add_new_action("ax_touch", "A/X touching", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  152. Ref<OpenXRAction> by_button = action_set->add_new_action("by_button", "B/Y button", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  153. Ref<OpenXRAction> by_touch = action_set->add_new_action("by_touch", "B/Y touching", OpenXRAction::OPENXR_ACTION_BOOL, "/user/hand/left,/user/hand/right");
  154. Ref<OpenXRAction> default_pose = action_set->add_new_action("default_pose", "Default pose", OpenXRAction::OPENXR_ACTION_POSE,
  155. "/user/hand/left,"
  156. "/user/hand/right,"
  157. // "/user/vive_tracker_htcx/role/handheld_object," <-- getting errors on this one
  158. "/user/vive_tracker_htcx/role/left_foot,"
  159. "/user/vive_tracker_htcx/role/right_foot,"
  160. "/user/vive_tracker_htcx/role/left_shoulder,"
  161. "/user/vive_tracker_htcx/role/right_shoulder,"
  162. "/user/vive_tracker_htcx/role/left_elbow,"
  163. "/user/vive_tracker_htcx/role/right_elbow,"
  164. "/user/vive_tracker_htcx/role/left_knee,"
  165. "/user/vive_tracker_htcx/role/right_knee,"
  166. "/user/vive_tracker_htcx/role/waist,"
  167. "/user/vive_tracker_htcx/role/chest,"
  168. "/user/vive_tracker_htcx/role/camera,"
  169. "/user/vive_tracker_htcx/role/keyboard");
  170. Ref<OpenXRAction> aim_pose = action_set->add_new_action("aim_pose", "Aim pose", OpenXRAction::OPENXR_ACTION_POSE, "/user/hand/left,/user/hand/right");
  171. Ref<OpenXRAction> grip_pose = action_set->add_new_action("grip_pose", "Grip pose", OpenXRAction::OPENXR_ACTION_POSE, "/user/hand/left,/user/hand/right");
  172. Ref<OpenXRAction> haptic = action_set->add_new_action("haptic", "Haptic", OpenXRAction::OPENXR_ACTION_HAPTIC,
  173. "/user/hand/left,"
  174. "/user/hand/right,"
  175. // "/user/vive_tracker_htcx/role/handheld_object," <-- getting errors on this one
  176. "/user/vive_tracker_htcx/role/left_foot,"
  177. "/user/vive_tracker_htcx/role/right_foot,"
  178. "/user/vive_tracker_htcx/role/left_shoulder,"
  179. "/user/vive_tracker_htcx/role/right_shoulder,"
  180. "/user/vive_tracker_htcx/role/left_elbow,"
  181. "/user/vive_tracker_htcx/role/right_elbow,"
  182. "/user/vive_tracker_htcx/role/left_knee,"
  183. "/user/vive_tracker_htcx/role/right_knee,"
  184. "/user/vive_tracker_htcx/role/waist,"
  185. "/user/vive_tracker_htcx/role/chest,"
  186. "/user/vive_tracker_htcx/role/camera,"
  187. "/user/vive_tracker_htcx/role/keyboard");
  188. // Create our interaction profiles
  189. Ref<OpenXRInteractionProfile> profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/khr/simple_controller");
  190. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  191. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  192. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  193. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click,/user/hand/right/input/menu/click");
  194. profile->add_new_binding(select_button, "/user/hand/left/input/select/click,/user/hand/right/input/select/click");
  195. // generic has no support for triggers, grip, A/B buttons, nor joystick/trackpad inputs
  196. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  197. add_interaction_profile(profile);
  198. // Create our Vive controller profile
  199. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/htc/vive_controller");
  200. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  201. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  202. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  203. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click,/user/hand/right/input/menu/click");
  204. profile->add_new_binding(select_button, "/user/hand/left/input/system/click,/user/hand/right/input/system/click");
  205. // wmr controller has no a/b/x/y buttons
  206. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  207. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/click,/user/hand/right/input/trigger/click");
  208. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click"); // OpenXR will convert bool to float
  209. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  210. // primary on our vive controller is our trackpad
  211. profile->add_new_binding(primary, "/user/hand/left/input/trackpad,/user/hand/right/input/trackpad");
  212. profile->add_new_binding(primary_click, "/user/hand/left/input/trackpad/click,/user/hand/right/input/trackpad/click");
  213. profile->add_new_binding(primary_touch, "/user/hand/left/input/trackpad/touch,/user/hand/right/input/trackpad/touch");
  214. // vive controllers have no secondary input
  215. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  216. add_interaction_profile(profile);
  217. // Create our WMR controller profile
  218. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/microsoft/motion_controller");
  219. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  220. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  221. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  222. // wmr controllers have no select button we can use
  223. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click,/user/hand/right/input/menu/click");
  224. // wmr controller has no a/b/x/y buttons
  225. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  226. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value"); // OpenXR will convert float to bool
  227. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click"); // OpenXR will convert bool to float
  228. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  229. // primary on our wmr controller is our thumbstick, no touch
  230. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  231. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  232. // secondary on our wmr controller is our trackpad
  233. profile->add_new_binding(secondary, "/user/hand/left/input/trackpad,/user/hand/right/input/trackpad");
  234. profile->add_new_binding(secondary_click, "/user/hand/left/input/trackpad/click,/user/hand/right/input/trackpad/click");
  235. profile->add_new_binding(secondary_touch, "/user/hand/left/input/trackpad/touch,/user/hand/right/input/trackpad/touch");
  236. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  237. add_interaction_profile(profile);
  238. // Create our Meta touch controller profile
  239. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/oculus/touch_controller");
  240. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  241. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  242. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  243. // touch controllers have no select button we can use
  244. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click,/user/hand/right/input/system/click"); // right hand system click may not be available
  245. profile->add_new_binding(ax_button, "/user/hand/left/input/x/click,/user/hand/right/input/a/click"); // x on left hand, a on right hand
  246. profile->add_new_binding(ax_touch, "/user/hand/left/input/x/touch,/user/hand/right/input/a/touch");
  247. profile->add_new_binding(by_button, "/user/hand/left/input/y/click,/user/hand/right/input/b/click"); // y on left hand, b on right hand
  248. profile->add_new_binding(by_touch, "/user/hand/left/input/y/touch,/user/hand/right/input/b/touch");
  249. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  250. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value"); // should be converted to boolean
  251. profile->add_new_binding(trigger_touch, "/user/hand/left/input/trigger/touch,/user/hand/right/input/trigger/touch");
  252. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/value,/user/hand/right/input/squeeze/value"); // should be converted to boolean
  253. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/value,/user/hand/right/input/squeeze/value");
  254. // primary on our touch controller is our thumbstick
  255. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  256. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  257. profile->add_new_binding(primary_touch, "/user/hand/left/input/thumbstick/touch,/user/hand/right/input/thumbstick/touch");
  258. // touch controller has no secondary input
  259. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  260. add_interaction_profile(profile);
  261. // Create our Valve index controller profile
  262. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/valve/index_controller");
  263. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  264. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  265. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  266. // index controllers have no select button we can use
  267. profile->add_new_binding(menu_button, "/user/hand/left/input/system/click,/user/hand/right/input/system/click");
  268. profile->add_new_binding(ax_button, "/user/hand/left/input/a/click,/user/hand/right/input/a/click"); // a on both controllers
  269. profile->add_new_binding(ax_touch, "/user/hand/left/input/a/touch,/user/hand/right/input/a/touch");
  270. profile->add_new_binding(by_button, "/user/hand/left/input/b/click,/user/hand/right/input/b/click"); // b on both controllers
  271. profile->add_new_binding(by_touch, "/user/hand/left/input/b/touch,/user/hand/right/input/b/touch");
  272. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  273. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/click,/user/hand/right/input/trigger/click");
  274. profile->add_new_binding(trigger_touch, "/user/hand/left/input/trigger/touch,/user/hand/right/input/trigger/touch");
  275. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/value,/user/hand/right/input/squeeze/value");
  276. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/value,/user/hand/right/input/squeeze/value"); // this should do a float to bool conversion
  277. // primary on our index controller is our thumbstick
  278. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  279. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  280. profile->add_new_binding(primary_touch, "/user/hand/left/input/thumbstick/touch,/user/hand/right/input/thumbstick/touch");
  281. // secondary on our index controller is our trackpad
  282. profile->add_new_binding(secondary, "/user/hand/left/input/trackpad,/user/hand/right/input/trackpad");
  283. profile->add_new_binding(secondary_click, "/user/hand/left/input/trackpad/force,/user/hand/right/input/trackpad/force"); // not sure if this will work but doesn't seem to support click...
  284. profile->add_new_binding(secondary_touch, "/user/hand/left/input/trackpad/touch,/user/hand/right/input/trackpad/touch");
  285. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  286. add_interaction_profile(profile);
  287. // Note, the following profiles are all part of extensions.
  288. // We include these regardless of whether the extension is active.
  289. // We want our action map to be as complete as possible so our game is as portable as possible.
  290. // It is very possible these will in due time become core.
  291. // Create our HP MR controller profile
  292. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/hp/mixed_reality_controller");
  293. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  294. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  295. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  296. // hpmr controllers have no select button we can use
  297. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click,/user/hand/right/input/menu/click");
  298. // hpmr controllers only register click, not touch, on our a/b/x/y buttons
  299. profile->add_new_binding(ax_button, "/user/hand/left/input/x/click,/user/hand/right/input/a/click"); // x on left hand, a on right hand
  300. profile->add_new_binding(by_button, "/user/hand/left/input/y/click,/user/hand/right/input/b/click"); // y on left hand, b on right hand
  301. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  302. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  303. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/value,/user/hand/right/input/squeeze/value");
  304. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/value,/user/hand/right/input/squeeze/value");
  305. // primary on our hpmr controller is our thumbstick
  306. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  307. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  308. // No secondary on our hpmr controller
  309. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  310. add_interaction_profile(profile);
  311. // Create our Samsung Odyssey controller profile,
  312. // Note that this controller is only identified specifically on WMR, on SteamVR this is identified as a normal WMR controller.
  313. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/samsung/odyssey_controller");
  314. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  315. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  316. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  317. // Odyssey controllers have no select button we can use
  318. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click,/user/hand/right/input/menu/click");
  319. // Odyssey controller has no a/b/x/y buttons
  320. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  321. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  322. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  323. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  324. // primary on our Odyssey controller is our thumbstick, no touch
  325. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  326. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  327. // secondary on our Odyssey controller is our trackpad
  328. profile->add_new_binding(secondary, "/user/hand/left/input/trackpad,/user/hand/right/input/trackpad");
  329. profile->add_new_binding(secondary_click, "/user/hand/left/input/trackpad/click,/user/hand/right/input/trackpad/click");
  330. profile->add_new_binding(secondary_touch, "/user/hand/left/input/trackpad/touch,/user/hand/right/input/trackpad/touch");
  331. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  332. add_interaction_profile(profile);
  333. // Create our Vive Cosmos controller
  334. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/htc/vive_cosmos_controller");
  335. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  336. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  337. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  338. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click");
  339. profile->add_new_binding(select_button, "/user/hand/left/input/system/click"); // we'll map system to select
  340. profile->add_new_binding(ax_button, "/user/hand/left/input/x/click,/user/hand/right/input/a/click"); // x on left hand, a on right hand
  341. profile->add_new_binding(by_button, "/user/hand/left/input/y/click,/user/hand/right/input/b/click"); // y on left hand, b on right hand
  342. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  343. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/click,/user/hand/right/input/trigger/click");
  344. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  345. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  346. // primary on our Cosmos controller is our thumbstick
  347. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  348. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  349. profile->add_new_binding(primary_touch, "/user/hand/left/input/thumbstick/touch,/user/hand/right/input/thumbstick/touch");
  350. // No secondary on our cosmos controller
  351. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  352. add_interaction_profile(profile);
  353. // Create our Vive Focus 3 controller
  354. // Note, Vive Focus 3 currently is not yet supported as a stand alone device
  355. // however HTC currently has a beta OpenXR runtime in testing we may support in the near future
  356. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/htc/vive_focus3_controller");
  357. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  358. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  359. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  360. profile->add_new_binding(menu_button, "/user/hand/left/input/menu/click");
  361. profile->add_new_binding(select_button, "/user/hand/left/input/system/click"); // we'll map system to select
  362. profile->add_new_binding(ax_button, "/user/hand/left/input/x/click,/user/hand/right/input/a/click"); // x on left hand, a on right hand
  363. profile->add_new_binding(by_button, "/user/hand/left/input/y/click,/user/hand/right/input/b/click"); // y on left hand, b on right hand
  364. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  365. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/click,/user/hand/right/input/trigger/click");
  366. profile->add_new_binding(trigger_touch, "/user/hand/left/input/trigger/touch,/user/hand/right/input/trigger/touch");
  367. profile->add_new_binding(grip, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  368. profile->add_new_binding(grip_click, "/user/hand/left/input/squeeze/click,/user/hand/right/input/squeeze/click");
  369. // primary on our Focus 3 controller is our thumbstick
  370. profile->add_new_binding(primary, "/user/hand/left/input/thumbstick,/user/hand/right/input/thumbstick");
  371. profile->add_new_binding(primary_click, "/user/hand/left/input/thumbstick/click,/user/hand/right/input/thumbstick/click");
  372. profile->add_new_binding(primary_touch, "/user/hand/left/input/thumbstick/touch,/user/hand/right/input/thumbstick/touch");
  373. // We only have a thumb rest
  374. profile->add_new_binding(secondary_touch, "/user/hand/left/input/thumbrest/touch,/user/hand/right/input/thumbrest/touch");
  375. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  376. add_interaction_profile(profile);
  377. // Create our Huawei controller
  378. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/huawei/controller");
  379. profile->add_new_binding(default_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  380. profile->add_new_binding(aim_pose, "/user/hand/left/input/aim/pose,/user/hand/right/input/aim/pose");
  381. profile->add_new_binding(grip_pose, "/user/hand/left/input/grip/pose,/user/hand/right/input/grip/pose");
  382. profile->add_new_binding(menu_button, "/user/hand/left/input/home/click,/user/hand/right/input/home/click");
  383. profile->add_new_binding(trigger, "/user/hand/left/input/trigger/value,/user/hand/right/input/trigger/value");
  384. profile->add_new_binding(trigger_click, "/user/hand/left/input/trigger/click,/user/hand/right/input/trigger/click");
  385. // primary on our Huawei controller is our trackpad
  386. profile->add_new_binding(primary, "/user/hand/left/input/trackpad,/user/hand/right/input/trackpad");
  387. profile->add_new_binding(primary_click, "/user/hand/left/input/trackpad/click,/user/hand/right/input/trackpad/click");
  388. profile->add_new_binding(primary_touch, "/user/hand/left/input/trackpad/touch,/user/hand/right/input/trackpad/touch");
  389. profile->add_new_binding(haptic, "/user/hand/left/output/haptic,/user/hand/right/output/haptic");
  390. // Create our HTC Vive tracker profile
  391. profile = OpenXRInteractionProfile::new_profile("/interaction_profiles/htc/vive_tracker_htcx");
  392. profile->add_new_binding(default_pose,
  393. // "/user/vive_tracker_htcx/role/handheld_object/input/grip/pose," <-- getting errors on this one
  394. "/user/vive_tracker_htcx/role/left_foot/input/grip/pose,"
  395. "/user/vive_tracker_htcx/role/right_foot/input/grip/pose,"
  396. "/user/vive_tracker_htcx/role/left_shoulder/input/grip/pose,"
  397. "/user/vive_tracker_htcx/role/right_shoulder/input/grip/pose,"
  398. "/user/vive_tracker_htcx/role/left_elbow/input/grip/pose,"
  399. "/user/vive_tracker_htcx/role/right_elbow/input/grip/pose,"
  400. "/user/vive_tracker_htcx/role/left_knee/input/grip/pose,"
  401. "/user/vive_tracker_htcx/role/right_knee/input/grip/pose,"
  402. "/user/vive_tracker_htcx/role/waist/input/grip/pose,"
  403. "/user/vive_tracker_htcx/role/chest/input/grip/pose,"
  404. "/user/vive_tracker_htcx/role/camera/input/grip/pose,"
  405. "/user/vive_tracker_htcx/role/keyboard/input/grip/pose");
  406. profile->add_new_binding(haptic,
  407. // "/user/vive_tracker_htcx/role/handheld_object/output/haptic," <-- getting errors on this one
  408. "/user/vive_tracker_htcx/role/left_foot/output/haptic,"
  409. "/user/vive_tracker_htcx/role/right_foot/output/haptic,"
  410. "/user/vive_tracker_htcx/role/left_shoulder/output/haptic,"
  411. "/user/vive_tracker_htcx/role/right_shoulder/output/haptic,"
  412. "/user/vive_tracker_htcx/role/left_elbow/output/haptic,"
  413. "/user/vive_tracker_htcx/role/right_elbow/output/haptic,"
  414. "/user/vive_tracker_htcx/role/left_knee/output/haptic,"
  415. "/user/vive_tracker_htcx/role/right_knee/output/haptic,"
  416. "/user/vive_tracker_htcx/role/waist/output/haptic,"
  417. "/user/vive_tracker_htcx/role/chest/output/haptic,"
  418. "/user/vive_tracker_htcx/role/camera/output/haptic,"
  419. "/user/vive_tracker_htcx/role/keyboard/output/haptic");
  420. add_interaction_profile(profile);
  421. }
  422. void OpenXRActionMap::create_editor_action_sets() {
  423. // TODO implement
  424. }
  425. Ref<OpenXRAction> OpenXRActionMap::get_action(const String p_path) const {
  426. PackedStringArray paths = p_path.split("/", false);
  427. ERR_FAIL_COND_V(paths.size() != 2, Ref<OpenXRAction>());
  428. Ref<OpenXRActionSet> action_set = find_action_set(paths[0]);
  429. if (action_set.is_valid()) {
  430. return action_set->get_action(paths[1]);
  431. }
  432. return Ref<OpenXRAction>();
  433. }
  434. void OpenXRActionMap::remove_action(const String p_path) {
  435. Ref<OpenXRAction> action = get_action(p_path);
  436. if (action.is_valid()) {
  437. OpenXRActionSet *action_set = action->get_action_set();
  438. if (action_set != nullptr) {
  439. // Remove the action from this action set
  440. action_set->remove_action(action);
  441. }
  442. for (int i = 0; i < interaction_profiles.size(); i++) {
  443. Ref<OpenXRInteractionProfile> interaction_profile = interaction_profiles[i];
  444. // Remove any bindings for this action
  445. interaction_profile->remove_binding_for_action(action);
  446. }
  447. }
  448. }
  449. PackedStringArray OpenXRActionMap::get_top_level_paths(Ref<OpenXRAction> p_action) {
  450. PackedStringArray arr;
  451. for (int i = 0; i < interaction_profiles.size(); i++) {
  452. Ref<OpenXRInteractionProfile> ip = interaction_profiles[i];
  453. const OpenXRDefs::InteractionProfile *profile = OpenXRDefs::get_profile(ip->get_interaction_profile_path());
  454. if (profile != nullptr) {
  455. for (int j = 0; j < ip->get_binding_count(); j++) {
  456. Ref<OpenXRIPBinding> binding = ip->get_binding(j);
  457. if (binding->get_action() == p_action) {
  458. PackedStringArray paths = binding->get_paths();
  459. for (int k = 0; k < paths.size(); k++) {
  460. const OpenXRDefs::IOPath *io_path = profile->get_io_path(paths[k]);
  461. if (io_path != nullptr) {
  462. String top_path = String(io_path->top_level_path->openxr_path);
  463. if (!arr.has(top_path)) {
  464. arr.push_back(top_path);
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. print_line("Toplevel paths for", p_action->get_name_with_set(), "are", arr);
  473. return arr;
  474. }
  475. OpenXRActionMap::~OpenXRActionMap() {
  476. action_sets.clear();
  477. interaction_profiles.clear();
  478. }