mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 05:53:26 +02:00
text edit improvments (not finished)
This commit is contained in:
@@ -111,7 +111,7 @@ void UIContainer::onInputEvent(const InputEvent& event)
|
||||
}
|
||||
// mouse events
|
||||
} else if(event.type & EV_MOUSE) {
|
||||
// mouse down and weel events only go to elements that contains the mouse position
|
||||
// mouse down and wheel events only go to elements that contains the mouse position
|
||||
if(event.type & EV_DOWN || event.type & EV_MOUSE_WHEEL) {
|
||||
if(child->getRect().contains(event.mousePos)) {
|
||||
// focus it
|
||||
@@ -134,7 +134,11 @@ void UIContainer::onInputEvent(const InputEvent& event)
|
||||
|
||||
void UIContainer::setFocusedElement(UIElementPtr focusedElement)
|
||||
{
|
||||
if(m_focusedElement)
|
||||
if(m_focusedElement) {
|
||||
m_focusedElement->setFocused(false);
|
||||
m_focusedElement->onFocusChange();
|
||||
}
|
||||
m_focusedElement = focusedElement;
|
||||
m_focusedElement->setFocused(true);
|
||||
m_focusedElement->onFocusChange();
|
||||
}
|
||||
|
@@ -48,6 +48,7 @@ public:
|
||||
|
||||
virtual void render();
|
||||
virtual void onInputEvent(const InputEvent& event) { }
|
||||
virtual void onFocusChange() { }
|
||||
|
||||
UIElementPtr backwardsGetElementById(const std::string& id);
|
||||
|
||||
|
@@ -23,74 +23,45 @@
|
||||
|
||||
|
||||
#include "uitextedit.h"
|
||||
#include "uitexteditskin.h"
|
||||
#include "graphics/fonts.h"
|
||||
|
||||
UITextEdit::UITextEdit() :
|
||||
UIElement(UI::TextEdit),
|
||||
m_cursorPos(0),
|
||||
m_startRenderPos(0)
|
||||
UIElement(UI::TextEdit)
|
||||
{
|
||||
|
||||
UITextEditSkin *skin = static_cast<UITextEditSkin*>(getSkin());
|
||||
m_textArea.setFont(skin->getFont());
|
||||
m_textArea.enableCursor();
|
||||
}
|
||||
|
||||
void UITextEdit::onInputEvent(const InputEvent& event)
|
||||
{
|
||||
if(event.type == EV_TEXT_ENTER) {
|
||||
appendCharacter(event.keychar);
|
||||
m_textArea.appendCharacter(event.keychar);
|
||||
} else if(event.type == EV_KEY_DOWN) {
|
||||
if(event.keycode == KC_DELETE)
|
||||
removeCharacter(true);
|
||||
m_textArea.removeCharacter(true);
|
||||
else if(event.keycode == KC_BACK)
|
||||
removeCharacter(false);
|
||||
else if(event.keycode == KC_RIGHT) {
|
||||
if(m_cursorPos < m_text.length())
|
||||
m_cursorPos++;
|
||||
} else if(event.keycode == KC_LEFT) {
|
||||
if(m_cursorPos > 0)
|
||||
m_cursorPos--;
|
||||
}
|
||||
m_textArea.removeCharacter(false);
|
||||
else if(event.keycode == KC_RIGHT)
|
||||
m_textArea.moveCursor(true);
|
||||
else if(event.keycode == KC_LEFT)
|
||||
m_textArea.moveCursor(false);
|
||||
}
|
||||
}
|
||||
|
||||
void UITextEdit::clearText()
|
||||
{
|
||||
m_text = "";
|
||||
m_cursorPos = 0;
|
||||
}
|
||||
|
||||
void UITextEdit::setText(const std::string& text)
|
||||
{
|
||||
m_text = text;
|
||||
m_cursorPos = 0;
|
||||
}
|
||||
|
||||
void UITextEdit::appendCharacter(char c)
|
||||
{
|
||||
std::string tmp;
|
||||
tmp = c;
|
||||
m_text.insert(m_cursorPos, tmp);
|
||||
m_cursorPos++;
|
||||
}
|
||||
|
||||
void UITextEdit::removeCharacter(bool right)
|
||||
{
|
||||
if(right && m_cursorPos < m_text.length())
|
||||
m_text.erase(m_text.begin() + m_cursorPos);
|
||||
else if(m_text.length() >= m_cursorPos && m_cursorPos > 0)
|
||||
m_text.erase(m_text.begin() + (--m_cursorPos));
|
||||
}
|
||||
|
||||
void UITextEdit::setCursorPos(uint pos)
|
||||
{
|
||||
if(pos > m_text.length())
|
||||
m_cursorPos = m_text.length();
|
||||
else
|
||||
m_cursorPos = pos;
|
||||
}
|
||||
|
||||
void UITextEdit::onLayoutRectChange(const Rect& rect)
|
||||
{
|
||||
m_textRect = rect;
|
||||
UITextEditSkin *skin = static_cast<UITextEditSkin*>(getSkin());
|
||||
Rect textRect = rect;
|
||||
int margin = skin->getTextMargin();
|
||||
textRect.setLeft(textRect.left()+margin);
|
||||
textRect.setRight(textRect.right()-margin);
|
||||
m_textArea.setScreenCoords(textRect);
|
||||
}
|
||||
|
||||
void UITextEdit::onFocusChange()
|
||||
{
|
||||
m_textArea.setCursorVisible(isFocused());
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,7 @@
|
||||
|
||||
#include "prerequisites.h"
|
||||
#include "uielement.h"
|
||||
#include "graphics/textarea.h"
|
||||
|
||||
class Font;
|
||||
|
||||
@@ -37,24 +38,16 @@ public:
|
||||
|
||||
void onInputEvent(const InputEvent& event);
|
||||
|
||||
void clearText();
|
||||
void setText(const std::string& text);
|
||||
const std::string& getText() const { return m_text; }
|
||||
const std::string& getText() const { return m_textArea.getText(); }
|
||||
|
||||
void setCursorPos(uint pos);
|
||||
uint getCursorPos() { return m_cursorPos; }
|
||||
TextArea& getTextArea() { return m_textArea; }
|
||||
|
||||
void onLayoutRectChange(const Rect& rect);
|
||||
void onFocusChange();
|
||||
|
||||
private:
|
||||
void appendCharacter(char c);
|
||||
void removeCharacter(bool right);
|
||||
void recalculate();
|
||||
|
||||
Rect m_textRect;
|
||||
uint m_cursorPos;
|
||||
int m_startRenderPos;
|
||||
std::string m_text;
|
||||
TextArea m_textArea;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UITextEdit> UITextEditPtr;
|
||||
|
@@ -51,5 +51,5 @@ void UITextEditSkin::draw(UIElement* element)
|
||||
UIElementSkin::draw(element);
|
||||
|
||||
UITextEdit *textEdit = static_cast<UITextEdit*>(element);
|
||||
m_font->renderText(textEdit->getText(), textEdit->getRect(), ALIGN_TOP_LEFT, m_textColor);
|
||||
textEdit->getTextArea().draw();
|
||||
}
|
||||
|
Reference in New Issue
Block a user