Jypeli  5
The simple game programming library
Slider.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2009 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 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using Jypeli.WP7;
32 
33 namespace Jypeli.Widgets
34 {
38  public class Slider : BindableWidget
39  {
40  private bool pressedDown = false;
41  private Touch touchObject = null;
42 
43  private Color _activeColor = Color.Red;
44  private Color _inactiveColor = Color.DarkGray;
45 
49  public Widget Knob { get; private set; }
50 
54  public Color ActiveColor
55  {
56  get { return _activeColor; }
57  set { _activeColor = value; }
58  }
59 
63  public Color InactiveColor
64  {
65  get { return _inactiveColor; }
66  set { _inactiveColor = value; }
67  }
68 
72  public Widget Track { get; private set; }
73 
79  public Slider( double width, double height )
80  : base( width, height )
81  {
83 
84  Track = new Widget( width, height / 3 );
85  Add( Track );
86 
87  Knob = new Widget( height, height, Shape.Circle );
88  Knob.Color = Color.DarkGray;
89  Add( Knob );
90 
91  Game.AssertInitialized( InitializeControls );
92  }
93 
101  public Slider(double width, double height, Meter meter)
102  : this(width, height)
103  {
104  BindTo(meter);
105  }
106 
107  private void InitializeControls()
108  {
109  Game.Controls.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Pressed, MousePress, null ).InContext( this );
110  Game.Controls.Mouse.Listen( MouseButton.Left, ButtonState.Released, MouseRelease, null ).InContext( this );
111  Game.Controls.Mouse.ListenMovement( 1.0, MouseMove, null ).InContext( this );
112 
113  Game.Controls.TouchPanel.ListenOn( this, ButtonState.Pressed, TouchPress, null ).InContext( this );
114  Game.Controls.TouchPanel.Listen( ButtonState.Released, TouchRelease, null ).InContext( this );
115  Game.Controls.TouchPanel.Listen( ButtonState.Down, TouchMove, null ).InContext( this );
116  }
117 
118  public override void BindTo( Meter meter )
119  {
120  pressedDown = false;
121  base.BindTo( meter );
122  }
123 
124  public override void Unbind()
125  {
126  pressedDown = false;
127  base.Unbind();
128  }
129 
130  protected override void UpdateValue()
131  {
132  if ( Knob != null && Track != null && Meter != null )
133  Knob.X = Track.Left + Track.Width * Meter.RelativeValue;
134  }
135 
136  private void GenMove( Vector newPos )
137  {
138  Vector u = Vector.FromLengthAndAngle( 1, this.Angle );
139  double newVal = newPos.ScalarProjection( u );
140 
141  if ( newVal < Track.Left ) Knob.X = Track.Left;
142  else if ( newVal > Track.Right ) Knob.X = Track.Right;
143  else Knob.X = newVal;
144 
145  Meter.RelativeValue = ( newVal - Track.Left ) / Track.Width;
146  }
147 
148  private void MousePress()
149  {
150  if ( pressedDown ) return;
151  UnsetChangedEvent();
152  pressedDown = true;
153  }
154 
155  private void MouseMove( AnalogState state )
156  {
157  Knob.Color = pressedDown || Game.Mouse.IsCursorOn( this ) ? _activeColor : _inactiveColor;
158 
159  if ( pressedDown )
160  GenMove( Game.Mouse.PositionOnScreen - this.Position );
161  }
162 
163  private void MouseRelease()
164  {
165  if ( !pressedDown ) return;
166  pressedDown = false;
167  SetChangedEvent();
168  }
169 
170  private void TouchPress( Touch touch )
171  {
172  if ( touchObject != null ) return;
173  UnsetChangedEvent();
174  touchObject = touch;
175  Knob.Color = _activeColor;
176  }
177 
178  private void TouchMove( Touch touch )
179  {
180  if ( touchObject == touch )
181  GenMove( touch.PositionOnScreen - this.Position );
182  }
183 
184  private void TouchRelease( Touch touch )
185  {
186  if ( touchObject == null ) return;
187  touchObject = null;
188  SetChangedEvent();
189  Knob.Color = _inactiveColor;
190  }
191  }
192 }
Vector PositionOnScreen
Kosketuksen paikka ruudulla.
Definition: Touch.cs:31
Kuvio.
Definition: Shapes.cs:48
Liukusäädin
Definition: Slider.cs:38
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
Slider(double width, double height, Meter meter)
Luo uuden liukusäätimen. Sitoo liukusäätimen arvon mittarin arvoon.
Definition: Slider.cs:101
override void Unbind()
Lopettaa mittarin arvon seuraamisen.
Definition: Slider.cs:124
Widget, joka voidaan asettaa näyttämään halutun mittarin arvoa.
override void UpdateValue()
Kutsutaan automaattisesti, kun mittarin arvo on muuttunut. Ylikirjoita tämä koodilla, joka muuttaa widgetin ulkonäköä asianmukaisesti.
Definition: Slider.cs:130
Kosketuspaneelin kosketus.
Definition: Touch.cs:16
static Jypeli.Controls.Controls Controls
Pelin kontrollit.
Definition: Game.cs:207
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
MouseButton
Hiiren napit.
Definition: MouseButton.cs:11
Käyttöliittymän komponentti.
Definition: Appearance.cs:9
Slider(double width, double height)
Luo uuden liukusäätimen.
Definition: Slider.cs:79
double ScalarProjection(Vector unitVector)
Definition: Vector.cs:169
override void BindTo(Meter meter)
Asettaa kontrollin seuraamaan mittarin arvoa.
Definition: Slider.cs:118
static readonly Color Transparent
Läpinäkyvä väri.
Definition: Color.cs:869
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
Parametrit analogisen ohjauksen (hiiren tai ohjaustikun) tapahtumalle.
Definition: AnalogState.cs:35
static readonly Ellipse Circle
Ympyrä tai ellipsi.
Definition: Shapes.cs:62
Väri.
Definition: Color.cs:13
static readonly Color DarkGray
Tumma harmaa.
Definition: Color.cs:569
Mouse Mouse
Hiiri.
Definition: Game.cs:253
2D-vektori.
Definition: Vector.cs:56
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: Game.cs:630
Mittari, joka mittaa erityyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge...
Definition: Meter.cs:60