Jypeli 10
The simple game programming library
PushButton.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
30using System;
31using System.Collections.Generic;
32using Microsoft.Xna.Framework;
33
34namespace Jypeli
35{
39 public class PushButton : Label
40 {
41 internal enum State
42 {
44 Hover,
45 LeftPressed,
46 RightPressed,
47 Selected
48 }
49
54
59
63
64 private State state = State.Released;
65
66 private bool isPressed { get { return state == State.LeftPressed || state == State.RightPressed; } }
67
72
76 public static Color defaultColor = new Color(29, 41, 81, 223);
77
81 public static Color defaultTextColor = new Color(250, 250, 250, 240);
82
87 {
88 get { return imageReleased; }
89 set
90 {
91 imageReleased = value;
92 if ( !isPressed && !Game.Mouse.IsCursorOn( this ) )
93 Image = value;
94 }
95 }
96
101 {
102 get { return imagePressed; }
103 set
104 {
105 imagePressed = value;
106 if ( isPressed )
107 Image = value;
108 }
109 }
110
115 {
116 get { return imageHover; }
117 set
118 {
119 imageHover = value;
120 if ( !isPressed && Game.Mouse.IsCursorOn( this ) )
121 Image = value;
122 }
123 }
124
128 public override Vector Size
129 {
130 get
131 {
132 return base.Size;
133 }
134 set
135 {
136 base.Size = value;
138 }
139 }
140
146 public override Color Color
147 {
148 get
149 {
150 return base.Color;
151 }
152 set
153 {
154 releasedColor = value;
155 hoverColor = Color.Lighter(value, 40);
156 selectedColor = Color.Lighter(value, 40);
157 pressedColor = Color.Darker(value, 40);
158 base.Color = value;
159 }
160 }
161
166 {
167 get
168 {
169 return hoverColor;
170 }
171 set
172 {
173 hoverColor = value;
174 }
175 }
176
181 {
182 get
183 {
184 return pressedColor;
185 }
186 set
187 {
188 pressedColor = value;
189 }
190 }
191
196 {
197 get
198 {
199 return selectedColor;
200 }
201 set
202 {
203 selectedColor = value;
204 }
205 }
206
210 public event Action Clicked;
211
215 public event Action RightClicked;
216
217 private void TouchHover( Touch touch )
218 {
219 double touchX = touch.PositionOnScreen.X;
220 double touchY = touch.PositionOnScreen.Y;
221
222 if ( touchX >= Left && touchX <= Right && touchY >= Bottom && touchY <= Top )
223 SetState( State.Hover );
224 else if ( Game.TouchPanel.NumTouches == 1 )
225 SetState( State.Released );
226 }
227
228 private void TouchRelease( Touch touch )
229 {
230 if ( Game.TouchPanel.NumTouches <= 1 )
231 SetState( State.Released );
232 }
233
234 private void TouchClick( Touch touch )
235 {
236 Click();
237 }
238
243 public PushButton( string text )
244 : base( text )
245 {
246 Initialize();
247 }
248
253 public PushButton( Image image )
254 : base( image )
255 {
256 Initialize();
257 this.Image = image;
258 }
259
265 public PushButton( double width, double height )
266 : base( width, height )
267 {
268 Initialize();
269 }
270
271 private void Initialize()
272 {
277 CapturesMouse = true;
279 }
280
281 private void InitializeMargins()
282 {
283 XMargin = 15;
284 YMargin = 10;
285 }
286
293 public PushButton( double width, double height, Image image )
294 : this( width, height )
295 {
296 this.Image = image;
297 }
298
305 public PushButton( double width, double height, string text )
306 : base( width, height, text )
307 {
308 Initialize();
309 }
310
311 private void InitializeControls()
312 {
313 var l1 = Game.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Pressed, SetState, null, State.LeftPressed ).InContext( this );
314 var l2 = Game.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Released, Release, null ).InContext( this );
315 var l3 = Game.Mouse.Listen( MouseButton.Left, ButtonState.Released, Release, null ).InContext( this );
316
317 var l4 = Game.Mouse.ListenOn( this, MouseButton.Right, ButtonState.Pressed, SetState, null, State.RightPressed ).InContext( this );
318 var l5 = Game.Mouse.ListenOn( this, MouseButton.Right, ButtonState.Released, Release, null ).InContext( this );
319 var l6 = Game.Mouse.Listen( MouseButton.Right, ButtonState.Released, Release, null ).InContext( this );
320
321 var l7 = Game.Mouse.ListenMovement( 1.0, CheckHover, null ).InContext( this );
322
323 var l8 = Game.Instance.TouchPanel.Listen( ButtonState.Down, TouchHover, null ).InContext( this );
324 var l9 = Game.Instance.TouchPanel.ListenOn( this, ButtonState.Released, TouchRelease, null ).InContext( this );
325 var l10 = Game.Instance.TouchPanel.Listen( ButtonState.Released, TouchRelease, null ).InContext( this );
326 var l11 = Game.Instance.TouchPanel.ListenOn( this, ButtonState.Released, TouchClick, null ).InContext( this );
327
328 associatedListeners.AddItems(l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11);
329 }
330
331 private void InitializeShape()
332 {
333 //double edgeSize = Math.Min( Width, Height ) / 5;
334 double edgeSize = 5;
335 double relativeHorizontalSize = edgeSize / Width;
336 double relativeVerticalSize = edgeSize / Height;
337
338 Vector topLeftOuter = new Vector( -0.5, 0.5 );
339 Vector topLeftInner = new Vector( -0.5 + relativeHorizontalSize, 0.5 - relativeVerticalSize );
340 Vector bottomLeftOuter = new Vector( -0.5, -0.5 );
341 Vector bottomLeftInner = new Vector( -0.5 + relativeHorizontalSize, -0.5 + relativeVerticalSize );
342 Vector topRightOuter = new Vector( 0.5, 0.5 );
343 Vector topRightInner = new Vector( 0.5 - relativeHorizontalSize, 0.5 - relativeVerticalSize );
344 Vector bottomRightOuter = new Vector( 0.5, -0.5 );
345 Vector bottomRightInner = new Vector( 0.5 - relativeHorizontalSize, -0.5 + relativeVerticalSize );
346
347 IndexTriangle[] triangles = { new IndexTriangle( 0, 1, 2 ), new IndexTriangle( 1, 3, 2 ), };
348
349 Vector[] leftSideVertices = { topLeftOuter, topLeftInner, bottomLeftOuter, bottomLeftInner, };
350 Vector[] topSideVertices = { topLeftOuter, topRightOuter, topLeftInner, topRightInner, };
351 Vector[] rightSideVertices = { topRightOuter, bottomRightOuter, topRightInner, bottomRightInner, };
352 Vector[] bottomSideVertices = { bottomRightOuter, bottomLeftOuter, bottomRightInner, bottomLeftInner, };
353
354 leftSideShape = new ShapeCache( leftSideVertices, triangles );
355 topSideShape = new ShapeCache( topSideVertices, triangles );
356 RightSideShape = new ShapeCache( rightSideVertices, triangles );
357 BottomSideShape = new ShapeCache( bottomSideVertices, triangles );
358
359 const double scale = 1.4;
360
361 topLeftOuter = new Vector( -0.5, 0.5 );
362 topLeftInner = new Vector( -0.5 + relativeHorizontalSize / scale, 0.5 - relativeVerticalSize / scale );
363 bottomLeftOuter = new Vector( -0.5, -0.5 );
364 bottomLeftInner = new Vector( -0.5 + relativeHorizontalSize / scale, -0.5 + relativeVerticalSize * scale );
365 topRightOuter = new Vector( 0.5, 0.5 );
366 topRightInner = new Vector( 0.5 - relativeHorizontalSize * scale, 0.5 - relativeVerticalSize / scale );
367 bottomRightOuter = new Vector( 0.5, -0.5 );
368 bottomRightInner = new Vector( 0.5 - relativeHorizontalSize * scale, -0.5 + relativeVerticalSize * scale );
369
370 Vector[] leftSidePressedVertices = { topLeftOuter, topLeftInner, bottomLeftOuter, bottomLeftInner, };
371 Vector[] topSidePressedVertices = { topLeftOuter, topRightOuter, topLeftInner, topRightInner, };
372 Vector[] rightSidePressedVertices = { topRightOuter, bottomRightOuter, topRightInner, bottomRightInner, };
373 Vector[] bottomSidePressedVertices = { bottomRightOuter, bottomLeftOuter, bottomRightInner, bottomLeftInner, };
374
375 leftSidePressedShape = new ShapeCache( leftSidePressedVertices, triangles );
376 topSidePressedShape = new ShapeCache( topSidePressedVertices, triangles );
377 RightSidePressedShape = new ShapeCache( rightSidePressedVertices, triangles );
378 BottomSidePressedShape = new ShapeCache( bottomSidePressedVertices, triangles );
379 }
380
381 internal void SetState( State state )
382 {
383 this.state = state;
384
385 switch ( state )
386 {
387 case State.Hover:
388 base.Color = hoverColor;
389 if ( ImageHover != null )
391 break;
392 case State.RightPressed:
393 case State.LeftPressed:
394 base.Color = pressedColor;
395 if ( ImagePressed != null )
397 break;
398 case State.Selected:
399 base.Color = selectedColor;
400 break;
401 default:
402 base.Color = releasedColor;
403 if ( ImageReleased != null )
405 break;
406 }
407 }
408
412 public void Click()
413 {
414 if ( Clicked != null )
415 Clicked();
416 }
417
421 public void RightClick()
422 {
423 if ( RightClicked != null )
424 RightClicked();
425 }
426
432 {
433 return Jypeli.Game.Instance.Keyboard.Listen( key, ButtonState.Pressed, Click, null ).InContext( this );
434 }
435
440 public List<Listener> AddShortcut( Button button )
441 {
442 var listeners = new List<Listener>(Game.GameControllers.Count);
443 Game.Instance.GameControllers.ForEach( c => listeners.Add(AddShortcut( c, button )) );
444 return listeners;
445 }
446
452 public Listener AddShortcut( int player, Button button )
453 {
454 return AddShortcut( Game.Instance.GameControllers[player], button );
455 }
456
462 public Listener AddShortcut( GamePad controller, Button button )
463 {
464 return controller.Listen( button, ButtonState.Pressed, Click, null ).InContext( this );
465 }
466
467 private void Release()
468 {
469 bool wasLeft = state == State.LeftPressed;
470 bool wasRight = state == State.RightPressed;
471
472 if ( Game.Mouse.IsCursorOn( this ) )
473 {
474 SetState( State.Hover );
475 }
476 else
477 {
478 SetState( State.Released );
479 return;
480 }
481
482 if ( wasLeft ) Click();
483 else if ( wasRight ) RightClick();
484 }
485
486 private void CheckHover()
487 {
488 if ( isPressed || state == State.Selected) return; // Ehkä voisi olla jonkinlainen lisäkorostus jos hiiri on päällä ja nappula on valittuna samanaikaisesti...
489 SetState( Game.Mouse.IsCursorOn( this ) ? State.Hover : State.Released );
490 }
491
493 public override void Draw( Matrix parentTransformation, Matrix transformation )
494 {
495 base.Draw( parentTransformation, transformation );
496
497 if ( Image == null )
498 {
499 Color color1 = Color.Lighter( Color, 20 );
500 Color color2 = Color.Darker( Color, 20 );
501
506
507 if ( isPressed )
508 {
509 color1 = Color.Darker( Color, 20 );
510 color2 = Color.Lighter( Color, 20 );
513 right = RightSidePressedShape;
514 bottom = BottomSidePressedShape;
515 }
516
517 Renderer.DrawFilledShape( left, ref transformation, color1 );
518 Renderer.DrawFilledShape( top, ref transformation, color1 );
519 Renderer.DrawFilledShape( right, ref transformation, color2 );
520 Renderer.DrawFilledShape( bottom, ref transformation, color2 );
521 }
522 }
523 }
524}
Mouse Mouse
Hiiri.
Definition: Controls.cs:49
List< GamePad > GameControllers
Lista kaikista peliohjaimista järjestyksessä.
Definition: Controls.cs:72
Keyboard Keyboard
Näppäimistö.
Definition: Controls.cs:44
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:96
TouchPanel TouchPanel
Kosketusnäyttö
Definition: Controls.cs:62
double Top
Olion yläreunan y-koordinaatti.
double Height
Olion korkeus (Y-suunnassa, korkeimmassa kohdassa).
double Bottom
Olion alareunan y-koordinaatti.
Action AddedToGame
Tapahtuu, kun olio lisätään peliin.
double Left
Olion vasemman reunan x-koordinaatti.
double Width
Olion leveys (X-suunnassa, leveimmässä kohdassa).
Listener Listen(Button button, ButtonState state, Action handler, string helpText)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:242
Kuva.
Definition: Image.cs:30
static Image Color(Image image, Color color)
Värittää kuvan.
Definition: Image.cs:1005
Listener Listen(Key k, ButtonState state, Action handler, string helpText)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:161
Tekstikenttä.
Definition: Label.cs:70
double XMargin
Marginaali vasemmasta/oikeasta reunasta.
Definition: Label.cs:244
Color TextColor
Tekstin väri.
Definition: Label.cs:204
double YMargin
Marginaali ylä-/alareunasta.
Definition: Label.cs:253
Listener ListenOn(GameObject obj, HoverState hoverstate, MouseButton button, ButtonState state, Action handler, string helpText)
Kuuntelee hiirenpainalluksia annetun peliolion päällä.
Definition: Mouse.cs:476
Listener Listen(MouseButton button, ButtonState state, Action handler, string helpText)
Kuuntelee hiiren nappulan painalluksia.
Definition: Mouse.cs:350
static bool IsCursorOn(ScreenView screen, MouseState state, GameObject obj)
Onko hiiren kursori annetun olion päällä.
Definition: Mouse.cs:320
Listener ListenMovement(double trigger, Action handler, string helpText)
Kuuntelee hiiren liikettä.
Definition: Mouse.cs:413
Painonappi.
Definition: PushButton.cs:40
override Color Color
Nappulan oletusväri. Asettaa myös hoverColor, selectedColor ja pressedColor -kenttien arvot....
Definition: PushButton.cs:147
List< Listener > AddShortcut(Button button)
Lisää pikanäppäimen kaikille ohjaimille.
Definition: PushButton.cs:440
Listener AddShortcut(Key key)
Lisää pikanäppäimen napille.
Definition: PushButton.cs:431
ShapeCache RightSidePressedShape
Definition: PushButton.cs:57
void InitializeControls()
Definition: PushButton.cs:311
void InitializeMargins()
Definition: PushButton.cs:281
Listener AddShortcut(GamePad controller, Button button)
Lisää pikanäppäimen yhdelle ohjaimelle.
Definition: PushButton.cs:462
void InitializeShape()
Definition: PushButton.cs:331
PushButton(double width, double height, Image image)
Luo uuden painonapin omalla kuvalla.
Definition: PushButton.cs:293
override void Draw(Matrix parentTransformation, Matrix transformation)
Piirtää elementin ruudulle
Definition: PushButton.cs:493
ShapeCache topSidePressedShape
Definition: PushButton.cs:56
ShapeCache leftSidePressedShape
Definition: PushButton.cs:55
PushButton(string text)
Luo uuden painonapin.
Definition: PushButton.cs:243
Image ImageReleased
Kuva kun nappi on vapautettu.
Definition: PushButton.cs:87
Color PressedColor
Nappulan väri kun sitä klikataan.
Definition: PushButton.cs:181
void TouchClick(Touch touch)
Definition: PushButton.cs:234
override Vector Size
Nappulan koko
Definition: PushButton.cs:129
void TouchRelease(Touch touch)
Definition: PushButton.cs:228
ShapeCache BottomSidePressedShape
Definition: PushButton.cs:58
void RightClick()
Suoritetaan, kun nappulaa klikattiin hiiren oikealla näppäimellä
Definition: PushButton.cs:421
PushButton(Image image)
Luo uuden painonapin.
Definition: PushButton.cs:253
Action Clicked
Tapahtuu kun nappia on painettu.
Definition: PushButton.cs:210
Color SelectedColor
Nappulan väri kun se on valittuna, esimerkiksi MultiSelectWindowissa.
Definition: PushButton.cs:196
ShapeCache BottomSideShape
Definition: PushButton.cs:53
void Click()
Suoritetaan, kun nappulaa klikattiin hiiren vasemmalla näppäimellä
Definition: PushButton.cs:412
void TouchHover(Touch touch)
Definition: PushButton.cs:217
ShapeCache topSideShape
Definition: PushButton.cs:51
ShapeCache leftSideShape
Definition: PushButton.cs:50
Image ImageHover
Kuva kun hiiren kursori on napin päällä.
Definition: PushButton.cs:115
static Color defaultColor
Kaikkien tulevien nappuloiden oletusväri
Definition: PushButton.cs:76
Listener AddShortcut(int player, Button button)
Lisää pikanäppäimen yhdelle ohjaimelle.
Definition: PushButton.cs:452
void SetState(State state)
Definition: PushButton.cs:381
Image ImagePressed
Kuva kun nappi on alaspainettuna.
Definition: PushButton.cs:101
Color HoverColor
Nappulan väri kun hiiri viedään sen päälle.
Definition: PushButton.cs:166
PushButton(double width, double height, string text)
Luo uuden painonapin.
Definition: PushButton.cs:305
Action RightClicked
Tapahtuu kun nappia on painettu oikealla hiirenpainikkeella.
Definition: PushButton.cs:215
PushButton(double width, double height)
Luo uuden painonapin.
Definition: PushButton.cs:265
ShapeCache RightSideShape
Definition: PushButton.cs:52
static Color defaultTextColor
Kaikkien tulevien nappuloiden tekstin oletusväri
Definition: PushButton.cs:81
Luokka, joka sisältää metodeita kuvioiden ja tekstuurien piirtämiseen 2D-tasossa.
Definition: Renderer.cs:50
static void DrawFilledShape(ShapeCache cache, ref Matrix matrix, Color color)
Definition: Renderer.cs:409
Sisältää valmiiksi lasketut kolmiot, joiden avulla piirtäminen on suoraviivaista.
Definition: Shapes.cs:628
Kosketuspaneelin kosketus.
Definition: Touch.cs:39
Vector PositionOnScreen
Kosketuksen paikka ruudulla.
Definition: Touch.cs:61
Listener ListenOn(GameObject obj, HoverState hoverstate, ButtonState buttonstate, TouchHandler handler, string helpText)
Kuuntelee kosketusnäyttöä olion päällä.
Definition: TouchPanel.cs:354
int NumTouches
Kuinka monta kosketusta tällä hetkellä ruudulla.
Definition: TouchPanel.cs:57
Listener Listen(ButtonState state, TouchHandler handler, string helpText)
Kuuntelee kosketusnäyttöä.
Definition: TouchPanel.cs:296
List< Listener > associatedListeners
Tähän listaan lisätyt kuuntelijat tuhotaan automaattisesti kun Widget poistetaan pelistä.
Definition: Control.cs:14
bool CapturesMouse
Kaappaako hiiren, eli meneekö hiiren tapahtumat tämän alla sijaitsevalle oliolle
Definition: Control.cs:30
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:72
Listener InContext(ListenContext context)
Kuuntelee tapahtumaa vain tietyssä kontekstissa.
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
@ Released
Nappi nostetaan ylös.
Button
Definition: Button.cs:36
Key
Näppäimistön näppäin.
Definition: Key.cs:39
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
MouseButton
Hiiren napit.
Definition: MouseButton.cs:7
Väri.
Definition: Color.cs:13
Color(XnaColor c)
Definition: Color.cs:38
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:470
static Color Lighter(Color c, int howMuch)
Antaa kirkkaamman värin. Kasvattaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:489
Muotojen määrityksessä käytettävä kolmio.
Definition: Shapes.cs:598
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
double X
Vektorin X-komponentti.
Definition: Vector.cs:334