Jypeli  9
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, Rami Pasanen
28  */
29 
30 
31 namespace Jypeli
32 {
36  public class Window : Widget
37  {
38  private Color _actColor = new Color( 255, 255, 255, 200 );
39  private Color _inactColor = new Color( 50, 50, 50, 50 );
40  private Color _actTitle = new Color( 255, 255, 255, 100 );
41  private Color _inactTitle = new Color( 128, 128, 128, 50 );
42 
43  private bool moving = false;
45  private bool prevMouseVisible = true;
46 
50  public override Color Color
51  {
52  get
53  {
54  return base.Color;
55  }
56  set
57  {
58  ActiveColor = value;
59  InactiveColor = Color.Darker( value, 75 );
60  base.Color = value;
61  }
62  }
63 
68  {
69  get { return _actColor; }
70  set { _actColor = value; }
71  }
72 
77  {
78  get { return _inactColor; }
79  set { _inactColor = value; }
80  }
81 
85  public delegate void WindowHandler( Window sender );
86 
92  public event WindowHandler Closed;
93 
94  private void OnClosed()
95  {
96  if ( Closed != null )
97  Closed( this );
98  }
99 
103  public Window()
104  : base( new VerticalLayout() )
105  {
106  initialize();
107  }
108 
114  public Window( double width, double height )
115  : base( width, height )
116  {
117  SizingByLayout = false;
118  Layout = new VerticalLayout();
119  initialize();
120  }
121 
125  protected override Vector GetMaximumSize()
126  {
127  var screen = Game.Screen;
128  return new Vector( screen.Width * ( 7.0 / 8.0 ), screen.Height * ( 7.0 / 8.0 ) );
129  }
130 
131  private void initialize()
132  {
133  AddedToGame += ShowMouse;
134  Removed += RestoreMouse;
135 
136  Removed += OnClosed;
137  ControlContext.Activated += Window_Activated;
138  ControlContext.Deactivated += Window_Deactivated;
139 
140  AddedToGame += AddControls;
141 
142  this.IsModal = true;
143 
144  Game.AssertInitialized( RefreshLayout );
145  }
146 
147  private void ShowMouse()
148  {
149  if (!IsModal)
150  return;
151 
152  if ( Game.Instance != null )
153  {
154  prevMouseVisible = Game.Instance.IsMouseVisible;
155  Game.Instance.IsMouseVisible = true;
156  }
157  }
158 
159  private void RestoreMouse()
160  {
161  if (!IsModal)
162  return;
163 
164  if ( Game.Instance != null )
165  Game.Instance.IsMouseVisible = prevMouseVisible;
166  }
167 
168  void AddControls()
169  {
170  var l1 = Game.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Pressed, StartMoveWindow, null ).InContext( this );
171  var l2 = Game.Mouse.Listen( MouseButton.Left, ButtonState.Down, MoveWindow, null ).InContext( this );
172  var l3 = Game.Mouse.ListenOn( this, MouseButton.Left, ButtonState.Released, EndMoveWindow, null ).InContext( this );
173  associatedListeners.AddItems(l1, l2, l3);
174  }
175 
177  {
178  if ( Game == null ) return;
179  foreach (var obj in Objects)
180  {
181  if (obj is Widget w && w.IsCapturingMouse)
182  return;
183  }
184 
185  movementCenter = this.Position - Game.Mouse.PositionOnScreen;
186  moving = true;
187  }
188 
189  void MoveWindow()
190  {
191  if ( !moving ) return;
192  this.Position = movementCenter + Game.Mouse.PositionOnScreen;
193  }
194 
196  {
197  moving = false;
198  }
199 
201  {
202  base.Color = ActiveColor;
203  }
204 
206  {
207  base.Color = InactiveColor;
208  }
209 
213  public void Close()
214  {
215  if ( Parent != null )
216  Parent.Remove( this );
217  else
218  Game.Remove( this );
219  }
220  }
221 }
Jypeli.Window.Window
Window(double width, double height)
Alustaa uuden ikkunan.
Definition: Window.cs:114
Jypeli.Mouse.Listen
Listener Listen(MouseButton button, ButtonState state, Action handler, string helpText)
Kuuntelee hiiren nappulan painalluksia.
Definition: Mouse.cs:350
Jypeli.Window.OnClosed
void OnClosed()
Definition: Window.cs:94
Jypeli.Mouse.PositionOnScreen
Vector PositionOnScreen
Kursorin paikka ruutukoordinaateissa.
Definition: Mouse.cs:75
Jypeli.Vector.Zero
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:63
Jypeli.Window.InactiveColor
Color InactiveColor
Ikkunan väri, kun ikkuna ei ole aktiivinen.
Definition: Window.cs:77
Jypeli
Definition: Automobile.cs:5
Jypeli.Widget.IsModal
bool IsModal
Jos true, pelin sekä ikkunan alla olevien widgettien ohjaimet eivät ole käytössä kun ikkuna on näkyvi...
Definition: Control.cs:22
Jypeli.Window.moving
bool moving
Definition: Window.cs:43
Jypeli.Window.Close
void Close()
Sulkee ikkunan.
Definition: Window.cs:213
Jypeli.Window.ActiveColor
Color ActiveColor
Ikkunan väri, kun ikkuna on aktiivinen.
Definition: Window.cs:68
Jypeli.Widget.IsCapturingMouse
bool IsCapturingMouse
Definition: Control.cs:26
Jypeli.Window.movementCenter
Vector movementCenter
Definition: Window.cs:44
Jypeli.Window.StartMoveWindow
void StartMoveWindow()
Definition: Window.cs:176
Jypeli.Window.AddControls
void AddControls()
Definition: Window.cs:168
Jypeli.Window.Closed
WindowHandler Closed
Tapahtuu kun ikkuna suljetaan. TODO: ClearAllin kutsuminen samalla updatella kuin Closed-eventti tapa...
Definition: Window.cs:92
Jypeli.Game.Mouse
Mouse Mouse
Hiiri.
Definition: Controls.cs:49
Jypeli.Game.AssertInitialized
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: DelayedActions.cs:53
Jypeli.Window.prevMouseVisible
bool prevMouseVisible
Definition: Window.cs:45
Jypeli.Window._actColor
Color _actColor
Definition: Window.cs:38
Jypeli.Window.EndMoveWindow
void EndMoveWindow()
Definition: Window.cs:195
Jypeli.Window.Color
override Color Color
Ikkunan väri.
Definition: Window.cs:51
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Widget
Käyttöliittymän komponentti.
Definition: Appearance.cs:6
Jypeli.Game.Remove
void Remove(IGameObject o)
Poistaa olion pelistä. Jos haluat tuhota olion, kutsu mielummin olion Destroy-metodia.
Definition: Layers.cs:203
Jypeli.VerticalLayout
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
Definition: VerticalLayout.cs:39
Jypeli.Game.Screen
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Graphics.cs:90
Jypeli.Window.initialize
void initialize()
Definition: Window.cs:131
Jypeli.Window.ShowMouse
void ShowMouse()
Definition: Window.cs:147
Jypeli.Window._inactTitle
Color _inactTitle
Definition: Window.cs:41
Jypeli.Widget.ControlContext
ListenContext ControlContext
Definition: Control.cs:16
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.Mouse.ListenOn
Listener ListenOn(GameObject obj, HoverState hoverstate, MouseButton button, ButtonState state, Action handler, string helpText)
Kuuntelee hiirenpainalluksia annetun peliolion päällä.
Definition: Mouse.cs:476
Jypeli.Window._inactColor
Color _inactColor
Definition: Window.cs:39
Jypeli.MouseButton
MouseButton
Hiiren napit.
Definition: MouseButton.cs:7
Jypeli.Window.Window_Activated
void Window_Activated()
Definition: Window.cs:200
Jypeli.ButtonState
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
Jypeli.Widget.associatedListeners
List< Listener > associatedListeners
Tähän listaan lisätyt kuuntelijat tuhotaan automaattisesti kun Widget poistetaan pelistä.
Definition: Control.cs:14
Jypeli.Color.Darker
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:417
Jypeli.Window.Window_Deactivated
void Window_Deactivated()
Definition: Window.cs:205
Jypeli.Window
Ikkuna.
Definition: Window.cs:37
Jypeli.Window.GetMaximumSize
override Vector GetMaximumSize()
Ikkunalla maksimikoko on siten, että se mahtuu näytölle.
Definition: Window.cs:125
Jypeli.Window.WindowHandler
delegate void WindowHandler(Window sender)
Ikkunatapahtumien käsittelijä.
Jypeli.Window.RestoreMouse
void RestoreMouse()
Definition: Window.cs:159
Jypeli.Game
Definition: Content.cs:46
Jypeli.Color.Color
Color(XnaColor c)
Definition: Color.cs:38
Jypeli.Window._actTitle
Color _actTitle
Definition: Window.cs:40
Jypeli.Window.MoveWindow
void MoveWindow()
Definition: Window.cs:189
Jypeli.Window.Window
Window()
Alustaa uuden ikkunan.
Definition: Window.cs:103