Jypeli 10
The simple game programming library
VerticalLayout.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2009-2012 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.Collections.Generic;
31using System.ComponentModel;
32
33namespace Jypeli
34{
38 public class VerticalLayout : ILayout
39 {
45 private double _spacing = 0;
46 private double _topPadding = 0;
47 private double _bottomPadding = 0;
48 private double _leftPadding = 0;
49 private double _rightPadding = 0;
50
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public GameObject Parent { get; set; }
54
58 public double Spacing
59 {
60 get { return _spacing; }
61 set { _spacing = value; NotifyParent(); }
62 }
63
67 public double TopPadding
68 {
69 get { return _topPadding; }
70 set { _topPadding = value; NotifyParent(); }
71 }
72
76 public double BottomPadding
77 {
78 get { return _bottomPadding; }
79 set { _bottomPadding = value; NotifyParent(); }
80 }
81
85 public double LeftPadding
86 {
87 get { return _leftPadding; }
88 set { _leftPadding = value; NotifyParent(); }
89 }
90
94 public double RightPadding
95 {
96 get { return _rightPadding; }
97 set { _rightPadding = value; NotifyParent(); }
98 }
99
100 private void NotifyParent()
101 {
102 if (Parent != null)
103 {
105 }
106 }
107
112 {
113 }
114
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public void UpdateSizeHints(IList<GameObject> objects)
121 {
122 if (objects.Count == 0)
123 return;
124
125 double maxWidth = 0;
126 double heightOfExpandingObjects = 0;
127 double heightOfFixedSizeObjects = 0;
128 Sizing horizontalSizing = Sizing.FixedSize;
129 Sizing verticalSizing = Sizing.FixedSize;
130
131 foreach (var o in objects)
132 {
133 if (o.PreferredSize.X > maxWidth)
134 {
135 maxWidth = o.PreferredSize.X;
136 }
137
138 if (o.VerticalSizing == Sizing.Expanding)
139 {
140 verticalSizing = Sizing.Expanding;
141 heightOfExpandingObjects += o.PreferredSize.Y;
142 }
143 else if (o.VerticalSizing == Sizing.FixedSize)
144 {
145 heightOfFixedSizeObjects += o.PreferredSize.Y;
146 }
147
148 if (o.HorizontalSizing != Sizing.FixedSize)
149 {
150 horizontalSizing = Sizing.Expanding;
151 }
152 }
153
154 double preferredHeight = TopPadding + heightOfExpandingObjects + heightOfFixedSizeObjects + ((objects.Count - 1) * Spacing) + BottomPadding;
155 double preferredWidth = LeftPadding + maxWidth + RightPadding;
156
157 _horizontalSizing = horizontalSizing;
158 _verticalSizing = verticalSizing;
159 _spaceRequestedByExpandingObjects = heightOfExpandingObjects;
160 _spaceRequestedByFixedSizeObjects = heightOfFixedSizeObjects;
161 _preferredSize = new Vector(preferredWidth, preferredHeight);
162 }
163
165 [EditorBrowsable(EditorBrowsableState.Never)]
167 {
168 get { return _horizontalSizing; }
169 }
170
172 [EditorBrowsable(EditorBrowsableState.Never)]
174 {
175 get { return _verticalSizing; }
176 }
177
179 [EditorBrowsable(EditorBrowsableState.Never)]
181 {
182 get { return _preferredSize; }
183 }
184
190 [EditorBrowsable(EditorBrowsableState.Never)]
191 public void Update(IList<GameObject> objects, Vector maximumSize)
192 {
193 double contentHeight = maximumSize.Y - (TopPadding + BottomPadding);
194 double preferredContentHeight = PreferredSize.Y - (TopPadding + BottomPadding);
195 double contentWidth = maximumSize.X - (LeftPadding + RightPadding);
196 double fixedScale = 1.0;
197 double expandingScale = 0.0;
198 double top = maximumSize.Y / 2 - TopPadding;
199 double availableSpaceForObjects = contentHeight - (objects.Count - 1) * Spacing;
200
201 if ((availableSpaceForObjects < _spaceRequestedByFixedSizeObjects) && (_spaceRequestedByFixedSizeObjects > 0))
202 {
203 // Not enough space for all the fixed-size objects, they must be shrinked.
204 fixedScale = availableSpaceForObjects / _spaceRequestedByFixedSizeObjects;
205
206 // No space is left for expanding-size objects.
207 expandingScale = 0;
208 }
209 else if ((maximumSize.Y < PreferredSize.Y) && (_spaceRequestedByExpandingObjects > 0))
210 {
211 // Not as much space as was requested.
212
213 fixedScale = 1;
214
215 // The expanding objects must be shrinked.
216 double availableSpaceForExpandingObjects = availableSpaceForObjects - _spaceRequestedByFixedSizeObjects;
217 expandingScale = availableSpaceForExpandingObjects / _spaceRequestedByExpandingObjects;
218 }
219 else
220 {
221 // Enough space is available.
222
223 fixedScale = 1;
224
226 {
227 // The expanding-size objects shall use up all the extra space that is available.
228 expandingScale = (availableSpaceForObjects - _spaceRequestedByFixedSizeObjects) / _spaceRequestedByExpandingObjects;
229 }
230 }
231
232 foreach (var o in objects)
233 {
234 double scale = (o.VerticalSizing == Sizing.FixedSize) ? fixedScale : expandingScale;
235 double height = o.PreferredSize.Y * scale;
236 double width = o.PreferredSize.X;
237
238 if ((o.PreferredSize.X > contentWidth) || (o.HorizontalSizing == Sizing.Expanding))
239 {
240 width = contentWidth;
241 }
242
243 o.Size = new Vector(width, height);
244 o.X = (-maximumSize.X / 2) + (LeftPadding + contentWidth / 2) + Parent.X;
245 o.Y = top - height / 2 + Parent.Y;
246
247 top -= height + Spacing;
248 }
249 }
250 }
251}
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: Appearance.cs:34
void NotifyParentAboutChangedSizingAttributes()
Should be called whenever properties that might affect layouts are changed.
Definition: Layout.cs:83
double X
Olion paikan X-koordinaatti.
double Y
Olion paikan Y-koordinaatti.
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
double LeftPadding
Vasempaan reunaan jäävä tyhjä tila.
double RightPadding
Oikeaan reunaan jäävä tyhjä tila.
void UpdateSizeHints(IList< GameObject > objects)
Päivittää tämän layoutin koon sisällön pohjalta
double Spacing
Olioiden väliin jäävä tyhjä tila.
void Update(IList< GameObject > objects, Vector maximumSize)
Päivittää olioiden koon ja sijainnin
double BottomPadding
Alareunaan jäävä tyhjä tila.
VerticalLayout()
Luo uuden asettelijan.
double _spaceRequestedByFixedSizeObjects
double TopPadding
Yläreunaan jäävä tyhjä tila.
double _spaceRequestedByExpandingObjects
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