Jypeli  9
The simple game programming library
Label.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 Jypeli.Widgets;
32 using Microsoft.Xna.Framework;
33 using Microsoft.Xna.Framework.Graphics;
34 
35 namespace Jypeli
36 {
37  public enum TextSizeMode
38  {
43  None,
44 
48  AutoSize,
49 
53  StretchText,
54 
58  Wrapped,
59  }
60 
61 
65  public class Label : BindableWidget
66  {
68  string title = "";
69  string originalText = "";
70  string visibleText = "";
72  int decimalPlaces = 2;
73  string doubleFormatString = "{0:N2}";
74  string intFormatString = "{0:D1}";
75  double _xMargin;
76  private double _yMargin;
77  bool useDefaultHeight = false;
78  bool initialized = false;
79 
80  const double DefaultWidth = 100;
81 
82  static double GetDefaultHeight()
83  {
84  Vector2 fontDims = Font.Default.XnaFont.MeasureString( "A" );
85  return fontDims.Y;
86  }
87 
91  public virtual string Text
92  {
93  get { return originalText; }
94  set
95  {
96  if ( value == null )
97  throw new ArgumentException( "Text must not be null" );
98  originalText = value;
99  updateSize();
100  }
101  }
102 
106  public bool IsTruncated
107  {
108  get { return visibleText.Length < ( originalText.Length ); }
109  }
110 
111  Vector3 _textScale = new Vector3(1, 1, 1);
112 
117  {
118  get { return new Vector(_textScale.X, _textScale.Y); }
119  set
120  {
121  _textScale = new Vector3((float)value.X, (float)value.Y, 1.0f);
122  updateSize();
123  }
124  }
125 
126 
131  public int DecimalPlaces
132  {
133  get { return decimalPlaces; }
134  set
135  {
136  decimalPlaces = value;
137  DoubleFormatString = "{0:N" + decimalPlaces + "}";
138  }
139  }
140 
141 
145  public string DoubleFormatString
146  {
147  get { return doubleFormatString; }
148  set
149  {
150  doubleFormatString = value;
151  UpdateValue();
152  }
153 
154  }
155 
156 
165  public string IntFormatString
166  {
167  get { return intFormatString; }
168  set
169  {
170  intFormatString = value;
171  UpdateValue();
172  }
173 
174  }
175 
180  public string Title
181  {
182  set
183  {
184  IntFormatString = value + ": {0}";
185  DoubleFormatString = value + ": {0}";
186  }
187  }
188 
193  {
194  get { return sizeMode; }
195  set { sizeMode = value; updateSize(); }
196  }
197 
202  public Vector TextSize { get; private set; }
203 
207  public Color TextColor { get; set; }
208 
212  public virtual Font Font
213  {
214  get { return font; }
215  set { font = value; updateSize(); }
216  }
217 
224 
231 
235  public double XMargin
236  {
237  get { return _xMargin; }
238  set { _xMargin = value; updateSize(); }
239  }
240 
244  public double YMargin
245  {
246  get { return _yMargin; }
247  set { _yMargin = value; updateSize(); }
248  }
249 
255  public override Vector Size
256  {
257  get
258  {
259  return base.Size;
260  }
261  set
262  {
263  base.Size = value;
264  this.updateSize();
265  }
266  }
267 
272  public Label()
273  : this( DefaultWidth, 10 )
274  {
275  sizeMode = TextSizeMode.AutoSize;
276 
277  if ( Game.Instance != null )
278  {
279  this.Height = GetDefaultHeight();
280  updateSize();
281  }
282  else
284  }
285 
290  public Label( string text )
291  : this( DefaultWidth, 10, text )
292  {
293  sizeMode = TextSizeMode.AutoSize;
294 
295  if ( Game.Instance != null )
296  {
297  this.Height = GetDefaultHeight();
298  updateSize();
299  }
300  else
302  }
303 
307  public Label( Animation animation )
308  : base( animation )
309  {
311  }
312 
318  public Label( double width, double height )
319  : this( width, height, "" )
320  {
321  }
322 
329  public Label( double width, double height, string text )
330  : base( width, height, Shape.Rectangle )
331  {
332  this.sizeMode = TextSizeMode.None;
333  this.originalText = text;
335  }
336 
337  private void setDefaultHeight()
338  {
339  if ( !useDefaultHeight ) return;
340  this.Height = GetDefaultHeight();
341  }
342 
343  private void Initialize()
344  {
345  font = Font.Default;
350  updateSize();
351  initialized = true;
352  UpdateValue();
353  }
354 
355  protected override void UpdateValue()
356  {
357  if ( !initialized || !Bound ) return;
358 
359  if ( Meter is IntMeter )
360  {
361  int newNumber = ( (IntMeter)Meter ).Value;
362  Text = string.Format( intFormatString, newNumber );
363  }
364  else if ( Meter is DoubleMeter )
365  {
366  double newNumber = ( (DoubleMeter)Meter ).Value;
367  Text = string.Format( doubleFormatString, newNumber );
368  }
369  }
370 
374  private void updateSize()
375  {
376  SpriteFont xnaFont = this.Font.XnaFont;
377  visibleText = (title.Length > 0) ? (title + ": " + originalText) : originalText;
378 
379  if ( visibleText.Length == 0 )
380  {
381  this.TextSize = Vector.Zero;
382  NotifyParentAboutChangedSizingAttributes();
383  return;
384  }
385 
386  Vector2 rawTextDims = xnaFont.MeasureString( visibleText );
387  Vector clientArea = new Vector( this.Width - 2 * XMargin, this.Height - 2 * YMargin );
388  Vector fullTextDims = new Vector( _textScale.X * rawTextDims.X, _textScale.Y * rawTextDims.Y );
389  TextSize = new Vector( fullTextDims.X, fullTextDims.Y );
390 
391  switch ( SizeMode )
392  {
393  case TextSizeMode.None:
394  TruncateText();
395  break;
396 
397  case TextSizeMode.StretchText:
398  _textScale = new Vector( clientArea.X / rawTextDims.X, clientArea.Y / rawTextDims.Y );
399  TextSize = clientArea;
400  break;
401 
402  case TextSizeMode.AutoSize:
403  base.Size = PreferredSize = new Vector( fullTextDims.X + 2 * XMargin, fullTextDims.Y + 2 * YMargin );
404  break;
405 
406  case TextSizeMode.Wrapped:
407  WrapText();
408  break;
409  }
410 
411  NotifyParentAboutChangedSizingAttributes();
412  }
413 
414  private void TruncateText()
415  {
416  double textWidth = this.Width - 2 * XMargin;
417  if ( textWidth <= 0 )
418  {
419  textWidth = this.Width;
420  }
421 
422  visibleText = font.TruncateText( Text, textWidth );
423 
424  Vector2 textDims = Font.XnaFont.MeasureString( visibleText );
425  TextSize = new Vector( textDims.X, textDims.Y );
426  }
427 
428  private void WrapText()
429  {
430  SpriteFont xnaFont = this.Font.XnaFont;
431  Vector2 rawTextDims = xnaFont.MeasureString( visibleText );
432  Vector2 fullTextDims = new Vector2( _textScale.X * rawTextDims.X, _textScale.Y * rawTextDims.Y );
433 
434  if ( Width <= 0 || fullTextDims.X <= Width )
435  return;
436 
437  double hardBreak = base.Size.X - 2 * XMargin;
438  double softBreak = Math.Max( hardBreak / 2, hardBreak - 5 * Font.CharacterWidth );
439 
440  visibleText = Font.WrapText( visibleText, softBreak, hardBreak );
441  Vector2 textDims = xnaFont.MeasureString( visibleText );
442  base.Size = PreferredSize = new Vector( base.Size.X, textDims.Y + 2 * YMargin );
443  TextSize = new Vector( textDims.X, textDims.Y );
444  }
445 
446  private double GetHorizontalAlignment()
447  {
448  switch ( HorizontalAlignment )
449  {
450  case HorizontalAlignment.Center:
451  return 0;
452  case HorizontalAlignment.Left:
453  return ( -Width + TextSize.X ) / 2 + XMargin;
454  case HorizontalAlignment.Right:
455  return ( Width - TextSize.X ) / 2 - XMargin;
456  default:
457  return XMargin;
458  }
459  }
460 
461  private double GetVerticalAlignment()
462  {
463  switch ( VerticalAlignment )
464  {
465  case VerticalAlignment.Center:
466  return 0;
467  case VerticalAlignment.Top:
468  return ( Size.Y - TextSize.Y ) / 2 - YMargin;
469  case VerticalAlignment.Bottom:
470  return ( -Size.Y + TextSize.Y ) / 2 + YMargin;
471  default:
472  return YMargin;
473  }
474  }
475 
476  public override void Draw( Matrix parentTransformation, Matrix transformation )
477  {
478  Draw( parentTransformation, transformation, visibleText );
479  }
480 
481  protected void Draw( Matrix parentTransformation, Matrix transformation, string text )
482  {
483  Matrix m = Matrix.CreateScale( _textScale )
484  * Matrix.CreateTranslation( (float)GetHorizontalAlignment(), (float)GetVerticalAlignment(), 0 )
485  * Matrix.CreateRotationZ( (float)Angle.Radians )
486  * Matrix.CreateTranslation( (float)Position.X, (float)Position.Y, 0 )
487  * parentTransformation;
488 
489  Renderer.DrawText( text, ref m, Font, TextColor );
490  base.Draw( parentTransformation, transformation );
491  }
492  }
493 }
Jypeli.Meter
Mittari, joka mittaa erityyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge.
Definition: Meter.cs:61
Jypeli.Label.IntFormatString
string IntFormatString
Millä tavalla int numerot muotoillaan.
Definition: Label.cs:166
Jypeli.Label.TruncateText
void TruncateText()
Definition: Label.cs:414
Jypeli.Font.CharacterWidth
double CharacterWidth
Merkin leveys.
Definition: Font.cs:98
Jypeli.Label.TextSize
Vector TextSize
Näytettävän tekstin koko. Ei välttämättä sama kuin Size.
Definition: Label.cs:202
Jypeli.Label.Draw
override void Draw(Matrix parentTransformation, Matrix transformation)
Definition: Label.cs:476
Jypeli.Renderer
Luokka, joka sisältää metodeita kuvioiden ja tekstuurien piirtämiseen 2D-tasossa.
Definition: Renderer.cs:48
Jypeli.DoubleMeter
Mittari, joka mittaa double-tyyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGa...
Definition: DoubleMeter.cs:11
Jypeli.Label.sizeMode
TextSizeMode sizeMode
Definition: Label.cs:71
Jypeli.Label.visibleText
string visibleText
Definition: Label.cs:70
Jypeli.Color.Black
static readonly Color Black
Musta.
Definition: Color.cs:503
Jypeli.Matrix
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.Label.useDefaultHeight
bool useDefaultHeight
Definition: Label.cs:77
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.Vector.Zero
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:63
Jypeli.Label.Label
Label()
Luo uuden tekstikentän. Asettaa koon tekstin mukaan.
Definition: Label.cs:272
Jypeli.Label.XMargin
double XMargin
Marginaali vasemmasta/oikeasta reunasta.
Definition: Label.cs:236
Jypeli
Definition: Automobile.cs:5
Jypeli.TextSizeMode
TextSizeMode
Definition: Label.cs:38
Jypeli.Font.Default
static readonly Font Default
Oletusfontti.
Definition: Font.cs:30
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Label.DoubleFormatString
string DoubleFormatString
Millä tavalla desimaalinumerot muotoillaan
Definition: Label.cs:146
Jypeli.Label
Tekstikenttä.
Definition: Label.cs:66
Jypeli.Label.Initialize
void Initialize()
Definition: Label.cs:343
Jypeli.Label.Label
Label(double width, double height, string text)
Luo uuden tekstikentän.
Definition: Label.cs:329
Jypeli.Label.GetHorizontalAlignment
double GetHorizontalAlignment()
Definition: Label.cs:446
Jypeli.Rectangle
Suorakulmio.
Definition: Shapes.cs:318
Jypeli.Font.XnaFont
SpriteFont XnaFont
Definition: Font.cs:90
Jypeli.Shape
Kuvio.
Definition: Shapes.cs:47
Jypeli.Label.TextScale
Vector TextScale
Tekstin skaalaus. Oletus (1,1) ; isompi suurempi.
Definition: Label.cs:117
Jypeli.Widgets.BindableWidget
Widget, joka voidaan asettaa näyttämään halutun mittarin arvoa.
Definition: BindableWidget.cs:9
Jypeli.Angle.Radians
double Radians
Palauttaa tai asettaa kulman radiaaneina.
Definition: Angle.cs:85
Jypeli.Game.AssertInitialized
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: DelayedActions.cs:53
Jypeli.Label.DefaultWidth
const double DefaultWidth
Definition: Label.cs:80
Jypeli.Label.UpdateValue
override void UpdateValue()
Kutsutaan automaattisesti, kun mittarin arvo on muuttunut. Ylikirjoita tämä koodilla,...
Definition: Label.cs:355
Jypeli.Label.SizeMode
TextSizeMode SizeMode
Kuinka tekstikentän koko määräytyy.
Definition: Label.cs:193
Jypeli.Label.GetVerticalAlignment
double GetVerticalAlignment()
Definition: Label.cs:461
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Label.Title
string Title
Voidaan käyttää tekstin helpompaan asettamiseen. Asettaa IntFormatStringin ja DoubleFormatStringin.
Definition: Label.cs:181
Jypeli.Color.Transparent
static readonly Color Transparent
Läpinäkyvä väri.
Definition: Color.cs:878
Jypeli.HorizontalAlignment
HorizontalAlignment
Asemointi vaakasuunnassa.
Definition: View.cs:441
Jypeli.Label.setDefaultHeight
void setDefaultHeight()
Definition: Label.cs:337
Jypeli.Label.decimalPlaces
int decimalPlaces
Definition: Label.cs:72
Jypeli.Label.Label
Label(string text)
Luo uuden tekstikentän annetulla tekstillä. Asettaa koon tekstin mukaan.
Definition: Label.cs:290
Jypeli.Label.Label
Label(Animation animation)
Luo uuden tekstikentän animaatiolla.
Definition: Label.cs:307
Jypeli.Font.TruncateText
string TruncateText(string str, double maxLineWidth)
Katkaisee merkkijonon loppupäästä niin että se sopii annettuun pikselileveyteen fontilla kirjoitettun...
Definition: Font.cs:263
Jypeli.Label._yMargin
double _yMargin
Definition: Label.cs:76
Jypeli.Animation
Sarja kuvia, jotka vaihtuvat halutulla nopeudella. Yksi animaatio koostuu yhdestä tai useammasta kuva...
Definition: Animation.cs:62
Jypeli.Label.initialized
bool initialized
Definition: Label.cs:78
Jypeli.Game.InstanceInitialized
static Action InstanceInitialized
Tapahtuu kun Game.Instance on alustettu.
Definition: DelayedActions.cs:42
Jypeli.Label.YMargin
double YMargin
Marginaali ylä-/alareunasta.
Definition: Label.cs:245
Jypeli.Label.title
string title
Definition: Label.cs:68
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.Label.Label
Label(double width, double height)
Luo uuden tekstikentän.
Definition: Label.cs:318
Jypeli.Label.IsTruncated
bool IsTruncated
Onko tekstiä katkaistu
Definition: Label.cs:107
Jypeli.Label.font
Font font
Definition: Label.cs:67
Jypeli.Label.WrapText
void WrapText()
Definition: Label.cs:428
Jypeli.Font.WrapText
string WrapText(string text, double softLineWidth, double hardLineWidth)
Rivittää tekstin.
Definition: Font.cs:302
Jypeli.Label.GetDefaultHeight
static double GetDefaultHeight()
Definition: Label.cs:82
Jypeli.Widgets
Definition: Background.cs:34
Jypeli.Label.TextColor
Color TextColor
Tekstin väri.
Definition: Label.cs:207
Jypeli.Label.DecimalPlaces
int DecimalPlaces
Kuinka monta desimaalia näytetään, kun tekstikenttä on sidottu näyttämään desimaalilukua.
Definition: Label.cs:132
Jypeli.Label._xMargin
double _xMargin
Definition: Label.cs:75
Jypeli.Label.Size
override Vector Size
Tekstikentän koko. Jos SizeMode on SizeMode.StretchText, teksti venytetään kentän koon mukaiseksi.
Definition: Label.cs:256
Jypeli.Label.updateSize
void updateSize()
Päivittää tekstikentän/tekstin koon ja rivittää tekstin.
Definition: Label.cs:374
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.IntMeter
Mittari, joka mittaa int-tyyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge...
Definition: IntMeter.cs:11
Jypeli.Label.intFormatString
string intFormatString
Definition: Label.cs:74
Jypeli.Label._textScale
Vector3 _textScale
Definition: Label.cs:111
Jypeli.Font
Fontti.
Definition: Font.cs:23
Jypeli.Label.doubleFormatString
string doubleFormatString
Definition: Label.cs:73
Jypeli.TextSizeMode.None
@ None
Tekstikentän koko on käyttäjän asettama. Ylipitkä teksti katkaistaan.
Jypeli.Widgets.BindableWidget.Bound
bool Bound
Onko komponentti sidottu mittariin.
Definition: BindableWidget.cs:21
Jypeli.Label.Text
virtual string Text
Teksti.
Definition: Label.cs:92
Jypeli.VerticalAlignment
VerticalAlignment
Asemointi pystysuunnassa.
Definition: View.cs:462
Jypeli.Label.Draw
void Draw(Matrix parentTransformation, Matrix transformation, string text)
Definition: Label.cs:481
Jypeli.Game
Definition: Content.cs:46
Jypeli.Label.originalText
string originalText
Definition: Label.cs:69
Jypeli.Vector.Y
double Y
Definition: Vector.cs:313
Jypeli.Angle
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
Jypeli.Renderer.DrawText
static void DrawText(string text, Vector position, Font font, Color color)
Piirtää tekstiä ruudulle
Definition: Renderer.cs:272