| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include "Base.h"
- #include "Control.h"
- namespace gameplay
- {
- Control::Control()
- : _id(""), _state(Control::STATE_NORMAL), _size(Vector2::zero()), _position(Vector2::zero()), _border(Vector2::zero()), _padding(Vector2::zero()),
- _autoWidth(true), _autoHeight(true), _dirty(true)
- {
- }
- Control::Control(const Control& copy)
- {
- }
- Control::~Control()
- {
- }
- const char* Control::getID()
- {
- return _id.c_str();
- }
- const Rectangle& Control::getBounds(bool includePadding) const
- {
- return Rectangle();
- }
- void Control::setPosition(float x, float y)
- {
- _position.set(x, y);
- }
- const Vector2& Control::getPosition() const
- {
- return _position;
- }
- void Control::setSize(float width, float height)
- {
- _size.set(width, height);
- }
- const Vector2& Control::getSize() const
- {
- return _size;
- }
- void Control::setAutoSize(bool width, bool height)
- {
- _autoWidth = width;
- _autoHeight = height;
- }
- void Control::setStyle(Theme::Style* style)
- {
- _style = style;
- }
- Theme::Style* Control::getStyle() const
- {
- return _style;
- }
- void Control::setState(State state)
- {
- _state = state;
- }
- Control::State Control::getState()
- {
- return _state;
- }
- void Control::disable()
- {
- _state = STATE_DISABLED;
- }
- void Control::enable()
- {
- _state = STATE_NORMAL;
- }
- Theme::Style::OverlayType Control::getOverlayType() const
- {
- switch (_state)
- {
- case Control::STATE_NORMAL:
- return Theme::Style::OVERLAY_NORMAL;
- case Control::STATE_FOCUS:
- return Theme::Style::OVERLAY_FOCUS;
- case Control::STATE_ACTIVE:
- return Theme::Style::OVERLAY_ACTIVE;
- default:
- return Theme::Style::OVERLAY_NORMAL;
- }
- }
- void Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
- {
- // Empty stub to be implemented by Button and its descendents.
- }
- void Control::keyEvent(Keyboard::KeyEvent evt, int key)
- {
- }
- void Control::update(const Vector2& position)
- {
- }
- void Control::drawBorder(SpriteBatch* spriteBatch, const Vector2& position)
- {
- Vector2 pos(position.x + _position.x, position.y + _position.y);
- // Get the border and background images for this control's current state.
- Theme::ContainerRegion* containerRegion = _style->getOverlay(getOverlayType())->getContainerRegion();
- if (containerRegion)
- {
- // Get the UVs.
- Theme::UVs topLeft, top, topRight, left, center, right, bottomLeft, bottom, bottomRight;
- topLeft = containerRegion->getUVs(Theme::ContainerRegion::TOP_LEFT);
- top = containerRegion->getUVs(Theme::ContainerRegion::TOP);
- topRight = containerRegion->getUVs(Theme::ContainerRegion::TOP_RIGHT);
- left = containerRegion->getUVs(Theme::ContainerRegion::LEFT);
- center = containerRegion->getUVs(Theme::ContainerRegion::CENTER);
- right = containerRegion->getUVs(Theme::ContainerRegion::RIGHT);
- bottomLeft = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM_LEFT);
- bottom = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM);
- bottomRight = containerRegion->getUVs(Theme::ContainerRegion::BOTTOM_RIGHT);
- // Calculate screen-space positions.
- Theme::Border border = containerRegion->getBorder();
- Theme::Padding padding = _style->getPadding();
- Vector4 borderColor = containerRegion->getColor();
- float midWidth = _size.x - border.left - border.right;
- float midHeight = _size.y - border.top - border.bottom;
- float midX = pos.x + border.left;
- float midY = pos.y + border.top;
- float rightX = pos.x + _size.x - border.right;
- float bottomY = pos.y + _size.y - border.bottom;
- // Draw themed border sprites.
- if (border.left && border.top)
- spriteBatch->draw(pos.x, pos.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, borderColor);
- if (border.top)
- spriteBatch->draw(pos.x + border.left, pos.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, borderColor);
- if (border.right && border.top)
- spriteBatch->draw(rightX, pos.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, borderColor);
- if (border.left)
- spriteBatch->draw(pos.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, borderColor);
- if (border.left && border.right && border.top && border.bottom)
- spriteBatch->draw(pos.x + border.left, pos.y + border.top, _size.x - border.left - border.right, _size.y - border.top - border.bottom,
- center.u1, center.v1, center.u2, center.v2, borderColor);
- if (border.right)
- spriteBatch->draw(rightX, midY, border.right, midHeight,
- right.u1, right.v1, right.u2, right.v2, borderColor);
- if (border.bottom && border.left)
- spriteBatch->draw(pos.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, borderColor);
- if (border.bottom)
- spriteBatch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, borderColor);
- if (border.bottom && border.right)
- spriteBatch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, borderColor);
- }
- }
- void Control::drawSprites(SpriteBatch* spriteBatch, const Vector2& position)
- {
- }
- void Control::drawText(const Vector2& position)
- {
- }
- bool Control::isDirty()
- {
- return _dirty;
- }
- }
|