| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include <osg/io_utils> |
|---|
| 5 | #include <osg/Math> |
|---|
| 6 | #include <osg/TextureRectangle> |
|---|
| 7 | #include <osgDB/ReadFile> |
|---|
| 8 | #include <osgDB/FileUtils> |
|---|
| 9 | #include <osgWidget/WindowManager> |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #define MACRO_WIDGET_X(v) (*v)[LL].x() |
|---|
| 13 | #define MACRO_WIDGET_Y(v) (*v)[LL].y() |
|---|
| 14 | #define MACRO_WIDGET_W(v) (*v)[LR].x() - (*v)[LL].x() |
|---|
| 15 | #define MACRO_WIDGET_H(v) (*v)[UL].y() - (*v)[LL].y() |
|---|
| 16 | |
|---|
| 17 | namespace osgWidget { |
|---|
| 18 | |
|---|
| 19 | osg::ref_ptr<PointArray> Widget::_norms; |
|---|
| 20 | |
|---|
| 21 | Widget::Widget(const std::string& name, point_type w, point_type h): |
|---|
| 22 | _parent (0), |
|---|
| 23 | _index (0), |
|---|
| 24 | _layer (LAYER_LOW), |
|---|
| 25 | _padLeft (0.0f), |
|---|
| 26 | _padRight (0.0f), |
|---|
| 27 | _padTop (0.0f), |
|---|
| 28 | _padBottom (0.0f), |
|---|
| 29 | _valign (VA_CENTER), |
|---|
| 30 | _halign (HA_CENTER), |
|---|
| 31 | _coordMode (CM_ABSOLUTE), |
|---|
| 32 | _canFill (false), |
|---|
| 33 | _canClone (true), |
|---|
| 34 | _isManaged (false), |
|---|
| 35 | _isStyled (false), |
|---|
| 36 | _minWidth (0.0f), |
|---|
| 37 | _minHeight (0.0f) { |
|---|
| 38 | _name = name.size() ? name : generateRandomName("Widget"); |
|---|
| 39 | |
|---|
| 40 | if(!_norms.valid()) { |
|---|
| 41 | _norms = new PointArray(1); |
|---|
| 42 | |
|---|
| 43 | (*_norms)[0].set(0.0f, 0.0f, 1.0f); |
|---|
| 44 | (*_norms)[0].normalize(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | TexCoordArray* texs = new TexCoordArray(4); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | std::fill(texs->begin(), texs->end(), osg::Vec2(0.0f, 0.0f)); |
|---|
| 52 | |
|---|
| 53 | setUseDisplayList(false); |
|---|
| 54 | setDataVariance(osg::Object::DYNAMIC); |
|---|
| 55 | setVertexArray(new PointArray(4)); |
|---|
| 56 | setColorArray(new ColorArray(4)); |
|---|
| 57 | setNormalArray(_norms.get()); |
|---|
| 58 | setTexCoordArray(0, texs); |
|---|
| 59 | setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 60 | setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 61 | addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4)); |
|---|
| 62 | |
|---|
| 63 | setDimensions(0.0f, 0.0f, w, h); |
|---|
| 64 | setColor(1.0f, 1.0f, 1.0f, 1.0f); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | Widget::Widget(const Widget& widget, const osg::CopyOp& co): |
|---|
| 68 | osg::Geometry (widget, co), |
|---|
| 69 | EventInterface (widget), |
|---|
| 70 | StyleInterface (widget), |
|---|
| 71 | _parent (0), |
|---|
| 72 | _index (0), |
|---|
| 73 | _layer (widget._layer), |
|---|
| 74 | _padLeft (widget._padLeft), |
|---|
| 75 | _padRight (widget._padRight), |
|---|
| 76 | _padTop (widget._padTop), |
|---|
| 77 | _padBottom (widget._padBottom), |
|---|
| 78 | _valign (widget._valign), |
|---|
| 79 | _halign (widget._halign), |
|---|
| 80 | _coordMode (widget._coordMode), |
|---|
| 81 | _canFill (widget._canFill), |
|---|
| 82 | _canClone (widget._canClone), |
|---|
| 83 | _isManaged (false), |
|---|
| 84 | _isStyled (widget._isStyled), |
|---|
| 85 | _minWidth (widget._minWidth), |
|---|
| 86 | _minHeight (widget._minHeight) { |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | point_type Widget::_calculateZ(unsigned int layer) const { |
|---|
| 92 | point_type zRange = 0.0f; |
|---|
| 93 | |
|---|
| 94 | if(_parent) zRange = _parent->getZRange(); |
|---|
| 95 | |
|---|
| 96 | return (static_cast<point_type>(layer) / static_cast<point_type>(LAYER_TOP + 1)) * zRange; |
|---|
| 97 | |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | WindowManager* Widget::_getWindowManager() const { |
|---|
| 101 | if(!_parent) return 0; |
|---|
| 102 | |
|---|
| 103 | return _parent->getWindowManager(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | osg::Image* Widget::_getImage() const { |
|---|
| 107 | const osg::Texture* texture = _texture(); |
|---|
| 108 | |
|---|
| 109 | if(texture) return const_cast<osg::Image*>(texture->getImage(0)); |
|---|
| 110 | |
|---|
| 111 | return 0; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | void Widget::setDimensions(point_type x, point_type y, point_type w, point_type h, point_type z) { |
|---|
| 115 | if(w != -1.0f && w < _minWidth) { |
|---|
| 116 | warn() |
|---|
| 117 | << "Widget [" << _name |
|---|
| 118 | << "] was asked to set it's width to " << w |
|---|
| 119 | << ", but the minimum width is " << _minWidth |
|---|
| 120 | << "." << std::endl |
|---|
| 121 | ; |
|---|
| 122 | |
|---|
| 123 | w = _minWidth; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if(h != -1.0f && h < _minHeight) { |
|---|
| 127 | warn() |
|---|
| 128 | << "Widget [" << _name |
|---|
| 129 | << "] was asked to set it's height to " << h |
|---|
| 130 | << ", but the minimum height is " << _minHeight |
|---|
| 131 | << "." << std::endl |
|---|
| 132 | ; |
|---|
| 133 | |
|---|
| 134 | h = _minHeight; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | PointArray* verts = _verts(); |
|---|
| 138 | |
|---|
| 139 | if(_coordMode == CM_ABSOLUTE) { |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | if(x < 0.0f) x = MACRO_WIDGET_X(verts); |
|---|
| 144 | if(y < 0.0f) y = MACRO_WIDGET_Y(verts); |
|---|
| 145 | if(w < 0.0f) w = MACRO_WIDGET_W(verts); |
|---|
| 146 | if(h < 0.0f) h = MACRO_WIDGET_H(verts); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | else { |
|---|
| 150 | if(x < 0.0f) x = _relCoords[0]; |
|---|
| 151 | if(y < 0.0f) y = _relCoords[1]; |
|---|
| 152 | if(w < 0.0f) w = _relCoords[2]; |
|---|
| 153 | if(h < 0.0f) h = _relCoords[3]; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if(z < 0.0f) z = _calculateZ(_layer); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | if(_coordMode == CM_RELATIVE) { |
|---|
| 162 | XYCoord size; |
|---|
| 163 | |
|---|
| 164 | if(_parent) size = _parent->getSize(); |
|---|
| 165 | |
|---|
| 166 | if(x >= 0.0f && x <= 1.0f) { |
|---|
| 167 | _relCoords[0] = x; |
|---|
| 168 | |
|---|
| 169 | x = size.x() * x; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if(y >= 0.0f && y <= 1.0f) { |
|---|
| 173 | _relCoords[1] = y; |
|---|
| 174 | |
|---|
| 175 | y = size.y() * y; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | if(w >= 0.0f && w <= 1.0f) { |
|---|
| 179 | _relCoords[2] = w; |
|---|
| 180 | |
|---|
| 181 | w = size.x() * w; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if(h >= 0.0f && h <= 1.0f) { |
|---|
| 185 | _relCoords[3] = h; |
|---|
| 186 | |
|---|
| 187 | h = size.y() * h; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | const WindowManager* wm = _getWindowManager(); |
|---|
| 192 | |
|---|
| 193 | if(wm && wm->isUsingRenderBins()) { |
|---|
| 194 | getOrCreateStateSet()->setRenderBinDetails(static_cast<int>(z), "RenderBin"); |
|---|
| 195 | |
|---|
| 196 | z = 0.0f; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | (*verts)[LL].set(x, y, z); |
|---|
| 200 | (*verts)[LR].set(x + w, y, z); |
|---|
| 201 | (*verts)[UR].set(x + w, y + h, z); |
|---|
| 202 | (*verts)[UL].set(x, y + h, z); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void Widget::setColor(color_type r, color_type g, color_type b, color_type a, Corner p) { |
|---|
| 206 | ColorArray* cols = _cols(); |
|---|
| 207 | |
|---|
| 208 | if(p == ALL_CORNERS) { |
|---|
| 209 | (*cols)[LL].set(r, g, b, a); |
|---|
| 210 | (*cols)[LR].set(r, g, b, a); |
|---|
| 211 | (*cols)[UR].set(r, g, b, a); |
|---|
| 212 | (*cols)[UL].set(r, g, b, a); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | else (*cols)[p].set(r, g, b, a); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | void Widget::addColor(color_type r, color_type g, color_type b, color_type a, Corner p) { |
|---|
| 219 | ColorArray* cols = _cols(); |
|---|
| 220 | |
|---|
| 221 | if(p == ALL_CORNERS) { |
|---|
| 222 | (*cols)[LL] += Color(r, g, b, a); |
|---|
| 223 | (*cols)[LR] += Color(r, g, b, a); |
|---|
| 224 | (*cols)[UR] += Color(r, g, b, a); |
|---|
| 225 | (*cols)[UL] += Color(r, g, b, a); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | else (*cols)[p] += Color(r, g, b, a); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | void Widget::setTexCoord(texcoord_type tx, texcoord_type ty, Corner p) { |
|---|
| 232 | TexCoordArray* texs = _texs(); |
|---|
| 233 | |
|---|
| 234 | if(p == ALL_CORNERS) { |
|---|
| 235 | (*texs)[LL].set(tx, ty); |
|---|
| 236 | (*texs)[LR].set(tx, ty); |
|---|
| 237 | (*texs)[UR].set(tx, ty); |
|---|
| 238 | (*texs)[UL].set(tx, ty); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | else (*texs)[p].set(tx, ty); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | void Widget::setLayer(Layer layer, unsigned int offset) { |
|---|
| 247 | if(layer == LAYER_TOP) offset = 0; |
|---|
| 248 | |
|---|
| 249 | _layer = layer + offset; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | void Widget::setTexCoordRegion(point_type x, point_type y, point_type w, point_type h) { |
|---|
| 253 | osg::Image* image = _image(); |
|---|
| 254 | |
|---|
| 255 | if(!image) return; |
|---|
| 256 | |
|---|
| 257 | point_type tw = image->s(); |
|---|
| 258 | point_type th = image->t(); |
|---|
| 259 | |
|---|
| 260 | TexCoordArray* texs = _texs(); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | XYCoord t(x / tw, y / tw); |
|---|
| 264 | |
|---|
| 265 | (*texs)[LL] = t; |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | t += XYCoord(w / tw, 0.0f); |
|---|
| 269 | |
|---|
| 270 | (*texs)[LR] = t; |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | t += XYCoord(0.0f, h / th); |
|---|
| 274 | |
|---|
| 275 | (*texs)[UR] = t; |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | t += XYCoord(-(w / tw), 0.0f); |
|---|
| 279 | |
|---|
| 280 | (*texs)[UL] = t; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | void Widget::setTexCoordWrapHorizontal() { |
|---|
| 284 | osg::Image* image = _image(); |
|---|
| 285 | osg::Texture* texture = _texture(); |
|---|
| 286 | |
|---|
| 287 | if(!image || !texture || image->s() == 0.0f) return; |
|---|
| 288 | |
|---|
| 289 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); |
|---|
| 290 | |
|---|
| 291 | setTexCoord(getWidth() / image->s(), 0.0f, LOWER_RIGHT); |
|---|
| 292 | setTexCoord(getWidth() / image->s(), 1.0f, UPPER_RIGHT); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | void Widget::setTexCoordWrapVertical() { |
|---|
| 296 | osg::Image* image = _image(); |
|---|
| 297 | osg::Texture* texture = _texture(); |
|---|
| 298 | |
|---|
| 299 | if(!image || !texture || image->t() == 0.0f) return; |
|---|
| 300 | |
|---|
| 301 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); |
|---|
| 302 | |
|---|
| 303 | setTexCoord(0.0f, getHeight() / image->t(), UPPER_LEFT); |
|---|
| 304 | setTexCoord(1.0f, getHeight() / image->t(), UPPER_RIGHT); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | XYCoord Widget::localXY(double _x, double _y) const { |
|---|
| 308 | if(!_parent) return XYCoord(_x, _y); |
|---|
| 309 | |
|---|
| 310 | return _parent->localXY(_x, _y) - getOrigin(); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | bool Widget::setImage(osg::Image* image, bool setTexCoords, bool useTextRect) { |
|---|
| 314 | if(!image) { |
|---|
| 315 | warn() << "Widget [" << _name << "] cannot use a NULL image." << std::endl; |
|---|
| 316 | |
|---|
| 317 | return false; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | osg::Texture* texture = 0; |
|---|
| 321 | |
|---|
| 322 | if(useTextRect) texture = new osg::TextureRectangle(); |
|---|
| 323 | |
|---|
| 324 | else texture = new osg::Texture2D(); |
|---|
| 325 | |
|---|
| 326 | if(!texture) return false; |
|---|
| 327 | |
|---|
| 328 | texture->setImage(0, image); |
|---|
| 329 | |
|---|
| 330 | return setTexture(texture, setTexCoords, useTextRect); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | bool Widget::setImage(const std::string& filePath, bool setTexCoords, bool useTextRect) { |
|---|
| 334 | if(!osgDB::findDataFile(filePath).size()) { |
|---|
| 335 | warn() |
|---|
| 336 | << "Widget [" << _name |
|---|
| 337 | << "] cannot find file " << filePath |
|---|
| 338 | << " to set as it's Image." << std::endl |
|---|
| 339 | ; |
|---|
| 340 | |
|---|
| 341 | return false; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | return setImage(osgDB::readImageFile(filePath), setTexCoords, useTextRect); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | bool Widget::setTexture(osg::Texture* texture, bool setTexCoords, bool useTextRect) { |
|---|
| 348 | if(!texture) return false; |
|---|
| 349 | |
|---|
| 350 | getOrCreateStateSet()->setTextureAttributeAndModes( |
|---|
| 351 | 0, |
|---|
| 352 | texture, |
|---|
| 353 | osg::StateAttribute::ON |
|---|
| 354 | ); |
|---|
| 355 | |
|---|
| 356 | if(setTexCoords) { |
|---|
| 357 | if(useTextRect) { |
|---|
| 358 | osg::Image* image = texture->getImage(0); |
|---|
| 359 | |
|---|
| 360 | setTexCoord(0.0f, 0.0f, LOWER_LEFT); |
|---|
| 361 | setTexCoord(image->s(), 0.0f, LOWER_RIGHT); |
|---|
| 362 | setTexCoord(image->s(), image->t(), UPPER_RIGHT); |
|---|
| 363 | setTexCoord(0.0f, image->t(), UPPER_LEFT); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | else { |
|---|
| 367 | setTexCoord(0.0f, 0.0f, LOWER_LEFT); |
|---|
| 368 | setTexCoord(1.0f, 0.0f, LOWER_RIGHT); |
|---|
| 369 | setTexCoord(1.0f, 1.0f, UPPER_RIGHT); |
|---|
| 370 | setTexCoord(0.0f, 1.0f, UPPER_LEFT); |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | return true; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | void Widget::setPadding(point_type pad) { |
|---|
| 378 | _padLeft = _padRight = _padTop = _padBottom = pad; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | void Widget::addX(point_type x) { |
|---|
| 382 | if(_coordMode == CM_ABSOLUTE) setDimensions(MACRO_WIDGET_X(_verts()) + x); |
|---|
| 383 | |
|---|
| 384 | else setDimensions(_relCoords[0] + x); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | void Widget::addY(point_type y) { |
|---|
| 388 | if(_coordMode == CM_ABSOLUTE) setDimensions(-1.0f, MACRO_WIDGET_Y(_verts()) + y); |
|---|
| 389 | |
|---|
| 390 | else setDimensions(-1.0f, _relCoords[1] + y); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | void Widget::addWidth(point_type w) { |
|---|
| 394 | if(_coordMode == CM_ABSOLUTE) setDimensions(-1.0f, -1.0f, MACRO_WIDGET_W(_verts()) + w); |
|---|
| 395 | |
|---|
| 396 | else setDimensions(-1.0f, -1.0f, _relCoords[2] + w); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | void Widget::addHeight(point_type h) { |
|---|
| 400 | if(_coordMode == CM_ABSOLUTE) setDimensions( |
|---|
| 401 | -1.0f, |
|---|
| 402 | -1.0f, |
|---|
| 403 | -1.0f, |
|---|
| 404 | MACRO_WIDGET_H(_verts()) + h |
|---|
| 405 | ); |
|---|
| 406 | |
|---|
| 407 | else setDimensions(-1.0f, -1.0f, -1.0f, _relCoords[3] + h); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | void Widget::addOrigin(point_type x, point_type y) { |
|---|
| 411 | if(_coordMode == CM_ABSOLUTE) { |
|---|
| 412 | PointArray* verts = _verts(); |
|---|
| 413 | |
|---|
| 414 | setDimensions( |
|---|
| 415 | MACRO_WIDGET_X(verts) + x, |
|---|
| 416 | MACRO_WIDGET_Y(verts) + y |
|---|
| 417 | ); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | else setDimensions(_relCoords[0] + x, _relCoords[1] + y); |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | void Widget::addSize(point_type w, point_type h) { |
|---|
| 424 | if(_coordMode == CM_ABSOLUTE) { |
|---|
| 425 | PointArray* verts = _verts(); |
|---|
| 426 | |
|---|
| 427 | setDimensions( |
|---|
| 428 | -1.0f, |
|---|
| 429 | -1.0f, |
|---|
| 430 | MACRO_WIDGET_W(verts) + w, |
|---|
| 431 | MACRO_WIDGET_H(verts) + h |
|---|
| 432 | ); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | else setDimensions(-1.0f, -1.0f, _relCoords[2] + w, _relCoords[3] + h); |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | point_type Widget::getWidth() const { |
|---|
| 439 | const PointArray* verts = _verts(); |
|---|
| 440 | |
|---|
| 441 | return MACRO_WIDGET_W(verts); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | point_type Widget::getHeight() const { |
|---|
| 445 | const PointArray* verts = _verts(); |
|---|
| 446 | |
|---|
| 447 | return MACRO_WIDGET_H(verts); |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | point_type Widget::getX() const { |
|---|
| 451 | return MACRO_WIDGET_X(_verts()); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | point_type Widget::getY() const { |
|---|
| 455 | return MACRO_WIDGET_Y(_verts()); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | point_type Widget::getZ() const { |
|---|
| 459 | return (*_verts())[LL].z(); |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | point_type Widget::getPadHorizontal() const { |
|---|
| 463 | return _padLeft + _padRight; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | point_type Widget::getPadVertical() const { |
|---|
| 467 | return _padTop + _padBottom; |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | const Point& Widget::getPoint(Corner p) const { |
|---|
| 471 | Corner point = p; |
|---|
| 472 | |
|---|
| 473 | if(p == ALL_CORNERS) point = UPPER_LEFT; |
|---|
| 474 | |
|---|
| 475 | return (*_verts())[point]; |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | const Color& Widget::getColor(Corner p) const { |
|---|
| 479 | Corner point = p; |
|---|
| 480 | |
|---|
| 481 | if(p == ALL_CORNERS) point = UPPER_LEFT; |
|---|
| 482 | |
|---|
| 483 | return (*_cols())[point]; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | const TexCoord& Widget::getTexCoord(Corner p) const { |
|---|
| 487 | Corner point = p; |
|---|
| 488 | |
|---|
| 489 | if(p == ALL_CORNERS) point = UPPER_LEFT; |
|---|
| 490 | |
|---|
| 491 | return (*_texs())[point]; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | Color Widget::getImageColorAtXY(point_type x, point_type y) const { |
|---|
| 495 | const osg::Image* image = _image(); |
|---|
| 496 | |
|---|
| 497 | if(!image) return Color(); |
|---|
| 498 | |
|---|
| 499 | const TexCoordArray* texs = _texs(); |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | point_type width = fabs((*texs)[LR].x() - (*texs)[LL].x()); |
|---|
| 511 | point_type height = fabs((*texs)[LR].y() - (*texs)[UR].y()); |
|---|
| 512 | |
|---|
| 513 | point_type X = ((x / getWidth()) * width) + (*texs)[LL].x(); |
|---|
| 514 | point_type Y = (((getHeight() - y) / getHeight()) * height) + (*texs)[UR].y(); |
|---|
| 515 | |
|---|
| 516 | return image->getColor(TexCoord(X, Y)); |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | bool Widget::isPaddingUniform() const { |
|---|
| 520 | return |
|---|
| 521 | _padLeft == _padRight && |
|---|
| 522 | _padLeft == _padTop && |
|---|
| 523 | _padLeft == _padBottom |
|---|
| 524 | ; |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | } |
|---|