Jypeli  5
The simple game programming library
RowLayout.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using Jypeli;
5 
6 namespace Jypeli
7 {
13  public class RowLayout : ILayout
14  {
15  private Vector _preferredSize;
16  private double _targetWidth = 400;
17  private double _horizontalSpacing = 0;
18  private double _verticalSpacing = 0;
19  private double _topPadding = 0;
20  private double _bottomPadding = 0;
21  private double _leftPadding = 0;
22  private double _rightPadding = 0;
23 
24  [EditorBrowsable( EditorBrowsableState.Never )]
25  public GameObject Parent { get; set; }
26 
30  public double HorizontalSpacing
31  {
32  get { return _horizontalSpacing; }
33  set { _horizontalSpacing = value; NotifyParent(); }
34  }
35 
36  public double VerticalSpacing
37  {
38  get { return _verticalSpacing; }
39  set { _verticalSpacing = value; NotifyParent(); }
40  }
41 
45  public double TopPadding
46  {
47  get { return _topPadding; }
48  set { _topPadding = value; NotifyParent(); }
49  }
50 
54  public double BottomPadding
55  {
56  get { return _bottomPadding; }
57  set { _bottomPadding = value; NotifyParent(); }
58  }
59 
63  public double LeftPadding
64  {
65  get { return _leftPadding; }
66  set { _leftPadding = value; NotifyParent(); }
67  }
68 
72  public double RightPadding
73  {
74  get { return _rightPadding; }
75  set { _rightPadding = value; NotifyParent(); }
76  }
77 
81  public double TargetWidth
82  {
83  get { return _targetWidth; }
84  set { _targetWidth = value; NotifyParent(); }
85  }
86 
87 
88  private void NotifyParent()
89  {
90  if (Parent != null)
91  {
93  }
94  }
95 
96  [EditorBrowsable(EditorBrowsableState.Never)]
97  public void UpdateSizeHints( IList<GameObject> objects )
98  {
99  if ( objects.Count == 0 )
100  return;
101 
102  double targetContentWidth = TargetWidth - ( LeftPadding + RightPadding );
103  int i = 0;
104  int objectsInRow = 0;
105  double left = 0;
106  double totalHeight = 0;
107  double rowMaxHeight = 0;
108  double maxRowWidth = 0;
109  bool atFirstColumn = true;
110 
111  while ( i < objects.Count )
112  {
113  var o = objects[i];
114  bool rowIsTooWide = ( left + HorizontalSpacing + o.PreferredSize.X ) > targetContentWidth;
115 
116  if ( ( objectsInRow >= 1 ) && rowIsTooWide )
117  {
118  if ( left > maxRowWidth )
119  maxRowWidth = left;
120 
121  totalHeight += rowMaxHeight + VerticalSpacing;
122 
123  left = 0;
124  objectsInRow = 0;
125  rowMaxHeight = 0;
126  atFirstColumn = true;
127  continue;
128  }
129 
130  if ( o.PreferredSize.Y > rowMaxHeight ) rowMaxHeight = o.PreferredSize.Y;
131 
132  if ( !atFirstColumn ) left += HorizontalSpacing;
133  atFirstColumn = false;
134  left += o.PreferredSize.X;
135  objectsInRow++;
136  i++;
137  }
138 
139  if ( left > maxRowWidth ) maxRowWidth = left;
140  totalHeight += rowMaxHeight;
141 
142  double preferredWidth = LeftPadding + maxRowWidth + RightPadding;
143  double preferredHeight = TopPadding + totalHeight + BottomPadding;
144 
145  _preferredSize = new Vector( preferredWidth, preferredHeight );
146  }
147 
148  [EditorBrowsable( EditorBrowsableState.Never )]
149  public Sizing HorizontalSizing
150  {
151  get { return Sizing.FixedSize; }
152  }
153 
154  [EditorBrowsable( EditorBrowsableState.Never )]
155  public Sizing VerticalSizing
156  {
157  get { return Sizing.FixedSize; }
158  }
159 
160  [EditorBrowsable( EditorBrowsableState.Never )]
161  public Vector PreferredSize
162  {
163  get { return _preferredSize; }
164  }
165 
166 
167  [EditorBrowsable( EditorBrowsableState.Never )]
168  public void Update( IList<GameObject> objects, Vector maximumSize )
169  {
170  double rightLimit = maximumSize.X / 2 - RightPadding;
171  double leftLimit = -maximumSize.X / 2 + LeftPadding;
172 
173  int objectsInRow = 0;
174  double rowMaxHeight = 0;
175  double top = maximumSize.Y / 2 - TopPadding;
176  double left = leftLimit;
177  bool atFirstColumn = true;
178  int i = 0;
179 
180  while ( i < objects.Count )
181  {
182  var o = objects[i];
183  bool rowIsTooWide = ( left + HorizontalSpacing + o.PreferredSize.X ) > rightLimit;
184 
185  if ( ( objectsInRow >= 1 ) && rowIsTooWide )
186  {
187  left = leftLimit;
188  top -= rowMaxHeight + VerticalSpacing;
189  objectsInRow = 0;
190  rowMaxHeight = 0;
191  atFirstColumn = true;
192  continue;
193  }
194 
195  if ( !atFirstColumn ) left += HorizontalSpacing;
196  atFirstColumn = false;
197 
198  o.Size = o.PreferredSize;
199  o.X = left + o.Width / 2;
200  o.Y = top - o.Height / 2;
201 
202  if ( o.PreferredSize.Y > rowMaxHeight ) rowMaxHeight = o.PreferredSize.Y;
203 
204  left += o.Width;
205  objectsInRow++;
206  i++;
207  }
208  }
209  }
210 }
double TargetWidth
Kuinka leveitä rivien tulisi korkeintaan olla.
Definition: RowLayout.cs:82
Sizing VerticalSizing
Definition: RowLayout.cs:156
Rajapinta asettelijalle. Asettelija asettelee widgetin lapsioliot siten, että ne mahtuvat widgetin si...
Definition: ILayout.cs:83
double TopPadding
Yläreunaan jäävä tyhjä tila.
Definition: RowLayout.cs:46
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:38
double LeftPadding
Vasempaan reunaan jäävä tyhjä tila.
Definition: RowLayout.cs:64
double HorizontalSpacing
Olioiden väliin vaakasuunnassa jäävä tyhjä tila.
Definition: RowLayout.cs:31
double Y
Definition: Vector.cs:275
double BottomPadding
Alareunaan jäävä tyhjä tila.
Definition: RowLayout.cs:55
double VerticalSpacing
Definition: RowLayout.cs:37
void Update(IList< GameObject > objects, Vector maximumSize)
Definition: RowLayout.cs:168
double X
Definition: Vector.cs:274
void NotifyParentAboutChangedSizingAttributes()
Should be called whenever properties that might affect layouts are changed.
Definition: Layout.cs:86
Vector PreferredSize
Definition: RowLayout.cs:162
void UpdateSizeHints(IList< GameObject > objects)
Definition: RowLayout.cs:97
Sizing HorizontalSizing
Definition: RowLayout.cs:150
Asettelee widgetit riveihin. TargetWidth määrittää kuinka leveä yhden rivin tulisi olla...
Definition: RowLayout.cs:13
2D-vektori.
Definition: Vector.cs:56
double RightPadding
Oikeaan reunaan jäävä tyhjä tila.
Definition: RowLayout.cs:73
GameObject Parent
Definition: RowLayout.cs:25
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: __GameObject.cs:54