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 zcmWFvc6Md}0RaXEemMq)m+TA-l}ro_j0_A7j0}tlY!1v03=BL9EL;JJ$=RtX!O5w4 zsS3OgOc@wh6zmeB8Tb@9_=-}C^Gl18Q}y!_b5j{O6$IJilS=baa#B+gU=`6u1=Zd9C#Ilk@XRit=-c_0m%kR2aAvBw2FulM{0kq?rsD z?key)Fg##C;n2dsq#(f{rNHbE#=xq;&cLR??9jzj%%G&e$zZC$>|lBzo+Z9ClYv7) zn8CRrpOJxAftxozGq)hWs3g81u_S{*SV4fREVDQ>DKjUtq*6bDi!mql0lOG;enDcg zf*d1*!~<4cmg3Z$wD<$k3}O#hr8#pl(=$rqa}$fR6H3@~6N}O_^Yn`xL=!a@2(TyR zmz3n^GKebJu&3oGmlnt8r79>fBq=7b>F1=Tl`x1r;F4e}$uCIAeZX$e5W^t;fXi?} zs>7QE6F&XS^t}9{)cD-|(&AJG!2^bj`o#)S3q%~w@fT-SrN*b_B&HYZXXF=URw=qL zCgvnedcY*fQkGg&lBu|pkwH?y?E#ZGPjN|6YDscNd{JUaW`4q_CXqwZNmd;3xv3?I z@$n1-3f7LBxl$`iN)vP9Gg5O35^u8jBqpWiBwk#opD4n>t)RPrUtvR%4M$O8UV5s2 zZf4#CW?sg`iUh3&Y3AaR)Pe_0e9Q%j>8S}34YF)yi8-aI4Ezca4gBn>6$SCBDVZe= zLJavz51CR^;uBODij&G5RwgWDNK|~mkeAe}AnU;8aGfo&v?Ra0D6wG{TWLW-YEg2c zV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