| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <ctype.h> |
|---|
| 4 | #include <osgWidget/Box> |
|---|
| 5 | |
|---|
| 6 | namespace osgWidget { |
|---|
| 7 | |
|---|
| 8 | Box::Box(const std::string& name, BoxType bt, bool uniform): |
|---|
| 9 | Window (name), |
|---|
| 10 | _boxType (bt), |
|---|
| 11 | _uniform (uniform), |
|---|
| 12 | _lastAdd (0) { |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | Box::Box(const Box& box, const osg::CopyOp& co): |
|---|
| 16 | Window (box, co), |
|---|
| 17 | _boxType (box._boxType), |
|---|
| 18 | _uniform (box._uniform), |
|---|
| 19 | _lastAdd (box._lastAdd) { |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | void Box::_resizeImplementation(point_type w, point_type h) { |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | point_type numFill = _getNumFill(); |
|---|
| 28 | int iw = static_cast<int>(w); |
|---|
| 29 | int ih = static_cast<int>(h); |
|---|
| 30 | int inumFill = static_cast<int>(numFill); |
|---|
| 31 | int wrem = 0; |
|---|
| 32 | int hrem = 0; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | if(inumFill) { |
|---|
| 37 | wrem = iw % inumFill; |
|---|
| 38 | hrem = ih % inumFill; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | if(numFill > 0.0f && (w != 0.0f || h != 0.0f)) { |
|---|
| 45 | unsigned int cur = 0; |
|---|
| 46 | |
|---|
| 47 | for(Iterator i = begin(); i != end(); i++) if(i->valid() && i->get()->canFill()) { |
|---|
| 48 | point_type addWidth = 0.0f; |
|---|
| 49 | point_type addHeight = 0.0f; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | if(_lastAdd >= size()) _lastAdd = 0; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | if(_boxType == HORIZONTAL) { |
|---|
| 58 | if(w) { |
|---|
| 59 | addWidth += static_cast<point_type>(iw / inumFill); |
|---|
| 60 | |
|---|
| 61 | if(cur >= _lastAdd && wrem) { |
|---|
| 62 | _lastAdd++; |
|---|
| 63 | addWidth++; |
|---|
| 64 | wrem--; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | if(h) addHeight += h; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | else { |
|---|
| 72 | if(w) addWidth += w; |
|---|
| 73 | |
|---|
| 74 | if(h) { |
|---|
| 75 | addHeight += static_cast<point_type>(ih / inumFill); |
|---|
| 76 | |
|---|
| 77 | if(cur >= _lastAdd && hrem) { |
|---|
| 78 | _lastAdd++; |
|---|
| 79 | addHeight++; |
|---|
| 80 | hrem--; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if(addWidth != 0.0f) i->get()->addWidth(addWidth); |
|---|
| 86 | |
|---|
| 87 | if(addHeight != 0.0f) i->get()->addHeight(addHeight); |
|---|
| 88 | |
|---|
| 89 | cur++; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | point_type maxWidth = _getMaxWidgetWidthTotal(); |
|---|
| 96 | point_type maxHeight = _getMaxWidgetHeightTotal(); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | point_type xoff = 0.0f; |
|---|
| 100 | point_type yoff = 0.0f; |
|---|
| 101 | point_type xadd = 0.0f; |
|---|
| 102 | point_type yadd = 0.0f; |
|---|
| 103 | |
|---|
| 104 | for(Iterator i = begin(); i != end(); i++) { |
|---|
| 105 | Widget* widget = i->get(); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | if(_boxType == HORIZONTAL) { |
|---|
| 110 | |
|---|
| 111 | widget->setOrigin(xoff, 0.0f); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | if(_uniform) { |
|---|
| 115 | _positionWidget(widget, maxWidth, maxHeight); |
|---|
| 116 | |
|---|
| 117 | xadd = maxWidth; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | else { |
|---|
| 121 | _positionWidget(widget, widget->getWidthTotal(), maxHeight); |
|---|
| 122 | |
|---|
| 123 | xadd = widget->getWidthTotal(); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | else { |
|---|
| 128 | widget->setOrigin(0.0f, yoff); |
|---|
| 129 | |
|---|
| 130 | if(_uniform) { |
|---|
| 131 | _positionWidget(widget, maxWidth, maxHeight); |
|---|
| 132 | |
|---|
| 133 | yadd = maxHeight; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | else { |
|---|
| 137 | _positionWidget(widget, maxWidth, widget->getHeightTotal()); |
|---|
| 138 | |
|---|
| 139 | yadd = widget->getHeightTotal(); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | xoff += xadd; |
|---|
| 144 | yoff += yadd; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | Window::Sizes Box::_getWidthImplementation() const { |
|---|
| 149 | |
|---|
| 150 | if(_boxType == HORIZONTAL) { |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | if(_uniform) return Sizes( |
|---|
| 155 | _getMaxWidgetWidthTotal() * size(), |
|---|
| 156 | _getMaxWidgetMinWidthTotal() * size() |
|---|
| 157 | ); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | else return Sizes( |
|---|
| 162 | _accumulate<Plus>(&Widget::getWidthTotal), |
|---|
| 163 | _accumulate<Plus>(&Widget::getMinWidthTotal) |
|---|
| 164 | ); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | else return Sizes( |
|---|
| 170 | _getMaxWidgetWidthTotal(), |
|---|
| 171 | _getMaxWidgetMinWidthTotal() |
|---|
| 172 | ); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | Window::Sizes Box::_getHeightImplementation() const { |
|---|
| 176 | if(_boxType == VERTICAL) { |
|---|
| 177 | if(_uniform) return Sizes( |
|---|
| 178 | _getMaxWidgetHeightTotal() * size(), |
|---|
| 179 | _getMaxWidgetMinHeightTotal() * size() |
|---|
| 180 | ); |
|---|
| 181 | |
|---|
| 182 | else return Sizes( |
|---|
| 183 | _accumulate<Plus>(&Widget::getHeightTotal), |
|---|
| 184 | _accumulate<Plus>(&Widget::getMinHeightTotal) |
|---|
| 185 | ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | else return Sizes( |
|---|
| 189 | _getMaxWidgetHeightTotal(), |
|---|
| 190 | _getMaxWidgetMinHeightTotal() |
|---|
| 191 | ); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | } |
|---|