Example #1
0
        /// <summary>Handle a click at the given position, if applicable.</summary>
        /// <param name="x">The X-position that was clicked.</param>
        /// <param name="y">The Y-position that was clicked.</param>
        /// <param name="itemClicked">Whether a dropdown item was clicked.</param>
        /// <returns>Returns whether the click was handled.</returns>
        public bool TryClick(int x, int y, out bool itemClicked)
        {
            // dropdown value
            var option = this.Options.FirstOrDefault(p => p.visible && p.containsPoint(x, y));

            if (option != null)
            {
                this.SelectedOption = option;
                itemClicked         = true;
                return(true);
            }
            itemClicked = false;

            // arrows
            if (this.UpArrow.containsPoint(x, y))
            {
                this.Scroll(-1);
                return(true);
            }
            if (this.DownArrow.containsPoint(x, y))
            {
                this.Scroll(1);
                return(true);
            }

            return(false);
        }
Example #2
0
        public bool TryClick(int x, int y, out bool itemClicked)
        {
            var option = _options.FirstOrDefault(p => p.visible && p.containsPoint(x, y));

            if (option != null)
            {
                _selectedOption = option;
                itemClicked     = true;
                return(true);
            }

            itemClicked = false;

            if (_upArrow.containsPoint(x, y))
            {
                Scroll(-1);
                return(true);
            }

            if (_downArrow.containsPoint(x, y))
            {
                Scroll(1);
                return(true);
            }

            return(false);
        }
Example #3
0
        /// <summary>Select an item in the list matching the given value.</summary>
        /// <param name="value">The value to search.</param>
        /// <returns>Returns whether an item was selected.</returns>
        public bool TrySelect(TValue value)
        {
            var entry = this.Options.FirstOrDefault(p =>
                                                    (p.Value == null && value == null) ||
                                                    p.Value?.Equals(value) == true
                                                    );

            if (entry == null)
            {
                return(false);
            }

            this.SelectedOption = entry;
            return(true);
        }
Example #4
0
        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="selectedValue">The selected value.</param>
        /// <param name="items">The items in the list.</param>
        /// <param name="getLabel">Get the display label for an item.</param>
        /// <param name="x">The X-position from which to render the list.</param>
        /// <param name="y">The Y-position from which to render the list.</param>
        /// <param name="font">The font with which to render text.</param>
        public DropdownList(TValue selectedValue, TValue[] items, Func <TValue, string> getLabel, int x, int y, SpriteFont font)
            : base(new Rectangle(), nameof(DropdownList <TValue>))
        {
            // save values
            this.Options = items
                           .Select((item, index) => new DropListOption(Rectangle.Empty, index, getLabel(item), item, font))
                           .ToArray();
            this.Font           = font;
            this.MaxLabelHeight = this.Options.Max(p => p.LabelHeight);

            // set initial selection
            int selectedIndex = Array.IndexOf(items, selectedValue);

            this.SelectedOption = selectedIndex >= 0
                ? this.Options[selectedIndex]
                : this.Options.First();

            // initialize UI
            this.bounds.X = x;
            this.bounds.Y = y;
            this.ReinitializeComponents();
        }
Example #5
0
        public DropdownList(TValue selectedValue, TValue[] items, Func <TValue, string> getLabel, int x, int y,
                            SpriteFont font)
            : base(new Rectangle(), nameof(DropdownList <TValue>))
        {
            _options = items
                       .Select((i, index) => new DropListOption(Rectangle.Empty, index, getLabel(i), i, font))
                       .ToArray();

            _font       = font;
            _labelWidth = RenderHelpers.GetLabelWidth(_font);

            MaxLabelHeight = _options.Max(p => p.LabelHeight);

            var selectedIndex = Array.IndexOf(items, selectedValue);

            _selectedOption = selectedIndex >= 0 ? _options[selectedIndex] : _options.First();

            bounds.X = x;
            bounds.Y = y;

            ReinitializeComponents();
        }