Jypeli 10
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
30using System.ComponentModel;
31using System.Collections.Generic;
32
33namespace Jypeli.GameObjects
34{
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 ) + Parent.X;
282 o.Y = top - height / 2 + offset + Parent.Y;
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}
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: Appearance.cs:34
virtual Sizing HorizontalSizing
Koon asettaminen vaakasuunnassa, kun olio on asettelijan sisällä.
Definition: Layout.cs:18
void NotifyParentAboutChangedSizingAttributes()
Should be called whenever properties that might affect layouts are changed.
Definition: Layout.cs:83
override Vector Size
Olion koko pelimaailmassa. Kertoo olion äärirajat, ei muotoa.
Definition: Dimensions.cs:44
virtual Vector PreferredSize
Koko, jota oliolla tulisi olla asettelijan sisällä. Todellinen koko voi olla pienempi,...
Definition: Layout.cs:56
double X
Olion paikan X-koordinaatti.
double Y
Olion paikan Y-koordinaatti.
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas. Jos widgeteille ei ole tarpeeksi tilaa,...
double BottomPadding
Alareunaan jäävä tyhjä tila.
int StartIndex
Ylhäältä lukien ensimmäisen piirtoalueen sisällä olevan widgetin indeksi.
int EndIndex
Viimeisen piirtoalueella olevan widgetin jälkeinen indeksi.
void Scroll(IList< GameObject > objects, double amount)
Listan portaaton vieritys.
void ScrollUp(IList< GameObject > objects)
double Spacing
Olioiden väliin jäävä tyhjä tila.
void Update(IList< GameObject > objects, Vector maximumSize)
Päivittää asettelijan olioiden sijainnit
double RightPadding
Oikeaan reunaan jäävä tyhjä tila.
double LeftPadding
Vasempaan reunaan jäävä tyhjä tila.
void UpdateSizeHints(IList< GameObject > objects)
Päivittää asettelijan koon
double TopPadding
Yläreunaan jäävä tyhjä tila.
void ScrollDown(IList< GameObject > objects)
Rajapinta asettelijalle. Asettelija asettelee widgetin lapsioliot siten, että ne mahtuvat widgetin si...
Definition: ILayout.cs:95
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:39
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
double X
Vektorin X-komponentti.
Definition: Vector.cs:334