/// <summary> /// Draw the entity. /// </summary> /// <param name="spriteBatch">Sprite batch to draw on.</param> override protected void DrawEntity(SpriteBatch spriteBatch) { // if size changed, update paragraphs list if ((_prevSize.Y != _destRectInternal.Size.Y) || _hadResizeWhileNotVisible) { OnResize(); } // store last known size _prevSize = _destRectInternal.Size; // call base draw function to draw the panel part base.DrawEntity(spriteBatch); // update paragraphs list values for (int i = 0; i < _paragraphs.Count; ++i) { // if paragraph is within items range: int item_index = i + (int)_scrollbar.Value; if (item_index < _list.Count) { // set paragraph text, make visible, and remove background. _paragraphs[i].Text = _list[item_index]; _paragraphs[i].Background = null; _paragraphs[i].Visible = true; // set locked state bool isLocked = false; LockedItems.TryGetValue(item_index, out isLocked); _paragraphs[i].Locked = isLocked; } // if paragraph out of range (eg more paragraphs than list items), make this paragraph invisible. else { _paragraphs[i].Visible = false; _paragraphs[i].Text = string.Empty; } } // highlight the currently selected item paragraph (eg the paragraph that represent the selected value, if currently visible) int selectedParagraphIndex = _index; if (selectedParagraphIndex != -1) { int i = selectedParagraphIndex - _scrollbar.Value; if (i >= 0 && i < _paragraphs.Count) { // add background to selected paragraph Paragraph paragraph = _paragraphs[i]; Rectangle destRect = paragraph.GetActualDestRect(); Vector2 size = new Vector2(0, destRect.Height * 1.35f / UserInterface.Active.GlobalScale); paragraph.State = EntityState.MouseDown; paragraph.Padding = new Vector2(-Padding.X, 0); paragraph.CalcDestRect(); paragraph.CalcInternalRect(); ColoredRectangle selectMark = new ColoredRectangle(GetActiveStyle("SelectedHighlightColor").asColor, size, Anchor.TopCenter, new Vector2(0, -4)); paragraph.Background = selectMark; } } }
/// <summary> /// Prepare the input paragraph for display. /// </summary> /// <param name="usePlaceholder">If true, will use the placeholder text. Else, will use the real input text.</param> /// <param name="showCaret">If true, will also add the caret text when needed. If false, will not show caret.</param> /// <returns>Processed text that will actually be displayed on screen.</returns> protected string PrepareInputTextForDisplay(bool usePlaceholder, bool showCaret) { // set main paragraph text and add caret mark if needed string caretShow = showCaret ? ((int)_caretAnim % 2 == 0) ? "|" : " " : ""; TextParagraph.Text = _value.Insert(_caret >= 0 ? _caret : _value.Length, caretShow); // update placeholder text PlaceholderParagraph.Text = _placeholderText; // get current paragraph and prepare to draw Paragraph currParagraph = usePlaceholder ? PlaceholderParagraph : TextParagraph; TextParagraph.CalcDestRect(); TextParagraph.PrepareForDraw(); // get text to display return(currParagraph.GetProcessedText()); }
/// <summary> /// When list is resized (also called on init), create the labels to show item values and init graphical stuff. /// </summary> /// <param name="recalcDestRect">If true, will also recalculate destination rectangle.</param> protected virtual void OnResize(bool recalcDestRect = true) { // store current size _prevSize = _size; // remove all children before re-creating them ClearChildren(); // remove previous paragraphs list _paragraphs.Clear(); // calculate self destination rect if (recalcDestRect) { _destRect = CalcDestRect(); } // calculate paragraphs quantity int i = 0; while (true) { // create and add new paragraph Paragraph paragraph = new Paragraph(".", Anchor.Auto, new Vector2(0, 40)); // note: we set negative padding for the selected mark size so it won't be negative size paragraph.WrapWords = false; paragraph.UpdateStyle(DefaultParagraphStyle); paragraph.Scale = paragraph.Scale * ItemsScale; paragraph.SpaceAfter = paragraph.SpaceAfter + new Vector2(0, ExtraSpaceBetweenLines); paragraph.AttachedData = new ParagraphData(this, i++); paragraph.UseActualSizeForCollision = false; AddChild(paragraph); _paragraphs.Add(paragraph); OnCreatedListParagraph(paragraph); // add callback to selection paragraph.OnClick = (Entity entity) => { ParagraphData data = (ParagraphData)entity.AttachedData; if (!data.list.LockSelection) { data.list.Select(data.relativeIndex, true); } }; // to calculate paragraph actual bottom paragraph.CalcDestRect(); paragraph.PrepareForDraw(); // if out of list bounderies remove this paragraph and stop if ((paragraph.GetActualDestRect().Bottom > _destRect.Bottom - Padding.Y) || i > _list.Count) { RemoveChild(paragraph); _paragraphs.Remove(paragraph); break; } } // add scrollbar last, but only if needed if (_paragraphs.Count < _list.Count) { // add scrollbar to list AddChild(_scrollbar, false); // calc max scroll value _scrollbar.Max = (uint)(_list.Count - _paragraphs.Count); if (_scrollbar.Max < 2) { _scrollbar.Max = 2; } _scrollbar.StepsCount = _scrollbar.Max; _scrollbar.Visible = true; } // if no scrollbar is needed, hide it else { _scrollbar.Visible = false; if (_scrollbar.Value > 0) { _scrollbar.Value = 0; } } }