|
17 | 17 | ////TODO: ensure that things are aligned properly for ARM; should that be done on the reading side or in the state layouts? |
18 | 18 | //// (make sure that alignment works the same on *all* platforms; otherwise editor will not be able to process events from players properly) |
19 | 19 |
|
20 | | -////FIXME: looks like `useStateFrom` is not working properly in combination with isModifyingExistingControl |
21 | | - |
22 | 20 | namespace UnityEngine.InputSystem.Layouts |
23 | 21 | { |
24 | 22 | /// <summary> |
@@ -202,28 +200,13 @@ private InputControl InstantiateLayout(InputControlLayout layout, InternedString |
202 | 200 | // controls, assign them their blocks now. |
203 | 201 | if (haveChildrenUsingStateFromOtherControl) |
204 | 202 | { |
205 | | - foreach (var controlLayout in layout.controls) |
| 203 | + var controls = layout.m_Controls; |
| 204 | + for (var i = 0; i < controls.Length; ++i) |
206 | 205 | { |
207 | | - if (string.IsNullOrEmpty(controlLayout.useStateFrom)) |
| 206 | + ref var item = ref controls[i]; |
| 207 | + if (string.IsNullOrEmpty(item.useStateFrom)) |
208 | 208 | continue; |
209 | | - |
210 | | - var child = InputControlPath.TryFindChild(control, controlLayout.name); |
211 | | - Debug.Assert(child != null, "Could not find child control which should be present at this point"); |
212 | | - |
213 | | - // Find the referenced control. |
214 | | - var referencedControl = InputControlPath.TryFindChild(control, controlLayout.useStateFrom); |
215 | | - if (referencedControl == null) |
216 | | - throw new InvalidOperationException( |
217 | | - $"Cannot find control '{controlLayout.useStateFrom}' referenced in 'useStateFrom' of control '{controlLayout.name}' in layout '{layout.name}'"); |
218 | | - |
219 | | - // Copy its state settings. |
220 | | - child.m_StateBlock = referencedControl.m_StateBlock; |
221 | | - |
222 | | - // At this point, all byteOffsets are relative to parents so we need to |
223 | | - // walk up the referenced control's parent chain and add offsets until |
224 | | - // we are at the same level that we are at. |
225 | | - for (var parentInChain = referencedControl.parent; parentInChain != control; parentInChain = parentInChain.parent) |
226 | | - child.m_StateBlock.byteOffset += parentInChain.m_StateBlock.byteOffset; |
| 209 | + ApplyUseStateFrom(control, ref item, layout); |
227 | 210 | } |
228 | 211 | } |
229 | 212 |
|
@@ -574,6 +557,28 @@ private InputControl InsertChildControl(InputControlLayout layout, InternedStrin |
574 | 557 | return control; |
575 | 558 | } |
576 | 559 |
|
| 560 | + private static void ApplyUseStateFrom(InputControl parent, ref InputControlLayout.ControlItem controlItem, InputControlLayout layout) |
| 561 | + { |
| 562 | + var child = InputControlPath.TryFindChild(parent, controlItem.name); |
| 563 | + Debug.Assert(child != null, "Could not find child control which should be present at this point"); |
| 564 | + |
| 565 | + // Find the referenced control. |
| 566 | + var referencedControl = InputControlPath.TryFindChild(parent, controlItem.useStateFrom); |
| 567 | + if (referencedControl == null) |
| 568 | + throw new InvalidOperationException( |
| 569 | + $"Cannot find control '{controlItem.useStateFrom}' referenced in 'useStateFrom' of control '{controlItem.name}' in layout '{layout.name}'"); |
| 570 | + |
| 571 | + // Copy its state settings. |
| 572 | + child.m_StateBlock = referencedControl.m_StateBlock; |
| 573 | + |
| 574 | + // At this point, all byteOffsets are relative to parents so we need to |
| 575 | + // walk up the referenced control's parent chain and add offsets until |
| 576 | + // we are at the same level that we are at. |
| 577 | + if (child.parent != referencedControl.parent) |
| 578 | + for (var parentInChain = referencedControl.parent; parentInChain != parent; parentInChain = parentInChain.parent) |
| 579 | + child.m_StateBlock.byteOffset += parentInChain.m_StateBlock.byteOffset; |
| 580 | + } |
| 581 | + |
577 | 582 | private static void ShiftChildIndicesInHierarchyOneUp(InputDevice device, int startIndex, InputControl exceptControl) |
578 | 583 | { |
579 | 584 | var controls = device.m_ChildrenForEachControl; |
|
0 commit comments