Jypeli  9
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 Microsoft.Xna.Framework;
31 
32 namespace Jypeli.Widgets
33 {
37  public class BarGauge : Widget
38  {
42  public enum BarDirection
43  {
47  BarVerticalUp,
51  BarHorizontalRight,
55  BarVerticalDown,
59  BarHorizontalLeft
60  };
61 
62 
63  private double h = -1;
64  private double w = -1;
65  private BarDirection direction = BarDirection.BarVerticalUp;
66 
72  {
73  get { return direction; }
74  set
75  {
76  direction = value;
77  if (h == -1 && w == -1) { h = Height; w = Width; };
78  switch (value)
79  {
80  case BarDirection.BarVerticalUp:
81  Angle = Angle.FromDegrees(0);
82  Height = h; Width = w;
83  return;
84  case BarDirection.BarVerticalDown:
85  Angle = Angle.FromDegrees(180);
86  Height = h; Width = w;
87  return;
88  case BarDirection.BarHorizontalLeft:
89  Angle = Angle.FromDegrees(-90);
90  Height = w; Width = h;
91  return;
92  case BarDirection.BarHorizontalRight:
93  Angle = Angle.FromDegrees(90);
94  Height = w; Width = h;
95  return;
96  }
97 
98  }
99  }
100 
101  private static readonly Vector[] barVertices = new Vector[]
102  {
103  new Vector(-0.5, 0),
104  new Vector(0.5, 0),
105  new Vector(0.5, 1.0),
106  new Vector(-0.5, 1.0)
107  };
108 
109  private static readonly IndexTriangle[] barIndices = new IndexTriangle[]
110  {
111  new IndexTriangle(0, 3, 2),
112  new IndexTriangle(0, 2, 1)
113  };
114 
115  private static readonly ShapeCache shapeCache = new ShapeCache(barVertices, barIndices);
116 
117  private static readonly DoubleMeter defaultMeter = new DoubleMeter( 0, 0, 100 );
118 
120 
124  public Color BarColor { get; set; }
125 
129  public BarGauge(double width, double height)
130  : base(width, height)
131  {
133  BarColor = Color.Red;
134  }
135 
140  public void BindTo(Meter meter)
141  {
142  boundMeter = meter;
143  }
144 
145  public override void Draw(Matrix parentTransformation, Matrix transformation)
146  {
147  double barLength = Size.Y * boundMeter.RelativeValue;
148  Matrix m =
149  Matrix.CreateScale((float)Size.X, (float)barLength, 1f)
150  * Matrix.CreateTranslation(0, (float)(-Height / 2), 0)
151  * Matrix.CreateRotationZ((float)(Angle).Radians)
152  * Matrix.CreateTranslation((float)Position.X, (float)Position.Y, 0.0f)
153  * parentTransformation;
154 
156 
157  Vector[] borderVertices = new Vector[]
158  {
159  new Vector(-0.5, 0.5),
160  new Vector(-0.5, -0.5),
161  new Vector(0.5, -0.5),
162  new Vector(0.5, 0.5),
163  };
164 
165  // The border that is drawn by base class gets obscured by the bar.
166  // Let's draw it again.
167  Renderer.DrawPolygon(borderVertices, ref transformation, BorderColor);
168 
169  base.Draw(parentTransformation, transformation);
170 
171  }
172  }
173 }
Jypeli.Meter
Mittari, joka mittaa erityyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge.
Definition: Meter.cs:61
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.Widgets.BarGauge.h
double h
Definition: BarGauge.cs:63
Jypeli.Matrix
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.Widgets.BarGauge.defaultMeter
static readonly DoubleMeter defaultMeter
Definition: BarGauge.cs:117
Jypeli.Color.Red
static readonly Color Red
Punainen.
Definition: Color.cs:813
Jypeli.Renderer.DrawFilledShape
static void DrawFilledShape(ShapeCache cache, ref Matrix matrix, Color color)
Definition: Renderer.cs:365
Jypeli.ShapeCache
Sisältää valmiiksi lasketut kolmiot, joiden avulla piirtäminen on suoraviivaista.
Definition: Shapes.cs:583
Jypeli.Direction
Perussuunta tasossa.
Definition: Direction.cs:47
Jypeli.Widgets.BarGauge.Draw
override void Draw(Matrix parentTransformation, Matrix transformation)
Definition: BarGauge.cs:145
Jypeli.Widgets.BarGauge.BarColor
Color BarColor
Palkin väri.
Definition: BarGauge.cs:124
Jypeli.Widgets.BarGauge.direction
BarDirection direction
Definition: BarGauge.cs:65
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Widgets.BarGauge.barVertices
static readonly Vector[] barVertices
Definition: BarGauge.cs:101
Jypeli.Widgets.BarGauge.shapeCache
static readonly ShapeCache shapeCache
Definition: BarGauge.cs:115
Jypeli.Widgets.BarGauge
Palkki, jonka korkeutta voi säätää.
Definition: BarGauge.cs:38
Jypeli.Widgets.BarGauge.BarGauge
BarGauge(double width, double height)
Palkin rakentaja.
Definition: BarGauge.cs:129
Jypeli.Widget
Käyttöliittymän komponentti.
Definition: Appearance.cs:6
Jypeli.Color.Transparent
static readonly Color Transparent
Läpinäkyvä väri.
Definition: Color.cs:878
Jypeli.Widgets.BarGauge.w
double w
Definition: BarGauge.cs:64
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.Widgets.BarGauge.boundMeter
Meter boundMeter
Definition: BarGauge.cs:119
Jypeli.IndexTriangle
Muotojen määrityksessä käytettävä kolmio.
Definition: Shapes.cs:553
Jypeli.Widgets
Definition: Background.cs:34
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
Jypeli.Angle.FromDegrees
static Angle FromDegrees(double degree)
Luo kulman annettujen asteiden mukaan.
Definition: Angle.cs:324
Jypeli.Widgets.BarGauge.barIndices
static readonly IndexTriangle[] barIndices
Definition: BarGauge.cs:109
Jypeli.Widget.BorderColor
Color BorderColor
Reunojen väri.
Definition: Appearance.cs:10
Jypeli.Widgets.BarGauge.BindTo
void BindTo(Meter meter)
Asettaa palkin näyttämään meter-olion arvoa. Palkin maksimiarvoksi tulee olion meter maksimiarvo.
Definition: BarGauge.cs:140
Jypeli.Widgets.BarGauge.BarDirection
BarDirection
Mihin suuntaa liut piirretään
Definition: BarGauge.cs:43
Jypeli.Renderer.DrawPolygon
static void DrawPolygon(Vector[] vertices, ref Matrix matrix, Color color)
Definition: Renderer.cs:410
Jypeli.Angle
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40