From d9e8cd4dde1d27a423fc192d0adf8dd8c1fcab35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Wed, 20 May 2015 17:38:41 +0200 Subject: [PATCH] Add input mapping GUI demo --- demos/gui/input_mapping/controls.gd | 49 +++++++++++++++++++++++++++ demos/gui/input_mapping/controls.scn | Bin 0 -> 2686 bytes demos/gui/input_mapping/engine.cfg | 18 ++++++++++ 3 files changed, 67 insertions(+) create mode 100644 demos/gui/input_mapping/controls.gd create mode 100644 demos/gui/input_mapping/controls.scn create mode 100644 demos/gui/input_mapping/engine.cfg diff --git a/demos/gui/input_mapping/controls.gd b/demos/gui/input_mapping/controls.gd new file mode 100644 index 0000000000..6ca059c812 --- /dev/null +++ b/demos/gui/input_mapping/controls.gd @@ -0,0 +1,49 @@ +# Note for the reader: +# +# This demo conveniently uses the same names for actions and for the container nodes +# that hold each remapping button. This allow to get back to the button based simply +# on the name of the corresponding action, but it might not be so simple in your project. +# +# A better approach for large-scale input remapping might be to do the connections between +# buttons and wait_for_input through the code, passing as arguments both the name of the +# action and the node, e.g.: +# button.connect("pressed", self, "wait_for_input", [ button, action ]) + +extends Control + +var player_actions = [ "move_up", "move_down", "move_left", "move_right", "jump" ] +var action # To register the action the UI is currently handling +var button # Button node corresponding to the above action + +func wait_for_input(action_bind): + action = action_bind + # See note at the beginning of the script + button = get_node("bindings").get_node(action).get_node("Button") + get_node("contextual_help").set_text("Press a key to assign to the '" + action + "' action.") + set_process_input(true) + +func _input(event): + # Handle the first pressed key + if (event.type == InputEvent.KEY): + # Register the event as handled and stop polling + get_tree().set_input_as_handled() + set_process_input(false) + # Reinitialise the contextual help label + get_node("contextual_help").set_text("Click a key binding to reassign it, or press the Cancel action.") + if (not event.is_action("ui_cancel")): + # Display the string corresponding to the pressed key + button.set_text(OS.get_scancode_string(event.scancode)) + # Start by removing previously key binding(s) + for old_event in InputMap.get_action_list(action): + InputMap.action_erase_event(action, old_event) + # Add the new key binding + InputMap.action_add_event(action, event) + +func _ready(): + # Initialise each button with the default key binding from InputMap + var input_event + for action in player_actions: + # We assume that the key binding that we want is the first one (0), if there are several + input_event = InputMap.get_action_list(action)[0] + # See note at the beginning of the script + get_node("bindings").get_node(action).get_node("Button").set_text(OS.get_scancode_string(input_event.scancode)) diff --git a/demos/gui/input_mapping/controls.scn b/demos/gui/input_mapping/controls.scn new file mode 100644 index 0000000000000000000000000000000000000000..276712ba2205efe1e3c9b04f52ba9d67e5d2ffbd GIT binary patch literal 2686 zcmY*b4Qw366@I&W_S$!G&W`Pz?eovvIgax~a!G8fl1Y=dw-?8b-Gc0fgeL9I-re4L zle^txch8QUsM?T*w9p6z`WGTKhgy}C1GObZs7h%r4wR@+Cs%Q~P z$Dkru>CMjUyzl*dGdpj_CuA8GpL&2`C&0TRzzh$-0k|A+q#6YTSVDaMTh-KlEj^La z42{%z0{}j!Kt=H>MICBLBgn4FK^+OyDSEzOT8>gsok?gU_5LZ{){}ZpcV=SL z&*d~vTrQXeHAOl(Xz{An`D`thQ7U0r?p1|rb9#2tQSz$2pE_b*wX(Vqv#?oRRjL=0 zrsJ4-XeOJ*jF~Fh3ZoFvQ9{D{7tz^jK zLrZMO(wx+!VyTX9(hu)!nhPffYm~g^sEPvh0fB2k06%EZB}#@c5-dLCtD(x7$%YrRItn1j%kU zC~5}^ik8+LcNv(`d0tB^Y}M@K6#j}n2x{aS7|G+L1O50)wOVw{X-jn{6M%GR~T%9+kG^JRe<6OKL?QqA1v~H`>laQuoeU6#U=A=Jz zX^s9(&~2sAy&G)OiF0Bqr>FKSrr|cxUxOa`7Ojaq502VRR&WJ{{z*PWi+tLgKD?FI z&2#(k#GQNPJA{43e1T@_rd8chGNz^IMxp4?KU9uluRI-X=WLzKfDqX&KZzmfNphSw zlvLz4aA+F{q_ffn26@@z+Y)`)CiT`%sg|x9jt!k825X2A|3w87EtpI^DzG1DD^7S^ zkRx&ILckCP=+8t z4$jh|J9&Ld^L#e0qa8xl($k)=HSqy$_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