Jypeli  5
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 
30 using System.Collections.Generic;
31 using System.ComponentModel;
32 
33 namespace Jypeli
34 {
38  public class VerticalLayout : ILayout
39  {
40  private Sizing _horizontalSizing;
41  private Sizing _verticalSizing;
42  private Vector _preferredSize;
43  private double _spaceRequestedByExpandingObjects;
44  private double _spaceRequestedByFixedSizeObjects;
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 
51  [EditorBrowsable(EditorBrowsableState.Never)]
52  public GameObject Parent { get; set; }
53 
57  public double Spacing
58  {
59  get { return _spacing; }
60  set { _spacing = value; NotifyParent(); }
61  }
62 
66  public double TopPadding
67  {
68  get { return _topPadding; }
69  set { _topPadding = value; NotifyParent(); }
70  }
71 
75  public double BottomPadding
76  {
77  get { return _bottomPadding; }
78  set { _bottomPadding = value; NotifyParent(); }
79  }
80 
84  public double LeftPadding
85  {
86  get { return _leftPadding; }
87  set { _leftPadding = value; NotifyParent(); }
88  }
89 
93  public double RightPadding
94  {
95  get { return _rightPadding; }
96  set { _rightPadding = value; NotifyParent(); }
97  }
98 
99  private void NotifyParent()
100  {
101  if (Parent != null)
102  {
104  }
105  }
106 
110  public VerticalLayout()
111  {
112  }
113 
114  [EditorBrowsable(EditorBrowsableState.Never)]
115  public void UpdateSizeHints(IList<GameObject> objects)
116  {
117  if (objects.Count == 0)
118  return;
119 
120  double maxWidth = 0;
121  double heightOfExpandingObjects = 0;
122  double heightOfFixedSizeObjects = 0;
123  Sizing horizontalSizing = Sizing.FixedSize;
124  Sizing verticalSizing = Sizing.FixedSize;
125 
126  foreach (var o in objects)
127  {
128  if (o.PreferredSize.X > maxWidth)
129  {
130  maxWidth = o.PreferredSize.X;
131  }
132 
133  if (o.VerticalSizing == Sizing.Expanding)
134  {
135  verticalSizing = Sizing.Expanding;
136  heightOfExpandingObjects += o.PreferredSize.Y;
137  }
138  else if (o.VerticalSizing == Sizing.FixedSize)
139  {
140  heightOfFixedSizeObjects += o.PreferredSize.Y;
141  }
142 
143  if (o.HorizontalSizing != Sizing.FixedSize)
144  {
145  horizontalSizing = Sizing.Expanding;
146  }
147  }
148 
149  double preferredHeight = TopPadding + heightOfExpandingObjects + heightOfFixedSizeObjects + ((objects.Count - 1) * Spacing) + BottomPadding;
150  double preferredWidth = LeftPadding + maxWidth + RightPadding;
151 
152  _horizontalSizing = horizontalSizing;
153  _verticalSizing = verticalSizing;
154  _spaceRequestedByExpandingObjects = heightOfExpandingObjects;
155  _spaceRequestedByFixedSizeObjects = heightOfFixedSizeObjects;
156  _preferredSize = new Vector(preferredWidth, preferredHeight);
157  }
158 
159  [EditorBrowsable(EditorBrowsableState.Never)]
160  public Sizing HorizontalSizing
161  {
162  get { return _horizontalSizing; }
163  }
164 
165  [EditorBrowsable(EditorBrowsableState.Never)]
166  public Sizing VerticalSizing
167  {
168  get { return _verticalSizing; }
169  }
170 
171  [EditorBrowsable(EditorBrowsableState.Never)]
172  public Vector PreferredSize
173  {
174  get { return _preferredSize; }
175  }
176 
177  [EditorBrowsable(EditorBrowsableState.Never)]
178  public void Update(IList<GameObject> objects, Vector maximumSize)
179  {
180  double contentHeight = maximumSize.Y - (TopPadding + BottomPadding);
181  double preferredContentHeight = PreferredSize.Y - (TopPadding + BottomPadding);
182  double contentWidth = maximumSize.X - (LeftPadding + RightPadding);
183  double fixedScale = 1.0;
184  double expandingScale = 0.0;
185  double top = maximumSize.Y / 2 - TopPadding;
186  double availableSpaceForObjects = contentHeight - (objects.Count - 1) * Spacing;
187 
188  if ((availableSpaceForObjects < _spaceRequestedByFixedSizeObjects) && (_spaceRequestedByFixedSizeObjects > 0))
189  {
190  // Not enough space for all the fixed-size objects, they must be shrinked.
191  fixedScale = availableSpaceForObjects / _spaceRequestedByFixedSizeObjects;
192 
193  // No space is left for expanding-size objects.
194  expandingScale = 0;
195  }
196  else if ((maximumSize.Y < PreferredSize.Y) && (_spaceRequestedByExpandingObjects > 0))
197  {
198  // Not as much space as was requested.
199 
200  fixedScale = 1;
201 
202  // The expanding objects must be shrinked.
203  double availableSpaceForExpandingObjects = availableSpaceForObjects - _spaceRequestedByFixedSizeObjects;
204  expandingScale = availableSpaceForExpandingObjects / _spaceRequestedByExpandingObjects;
205  }
206  else
207  {
208  // Enough space is available.
209 
210  fixedScale = 1;
211 
212  if (_spaceRequestedByExpandingObjects > 0)
213  {
214  // The expanding-size objects shall use up all the extra space that is available.
215  expandingScale = (availableSpaceForObjects - _spaceRequestedByFixedSizeObjects) / _spaceRequestedByExpandingObjects;
216  }
217  }
218 
219  foreach (var o in objects)
220  {
221  double scale = (o.VerticalSizing == Sizing.FixedSize) ? fixedScale : expandingScale;
222  double height = o.PreferredSize.Y * scale;
223  double width = o.PreferredSize.X;
224 
225  if ((o.PreferredSize.X > contentWidth) || (o.HorizontalSizing == Sizing.Expanding))
226  {
227  width = contentWidth;
228  }
229 
230  o.Size = new Vector(width, height);
231  o.X = (-maximumSize.X / 2) + (LeftPadding + contentWidth / 2);
232  o.Y = top - height / 2;
233 
234  top -= height + Spacing;
235  }
236  }
237  }
238 }
double Spacing
Olioiden väliin jäävä tyhjä tila.
Rajapinta asettelijalle. Asettelija asettelee widgetin lapsioliot siten, että ne mahtuvat widgetin si...
Definition: ILayout.cs:83
double BottomPadding
Alareunaan jäävä tyhjä tila.
void Update(IList< GameObject > objects, Vector maximumSize)
double TopPadding
Yläreunaan jäävä tyhjä tila.
VerticalLayout()
Luo uuden asettelijan.
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:38
double RightPadding
Oikeaan reunaan jäävä tyhjä tila.
double Y
Definition: Vector.cs:275
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
double X
Definition: Vector.cs:274
void NotifyParentAboutChangedSizingAttributes()
Should be called whenever properties that might affect layouts are changed.
Definition: Layout.cs:86
double LeftPadding
Vasempaan reunaan jäävä tyhjä tila.
2D-vektori.
Definition: Vector.cs:56
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: __GameObject.cs:54
void UpdateSizeHints(IList< GameObject > objects)