File: horizontal-layout.cpp

package info (click to toggle)
higan 106-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 9,640 kB
  • sloc: cpp: 108,736; ansic: 809; makefile: 22; sh: 7
file content (141 lines) | stat: -rw-r--r-- 4,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#if defined(Hiro_HorizontalLayout)

auto mHorizontalLayout::append(sSizable sizable, Size size, float spacing) -> type& {
  properties.append({size.width(), size.height(), spacing < 0 ? settings.spacing : spacing});
  mLayout::append(sizable);
  return *this;
}

auto mHorizontalLayout::modify(sSizable sizable, Size size, float spacing) -> type& {
  if(sizable && this->sizable(sizable->offset()) == sizable) {
    auto& properties = this->properties[sizable->offset()];
    properties.setWidth(size.width());
    properties.setHeight(size.height());
    properties.setSpacing(spacing);
  }
  return *this;
}

auto mHorizontalLayout::minimumSize() const -> Size {
  float width = 0, height = 0;

  for(auto n : range(sizableCount())) {
    auto& child = properties[sizable(n)->offset()];
    if(child.width() == Size::Minimum || child.width() == Size::Maximum) {
      width += sizable(n)->minimumSize().width();
    } else {
      width += child.width();
    }
    if(&child != &properties.right()) width += child.spacing();
  }

  for(auto n : range(sizableCount())) {
    auto& child = properties[sizable(n)->offset()];
    if(child.height() == Size::Minimum || child.height() == Size::Maximum) {
      height = max(height, sizable(n)->minimumSize().height());
      continue;
    }
    height = max(height, child.height());
  }

  return {settings.margin * 2 + width, settings.margin * 2 + height};
}

auto mHorizontalLayout::remove(sSizable sizable) -> type& {
  properties.remove(sizable->offset());
  mLayout::remove(sizable);
  return *this;
}

auto mHorizontalLayout::reset() -> type& {
  mLayout::reset();
  properties.reset();
  return *this;
}

auto mHorizontalLayout::setAlignment(float alignment) -> type& {
  settings.alignment = max(0.0, min(1.0, alignment));
  return *this;
}

auto mHorizontalLayout::setEnabled(bool enabled) -> type& {
  mLayout::setEnabled(enabled);
  for(auto n : range(sizableCount())) {
    sizable(n)->setEnabled(sizable(n)->enabled());
  }
  return *this;
}

auto mHorizontalLayout::setFont(const Font& font) -> type& {
  mLayout::setFont(font);
  for(auto n : range(sizableCount())) {
    sizable(n)->setFont(sizable(n)->font());
  }
  return *this;
}

auto mHorizontalLayout::setGeometry(Geometry containerGeometry) -> type& {
  mLayout::setGeometry(containerGeometry);

  auto properties = this->properties;
  for(auto n : range(sizableCount())) {
    auto& child = properties[sizable(n)->offset()];
    if(child.width()  == Size::Minimum) child.setWidth(sizable(n)->minimumSize().width());
    if(child.height() == Size::Minimum) child.setHeight(sizable(n)->minimumSize().height());
  }

  Geometry geometry = containerGeometry;
  geometry.setX     (geometry.x()      + settings.margin    );
  geometry.setY     (geometry.y()      + settings.margin    );
  geometry.setWidth (geometry.width()  - settings.margin * 2);
  geometry.setHeight(geometry.height() - settings.margin * 2);

  float minimumWidth = 0, maximumWidthCounter = 0;
  for(auto& child : properties) {
    if(child.width() == Size::Maximum) maximumWidthCounter++;
    if(child.width() != Size::Maximum) minimumWidth += child.width();
    if(&child != &properties.right()) minimumWidth += child.spacing();
  }

  for(auto& child : properties) {
    if(child.width()  == Size::Maximum) child.setWidth((geometry.width() - minimumWidth) / maximumWidthCounter);
    if(child.height() == Size::Maximum) child.setHeight(geometry.height());
  }

  float maximumHeight = 0;
  for(auto& child : properties) maximumHeight = max(maximumHeight, child.height());

  for(auto n : range(sizableCount())) {
    auto& child = properties[sizable(n)->offset()];
    float pivot = (maximumHeight - child.height()) * settings.alignment;
    Geometry childGeometry = {geometry.x(), geometry.y() + pivot, child.width(), child.height()};
    if(childGeometry.width()  < 1) childGeometry.setWidth (1);
    if(childGeometry.height() < 1) childGeometry.setHeight(1);
    sizable(n)->setGeometry(childGeometry);

    geometry.setX    (geometry.x()     + child.width() + child.spacing());
    geometry.setWidth(geometry.width() - child.width() + child.spacing());
  }

  return *this;
}

auto mHorizontalLayout::setMargin(float margin) -> type& {
  settings.margin = margin;
  return *this;
}

auto mHorizontalLayout::setSpacing(float spacing) -> type& {
  settings.spacing = spacing;
  return *this;
}

auto mHorizontalLayout::setVisible(bool visible) -> type& {
  mLayout::setVisible(visible);
  for(auto n : range(sizableCount())) {
    sizable(n)->setVisible(sizable(n)->visible());
  }
  return *this;
}

#endif