Example #1
0
        Func <bool> InitPanel(Widget panel)
        {
            hotkeyList        = panel.Get <ScrollPanelWidget>("HOTKEY_LIST");
            hotkeyList.Layout = new GridLayout(hotkeyList);
            var hotkeyHeader = hotkeyList.Get <ScrollItemWidget>("HEADER");
            var templates    = hotkeyList.Get("TEMPLATES");

            hotkeyList.RemoveChildren();

            Func <bool> returnTrue = () => true;
            Action      doNothing  = () => { };

            if (logicArgs.TryGetValue("HotkeyGroups", out var hotkeyGroups))
            {
                InitHotkeyRemapDialog(panel);

                foreach (var hg in hotkeyGroups.Nodes)
                {
                    var templateNode = hg.Value.Nodes.FirstOrDefault(n => n.Key == "Template");
                    var typesNode    = hg.Value.Nodes.FirstOrDefault(n => n.Key == "Types");
                    if (templateNode == null || typesNode == null)
                    {
                        continue;
                    }

                    var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing);
                    header.Get <LabelWidget>("LABEL").GetText = () => hg.Key;
                    hotkeyList.AddChild(header);

                    var types    = FieldLoader.GetValue <string[]>("Types", typesNode.Value.Value);
                    var added    = new HashSet <HotkeyDefinition>();
                    var template = templates.Get(templateNode.Value.Value);

                    foreach (var t in types)
                    {
                        foreach (var hd in modData.Hotkeys.Definitions.Where(k => k.Types.Contains(t)))
                        {
                            if (added.Add(hd))
                            {
                                if (selectedHotkeyDefinition == null)
                                {
                                    selectedHotkeyDefinition = hd;
                                }

                                BindHotkeyPref(hd, template);
                            }
                        }
                    }
                }
            }

            return(() =>
            {
                hotkeyEntryWidget.Key = modData.Hotkeys[selectedHotkeyDefinition.Name].GetValue();
                hotkeyEntryWidget.ForceYieldKeyboardFocus();

                return false;
            });
        }
        Func <bool> InitPanel(Widget panel)
        {
            hotkeyList        = panel.Get <ScrollPanelWidget>("HOTKEY_LIST");
            hotkeyList.Layout = new GridLayout(hotkeyList);
            headerTemplate    = hotkeyList.Get("HEADER");
            template          = hotkeyList.Get("TEMPLATE");
            emptyListMessage  = panel.Get("HOTKEY_EMPTY_LIST");
            remapDialog       = panel.Get("HOTKEY_REMAP_DIALOG");

            foreach (var hd in modData.Hotkeys.Definitions)
            {
                contexts.UnionWith(hd.Contexts);
            }

            filterInput = panel.Get <TextFieldWidget>("FILTER_INPUT");
            filterInput.OnTextEdited = () => InitHotkeyList();
            filterInput.OnEscKey     = _ =>
            {
                if (string.IsNullOrEmpty(filterInput.Text))
                {
                    filterInput.YieldKeyboardFocus();
                }
                else
                {
                    filterInput.Text = "";
                    filterInput.OnTextEdited();
                }

                return(true);
            };

            var contextDropdown = panel.GetOrNull <DropDownButtonWidget>("CONTEXT_DROPDOWN");

            if (contextDropdown != null)
            {
                contextDropdown.OnMouseDown = _ => ShowContextDropdown(contextDropdown);
                var contextName = new CachedTransform <string, string>(GetContextDisplayName);
                contextDropdown.GetText = () => contextName.Update(currentContext);
            }

            if (logicArgs.TryGetValue("HotkeyGroups", out var hotkeyGroupsYaml))
            {
                foreach (var hg in hotkeyGroupsYaml.Nodes)
                {
                    var typesNode = hg.Value.Nodes.FirstOrDefault(n => n.Key == "Types");
                    if (typesNode != null)
                    {
                        hotkeyGroups.Add(hg.Key, FieldLoader.GetValue <HashSet <string> >("Types", typesNode.Value.Value));
                    }
                }

                InitHotkeyRemapDialog(panel);
                InitHotkeyList();
            }

            return(() =>
            {
                hotkeyEntryWidget.Key =
                    selectedHotkeyDefinition != null ?
                    modData.Hotkeys[selectedHotkeyDefinition.Name].GetValue() :
                    Hotkey.Invalid;

                hotkeyEntryWidget.ForceYieldKeyboardFocus();

                return false;
            });
        }