Jypeli  5
The simple game programming library
StringListWidget.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2009-2011 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tomi Karppinen, Tero Jäntti
28  */
29 
30 using System;
31 using Microsoft.Xna.Framework;
32 using System.Text;
33 using Microsoft.Xna.Framework.Graphics;
34 using Jypeli.GameObjects;
35 
36 namespace Jypeli.Widgets
37 {
41  public class StringListWidget : ListWidget<string, Label>
42  {
43  Font _font = Font.Default;
44  Color _textColor = Color.Black;
45  HorizontalAlignment _hAlignment = HorizontalAlignment.Left;
46 
50  public Font Font
51  {
52  get { return _font; }
53  set
54  {
55  _font = value;
56  for ( int i = 0; i < Content.ItemCount; i++ )
57  Content[i].Font = value;
58  }
59  }
60 
64  public Color TextColor
65  {
66  get { return _textColor; }
67  set
68  {
69  _textColor = value;
70  for ( int i = 0; i < Content.ItemCount; i++ )
71  Content[i].TextColor = value;
72  }
73  }
74 
78  public HorizontalAlignment ItemAligment
79  {
80  get { return _hAlignment; }
81  set
82  {
83  _hAlignment = value;
84  for ( int i = 0; i < Content.ItemCount; i++ )
85  Content[i].HorizontalAlignment = value;
86  }
87  }
88 
92  public string Text
93  {
94  get
95  {
96  StringBuilder result = new StringBuilder();
97 
98  foreach ( var item in Items )
99  {
100  result.Append( item ).Append( "\n" );
101  }
102 
103  return result.RemoveLast( 2 ).ToString();
104  }
105  set
106  {
107  if ( Width == 0 )
108  throw new InvalidOperationException( "You must set the list width before assigning text!" );
109 
110  SpriteFont xnaFont = this.Font.XnaFont;
111  Vector2 textDims = xnaFont.MeasureString( value );
112  double softWidth = 4 * Font.CharacterWidth < Width ? Width - 4 * Font.CharacterWidth : Width;
113  string wrapped = Font.WrapText( value, softWidth, Width );
114 
115  StringList newList = new StringList( wrapped.Split( '\n' ) );
116  this.Bind( newList );
117  }
118  }
119 
126  : base( list )
127  {
128  }
129 
134  : base( new StringList() )
135  {
136  SizingByLayout = false;
137  }
138 
139  internal protected override Label CreateWidget( string item )
140  {
141  return new Label( item )
142  {
143  HorizontalSizing = Sizing.Expanding,
144  Font = Font,
145  Color = Color.Transparent,
146  SizeMode = TextSizeMode.None,
147  TextColor = TextColor,
148  HorizontalAlignment = ItemAligment,
149  };
150  }
151  }
152 }
HorizontalAlignment
Asemointi vaakasuunnassa.
Definition: View.cs:177
Listakomponentti. Voidaan liittää listaan, joka toteuttaa INotifyList-rajapinnan. Tällöin listaan teh...
Definition: ListWidget.cs:155
static readonly Color Black
Musta.
Definition: Color.cs:494
string WrapText(string text, double softLineWidth, double hardLineWidth)
Rivittää tekstin.
Definition: Font.cs:162
Fontti.
Definition: Font.cs:22
double CharacterWidth
Merkin leveys.
Definition: Font.cs:68
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:38
Tekstikenttä.
Definition: Label.cs:65
StringListWidget(StringList list)
Luo uuden (tyhjän) merkkijonolistakomponentin, joka on sidottu olemassaolevaan listaan.
override Label CreateWidget(string item)
static readonly Font Default
OletusFontti.
Definition: Font.cs:27
Väri.
Definition: Color.cs:13
Järjestetty lista merkkijonoja.
Definition: StringList.cs:10
StringListWidget()
Luo uuden (tyhjän) merkkijonolistakomponentin.
Käyttöliittymäkomponentti, joka näyttää listan merkkijonoja.