| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188 |
- #include "Base.h"
- #include "Game.h"
- #include "Control.h"
- namespace gameplay
- {
- Control::Control()
- : _id(""), _state(Control::NORMAL), _bounds(Rectangle::empty()), _clipBounds(Rectangle::empty()), _clip(Rectangle::empty()),
- _dirty(true), _consumeTouchEvents(true), _listeners(NULL), _styleOverridden(false)
- {
- }
- Control::Control(const Control& copy)
- {
- }
- Control::~Control()
- {
- if (_listeners)
- {
- for (std::map<Listener::EventType, std::list<Listener*>*>::const_iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
- {
- std::list<Listener*>* list = itr->second;
- SAFE_DELETE(list);
- }
- SAFE_DELETE(_listeners);
- }
- if (_styleOverridden)
- {
- SAFE_DELETE(_style);
- }
- }
- void Control::initialize(Theme::Style* style, Properties* properties)
- {
- GP_ASSERT(properties);
- _style = style;
- // Properties not defined by the style.
- _alignment = getAlignment(properties->getString("alignment"));
- _autoWidth = properties->getBool("autoWidth");
- _autoHeight = properties->getBool("autoHeight");
- Vector2 position;
- Vector2 size;
- if (properties->exists("position"))
- {
- properties->getVector2("position", &position);
- }
- else
- {
- position.x = properties->getFloat("x");
- position.y = properties->getFloat("y");
- }
-
- if (properties->exists("size"))
- {
- properties->getVector2("size", &size);
- }
- else
- {
- size.x = properties->getFloat("width");
- size.y = properties->getFloat("height");
- }
- _bounds.set(position.x, position.y, size.x, size.y);
- _state = Control::getState(properties->getString("state"));
- const char* id = properties->getId();
- if (id)
- _id = id;
- // Potentially override themed properties for all states.
- overrideThemedProperties(properties, STATE_ALL);
- // Override themed properties on specific states.
- Properties* stateSpace = properties->getNextNamespace();
- while (stateSpace != NULL)
- {
- std::string stateName(stateSpace->getNamespace());
- std::transform(stateName.begin(), stateName.end(), stateName.begin(), (int(*)(int))toupper);
- if (stateName == "STATENORMAL")
- {
- overrideThemedProperties(stateSpace, NORMAL);
- }
- else if (stateName == "STATEFOCUS")
- {
- overrideThemedProperties(stateSpace, FOCUS);
- }
- else if (stateName == "STATEACTIVE")
- {
- overrideThemedProperties(stateSpace, ACTIVE);
- }
- else if (stateName == "STATEDISABLED")
- {
- overrideThemedProperties(stateSpace, DISABLED);
- }
- else
- {
- // Ignore this case because there can be an inner namespace of a control that does not override a state.
- }
- stateSpace = properties->getNextNamespace();
- }
- }
- const char* Control::getID() const
- {
- return _id.c_str();
- }
- void Control::setPosition(float x, float y)
- {
- _bounds.x = x;
- _bounds.y = y;
- _dirty = true;
- }
- void Control::setSize(float width, float height)
- {
- _bounds.width = width;
- _bounds.height = height;
- _dirty = true;
- }
- void Control::setBounds(const Rectangle& bounds)
- {
- _bounds.set(bounds);
- }
- const Rectangle& Control::getBounds() const
- {
- return _bounds;
- }
- float Control::getX() const
- {
- return _bounds.x;
- }
- float Control::getY() const
- {
- return _bounds.y;
- }
- float Control::getWidth() const
- {
- return _bounds.width;
- }
- float Control::getHeight() const
- {
- return _bounds.height;
- }
- void Control::setAlignment(Alignment alignment)
- {
- _alignment = alignment;
- }
- Control::Alignment Control::getAlignment() const
- {
- return _alignment;
- }
- void Control::setAutoWidth(bool autoWidth)
- {
- _autoWidth = autoWidth;
- }
- bool Control::getAutoWidth() const
- {
- return _autoWidth;
- }
- void Control::setAutoHeight(bool autoHeight)
- {
- _autoHeight = autoHeight;
- }
- void Control::setOpacity(float opacity, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setOpacity(opacity);
- }
-
- _dirty = true;
- }
- float Control::getOpacity(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getOpacity();
- }
- void Control::setBorder(float top, float bottom, float left, float right, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setBorder(top, bottom, left, right);
- }
- _dirty = true;
- }
- const Theme::Border& Control::getBorder(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getBorder();
- }
- void Control::setSkinRegion(const Rectangle& region, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setSkinRegion(region, _style->_tw, _style->_th);
- }
- _dirty = true;
- }
- const Rectangle& Control::getSkinRegion(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getSkinRegion();
- }
- const Theme::UVs& Control::getSkinUVs(Theme::Skin::SkinArea area, State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getSkinUVs(area);
- }
- void Control::setSkinColor(const Vector4& color, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setSkinColor(color);
- }
- _dirty = true;
- }
- const Vector4& Control::getSkinColor(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getSkinColor();
- }
- void Control::setMargin(float top, float bottom, float left, float right)
- {
- GP_ASSERT(_style);
- _style->setMargin(top, bottom, left, right);
- _dirty = true;
- }
- const Theme::Margin& Control::getMargin() const
- {
- GP_ASSERT(_style);
- return _style->getMargin();
- }
- void Control::setPadding(float top, float bottom, float left, float right)
- {
- GP_ASSERT(_style);
- _style->setPadding(top, bottom, left, right);
- _dirty = true;
- }
-
- const Theme::Padding& Control::getPadding() const
- {
- GP_ASSERT(_style);
- return _style->getPadding();
- }
- void Control::setImageRegion(const char* id, const Rectangle& region, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setImageRegion(id, region, _style->_tw, _style->_th);
- }
- _dirty = true;
- }
- const Rectangle& Control::getImageRegion(const char* id, State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getImageRegion(id);
- }
- void Control::setImageColor(const char* id, const Vector4& color, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setImageColor(id, color);
- }
- _dirty = true;
- }
- const Vector4& Control::getImageColor(const char* id, State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getImageColor(id);
- }
- const Theme::UVs& Control::getImageUVs(const char* id, State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getImageUVs(id);
- }
- void Control::setCursorRegion(const Rectangle& region, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setCursorRegion(region, _style->_tw, _style->_th);
- }
- _dirty = true;
- }
- const Rectangle& Control::getCursorRegion(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getCursorRegion();
- }
- void Control::setCursorColor(const Vector4& color, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setCursorColor(color);
- }
- _dirty = true;
- }
- const Vector4& Control::getCursorColor(State state)
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getCursorColor();
- }
-
- const Theme::UVs& Control::getCursorUVs(State state)
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getCursorUVs();
- }
- void Control::setFont(Font* font, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setFont(font);
- }
- _dirty = true;
- }
- Font* Control::getFont(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getFont();
- }
- void Control::setFontSize(unsigned int fontSize, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setFontSize(fontSize);
- }
- _dirty = true;
- }
- unsigned int Control::getFontSize(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getFontSize();
- }
- void Control::setTextColor(const Vector4& color, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setTextColor(color);
- }
- _dirty = true;
- }
- const Vector4& Control::getTextColor(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getTextColor();
- }
- void Control::setTextAlignment(Font::Justify alignment, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setTextAlignment(alignment);
- }
- _dirty = true;
- }
- Font::Justify Control::getTextAlignment(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getTextAlignment();
- }
- void Control::setTextRightToLeft(bool rightToLeft, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setTextRightToLeft(rightToLeft);
- }
- _dirty = true;
- }
- bool Control::getTextRightToLeft(State state) const
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getTextRightToLeft();
- }
- const Rectangle& Control::getClipBounds() const
- {
- return _clipBounds;
- }
- const Rectangle& Control::getClip() const
- {
- return _clip;
- }
- void Control::setStyle(Theme::Style* style)
- {
- if (style != _style)
- {
- _dirty = true;
- }
- _style = style;
- }
- Theme::Style* Control::getStyle() const
- {
- return _style;
- }
- void Control::setState(State state)
- {
- _state = state;
- _dirty = true;
- }
- Control::State Control::getState() const
- {
- return _state;
- }
- void Control::disable()
- {
- _state = DISABLED;
- _dirty = true;
- }
- void Control::enable()
- {
- _state = NORMAL;
- _dirty = true;
- }
- bool Control::isEnabled()
- {
- return _state != DISABLED;
- }
- Theme::Style::OverlayType Control::getOverlayType() const
- {
- switch (_state)
- {
- case Control::NORMAL:
- return Theme::Style::OVERLAY_NORMAL;
- case Control::FOCUS:
- return Theme::Style::OVERLAY_FOCUS;
- case Control::ACTIVE:
- return Theme::Style::OVERLAY_ACTIVE;
- case Control::DISABLED:
- return Theme::Style::OVERLAY_DISABLED;
- default:
- return Theme::Style::OVERLAY_NORMAL;
- }
- }
- void Control::setConsumeTouchEvents(bool consume)
- {
- _consumeTouchEvents = consume;
- }
-
- bool Control::getConsumeTouchEvents()
- {
- return _consumeTouchEvents;
- }
- void Control::addListener(Control::Listener* listener, int eventFlags)
- {
- GP_ASSERT(listener);
- if ((eventFlags & Listener::PRESS) == Listener::PRESS)
- {
- addSpecificListener(listener, Listener::PRESS);
- }
- if ((eventFlags & Listener::RELEASE) == Listener::RELEASE)
- {
- addSpecificListener(listener, Listener::RELEASE);
- }
- if ((eventFlags & Listener::CLICK) == Listener::CLICK)
- {
- addSpecificListener(listener, Listener::CLICK);
- }
- if ((eventFlags & Listener::VALUE_CHANGED) == Listener::VALUE_CHANGED)
- {
- addSpecificListener(listener, Listener::VALUE_CHANGED);
- }
- if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
- {
- addSpecificListener(listener, Listener::TEXT_CHANGED);
- }
- }
- void Control::addSpecificListener(Control::Listener* listener, Listener::EventType eventType)
- {
- GP_ASSERT(listener);
- if (!_listeners)
- {
- _listeners = new std::map<Listener::EventType, std::list<Listener*>*>();
- }
- std::map<Listener::EventType, std::list<Listener*>*>::const_iterator itr = _listeners->find(eventType);
- if (itr == _listeners->end())
- {
- _listeners->insert(std::make_pair(eventType, new std::list<Listener*>()));
- itr = _listeners->find(eventType);
- }
- std::list<Listener*>* listenerList = itr->second;
- listenerList->push_back(listener);
- }
- bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
- {
- if (!isEnabled())
- {
- return false;
- }
- switch (evt)
- {
- case Touch::TOUCH_PRESS:
- notifyListeners(Listener::PRESS);
- break;
-
- case Touch::TOUCH_RELEASE:
- // Always trigger Listener::RELEASE
- notifyListeners(Listener::RELEASE);
- // Only trigger Listener::CLICK if both PRESS and RELEASE took place within the control's bounds.
- if (x > 0 && x <= _clipBounds.width &&
- y > 0 && y <= _clipBounds.height)
- {
- notifyListeners(Listener::CLICK);
- }
- break;
- }
- return _consumeTouchEvents;
- }
- void Control::keyEvent(Keyboard::KeyEvent evt, int key)
- {
- }
- void Control::notifyListeners(Listener::EventType eventType)
- {
- if (_listeners)
- {
- std::map<Listener::EventType, std::list<Listener*>*>::const_iterator itr = _listeners->find(eventType);
- if (itr != _listeners->end())
- {
- std::list<Listener*>* listenerList = itr->second;
- for (std::list<Listener*>::iterator listenerItr = listenerList->begin(); listenerItr != listenerList->end(); listenerItr++)
- {
- GP_ASSERT(*listenerItr);
- (*listenerItr)->controlEvent(this, eventType);
- }
- }
- }
- }
- void Control::update(const Rectangle& clip)
- {
- // Calculate the bounds.
- float x = clip.x + _bounds.x;
- float y = clip.y + _bounds.y;
- float width = _bounds.width;
- float height = _bounds.height;
- float clipX2 = clip.x + clip.width;
- float x2 = x + width;
- if (x2 > clipX2)
- width = clipX2 - x;
- float clipY2 = clip.y + clip.height;
- float y2 = y + height;
- if (y2 > clipY2)
- height = clipY2 - y;
- _clipBounds.set(_bounds.x, _bounds.y, width, height);
- // Calculate the clipping viewport.
- const Theme::Border& border = getBorder(_state);
- const Theme::Padding& padding = getPadding();
- x += border.left + padding.left;
- y += border.top + padding.top;
- width = _bounds.width - border.left - padding.left - border.right - padding.right;
- height = _bounds.height - border.top - padding.top - border.bottom - padding.bottom;
- _textBounds.set(x, y, width, height);
- clipX2 = clip.x + clip.width;
- x2 = x + width;
- if (x2 > clipX2)
- width = clipX2 - x;
- clipY2 = clip.y + clip.height;
- y2 = y + height;
- if (y2 > clipY2)
- height = clipY2 - y;
- if (x < clip.x)
- x = clip.x;
- if (y < clip.y)
- y = clip.y;
- _clip.set(x, y, width, height);
- _skin = getSkin(_state);
- _opacity = getOpacity(_state);
- }
- void Control::drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip)
- {
- if (!spriteBatch || !_skin || _bounds.width <= 0 || _bounds.height <= 0)
- return;
- Vector2 pos(clip.x + _bounds.x, clip.y + _bounds.y);
- // Get the border and background images for this control's current state.
- const Theme::UVs& topLeft = _skin->getUVs(Theme::Skin::TOP_LEFT);
- const Theme::UVs& top = _skin->getUVs(Theme::Skin::TOP);
- const Theme::UVs& topRight = _skin->getUVs(Theme::Skin::TOP_RIGHT);
- const Theme::UVs& left = _skin->getUVs(Theme::Skin::LEFT);
- const Theme::UVs& center = _skin->getUVs(Theme::Skin::CENTER);
- const Theme::UVs& right = _skin->getUVs(Theme::Skin::RIGHT);
- const Theme::UVs& bottomLeft = _skin->getUVs(Theme::Skin::BOTTOM_LEFT);
- const Theme::UVs& bottom = _skin->getUVs(Theme::Skin::BOTTOM);
- const Theme::UVs& bottomRight = _skin->getUVs(Theme::Skin::BOTTOM_RIGHT);
- // Calculate screen-space positions.
- const Theme::Border& border = getBorder(_state);
- const Theme::Padding& padding = getPadding();
- Vector4 skinColor = _skin->getColor();
- skinColor.w *= _opacity;
- float midWidth = _bounds.width - border.left - border.right;
- float midHeight = _bounds.height - border.top - border.bottom;
- float midX = pos.x + border.left;
- float midY = pos.y + border.top;
- float rightX = pos.x + _bounds.width - border.right;
- float bottomY = pos.y + _bounds.height - border.bottom;
- // Draw themed border sprites.
- if (!border.left && !border.right && !border.top && !border.bottom)
- {
- // No border, just draw the image.
- spriteBatch->draw(pos.x, pos.y, _bounds.width, _bounds.height, center.u1, center.v1, center.u2, center.v2, skinColor, clip);
- }
- else
- {
- if (border.left && border.top)
- spriteBatch->draw(pos.x, pos.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, skinColor, clip);
- if (border.top)
- spriteBatch->draw(pos.x + border.left, pos.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, skinColor, clip);
- if (border.right && border.top)
- spriteBatch->draw(rightX, pos.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, skinColor, clip);
- if (border.left)
- spriteBatch->draw(pos.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, skinColor, clip);
- if (border.left && border.right && border.top && border.bottom)
- spriteBatch->draw(pos.x + border.left, pos.y + border.top, _bounds.width - border.left - border.right, _bounds.height - border.top - border.bottom,
- center.u1, center.v1, center.u2, center.v2, skinColor, clip);
- if (border.right)
- spriteBatch->draw(rightX, midY, border.right, midHeight, right.u1, right.v1, right.u2, right.v2, skinColor, clip);
- if (border.bottom && border.left)
- spriteBatch->draw(pos.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, skinColor, clip);
- if (border.bottom)
- spriteBatch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, skinColor, clip);
- if (border.bottom && border.right)
- spriteBatch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, skinColor, clip);
- }
- }
- void Control::drawImages(SpriteBatch* spriteBatch, const Rectangle& position)
- {
- }
- void Control::drawText(const Rectangle& position)
- {
- }
- bool Control::isDirty()
- {
- return _dirty;
- }
- bool Control::isContainer()
- {
- return false;
- }
- Control::State Control::getState(const char* state)
- {
- if (!state)
- {
- return NORMAL;
- }
- if (strcmp(state, "NORMAL") == 0)
- {
- return NORMAL;
- }
- else if (strcmp(state, "ACTIVE") == 0)
- {
- return ACTIVE;
- }
- else if (strcmp(state, "FOCUS") == 0)
- {
- return FOCUS;
- }
- else if (strcmp(state, "DISABLED") == 0)
- {
- return DISABLED;
- }
- return NORMAL;
- }
- Theme::ThemeImage* Control::getImage(const char* id, State state)
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- Theme::ImageList* imageList = overlay->getImageList();
- GP_ASSERT(imageList);
- return imageList->getImage(id);
- }
- // Implementation of AnimationHandler
- unsigned int Control::getAnimationPropertyComponentCount(int propertyId) const
- {
- switch(propertyId)
- {
- case ANIMATE_POSITION:
- case ANIMATE_SIZE:
- return 2;
- case ANIMATE_POSITION_X:
- case ANIMATE_POSITION_Y:
- case ANIMATE_SIZE_WIDTH:
- case ANIMATE_SIZE_HEIGHT:
- case ANIMATE_OPACITY:
- return 1;
- default:
- return -1;
- }
- }
- void Control::getAnimationPropertyValue(int propertyId, AnimationValue* value)
- {
- GP_ASSERT(value);
- switch(propertyId)
- {
- case ANIMATE_POSITION:
- value->setFloat(0, _bounds.x);
- value->setFloat(1, _bounds.y);
- break;
- case ANIMATE_SIZE:
- value->setFloat(0, _bounds.width);
- value->setFloat(1, _bounds.height);
- break;
- case ANIMATE_POSITION_X:
- value->setFloat(0, _bounds.x);
- break;
- case ANIMATE_POSITION_Y:
- value->setFloat(0, _bounds.y);
- break;
- case ANIMATE_SIZE_WIDTH:
- value->setFloat(0, _bounds.width);
- break;
- case ANIMATE_SIZE_HEIGHT:
- value->setFloat(0, _bounds.height);
- break;
- case ANIMATE_OPACITY:
- default:
- break;
- }
- }
- void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
- {
- GP_ASSERT(value);
- switch(propertyId)
- {
- case ANIMATE_POSITION:
- _bounds.x = Curve::lerp(blendWeight, _bounds.x, value->getFloat(0));
- _bounds.y = Curve::lerp(blendWeight, _bounds.y, value->getFloat(1));
- _dirty = true;
- break;
- case ANIMATE_POSITION_X:
- _bounds.x = Curve::lerp(blendWeight, _bounds.x, value->getFloat(0));
- _dirty = true;
- break;
- case ANIMATE_POSITION_Y:
- _bounds.y = Curve::lerp(blendWeight, _bounds.y, value->getFloat(0));
- _dirty = true;
- break;
- case ANIMATE_SIZE:
- _bounds.width = Curve::lerp(blendWeight, _bounds.width, value->getFloat(0));
- _bounds.height = Curve::lerp(blendWeight, _bounds.height, value->getFloat(1));
- _dirty = true;
- break;
- case ANIMATE_SIZE_WIDTH:
- _bounds.width = Curve::lerp(blendWeight, _bounds.width, value->getFloat(0));
- _dirty = true;
- break;
- case ANIMATE_SIZE_HEIGHT:
- _bounds.height = Curve::lerp(blendWeight, _bounds.height, value->getFloat(0));
- _dirty = true;
- break;
- case ANIMATE_OPACITY:
- _dirty = true;
- default:
- break;
- }
- }
-
- Theme::Style::Overlay** Control::getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays)
- {
- GP_ASSERT(overlays);
- GP_ASSERT(_style);
- unsigned int index = 0;
- if ((overlayTypes & NORMAL) == NORMAL)
- {
- overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
- }
- if ((overlayTypes & FOCUS) == FOCUS)
- {
- overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
- }
- if ((overlayTypes & ACTIVE) == ACTIVE)
- {
- overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
- }
- if ((overlayTypes & DISABLED) == DISABLED)
- {
- overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
- }
- return overlays;
- }
- Theme::Style::Overlay* Control::getOverlay(State state) const
- {
- GP_ASSERT(_style);
- switch(state)
- {
- case Control::NORMAL:
- return _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
- case Control::FOCUS:
- return _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
- case Control::ACTIVE:
- return _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
- case Control::DISABLED:
- return _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
- default:
- return NULL;
- }
- }
- void Control::overrideStyle()
- {
- if (_styleOverridden)
- {
- return;
- }
- // Copy the style.
- GP_ASSERT(_style);
- _style = new Theme::Style(*_style);
- _styleOverridden = true;
- }
- void Control::overrideThemedProperties(Properties* properties, unsigned char states)
- {
- GP_ASSERT(properties);
- GP_ASSERT(_style);
- GP_ASSERT(_style->_theme);
- Theme::ImageList* imageList = NULL;
- Theme::ThemeImage* cursor = NULL;
- Theme::Skin* skin = NULL;
- _style->_theme->lookUpSprites(properties, &imageList, &cursor, &skin);
- if (imageList)
- {
- setImageList(imageList, states);
- }
- if (cursor)
- {
- setCursor(cursor, states);
- }
- if (skin)
- {
- setSkin(skin, states);
- }
- if (properties->exists("font"))
- {
- Font* font = Font::create(properties->getString("font"));
- setFont(font, states);
- font->release();
- }
- if (properties->exists("fontSize"))
- {
- setFontSize(properties->getInt("fontSize"), states);
- }
- if (properties->exists("textColor"))
- {
- Vector4 textColor(0, 0, 0, 1);
- properties->getColor("textColor", &textColor);
- setTextColor(textColor, states);
- }
- if (properties->exists("textAlignment"))
- {
- setTextAlignment(Font::getJustify(properties->getString("textAlignment")), states);
- }
- if (properties->exists("rightToLeft"))
- {
- setTextRightToLeft(properties->getBool("rightToLeft"), states);
- }
- if (properties->exists("opacity"))
- {
- setOpacity(properties->getFloat("opacity"), states);
- }
- }
- void Control::setImageList(Theme::ImageList* imageList, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setImageList(imageList);
- }
- _dirty = true;
- }
- void Control::setCursor(Theme::ThemeImage* cursor, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setCursor(cursor);
- }
- _dirty = true;
- }
- void Control::setSkin(Theme::Skin* skin, unsigned char states)
- {
- overrideStyle();
- Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
- getOverlays(states, overlays);
- for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
- {
- overlays[i]->setSkin(skin);
- }
- _dirty = true;
- }
- Theme::Skin* Control::getSkin(State state)
- {
- Theme::Style::Overlay* overlay = getOverlay(state);
- GP_ASSERT(overlay);
- return overlay->getSkin();
- }
- Control::Alignment Control::getAlignment(const char* alignment)
- {
- if (!alignment)
- {
- return Control::ALIGN_TOP_LEFT;
- }
- if (strcmp(alignment, "ALIGN_LEFT") == 0)
- {
- return Control::ALIGN_LEFT;
- }
- else if (strcmp(alignment, "ALIGN_HCENTER") == 0)
- {
- return Control::ALIGN_HCENTER;
- }
- else if (strcmp(alignment, "ALIGN_RIGHT") == 0)
- {
- return Control::ALIGN_RIGHT;
- }
- else if (strcmp(alignment, "ALIGN_TOP") == 0)
- {
- return Control::ALIGN_TOP;
- }
- else if (strcmp(alignment, "ALIGN_VCENTER") == 0)
- {
- return Control::ALIGN_VCENTER;
- }
- else if (strcmp(alignment, "ALIGN_BOTTOM") == 0)
- {
- return Control::ALIGN_BOTTOM;
- }
- else if (strcmp(alignment, "ALIGN_TOP_LEFT") == 0)
- {
- return Control::ALIGN_TOP_LEFT;
- }
- else if (strcmp(alignment, "ALIGN_VCENTER_LEFT") == 0)
- {
- return Control::ALIGN_VCENTER_LEFT;
- }
- else if (strcmp(alignment, "ALIGN_BOTTOM_LEFT") == 0)
- {
- return Control::ALIGN_BOTTOM_LEFT;
- }
- else if (strcmp(alignment, "ALIGN_TOP_HCENTER") == 0)
- {
- return Control::ALIGN_TOP_HCENTER;
- }
- else if (strcmp(alignment, "ALIGN_VCENTER_HCENTER") == 0)
- {
- return Control::ALIGN_VCENTER_HCENTER;
- }
- else if (strcmp(alignment, "ALIGN_BOTTOM_HCENTER") == 0)
- {
- return Control::ALIGN_BOTTOM_HCENTER;
- }
- else if (strcmp(alignment, "ALIGN_TOP_RIGHT") == 0)
- {
- return Control::ALIGN_TOP_RIGHT;
- }
- else if (strcmp(alignment, "ALIGN_VCENTER_RIGHT") == 0)
- {
- return Control::ALIGN_VCENTER_RIGHT;
- }
- else if (strcmp(alignment, "ALIGN_BOTTOM_RIGHT") == 0)
- {
- return Control::ALIGN_BOTTOM_RIGHT;
- }
- else
- {
- GP_ERROR("Failed to get corresponding control alignment for unsupported value \'%s\'.", alignment);
- }
- // Default.
- return Control::ALIGN_TOP_LEFT;
- }
- }
|