source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/Widgets/Slider.cs @ 4507

Revision 4507, 5.9 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
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
27using System;
28using System.Collections.Generic;
29using System.Linq;
30using System.Text;
31using Jypeli.WP7;
32
33namespace Jypeli.Widgets
34{
35    /// <summary>
36    /// Liukusäädin
37    /// </summary>
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
46        /// <summary>
47        /// Liukuva nuppi.
48        /// </summary>
49        public Widget Knob { get; private set; }
50
51        /// <summary>
52        /// Nupin väri kun hiiri on nupin päällä.
53        /// </summary>
54        public Color ActiveColor
55        {
56            get { return _activeColor; }
57            set { _activeColor = value; }
58        }
59
60        /// <summary>
61        /// Nupin väri kun hiiri ei ole nupin päällä.
62        /// </summary>
63        public Color InactiveColor
64        {
65            get { return _inactiveColor; }
66            set { _inactiveColor = value; }
67        }
68
69        /// <summary>
70        /// Ura, jossa liukusäädin liukuu.
71        /// </summary>
72        public Widget Track { get; private set; }
73
74        /// <summary>
75        /// Luo uuden liukusäätimen.
76        /// </summary>
77        /// <param name="width">Säätimen leveys.</param>
78        /// <param name="height">Säätimen korkeus.</param>
79        public Slider( double width, double height )
80            : base( width, height )
81        {
82            Color = Color.Transparent;
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
94        private void InitializeControls()
95        {
96            Game.Controls.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Pressed, MousePress, null ).InContext( this );
97            Game.Controls.Mouse.Listen( MouseButton.Left, ButtonState.Released, MouseRelease, null ).InContext( this );
98            Game.Controls.Mouse.ListenMovement( 1.0, MouseMove, null ).InContext( this );
99
100            Game.Controls.TouchPanel.ListenOn( this, ButtonState.Pressed, TouchPress, null ).InContext( this );
101            Game.Controls.TouchPanel.Listen( ButtonState.Released, TouchRelease, null ).InContext( this );
102            Game.Controls.TouchPanel.Listen( ButtonState.Down, TouchMove, null ).InContext( this );
103        }
104
105        public override void BindTo( Meter meter )
106        {
107            pressedDown = false;
108            base.BindTo( meter );
109        }
110
111        public override void Unbind()
112        {
113            pressedDown = false;
114            base.Unbind();
115        }
116
117        protected override void UpdateValue()
118        {
119            if ( Knob != null && Track != null && Meter != null )
120                Knob.X = Track.Left + Track.Width * Meter.RelativeValue;
121        }
122
123        private void GenMove( Vector newPos )
124        {
125            Vector u = Vector.FromLengthAndAngle( 1, this.Angle );
126            double newVal = newPos.ScalarProjection( u );
127
128            if ( newVal < Track.Left ) Knob.X = Track.Left;
129            else if ( newVal > Track.Right ) Knob.X = Track.Right;
130            else Knob.X = newVal;
131
132            Meter.RelativeValue = ( newVal - Track.Left ) / Track.Width;
133        }
134
135        private void MousePress()
136        {
137            if ( pressedDown ) return;
138            UnsetChangedEvent();
139            pressedDown = true;
140        }
141
142        private void MouseMove( AnalogState state )
143        {
144            Knob.Color = pressedDown || Game.Mouse.IsCursorOn( this ) ? _activeColor : _inactiveColor;
145
146            if ( pressedDown )
147                GenMove( Game.Mouse.PositionOnScreen - this.Position );
148        }
149
150        private void MouseRelease()
151        {
152            if ( !pressedDown ) return;
153            pressedDown = false;
154            SetChangedEvent();
155        }
156
157        private void TouchPress( Touch touch )
158        {
159            if ( touchObject != null ) return;
160            UnsetChangedEvent();
161            touchObject = touch;
162            Knob.Color = _activeColor;
163        }
164
165        private void TouchMove( Touch touch )
166        {
167            if ( touchObject == touch )
168                GenMove( touch.PositionOnScreen - this.Position );
169        }
170
171        private void TouchRelease( Touch touch )
172        {
173            if ( touchObject == null ) return;
174            touchObject = null;
175            SetChangedEvent();
176            Knob.Color = _inactiveColor;
177        }
178    }
179}
Note: See TracBrowser for help on using the repository browser.