source: 2014/24/EemeliK/Zombieland/Jypeli/Widgets/StringListWidget.cs @ 5974

Revision 5974, 4.9 KB checked in by empaheik, 4 years ago (diff)
Line 
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
30using System;
31using Microsoft.Xna.Framework;
32using System.Text;
33using Microsoft.Xna.Framework.Graphics;
34using Jypeli.GameObjects;
35
36namespace Jypeli.Widgets
37{
38    /// <summary>
39    /// Käyttöliittymäkomponentti, joka näyttää listan merkkijonoja.
40    /// </summary>
41    public class StringListWidget : ListWidget<string, Label>
42    {
43        Font _font = Font.Default;
44        Color _textColor = Color.Black;
45        HorizontalAlignment _hAlignment = HorizontalAlignment.Left;
46
47        /// <summary>
48        /// Tekstifontti.
49        /// </summary>
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
61        /// <summary>
62        /// Tekstin väri.
63        /// </summary>
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
75        /// <summary>
76        /// Listan alkioiden sijoitus vaakasuunnassa.
77        /// </summary>
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
89        /// <summary>
90        /// Kaikki listan alkiot rivinvaihdoilla erotettuna.
91        /// </summary>
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
120        /// <summary>
121        /// Luo uuden (tyhjän) merkkijonolistakomponentin, joka on sidottu olemassaolevaan
122        /// listaan.
123        /// </summary>
124        /// <param name="list">Olemassaoleva lista.</param>
125        public StringListWidget( StringList list )
126            : base( list )
127        {
128        }
129
130        /// <summary>
131        /// Luo uuden (tyhjän) merkkijonolistakomponentin.
132        /// </summary>
133        public StringListWidget()
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}
Note: See TracBrowser for help on using the repository browser.