ui improvements

This commit is contained in:
Eduardo Bart
2011-04-16 19:06:42 -03:00
parent dc39c965cc
commit c5da620d59
11 changed files with 150 additions and 42 deletions

View File

@@ -45,11 +45,23 @@ void UIContainer::addChild(UIElementPtr child)
void UIContainer::removeChild(UIElementPtr child)
{
// first check if its really a child
bool removed = false;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == child) {
removed = true;
m_children.erase(it);
break;
}
}
assert(removed);
if(m_focusedElement == child)
setFocusedElement(UIElementPtr());
m_children.remove(child);
if(child->getParent() == shared_from_this())
child->setParent(UIContainerPtr());
// child must have this container as parent
assert(child->getParent() == asUIContainer());
child->setParent(UIContainerPtr());
}
UIElementPtr UIContainer::getChildById(const std::string& id)
@@ -57,9 +69,8 @@ UIElementPtr UIContainer::getChildById(const std::string& id)
if(getId() == id)
return asUIElement();
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->getId() == id) {
if((*it)->getId() == id)
return (*it);
}
}
return UIElementPtr();
}
@@ -98,6 +109,7 @@ void UIContainer::render()
void UIContainer::onInputEvent(const InputEvent& event)
{
UIElementPtr focusedElement = m_focusedElement;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
bool shouldFire = false;
@@ -106,7 +118,7 @@ void UIContainer::onInputEvent(const InputEvent& event)
if(child->isEnabled() && child->isVisible()) {
if(event.type & EV_KEYBOARD) {
// keyboard events only go to focused elements or containers
if(child->asUIContainer() || child == getFocusedElement()) {
if(child->asUIContainer() || child == focusedElement) {
shouldFire = true;
}
// mouse events
@@ -130,13 +142,69 @@ void UIContainer::onInputEvent(const InputEvent& event)
}
}
void UIContainer::focusNextElement()
{
UIElementPtr element;
auto focusedIt = std::find(m_children.begin(), m_children.end(), m_focusedElement);
if(focusedIt != m_children.end()) {
for(auto it = ++focusedIt; it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
if(child->isFocusable()) {
element = child;
break;
}
}
}
if(!element) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
if(child->isFocusable()) {
element = child;
break;
}
}
}
if(element)
setFocusedElement(element);
}
void UIContainer::setFocusedElement(UIElementPtr focusedElement)
{
if(m_focusedElement) {
m_focusedElement->setFocused(false);
if(focusedElement != m_focusedElement) {
if(m_focusedElement) {
m_focusedElement->setFocused(false);
m_focusedElement->onFocusChange();
}
m_focusedElement = focusedElement;
m_focusedElement->setFocused(true);
m_focusedElement->onFocusChange();
}
m_focusedElement = focusedElement;
m_focusedElement->setFocused(true);
m_focusedElement->onFocusChange();
}
bool UIContainer::lockElement(UIElementPtr element)
{
bool found = false;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == element) {
(*it)->setEnabled(true);
found = true;
break;
}
}
if(found) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) != element)
(*it)->setEnabled(false);
}
return true;
}
return false;
}
void UIContainer::unlockElement()
{
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
(*it)->setEnabled(true);
}
}

View File

@@ -31,25 +31,38 @@
class UIContainer : public UIElement
{
public:
UIContainer(UI::EElementType type = UI::Container) : UIElement(type) { }
UIContainer(UI::EElementType type = UI::Container) :
UIElement(type) { }
virtual ~UIContainer() { }
virtual void render();
virtual void onInputEvent(const InputEvent& event);
/// Add an element, this must never be called from events loops
void addChild(UIElementPtr child);
/// Remove an element, this must never be called from events loops
void removeChild(UIElementPtr child);
/// Find an element in this container by id
UIElementPtr getChildById(const std::string& id);
/// Find an element in this container and in its children by id
UIElementPtr recursiveGetChildById(const std::string& id);
/// Disable all children except the specified element
bool lockElement(UIElementPtr element);
/// Renable all children
void unlockElement();
/// Focus next element
void focusNextElement();
/// Focus element
void setFocusedElement(UIElementPtr focusedElement);
/// Get focused element
UIElementPtr getFocusedElement() const { return m_focusedElement; }
virtual UI::EElementType getElementType() const { return UI::Container; }
UIContainerPtr asUIContainer() { return boost::static_pointer_cast<UIContainer>(shared_from_this()); }
/// Get root container (the container that contains everything)
static UIContainerPtr& getRootContainer();
private:

View File

@@ -26,6 +26,7 @@
#include "uiskins.h"
#include "uielementskin.h"
#include "graphics/graphics.h"
#include <core/dispatcher.h>
UIElement::UIElement(UI::EElementType type) :
UILayout(),
@@ -38,6 +39,16 @@ UIElement::UIElement(UI::EElementType type) :
}
void UIElement::destroy()
{
// we must always have a parent when destroying
assert(getParent());
// we cant delete now, as this call maybe in a event loop
g_dispatcher.addTask(boost::bind(&UIContainer::removeChild, getParent(), asUIContainer()));
// shared ptr must have 4 refs, (this + removeChild callback + parent + use_count call)
assert(asUIElement().use_count() == 4);
}
bool UIElement::setSkin(const std::string& skinName)
{
setSkin(g_uiSkins.getElementSkin(m_type, skinName));
@@ -49,7 +60,7 @@ void UIElement::setSkin(UIElementSkin* skin)
m_skin = skin;
if(skin && !getRect().isValid()) {
setSize(skin->getDefaultSize());
skin->onSkinApply(this);
skin->apply(this);
}
}

View File

@@ -46,7 +46,13 @@ public:
UIElement(UI::EElementType type = UI::Element);
virtual ~UIElement() { }
/// Destroy this element by removing it from its parent
void destroy();
/// Draw element
virtual void render();
// events
virtual void onInputEvent(const InputEvent& event) { }
virtual void onFocusChange() { }

View File

@@ -40,7 +40,7 @@ public:
virtual ~UIElementSkin() { }
virtual void load(const YAML::Node& node);
virtual void onSkinApply(UIElement *element) { }
virtual void apply(UIElement *element) { }
virtual void draw(UIElement *element);
const std::string& getName() const { return m_name; }

View File

@@ -106,7 +106,7 @@ void UILayout::addAnchoredElement(UILayoutPtr anchoredElement)
}
}
// if not anchor it
// if not, anchor it
if(!found)
m_anchoredElements.push_back(anchoredElement);
}

View File

@@ -25,6 +25,7 @@
#include "uitextedit.h"
#include "uitexteditskin.h"
#include "graphics/fonts.h"
#include "uicontainer.h"
UITextEdit::UITextEdit() :
UIElement(UI::TextEdit)
@@ -37,18 +38,20 @@ void UITextEdit::onInputEvent(const InputEvent& event)
if(event.type == EV_TEXT_ENTER) {
m_textArea.appendCharacter(event.keychar);
} else if(event.type == EV_KEY_DOWN) {
if(event.keycode == KC_DELETE)
if(event.keycode == KC_DELETE) // erase right character
m_textArea.removeCharacter(true);
else if(event.keycode == KC_BACK)
else if(event.keycode == KC_BACK) // erase left character
m_textArea.removeCharacter(false);
else if(event.keycode == KC_RIGHT)
else if(event.keycode == KC_RIGHT) // move cursor right
m_textArea.moveCursor(true);
else if(event.keycode == KC_LEFT)
else if(event.keycode == KC_LEFT) // move cursor left
m_textArea.moveCursor(false);
else if(event.keycode == KC_HOME)
else if(event.keycode == KC_HOME) // move cursor to first character
m_textArea.setCursorPos(0);
else if(event.keycode == KC_END)
else if(event.keycode == KC_END) // move cursor to last character
m_textArea.setCursorPos(m_textArea.getText().length());
else if(event.keycode == KC_TAB) // focus next parent element
getParent()->focusNextElement();
} else if(event.type == EV_MOUSE_LDOWN) {
} else if(event.type == EV_MOUSE_LUP && getRect().contains(event.mousePos)) {

View File

@@ -46,7 +46,7 @@ void UITextEditSkin::load(const YAML::Node& node)
m_textMargin = 2;
}
void UITextEditSkin::onSkinApply(UIElement* element)
void UITextEditSkin::apply(UIElement* element)
{
UITextEdit *textEdit = static_cast<UITextEdit*>(element);
textEdit->getTextArea().setFont(m_font);

View File

@@ -38,7 +38,7 @@ public:
UIElementSkin(name, UI::TextEdit) { }
void load(const YAML::Node& node);
void onSkinApply(UIElement *element);
void apply(UIElement *element);
void draw(UIElement *element);
Font *getFont() const { return m_font; }