Jypeli 10
The simple game programming library
HorizontalLayout.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{
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
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public GameObject Parent { get; set; }
56
60 public double Spacing
61 {
62 get { return _spacing; }
63 set { _spacing = value; NotifyParent(); }
64 }
65
69 public double TopPadding
70 {
71 get { return _topPadding; }
72 set { _topPadding = value; NotifyParent(); }
73 }
74
78 public double BottomPadding
79 {
80 get { return _bottomPadding; }
81 set { _bottomPadding = value; NotifyParent(); }
82 }
83
87 public double LeftPadding
88 {
89 get { return _leftPadding; }
90 set { _leftPadding = value; NotifyParent(); }
91 }
92
96 public double RightPadding
97 {
98 get { return _rightPadding; }
99 set { _rightPadding = value; NotifyParent(); }
100 }
101
102 private void NotifyParent()
103 {
104 if (Parent != null)
105 {
107 }
108 }
109
114 {
115 }
116
121 [EditorBrowsable(EditorBrowsableState.Never)]
122 public void UpdateSizeHints(IList<GameObject> objects)
123 {
124 if (objects.Count == 0)
125 return;
126
127 double maxHeight = 0;
128 double widthOfExpandingObjects = 0;
129 double widthOfFixedSizeObjects = 0;
130 Sizing horizontalSizing = Sizing.FixedSize;
131 Sizing verticalSizing = Sizing.FixedSize;
132
133 foreach (var o in objects)
134 {
135 if (o.PreferredSize.Y > maxHeight)
136 {
137 maxHeight = o.PreferredSize.Y;
138 }
139
140 if (o.HorizontalSizing == Sizing.Expanding)
141 {
142 widthOfExpandingObjects += o.PreferredSize.X;
143 horizontalSizing = Sizing.Expanding;
144 }
145 else if (o.HorizontalSizing == Sizing.FixedSize)
146 {
147 widthOfFixedSizeObjects += o.PreferredSize.X;
148 }
149
150 if (o.VerticalSizing != Sizing.FixedSize)
151 {
152 verticalSizing = Sizing.Expanding;
153 }
154 }
155
156 double preferredWidth = LeftPadding + widthOfExpandingObjects + widthOfFixedSizeObjects + ((objects.Count - 1) * Spacing) + RightPadding;
157 double preferredHeight = TopPadding + maxHeight + BottomPadding;
158
159 _horizontalSizing = horizontalSizing;
160 _verticalSizing = verticalSizing;
161 _spaceRequestedByExpandingObjects = widthOfExpandingObjects;
162 _spaceRequestedByFixedSizeObjects = widthOfFixedSizeObjects;
163 _preferredSize = new Vector(preferredWidth, preferredHeight);
164 }
165
169 [EditorBrowsable(EditorBrowsableState.Never)]
171 {
172 get { return _horizontalSizing; }
173 }
174
178 [EditorBrowsable(EditorBrowsableState.Never)]
180 {
181 get { return _verticalSizing; }
182 }
183
187 [EditorBrowsable(EditorBrowsableState.Never)]
189 {
190 get { return _preferredSize; }
191 }
192
198 [EditorBrowsable(EditorBrowsableState.Never)]
199 public void Update(IList<GameObject> objects, Vector maximumSize)
200 {
201 double contentWidth = maximumSize.X - (LeftPadding + RightPadding);
202 double preferredContentWidth = PreferredSize.X - (LeftPadding + RightPadding);
203 double contentHeight = maximumSize.Y - (TopPadding + BottomPadding);
204 double fixedScale = 1.0;
205 double expandingScale = 0.0;
206 double left = -maximumSize.X / 2 + LeftPadding;
207 double availableSpaceForObjects = contentWidth - (objects.Count - 1) * Spacing;
208
209 if ((availableSpaceForObjects < _spaceRequestedByFixedSizeObjects) && (_spaceRequestedByFixedSizeObjects > 0.0))
210 {
211 // Not enough space for all the fixed-size objects, they must be shrinked.
212 fixedScale = availableSpaceForObjects / _spaceRequestedByFixedSizeObjects;
213
214 // No space is left for expanding-size objects.
215 expandingScale = 0;
216 }
217 else if ((maximumSize.X < PreferredSize.X) && (_spaceRequestedByExpandingObjects > 0.0))
218 {
219 // Not as much space as was requested.
220
221 fixedScale = 1;
222
223 // The expanding objects must be shrinked.
224 double availableSpaceForExpandingObjects = availableSpaceForObjects - _spaceRequestedByFixedSizeObjects;
225 expandingScale = availableSpaceForExpandingObjects / _spaceRequestedByExpandingObjects;
226 }
227 else
228 {
229 // Enough space is available.
230
231 fixedScale = 1;
232
234 {
235 // The expanding-size objects shall use up all the extra space that is available.
236 expandingScale = (availableSpaceForObjects - _spaceRequestedByFixedSizeObjects) / _spaceRequestedByExpandingObjects;
237 }
238 }
239
240 foreach (var o in objects)
241 {
242 double scale = (o.HorizontalSizing == Sizing.FixedSize) ? fixedScale : expandingScale;
243 double width = o.PreferredSize.X * scale;
244 double height = o.PreferredSize.Y;
245
246 if ((o.PreferredSize.Y > contentHeight) || (o.VerticalSizing == Sizing.Expanding))
247 {
248 height = contentHeight;
249 }
250
251 o.Size = new Vector(width, height);
252 o.X = left + width / 2 + Parent.X;
253 o.Y = (maximumSize.Y / 2) - (TopPadding + contentHeight / 2) + Parent.Y;
254
255 left += width + Spacing;
256 }
257 }
258 }
259}
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 riviin vaakasuunnassa.
Vector PreferredSize
Olion suosima koko
double LeftPadding
Vasempaan reunaan jäävä tyhjä tila.
double RightPadding
Oikeaan reunaan jäävä tyhjä tila.
double Spacing
Olioiden väliin jäävä tyhjä tila.
HorizontalLayout()
Luo uuden asettelijan.
void Update(IList< GameObject > objects, Vector maximumSize)
Päivittää olioiden koon ja sijainnin
double TopPadding
Yläreunaan jäävä tyhjä tila.
void UpdateSizeHints(IList< GameObject > objects)
Päivittää kappaleiden kokovihjeet
Sizing VerticalSizing
Pystysuuntainen koon asettelija
double BottomPadding
Alareunaan jäävä tyhjä tila.
Sizing HorizontalSizing
Leveyssuuntainen koon asettelija
GameObject Parent
Asettelijan omistaja
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