Jypeli  5
The simple game programming library
BarGauge.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: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28  */
29 
30 using System;
31 using System.ComponentModel;
32 using Microsoft.Xna.Framework;
33 
34 namespace Jypeli.Widgets
35 {
39  public class BarGauge : Widget
40  {
44  public enum BarDirection
45  {
49  BarVerticalUp,
53  BarHorizontalRight,
57  BarVerticalDown,
61  BarHorizontalLeft
62  };
63 
64 
65  private double h = -1;
66  private double w = -1;
67  private BarDirection direction = BarDirection.BarVerticalUp;
68 
73  public BarDirection Direction
74  {
75  get { return direction; }
76  set
77  {
78  direction = value;
79  if (h == -1 && w == -1) { h = Height; w = Width; };
80  switch (value)
81  {
82  case BarDirection.BarVerticalUp:
83  Angle = Angle.FromDegrees(0);
84  Height = h; Width = w;
85  return;
86  case BarDirection.BarVerticalDown:
87  Angle = Angle.FromDegrees(180);
88  Height = h; Width = w;
89  return;
90  case BarDirection.BarHorizontalLeft:
91  Angle = Angle.FromDegrees(-90);
92  Height = w; Width = h;
93  return;
94  case BarDirection.BarHorizontalRight:
95  Angle = Angle.FromDegrees(90);
96  Height = w; Width = h;
97  return;
98  }
99 
100  }
101  }
102 
103  private static readonly Vector[] barVertices = new Vector[]
104  {
105  new Vector(-0.5, 0),
106  new Vector(0.5, 0),
107  new Vector(0.5, 1.0),
108  new Vector(-0.5, 1.0)
109  };
110 
111  private static readonly IndexTriangle[] barIndices = new IndexTriangle[]
112  {
113  new IndexTriangle(0, 3, 2),
114  new IndexTriangle(0, 2, 1)
115  };
116 
117  private static readonly ShapeCache shapeCache = new ShapeCache(barVertices, barIndices);
118 
119  private static readonly DoubleMeter defaultMeter = new DoubleMeter( 0, 0, 100 );
120 
121  private Meter boundMeter = defaultMeter;
122 
126  public Color BarColor { get; set; }
127 
133  public BarGauge(double width, double height)
134  : base(width, height)
135  {
137  BarColor = Color.Red;
138  }
139 
146  public BarGauge(double width, double height, Meter meter)
147  : this(width, height)
148  {
149  BindTo(meter);
150  }
151 
156  public void BindTo(Meter meter)
157  {
158  boundMeter = meter;
159  }
160 
161  protected override void Draw(Matrix parentTransformation, Matrix transformation)
162  {
163  double barLength = Size.Y * boundMeter.RelativeValue;
164  Matrix m =
165  Matrix.CreateScale((float)Size.X, (float)barLength, 1f)
166  * Matrix.CreateTranslation(0, (float)(-Height / 2), 0)
167  * Matrix.CreateRotationZ((float)(Angle).Radians)
168  * Matrix.CreateTranslation((float)Position.X, (float)Position.Y, 0.0f)
169  * parentTransformation;
170 
171  Renderer.DrawFilledShape(shapeCache, ref m, BarColor);
172 
173  Vector[] borderVertices = new Vector[]
174  {
175  new Vector(-0.5, 0.5),
176  new Vector(-0.5, -0.5),
177  new Vector(0.5, -0.5),
178  new Vector(0.5, 0.5),
179  };
180 
181  // The border that is drawn by base class gets obscured by the bar.
182  // Let's draw it again.
183  Renderer.DrawPolygon(borderVertices, ref transformation, BorderColor);
184 
185  base.Draw(parentTransformation, transformation);
186 
187  }
188  }
189 }
static readonly Color Red
Punainen.
Definition: Color.cs:804
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
Palkki, jonka korkeutta voi säätää.
Definition: BarGauge.cs:39
Käyttöliittymän komponentti.
Definition: Appearance.cs:9
BarDirection
Mihin suuntaa liut piirretään
Definition: BarGauge.cs:44
Mittari, joka mittaa double-tyyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge...
Definition: Meter.cs:515
BarGauge(double width, double height, Meter meter)
Palkin rakentaja. Sitoo palkin arvon mittarin arvoon.
Definition: BarGauge.cs:146
Perussuunta tasossa.
Definition: Direction.cs:50
Sisältää valmiiksi lasketut kolmiot, joiden avulla piirtäminen on suoraviivaista. ...
Definition: Shapes.cs:577
static readonly Color Transparent
Läpinäkyvä väri.
Definition: Color.cs:869
override void Draw(Matrix parentTransformation, Matrix transformation)
Definition: BarGauge.cs:161
void BindTo(Meter meter)
Asettaa palkin näyttämään meter-olion arvoa. Palkin maksimiarvoksi tulee olion meter maksimiarvo...
Definition: BarGauge.cs:156
Väri.
Definition: Color.cs:13
Luokka, joka sisältää metodeita kuvioiden ja tekstuurien piirtämiseen 2D-tasossa. ...
Definition: Renderer.cs:51
static void DrawPolygon(Vector[] vertices, ref Matrix matrix, Color color)
Definition: Renderer.cs:389
2D-vektori.
Definition: Vector.cs:56
Muotojen määrityksessä käytettävä kolmio.
Definition: Shapes.cs:547
Mittari, joka mittaa erityyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge...
Definition: Meter.cs:60
BarGauge(double width, double height)
Palkin rakentaja.
Definition: BarGauge.cs:133
static Angle FromDegrees(double degree)
Luo kulman annettujen asteiden mukaan.
Definition: Angle.cs:325