Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Layout.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright (C) 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: Layout.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Layout Resource class implementation
13 */
14 
15 #include "MemDebug.h"
16 #include "Layout.h"
17 
18 // +--------------------------------------------------------------------+
19 
21 { }
22 
24 { }
25 
26 // +--------------------------------------------------------------------+
27 
28 bool
30 {
31  if (!panel || panel->GetChildren().size() < 1)
32  return false;
33 
34  if (cols.size() < 1 || rows.size() < 1)
35  return false;
36 
37  std::vector<DWORD> cell_x;
38  std::vector<DWORD> cell_y;
39 
40  ScaleWeights();
41  CalcCells(panel->Width(), panel->Height(), cell_x, cell_y);
42 
43  ListIter<ActiveWindow> iter = panel->GetChildren();
44  while (++iter) {
45  ActiveWindow* w = iter.value();
46  Rect c = w->GetCells();
47  Rect r;
48  Rect rp = panel->GetRect();
49 
50  if (c.x < 0) c.x = 0;
51  else if (c.x >= (int)cell_x.size()) c.x = cell_x.size() - 1;
52  if (c.y < 0) c.y = 0;
53  else if (c.y >= (int)cell_y.size()) c.y = cell_y.size() - 1;
54  if (c.x+c.w >= (int)cell_x.size()) c.w = cell_x.size() - c.x - 1;
55  if (c.y+c.h >= (int)cell_y.size()) c.h = cell_y.size() - c.y - 1;
56 
57  r.x = cell_x[c.x] + w->GetCellInsets().left;
58  r.y = cell_y[c.y] + w->GetCellInsets().top;
59  r.w = cell_x[c.x+c.w] - w->GetCellInsets().right - r.x;
60  r.h = cell_y[c.y+c.h] - w->GetCellInsets().bottom - r.y;
61 
62  r.x += panel->X();
63  r.y += panel->Y();
64 
65  if (w->GetFixedWidth() && w->GetFixedWidth() < r.w)
66  r.w = w->GetFixedWidth();
67 
68  if (w->GetFixedHeight() && w->GetFixedHeight() < r.h)
69  r.h = w->GetFixedHeight();
70 
71  if (w->GetID() == 330 || w->GetID() == 125) {
72  int y1 = r.y + r.h;
73  int y2 = rp.y + rp.h;
74  }
75 
76  if (w->GetHidePartial() && (r.x + r.w > rp.x + rp.w)) {
77  w->MoveTo(Rect(0,0,0,0));
78  }
79 
80  else if (w->GetHidePartial() && (r.y + r.h > rp.y + rp.h)) {
81  w->MoveTo(Rect(0,0,0,0));
82  }
83 
84  else {
85  w->MoveTo(r);
86  }
87  }
88 
89  return true;
90 }
91 
92 // +--------------------------------------------------------------------+
93 
94 void
96 {
97  float total = 0;
98 
99  for (auto cwi = col_weights.begin(); cwi != col_weights.end(); ++cwi)
100  total += *cwi;
101 
102  if (total > 0) {
103  for (auto cwi = col_weights.begin(); cwi != col_weights.end(); ++cwi)
104  *cwi = *cwi / total;
105  }
106 
107  total = 0;
108  for (auto rwi = row_weights.begin(); rwi != row_weights.end(); ++rwi)
109  total += *rwi;
110 
111  if (total > 0) {
112  for (auto rwi = row_weights.begin(); rwi != row_weights.end(); ++rwi)
113  *rwi = *rwi / total;
114  }
115 }
116 
117 // +--------------------------------------------------------------------+
118 
119 void
120  Layout::CalcCells(DWORD w, DWORD h, std::vector<DWORD>& cell_x, std::vector<DWORD>& cell_y)
121 {
122  DWORD x = 0;
123  DWORD y = 0;
124  DWORD min_x = 0;
125  DWORD min_y = 0;
126  DWORD ext_x = 0;
127  DWORD ext_y = 0;
128 
129  for (auto cit = cols.begin(); cit != cols.end(); ++cit)
130  min_x += *cit;
131 
132  for (auto rit = rows.begin(); rit != rows.end(); ++rit)
133  min_y += *rit;
134 
135  if (min_x < w)
136  ext_x = w - min_x;
137 
138  if (min_y < h)
139  ext_y = h - min_y;
140 
141  cell_x.push_back(x);
142  for (auto cit = cols.begin(); cit != cols.end(); ++cit) {
143  x += *cit + (DWORD) (ext_x * col_weights[cit - cols.begin()]);
144  cell_x.push_back(x);
145  }
146 
147  cell_y.push_back(y);
148  for (auto rit = rows.begin(); rit != rows.end(); ++rit) {
149  y += *rit + (DWORD) (ext_y * row_weights[rit - rows.begin()]);
150  cell_y.push_back(y);
151  }
152 }
153 
154 // +--------------------------------------------------------------------+
155 
156 void
158 {
159  cols.clear();
160  rows.clear();
161 
162  col_weights.clear();
163  row_weights.clear();
164 }
165 
166 void
167 Layout::AddCol(DWORD min_width, float col_factor)
168 {
169  cols.push_back(min_width);
170  col_weights.push_back(col_factor);
171 }
172 
173 void
174 Layout::AddRow(DWORD min_height, float row_factor)
175 {
176  rows.push_back(min_height);
177  row_weights.push_back(row_factor);
178 }
179 
180 void
181 Layout::SetConstraints(const std::vector<DWORD>& min_x,
182 const std::vector<DWORD>& min_y,
183 const std::vector<float>& weight_x,
184 const std::vector<float>& weight_y)
185 {
186  Clear();
187 
188  if (min_x.size() == weight_x.size() && min_y.size() == weight_y.size()) {
189  for (auto iter = min_x.begin(); iter != min_x.end(); ++iter)
190  cols.push_back(*iter);
191 
192  for (auto iter = min_y.begin(); iter != min_y.end(); ++iter)
193  rows.push_back(*iter);
194 
195  for (auto iter = weight_x.begin(); iter != weight_x.end(); ++iter)
196  col_weights.push_back(*iter);
197 
198  for (auto iter = weight_y.begin(); iter != weight_y.end(); ++iter)
199  row_weights.push_back(*iter);
200  }
201 }
202 
203 void
204 Layout::SetConstraints(const std::vector<float>& min_x,
205 const std::vector<float>& min_y,
206 const std::vector<float>& weight_x,
207 const std::vector<float>& weight_y)
208 {
209  Clear();
210 
211  if (min_x.size() == weight_x.size() &&
212  min_y.size() == weight_y.size()) {
213 
214  for (auto iter = min_x.begin(); iter != min_x.begin(); ++iter)
215  cols.push_back((DWORD) *iter);
216 
217  for (auto iter = min_y.begin(); iter != min_y.begin(); ++iter)
218  rows.push_back((DWORD) *iter);
219 
220  for (auto iter = weight_x.begin(); iter != weight_x.end(); ++iter)
221  col_weights.push_back(*iter);
222 
223  for (auto iter = weight_y.begin(); iter != weight_y.end(); ++iter)
224  row_weights.push_back(*iter);
225  }
226 }
227 
228 void
230 int nrows,
231 const int* min_x,
232 const int* min_y,
233 const float* weight_x,
234 const float* weight_y)
235 {
236  Clear();
237 
238  if (nrows > 0 && ncols > 0) {
239  int i = 0;
240 
241  for (i = 0; i < ncols; i++) {
242  cols.push_back(min_x[i]);
243  col_weights.push_back(weight_x[i]);
244  }
245 
246  for (i = 0; i < nrows; i++) {
247  rows.push_back(min_y[i]);
248  row_weights.push_back(weight_y[i]);
249  }
250  }
251 }