Jypeli  5
The simple game programming library
Window.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 
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Text;
34 using Jypeli;
35 using Jypeli.GameObjects;
36 
37 namespace Jypeli.Widgets
38 {
42  public class Window : Widget
43  {
44  private Color _actColor = new Color( 255, 255, 255, 200 );
45  private Color _inactColor = new Color( 50, 50, 50, 50 );
46  private Color _actTitle = new Color( 255, 255, 255, 100 );
47  private Color _inactTitle = new Color( 128, 128, 128, 50 );
48 
49  private bool moving = false;
50  private Vector movementCenter = Vector.Zero;
51  private bool prevMouseVisible = true;
52 
56  public override Color Color
57  {
58  get
59  {
60  return base.Color;
61  }
62  set
63  {
64  ActiveColor = value;
65  InactiveColor = Color.Darker( value, 75 );
66  base.Color = value;
67  }
68  }
69 
73  public Color ActiveColor
74  {
75  get { return _actColor; }
76  set { _actColor = value; }
77  }
78 
82  public Color InactiveColor
83  {
84  get { return _inactColor; }
85  set { _inactColor = value; }
86  }
87 
91  public delegate void WindowHandler( Window sender );
92 
96  public event WindowHandler Closed;
97 
98  private void OnClosed()
99  {
100  if ( Closed != null )
101  Closed( this );
102  }
103 
107  public Window()
108  : base( new VerticalLayout() )
109  {
110  initialize();
111  }
112 
118  public Window( double width, double height )
119  : base( width, height, Shape.Rectangle )
120  {
121  SizingByLayout = false;
122  Layout = new VerticalLayout();
123  initialize();
124  }
125 
129  protected override Vector GetMaximumSize()
130  {
131  var screen = Game.Screen;
132  return new Vector( screen.Width * ( 7.0 / 8.0 ), screen.Height * ( 7.0 / 8.0 ) );
133  }
134 
135  private void initialize()
136  {
137  AddedToGame += ShowMouse;
138  Removed += RestoreMouse;
139 
140  Removed += OnClosed;
141  ControlContext.Activated += Window_Activated;
142  ControlContext.Deactivated += Window_Deactivated;
143 
144 #if WINDOWS
145  AddedToGame += AddControls;
146 #endif
147 
148  this.IsModal = true;
149 
150  Game.AssertInitialized( RefreshLayout );
151  }
152 
153  private void ShowMouse()
154  {
155  if ( Game.Instance != null )
156  {
157  prevMouseVisible = Game.Instance.IsMouseVisible;
158  Game.Instance.IsMouseVisible = true;
159  }
160  }
161 
162  private void RestoreMouse()
163  {
164  if ( Game.Instance != null )
165  Game.Instance.IsMouseVisible = prevMouseVisible;
166  }
167 
168  void AddControls()
169  {
170  Game.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Pressed, StartMoveWindow, null ).InContext( this );
171  Game.Mouse.Listen( MouseButton.Left, ButtonState.Down, MoveWindow, null ).InContext( this );
172  Game.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Released, EndMoveWindow, null ).InContext( this );
173  }
174 
175  void StartMoveWindow()
176  {
177  if ( Game == null ) return;
178  movementCenter = this.Position - Game.Mouse.PositionOnScreen;
179  moving = true;
180  }
181 
182  void MoveWindow()
183  {
184  if ( !moving ) return;
185  this.Position = movementCenter + Game.Mouse.PositionOnScreen;
186  }
187 
188  void EndMoveWindow()
189  {
190  moving = false;
191  }
192 
193  void Window_Activated()
194  {
195  base.Color = ActiveColor;
196  }
197 
198  void Window_Deactivated()
199  {
200  base.Color = InactiveColor;
201  }
202 
206  public void Close()
207  {
208  if ( Parent != null )
209  Parent.Remove( this );
210  else
211  Game.Remove( this );
212  }
213  }
214 }
Kuvio.
Definition: Shapes.cs:48
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:407
Suorakulmio.
Definition: Shapes.cs:315
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
static Game Instance
Definition: Game.cs:149
override Vector GetMaximumSize()
Ikkunalla maksimikoko on siten, että se mahtuu näytölle.
Definition: Window.cs:129
MouseButton
Hiiren napit.
Definition: MouseButton.cs:11
Käyttöliittymän komponentti.
Definition: Appearance.cs:9
WindowHandler Closed
Tapahtuu kun ikkuna suljetaan.
Definition: Window.cs:96
Window(double width, double height)
Alustaa uuden ikkunan.
Definition: Window.cs:118
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:61
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Game.cs:194
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
void Close()
Sulkee ikkunan.
Definition: Window.cs:206
Window()
Alustaa uuden ikkunan.
Definition: Window.cs:107
Väri.
Definition: Color.cs:13
void Remove(IGameObject o)
Poistaa olion pelistä. Jos haluat tuhota olion, kutsu mielummin olion Destroy-metodia.
Definition: Game.cs:764
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