source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/GameObjects/VerticalLayout.cs @ 4507

Revision 4507, 8.5 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
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{
35    /// <summary>
36    /// Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
37    /// </summary>
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
54        /// <summary>
55        /// Olioiden väliin jäävä tyhjä tila.
56        /// </summary>
57        public double Spacing
58        {
59            get { return _spacing; }
60            set { _spacing = value; NotifyParent(); }
61        }
62
63        /// <summary>
64        /// Yläreunaan jäävä tyhjä tila.
65        /// </summary>
66        public double TopPadding
67        {
68            get { return _topPadding; }
69            set { _topPadding = value; NotifyParent(); }
70        }
71
72        /// <summary>
73        /// Alareunaan jäävä tyhjä tila.
74        /// </summary>
75        public double BottomPadding
76        {
77            get { return _bottomPadding; }
78            set { _bottomPadding = value; NotifyParent(); }
79        }
80
81        /// <summary>
82        /// Vasempaan reunaan jäävä tyhjä tila.
83        /// </summary>
84        public double LeftPadding
85        {
86            get { return _leftPadding; }
87            set { _leftPadding = value; NotifyParent(); }
88        }
89
90        /// <summary>
91        /// Oikeaan reunaan jäävä tyhjä tila.
92        /// </summary>
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            {
103                Parent.NotifyParentAboutChangedSizingAttributes();
104            }
105        }
106
107        /// <summary>
108        /// Luo uuden asettelijan.
109        /// </summary>
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}
Note: See TracBrowser for help on using the repository browser.