Jypeli  9
The simple game programming library
VerticalScrollLayout.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.ComponentModel;
31 using System.Collections.Generic;
32 
33 namespace Jypeli.GameObjects
34 {
42  internal class VerticalScrollLayout : ILayout
43  {
49  private double _spacing = 0;
50  private double _topPadding = 0;
51  private double _bottomPadding = 0;
52  private double _leftPadding = 0;
53  private double _rightPadding = 0;
54  private double _firstVisibleItemOffset = 0;
55 
56 
57  [EditorBrowsable( EditorBrowsableState.Never )]
58  public GameObject Parent { get; set; }
59 
63  public double Spacing
64  {
65  get { return _spacing; }
66  set { _spacing = value; NotifyParent(); }
67  }
68 
72  public double TopPadding
73  {
74  get { return _topPadding; }
75  set { _topPadding = value; NotifyParent(); }
76  }
77 
81  public double BottomPadding
82  {
83  get { return _bottomPadding; }
84  set { _bottomPadding = value; NotifyParent(); }
85  }
86 
90  public double LeftPadding
91  {
92  get { return _leftPadding; }
93  set { _leftPadding = value; NotifyParent(); }
94  }
95 
99  public double RightPadding
100  {
101  get { return _rightPadding; }
102  set { _rightPadding = value; NotifyParent(); }
103  }
104 
108  public int StartIndex { get; set; }
109 
113  public int EndIndex { get; set; }
114 
115  private void NotifyParent()
116  {
117  if ( Parent != null )
118  {
120  }
121  }
122 
123  public void ScrollUp( IList<GameObject> objects )
124  {
125  if ( StartIndex > 0 )
126  {
127  StartIndex--;
129  Update( objects, Parent.Size );
130  }
131  }
132 
133  public void ScrollDown( IList<GameObject> objects )
134  {
135  if ( StartIndex < objects.Count - 1 )
136  {
137  StartIndex++;
139  Update( objects, Parent.Size );
140  }
141  }
142 
146  public void Scroll( IList<GameObject> objects, double amount )
147  {
148  if ( objects.Count == 0 ) return;
149 
150  if ( amount < 0 )
151  {
152  double totalScrollingAmount = -amount;
153  double spaceForCurrentObject = _firstVisibleItemOffset;
154 
155  while ( spaceForCurrentObject < totalScrollingAmount )
156  {
157  if ( StartIndex == 0 )
158  {
159  spaceForCurrentObject = 0;
160  break;
161  }
162 
163  totalScrollingAmount -= spaceForCurrentObject;
164  StartIndex--;
165  spaceForCurrentObject = objects[StartIndex].PreferredSize.Y + Spacing;
166  }
167 
168  spaceForCurrentObject -= totalScrollingAmount;
169  _firstVisibleItemOffset = spaceForCurrentObject;
170  }
171  else
172  {
173  double totalScrollingAmount = -amount;
174  double spaceForCurrentObject = objects[StartIndex].PreferredSize.Y + Spacing - _firstVisibleItemOffset;
175 
176  while ( spaceForCurrentObject < -totalScrollingAmount )
177  {
178  if ( StartIndex >= objects.Count - 1 )
179  {
180  spaceForCurrentObject = 0;
181  break;
182  }
183 
184  totalScrollingAmount += spaceForCurrentObject;
185  StartIndex++;
186  spaceForCurrentObject = objects[StartIndex].PreferredSize.Y + Spacing;
187  }
188 
189  spaceForCurrentObject += totalScrollingAmount;
190  _firstVisibleItemOffset = objects[StartIndex].PreferredSize.Y + Spacing - spaceForCurrentObject;
191  }
192 
193  Update( objects, Parent.Size );
194  }
195 
196  [EditorBrowsable( EditorBrowsableState.Never )]
197  public void UpdateSizeHints( IList<GameObject> objects )
198  {
199  if ( objects.Count == 0 )
200  return;
201 
202  double maxWidth = 0;
203  double heightOfExpandingObjects = 0;
204  double heightOfFixedSizeObjects = 0;
205  Sizing horizontalSizing = Sizing.FixedSize;
206  Sizing verticalSizing = Sizing.FixedSize;
207 
208  foreach ( var o in objects )
209  {
210  if ( o.PreferredSize.X > maxWidth )
211  {
212  maxWidth = o.PreferredSize.X;
213  }
214 
215  if ( o.VerticalSizing != Sizing.FixedSize )
216  {
217  verticalSizing = Sizing.Expanding;
218  heightOfExpandingObjects += o.PreferredSize.Y;
219  }
220  else if ( o.VerticalSizing == Sizing.FixedSize )
221  {
222  heightOfFixedSizeObjects += o.PreferredSize.Y;
223  }
224 
225  if ( o.HorizontalSizing != Sizing.FixedSize )
226  {
227  horizontalSizing = Sizing.Expanding;
228  }
229  }
230 
231  double preferredHeight = TopPadding + heightOfExpandingObjects + heightOfFixedSizeObjects + ( ( objects.Count - 1 ) * Spacing ) + BottomPadding;
232  double preferredWidth = LeftPadding + maxWidth + RightPadding;
233 
234  _horizontalSizing = horizontalSizing;
235  _verticalSizing = verticalSizing;
236  _heightRequestedByExpandingObjects = heightOfExpandingObjects;
237  _heightRequestedByFixedSizeObjects = heightOfFixedSizeObjects;
238  _preferredSize = new Vector( preferredWidth, preferredHeight );
239  }
240 
241  [EditorBrowsable( EditorBrowsableState.Never )]
243  {
244  get { return _horizontalSizing; }
245  }
246 
247  [EditorBrowsable( EditorBrowsableState.Never )]
249  {
250  get { return _verticalSizing; }
251  }
252 
253  [EditorBrowsable( EditorBrowsableState.Never )]
255  {
256  get { return _preferredSize; }
257  }
258 
259  [EditorBrowsable( EditorBrowsableState.Never )]
260  public void Update( IList<GameObject> objects, Vector maximumSize )
261  {
262  double contentHeight = maximumSize.Y - ( TopPadding + BottomPadding );
263  double contentWidth = maximumSize.X - ( LeftPadding + RightPadding );
264  double contentBottomLimit = -maximumSize.Y / 2 + BottomPadding;
265  double top = maximumSize.Y / 2 - TopPadding;
266  double offset = _firstVisibleItemOffset;
267  int i = StartIndex;
268 
269  while ( ( i < objects.Count ) && ( top >= contentBottomLimit ) )
270  {
271  GameObject o = objects[i];
272  double width = o.PreferredSize.X;
273  double height = o.PreferredSize.Y;
274 
275  if ( ( o.PreferredSize.X > contentWidth ) || ( o.HorizontalSizing != Sizing.FixedSize ) )
276  {
277  width = contentWidth;
278  }
279 
280  o.Size = new Vector( width, height );
281  o.X = ( -maximumSize.X / 2 ) + ( LeftPadding + contentWidth / 2 );
282  o.Y = top - height / 2 + offset;
283 
284  top -= height + Spacing - offset;
285 
286  // offset is zero for all but the first object.
287  offset = 0;
288  i++;
289  }
290 
291  EndIndex = i;
292  }
293  }
294 }
Jypeli.GameObjects.VerticalScrollLayout._leftPadding
double _leftPadding
Definition: VerticalScrollLayout.cs:52
Jypeli.GameObjects.VerticalScrollLayout.Update
void Update(IList< GameObject > objects, Vector maximumSize)
Definition: VerticalScrollLayout.cs:260
Jypeli.GameObjects.VerticalScrollLayout.LeftPadding
double LeftPadding
Vasempaan reunaan jäävä tyhjä tila.
Definition: VerticalScrollLayout.cs:91
Jypeli.GameObject.NotifyParentAboutChangedSizingAttributes
void NotifyParentAboutChangedSizingAttributes()
Should be called whenever properties that might affect layouts are changed.
Definition: Layout.cs:80
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.GameObjects.VerticalScrollLayout.ScrollDown
void ScrollDown(IList< GameObject > objects)
Definition: VerticalScrollLayout.cs:133
Jypeli.GameObjects.VerticalScrollLayout._horizontalSizing
Sizing _horizontalSizing
Definition: VerticalScrollLayout.cs:44
Jypeli.GameObject.HorizontalSizing
virtual Sizing HorizontalSizing
Koon asettaminen vaakasuunnassa, kun olio on asettelijan sisällä.
Definition: Layout.cs:18
Jypeli.GameObjects.VerticalScrollLayout._firstVisibleItemOffset
double _firstVisibleItemOffset
Definition: VerticalScrollLayout.cs:54
Jypeli.GameObjects.VerticalScrollLayout
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas. Jos widgeteille ei ole tarpeeksi tilaa,...
Definition: VerticalScrollLayout.cs:43
Jypeli.GameObjects.VerticalScrollLayout.Scroll
void Scroll(IList< GameObject > objects, double amount)
Listan portaaton vieritys.
Definition: VerticalScrollLayout.cs:146
Jypeli.GameObjects.VerticalScrollLayout._verticalSizing
Sizing _verticalSizing
Definition: VerticalScrollLayout.cs:45
Jypeli.GameObjects.VerticalScrollLayout._heightRequestedByFixedSizeObjects
double _heightRequestedByFixedSizeObjects
Definition: VerticalScrollLayout.cs:48
Jypeli.GameObjects
Definition: GameObjectBase.cs:5
Jypeli.GameObjects.VerticalScrollLayout.UpdateSizeHints
void UpdateSizeHints(IList< GameObject > objects)
Definition: VerticalScrollLayout.cs:197
Jypeli.GameObjects.VerticalScrollLayout._bottomPadding
double _bottomPadding
Definition: VerticalScrollLayout.cs:51
Jypeli.GameObjects.VerticalScrollLayout.VerticalSizing
Sizing VerticalSizing
Definition: VerticalScrollLayout.cs:249
Jypeli.ILayout
Rajapinta asettelijalle. Asettelija asettelee widgetin lapsioliot siten, että ne mahtuvat widgetin si...
Definition: ILayout.cs:84
Jypeli.GameObjects.VerticalScrollLayout.TopPadding
double TopPadding
Yläreunaan jäävä tyhjä tila.
Definition: VerticalScrollLayout.cs:73
Jypeli.GameObjects.VerticalScrollLayout.PreferredSize
Vector PreferredSize
Definition: VerticalScrollLayout.cs:255
Jypeli.GameObjects.VerticalScrollLayout.NotifyParent
void NotifyParent()
Definition: VerticalScrollLayout.cs:115
Jypeli.GameObjects.VerticalScrollLayout.HorizontalSizing
Sizing HorizontalSizing
Definition: VerticalScrollLayout.cs:243
Jypeli.GameObjects.VerticalScrollLayout.BottomPadding
double BottomPadding
Alareunaan jäävä tyhjä tila.
Definition: VerticalScrollLayout.cs:82
Jypeli.GameObjects.VerticalScrollLayout.RightPadding
double RightPadding
Oikeaan reunaan jäävä tyhjä tila.
Definition: VerticalScrollLayout.cs:100
Jypeli.GameObjects.VerticalScrollLayout.Spacing
double Spacing
Olioiden väliin jäävä tyhjä tila.
Definition: VerticalScrollLayout.cs:64
Jypeli.GameObjects.VerticalScrollLayout._heightRequestedByExpandingObjects
double _heightRequestedByExpandingObjects
Definition: VerticalScrollLayout.cs:47
Jypeli.GameObjects.VerticalScrollLayout._topPadding
double _topPadding
Definition: VerticalScrollLayout.cs:50
Jypeli.GameObject.PreferredSize
virtual Vector PreferredSize
Koko, jota oliolla tulisi olla asettelijan sisällä. Todellinen koko voi olla pienempi,...
Definition: Layout.cs:56
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.GameObjects.VerticalScrollLayout._rightPadding
double _rightPadding
Definition: VerticalScrollLayout.cs:53
Jypeli.GameObjects.VerticalScrollLayout._spacing
double _spacing
Definition: VerticalScrollLayout.cs:49
Jypeli.GameObject.Size
override Vector Size
Olion koko pelimaailmassa. Kertoo olion äärirajat, ei muotoa.
Definition: Dimensions.cs:44
Jypeli.GameObject
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: Appearance.cs:34
Jypeli.GameObjects.VerticalScrollLayout.StartIndex
int StartIndex
Ylhäältä lukien ensimmäisen piirtoalueen sisällä olevan widgetin indeksi.
Definition: VerticalScrollLayout.cs:108
Jypeli.GameObjects.VerticalScrollLayout._preferredSize
Vector _preferredSize
Definition: VerticalScrollLayout.cs:46
Jypeli.GameObjects.VerticalScrollLayout.EndIndex
int EndIndex
Viimeisen piirtoalueella olevan widgetin jälkeinen indeksi.
Definition: VerticalScrollLayout.cs:113
Jypeli.GameObjects.VerticalScrollLayout.Parent
GameObject Parent
Definition: VerticalScrollLayout.cs:58
Jypeli.Vector.Y
double Y
Definition: Vector.cs:313
Jypeli.Sizing
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:39
Jypeli.GameObjects.VerticalScrollLayout.ScrollUp
void ScrollUp(IList< GameObject > objects)
Definition: VerticalScrollLayout.cs:123