ManaPlus
Public Member Functions | Private Member Functions | Private Attributes | Friends
LayoutArray Class Reference

#include <layoutarray.h>

Public Member Functions

 LayoutArray ()
 
 ~LayoutArray ()
 
LayoutCellat (const int x, const int y, const int w, const int h)
 
LayoutCellplace (Widget *const widget, const int x, const int y, const int w, const int h)
 
void setColWidth (const int n, const int w)
 
void setRowHeight (const int n, const int h)
 
void matchColWidth (const int n1, const int n2)
 
void extend (const int x, const int y, const int w, const int h)
 
void reflow (const int nX, const int nY, const int nW, const int nH)
 

Private Member Functions

 LayoutArray (LayoutArray const &)
 
LayoutArrayoperator= (LayoutArray const &)
 
void align (int &pos, int &size, const int dim, LayoutCell const &cell, const int *const sizes, const int sizeCount) const
 
void resizeGrid (int w, const int h)
 
std::vector< int > getSizes (const int dim, int upp) const
 
int getSize (const int dim) const
 

Private Attributes

std::vector< int > mSizes [2]
 
std::vector< std::vector< LayoutCell * > > mCells
 
int mSpacing
 

Friends

class LayoutCell
 

Detailed Description

This class contains a rectangular array of cells.

Definition at line 37 of file layoutarray.h.

Constructor & Destructor Documentation

◆ LayoutArray() [1/2]

LayoutArray::LayoutArray ( )

Definition at line 35 of file layoutarray.cpp.

35  :
36  mCells(),
37  mSpacing(4)
38 {
39 }
std::vector< std::vector< LayoutCell * > > mCells
Definition: layoutarray.h:128

◆ ~LayoutArray()

LayoutArray::~LayoutArray ( )

Definition at line 41 of file layoutarray.cpp.

42 {
43  STD_VECTOR <STD_VECTOR <LayoutCell *> >::iterator
44  i = mCells.begin();
45  const STD_VECTOR <STD_VECTOR <LayoutCell *> >::iterator
46  i_end = mCells.end();
47  while (i != i_end)
48  {
49  STD_VECTOR< LayoutCell * >::iterator j = i->begin();
50  const STD_VECTOR< LayoutCell * >::iterator j_end = i->end();
51  while (j != j_end)
52  {
53  delete *j;
54  ++j;
55  }
56  ++i;
57  }
58 }

References mCells.

◆ LayoutArray() [2/2]

LayoutArray::LayoutArray ( LayoutArray const &  )
explicitprivate

Member Function Documentation

◆ align()

void LayoutArray::align ( int &  pos,
int &  size,
const int  dim,
LayoutCell const &  cell,
const int *const  sizes,
const int  sizeCount 
) const
private

Gets the position and size of a widget along a given axis

Definition at line 161 of file layoutarray.cpp.

165 {
166  if (dim < 0 || dim >= 2)
167  return;
168  int size_max = sizes[0];
169  int cnt = cell.mExtent[dim];
170  if ((sizeCount != 0) && cell.mExtent[dim] > sizeCount)
171  cnt = sizeCount;
172 
173  for (int i = 1; i < cnt; ++i)
174  size_max += sizes[i] + mSpacing;
175  size = std::min<int>(cell.mSize[dim], size_max);
176 
177  switch (cell.mAlign[dim])
178  {
179  case LayoutCell::LEFT:
180  return;
181  case LayoutCell::RIGHT:
182  pos += size_max - size;
183  return;
184  case LayoutCell::CENTER:
185  pos += (size_max - size) / 2;
186  return;
187  case LayoutCell::FILL:
188  size = size_max;
189  return;
190  default:
191  logger->log1("LayoutArray::align unknown layout");
192  return;
193  }
194 }
void log1(const char *const log_text)
Definition: logger.cpp:238
Logger * logger
Definition: logger.cpp:89
int size()
Definition: emotedb.cpp:306

References LayoutCell::CENTER, LayoutCell::FILL, LayoutCell::LEFT, Logger::log1(), logger, mSpacing, LayoutCell::RIGHT, and EmoteDB::size().

Referenced by reflow().

◆ at()

LayoutCell & LayoutArray::at ( const int  x,
const int  y,
const int  w,
const int  h 
)

Returns a reference on the cell at given position.

Definition at line 60 of file layoutarray.cpp.

62 {
63  resizeGrid(x + w, y + h);
64  LayoutCell *&cell = mCells[CAST_SIZE(y)][static_cast<size_t>(x)];
65  if (cell == nullptr)
66  cell = new LayoutCell;
67  return *cell;
68 }
#define CAST_SIZE
Definition: cast.h:34
void resizeGrid(int w, const int h)
Definition: layoutarray.cpp:70
friend class LayoutCell
Definition: layoutarray.h:39

References CAST_SIZE, LayoutCell, mCells, resizeGrid(), x, and y.

Referenced by LayoutCell::at(), extend(), and place().

◆ extend()

void LayoutArray::extend ( const int  x,
const int  y,
const int  w,
const int  h 
)

Spawns a cell over several columns/rows.

Definition at line 122 of file layoutarray.cpp.

123 {
124  LayoutCell &cell = at(x, y, w, h);
125  cell.mExtent[0] = w;
126  cell.mExtent[1] = h;
127 }
LayoutCell & at(const int x, const int y, const int w, const int h)
Definition: layoutarray.cpp:60
int mExtent[2]
Definition: layoutcell.h:199

References at(), LayoutCell::mExtent, x, and y.

Referenced by LayoutCell::extend().

◆ getSize()

int LayoutArray::getSize ( const int  dim) const
private

Gets the total size along a given axis.

Definition at line 268 of file layoutarray.cpp.

269 {
270  STD_VECTOR<int> sizes = getSizes(dim, LayoutType::DEF);
271  int size = 0;
272  const int nb = CAST_S32(sizes.size());
273  for (int i = 0; i < nb; ++i)
274  {
275  if (sizes[CAST_SIZE(i)] > LayoutType::DEF)
276  size += sizes[CAST_SIZE(i)];
277  size += mSpacing;
278  }
279  return size - mSpacing;
280 }
#define CAST_S32
Definition: cast.h:30
std::vector< int > getSizes(const int dim, int upp) const

References CAST_S32, CAST_SIZE, LayoutType::DEF, getSizes(), mSpacing, and EmoteDB::size().

Referenced by LayoutCell::computeSizes().

◆ getSizes()

std::vector< int > LayoutArray::getSizes ( const int  dim,
int  upp 
) const
private

Gets the column/row sizes along a given axis.

Parameters
upptarget size for the array. Ignored if AUTO_DEF.

Definition at line 196 of file layoutarray.cpp.

197 {
198  if (dim < 0 || dim >= 2)
199  return mSizes[1];
200 
201  const int gridW = CAST_S32(mSizes[0].size());
202  const int gridH = CAST_S32(mSizes[1].size());
203  STD_VECTOR<int> sizes = mSizes[dim];
204 
205  // Compute minimum sizes.
206  for (int gridY = 0; gridY < gridH; ++gridY)
207  {
208  for (int gridX = 0; gridX < gridW; ++gridX)
209  {
210  const LayoutCell *const cell = mCells[CAST_SIZE(gridY)]
211  [CAST_SIZE(gridX)];
212  if ((cell == nullptr) || cell->mType == LayoutCell::NONE)
213  continue;
214 
215  if (cell->mExtent[dim] == 1)
216  {
217  const int n = (dim == 0 ? gridX : gridY);
218  const int s = cell->mSize[dim] + cell->mVPadding * 2;
219  if (s > sizes[CAST_SIZE(n)])
220  sizes[CAST_SIZE(n)] = s;
221  }
222  }
223  }
224 
225  if (upp == LayoutType::DEF)
226  return sizes;
227 
228  // Compute the FILL sizes.
229  const int nb = CAST_S32(sizes.size());
230  int nbFill = 0;
231  for (int i = 0; i < nb; ++i)
232  {
233  if (mSizes[CAST_SIZE(dim)][static_cast<size_t>(i)]
234  <= LayoutType::DEF)
235  {
236  ++nbFill;
237  if (mSizes[CAST_SIZE(dim)][static_cast<size_t>(i)] ==
238  LayoutType::SET ||
239  sizes[CAST_SIZE(i)] <= LayoutType::DEF)
240  {
241  sizes[CAST_SIZE(i)] = 0;
242  }
243  }
244  upp -= sizes[CAST_SIZE(i)] + mSpacing;
245  }
246  upp = upp + mSpacing;
247 
248  if (nbFill == 0)
249  return sizes;
250 
251  for (int i = 0; i < nb; ++i)
252  {
253  if (mSizes[CAST_SIZE(dim)][static_cast<size_t>(i)] >
255  {
256  continue;
257  }
258 
259  const int s = upp / nbFill;
260  sizes[CAST_SIZE(i)] += s;
261  upp -= s;
262  --nbFill;
263  }
264 
265  return sizes;
266 }
std::vector< int > mSizes[2]
Definition: layoutarray.h:127
int mSize[2]
Definition: layoutcell.h:196
int mVPadding
Definition: layoutcell.h:198

References CAST_S32, CAST_SIZE, LayoutType::DEF, mCells, LayoutCell::mExtent, LayoutCell::mSize, mSizes, mSpacing, LayoutCell::mType, LayoutCell::mVPadding, LayoutCell::NONE, LayoutType::SET, and EmoteDB::size().

Referenced by getSize(), matchColWidth(), and reflow().

◆ matchColWidth()

void LayoutArray::matchColWidth ( const int  n1,
const int  n2 
)

Sets the widths of two columns to the maximum of their widths.

Definition at line 112 of file layoutarray.cpp.

113 {
114  resizeGrid(std::max(n1, n2) + 1, 0);
115  const STD_VECTOR<int> widths = getSizes(0, LayoutType::DEF);
116  const int s = std::max(widths[CAST_SIZE(n1)],
117  widths[CAST_SIZE(n2)]);
118  mSizes[0][CAST_SIZE(n1)] = s;
119  mSizes[0][CAST_SIZE(n2)] = s;
120 }

References CAST_SIZE, LayoutType::DEF, getSizes(), mSizes, and resizeGrid().

Referenced by LayoutCell::matchColWidth().

◆ operator=()

LayoutArray& LayoutArray::operator= ( LayoutArray const &  )
private

◆ place()

LayoutCell & LayoutArray::place ( Widget *const  widget,
const int  x,
const int  y,
const int  w,
const int  h 
)

Places a widget in a given cell.

Parameters
wnumber of columns the widget spawns.
hnumber of rows the widget spawns.
Note
When w is 1, the width of column x is reset to zero if it was AUTO_DEF. Similarly for h.

Definition at line 129 of file layoutarray.cpp.

131 {
132  LayoutCell &cell = at(x, y, w, h);
133  assert(cell.mType == LayoutCell::NONE);
134  cell.mType = LayoutCell::WIDGET;
135  cell.mWidget = widget;
136  if (widget != nullptr)
137  {
138  cell.mSize[0] = w == 1 ? widget->getWidth() : 0;
139  cell.mSize[1] = h == 1 ? widget->getHeight() : 0;
140  }
141  else
142  {
143  cell.mSize[0] = 1;
144  cell.mSize[1] = 1;
145  }
146  cell.mExtent[0] = w;
147  cell.mExtent[1] = h;
148  cell.mHPadding = 0;
149  cell.mVPadding = 0;
150  cell.mAlign[0] = LayoutCell::FILL;
151  cell.mAlign[1] = LayoutCell::FILL;
152  int &cs = mSizes[0][CAST_SIZE(x)];
153  int &rs = mSizes[1][CAST_SIZE(y)];
154  if (cs == LayoutType::DEF && w == 1)
155  cs = 0;
156  if (rs == LayoutType::DEF && h == 1)
157  rs = 0;
158  return cell;
159 }
Alignment mAlign[2]
Definition: layoutcell.h:200
int mHPadding
Definition: layoutcell.h:197
Widget * mWidget
Definition: layoutcell.h:181
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221

References at(), CAST_SIZE, LayoutType::DEF, LayoutCell::FILL, Widget::getHeight(), Widget::getWidth(), LayoutCell::mAlign, LayoutCell::mExtent, LayoutCell::mHPadding, LayoutCell::mSize, mSizes, LayoutCell::mType, LayoutCell::mVPadding, LayoutCell::mWidget, LayoutCell::NONE, LayoutCell::WIDGET, x, and y.

Referenced by LayoutCell::place().

◆ reflow()

void LayoutArray::reflow ( const int  nX,
const int  nY,
const int  nW,
const int  nH 
)

Computes and sets the positions of all the widgets.

Parameters
nWwidth of the array, used to resize the AUTO_ columns.
nHheight of the array, used to resize the AUTO_ rows.

Definition at line 282 of file layoutarray.cpp.

284 {
285  const int gridW = CAST_S32(mSizes[0].size());
286  const int gridH = CAST_S32(mSizes[1].size());
287 
288  STD_VECTOR<int> widths = getSizes(0, nw);
289  STD_VECTOR<int> heights = getSizes(1, nh);
290 
291  const int szW = CAST_S32(widths.size());
292  const int szH = CAST_S32(heights.size());
293  int y = ny;
294  for (int gridY = 0; gridY < gridH; ++gridY)
295  {
296  int x = nx;
297  for (int gridX = 0; gridX < gridW; ++gridX)
298  {
299  LayoutCell *const cell = mCells[CAST_SIZE(gridY)]
300  [CAST_SIZE(gridX)];
301  if ((cell != nullptr) && cell->mType != LayoutCell::NONE)
302  {
303  int dx = x;
304  int dy = y;
305  int dw = 0;
306  int dh = 0;
307  align(dx, dw, 0, *cell,
308  &widths[CAST_SIZE(gridX)], szW - gridX);
309  align(dy, dh, 1, *cell,
310  &heights[CAST_SIZE(gridY)], szH - gridY);
311  cell->reflow(dx, dy, dw, dh);
312  }
313  x += widths[CAST_SIZE(gridX)] + mSpacing;
314  }
315  y += heights[CAST_SIZE(gridY)] + mSpacing;
316  }
317 }
void align(int &pos, int &size, const int dim, LayoutCell const &cell, const int *const sizes, const int sizeCount) const
void reflow(int nx, int ny, int nw, int nh)
Definition: layoutcell.cpp:64

References align(), CAST_S32, CAST_SIZE, getSizes(), mCells, mSizes, mSpacing, LayoutCell::mType, LayoutCell::NONE, LayoutCell::reflow(), EmoteDB::size(), x, and y.

Referenced by LayoutCell::reflow().

◆ resizeGrid()

void LayoutArray::resizeGrid ( int  w,
const int  h 
)
private

Ensures the private vectors are large enough.

Definition at line 70 of file layoutarray.cpp.

71 {
72  const bool extW = (w != 0) && w > CAST_S32(mSizes[0].size());
73  const bool extH = (h != 0) && h > CAST_S32(mSizes[1].size());
74 
75  if (!extW && !extH)
76  return;
77 
78  if (extH)
79  {
80  mSizes[1].resize(CAST_SIZE(h), LayoutType::DEF);
81  mCells.resize(CAST_SIZE(h));
82  if (!extW)
83  w = CAST_S32(mSizes[0].size());
84  }
85 
86  if (extW)
87  mSizes[0].resize(CAST_SIZE(w), LayoutType::DEF);
88 
89  STD_VECTOR <STD_VECTOR <LayoutCell *> >::iterator
90  i = mCells.begin();
91  const STD_VECTOR <STD_VECTOR <LayoutCell *> >::iterator
92  i_end = mCells.end();
93  while (i != i_end)
94  {
95  i->resize(CAST_SIZE(w), nullptr);
96  ++i;
97  }
98 }

References CAST_S32, CAST_SIZE, LayoutType::DEF, mCells, mSizes, and EmoteDB::size().

Referenced by at(), matchColWidth(), setColWidth(), and setRowHeight().

◆ setColWidth()

void LayoutArray::setColWidth ( const int  n,
const int  w 
)

Sets the minimum width of a column.

Definition at line 100 of file layoutarray.cpp.

101 {
102  resizeGrid(n + 1, 0);
103  mSizes[0U][CAST_SIZE(n)] = w;
104 }

References CAST_SIZE, mSizes, and resizeGrid().

Referenced by LayoutCell::setColWidth().

◆ setRowHeight()

void LayoutArray::setRowHeight ( const int  n,
const int  h 
)

Sets the minimum height of a row.

Definition at line 106 of file layoutarray.cpp.

107 {
108  resizeGrid(0, n + 1);
109  mSizes[1][CAST_SIZE(n)] = h;
110 }

References CAST_SIZE, mSizes, and resizeGrid().

Referenced by LayoutCell::setRowHeight().

Friends And Related Function Documentation

◆ LayoutCell

friend class LayoutCell
friend

Definition at line 39 of file layoutarray.h.

Referenced by at().

Field Documentation

◆ mCells

std::vector< std::vector < LayoutCell * > > LayoutArray::mCells
private

Definition at line 128 of file layoutarray.h.

Referenced by at(), LayoutCell::computeSizes(), getSizes(), reflow(), resizeGrid(), and ~LayoutArray().

◆ mSizes

std::vector<int> LayoutArray::mSizes[2]
private

◆ mSpacing

int LayoutArray::mSpacing
private

Definition at line 130 of file layoutarray.h.

Referenced by align(), getSize(), getSizes(), and reflow().


The documentation for this class was generated from the following files: