예제 #1
0
        public void SetLayoutDefinition(ControllerLayoutDefinition layoutDef)
        {
            var lst = (from b in layoutDef.Buttons
                       orderby b.Position
                       select new ButtonStyleListViewItem()
                       {
                           Button = b
                       }).ToList();

            foreach (var item in lst)
            {
                item.Button.ButtonControl.Tag = item; // this is so backwards :(
                listview.Items.Add(item);
            }

            layoutDef.TopLevelPanel.Focusable = false;
            layoutDef.TopLevelPanel.IsHitTestVisible = false;
            cp.Content = layoutDef.TopLevelPanel;
        }
        private void OnSelectLayoutTemplate()
        {
            var dataTemplate = this.Resources["ButtonLayoutSelectorListViewItemDataTemplate"] as DataTemplate;

            MultiOptionSelector sel = new MultiOptionSelector(dataTemplate);

            sel.SetItemSource(_controllerLayoutTemplates.ToArray());

            sel.Width = 600;
            sel.Height = 600;

            Win.Modal(sel, this);

            //if (Win.Modal(sel, this))
            {
                ControllerLayoutDefinition def = (ControllerLayoutDefinition)sel.SelectedItem;

                this.SelectedLayoutDefinition = def;

                layoutTemplate.Value = def.Name;

                //var layoutConfig = new LayoutConfig();

                //layoutConfig.Name = def.Name;
                //layoutConfig.Description = def.Description;
                //layoutConfig.Buttons = new List<ButtonConfig>();

                //foreach(var p in def.ButtonPositionMapping)
                //{
                //    layoutConfig.Buttons.Add(new ButtonConfig() { Position = p.Key, HueOffset = def.ButtonPositionMapping[p.Key].HueOffset });
                //}

                HyperSearchSettings.Instance().Input.LayoutConfig = def;

            }
        }
        private void LoadTemplates()
        {
            try
            {// TODO: Log each failure and continue with next file
                // TODO: ALso apply some validation rules. Like missing Position defs , not sequential positions, duplicate posiitions, etc...
                var xamlPath = Global.BuildFilePathInAppDir("Resources\\ControllerLayouts");
                var xamlFiles = Directory.EnumerateFiles(xamlPath, "*.xaml");

                System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext();

                parserContext.XmlnsDictionary.Add("ex", "clr-namespace:HyperSearch.Attached;assembly=HyperSearch");
                parserContext.XmlnsDictionary.Add("hsc", "clr-namespace:HyperSearch.Attached;assembly=HyperSearch");

                MainWindow.LogStatic("Found {0} controller template(s) in {1}", xamlFiles.Count(), xamlPath);

                // build a list of all available controller layout templates
                foreach (var file in xamlFiles)
                {
                    try
                    {
                        ControllerLayoutDefinition def = new ControllerLayoutDefinition();

                        var xaml = File.ReadAllText(file);

                        var topLevelPanel = (FrameworkElement)System.Windows.Markup.XamlReader.Parse(xaml, parserContext);

                        def.RawXaml = xaml;
                        def.TopLevelPanel = topLevelPanel;

                        var name = ControllerLayout.GetName(topLevelPanel);
                        var description = ControllerLayout.GetDescription(topLevelPanel);

                        if (!string.IsNullOrEmpty(name)) def.Name = name;
                        else def.Name = new FileInfo(file).GetFilenameWithoutExtension();

                        def.Description = description;

                        var allChildControls = topLevelPanel.FindVisualChildren<Control>().ToList();

                        foreach (var c in allChildControls)
                        {
                            var pos = ControllerLayout.GetPosition(c);

                            if (!pos.HasValue) continue;

                            if (def.Buttons.Count(b => b.Position == pos.Value) > 0)
                            {
                                throw new InvalidDataException(string.Format("Duplicate position detected for position {0}. Make sure that Positions are only allocated once.", pos.Value));
                            }

                            def.Buttons.Add(new ButtonConfig() { ButtonControl = c, Position = pos.Value });
                        }

                        _controllerLayoutTemplates.Add(def);
                    }
                    catch (Exception ex)
                    {
                        ErrorHandler.HandleException(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.HandleException(ex);
            }
        }