mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
remake ui event system and reimplement line edit
This commit is contained in:
@@ -1,334 +0,0 @@
|
||||
#include "textarea.h"
|
||||
#include "font.h"
|
||||
#include "graphics.h"
|
||||
|
||||
#include <core/platform.h>
|
||||
|
||||
TextArea::TextArea() :
|
||||
m_align(AlignLeftCenter),
|
||||
m_color(Color::white),
|
||||
m_cursorPos(-1),
|
||||
m_startRenderPos(0),
|
||||
m_cursorVisible(false)
|
||||
{
|
||||
}
|
||||
|
||||
TextArea::TextArea(FontPtr font,
|
||||
const std::string& text,
|
||||
const Rect& screenCoords,
|
||||
AlignmentFlag align,
|
||||
const Color& color) :
|
||||
m_font(font),
|
||||
m_text(text),
|
||||
m_screenCoords(screenCoords),
|
||||
m_align(align),
|
||||
m_color(color),
|
||||
m_cursorPos(-1),
|
||||
m_startRenderPos(0),
|
||||
m_cursorVisible(false)
|
||||
{
|
||||
recalculate();
|
||||
}
|
||||
|
||||
void TextArea::draw()
|
||||
{
|
||||
//TODO: text rendering could be much optimized by using vertex buffer or caching the render into a texture
|
||||
|
||||
int textLength = m_text.length();
|
||||
const TexturePtr& texture = m_font->getTexture();
|
||||
for(int i=0;i<textLength;++i) {
|
||||
g_graphics.drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i], m_color);
|
||||
}
|
||||
|
||||
// render cursor
|
||||
if(m_cursorVisible && m_cursorPos >= 0) {
|
||||
assert(m_cursorPos <= textLength);
|
||||
const int delay = 500;
|
||||
int ticks = g_platform.getTicks();
|
||||
// draw every 500ms
|
||||
if(ticks - m_cursorTicks <= delay) {
|
||||
Rect cursorRect;
|
||||
// when cursor is at 0 or is the first visible element
|
||||
if(m_cursorPos == 0 || m_cursorPos == m_startRenderPos)
|
||||
cursorRect = Rect(m_drawArea.left()-1, m_drawArea.top(), 1, m_font->getGlyphHeight());
|
||||
else
|
||||
cursorRect = Rect(m_glyphsCoords[m_cursorPos-1].right(), m_glyphsCoords[m_cursorPos-1].top(), 1, m_font->getGlyphHeight());
|
||||
g_graphics.drawFilledRect(cursorRect, m_color);
|
||||
} else if(ticks - m_cursorTicks >= 2*delay) {
|
||||
m_cursorTicks = ticks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::recalculate()
|
||||
{
|
||||
int textLength = m_text.length();
|
||||
|
||||
// prevent glitches
|
||||
if(!m_screenCoords.isValid() || !m_font)
|
||||
return;
|
||||
|
||||
// map glyphs positions
|
||||
Size textBoxSize;
|
||||
const std::vector<Point>& glyphsPositions = m_font->calculateGlyphsPositions(m_text, m_align, &textBoxSize);
|
||||
const Rect *glyphsTextureCoords = m_font->getGlyphsTextureCoords();
|
||||
const Size *glyphsSize = m_font->getGlyphsSize();
|
||||
int glyph;
|
||||
|
||||
// resize just on demand
|
||||
if(textLength > (int)m_glyphsCoords.size()) {
|
||||
m_glyphsCoords.resize(textLength);
|
||||
m_glyphsTexCoords.resize(textLength);
|
||||
}
|
||||
|
||||
// readjust start view area based on cursor position
|
||||
if(m_cursorPos >= 0 && textLength > 0) {
|
||||
assert(m_cursorPos <= textLength);
|
||||
if(m_cursorPos < m_startRenderPos) // cursor is before the previuos first rendered glyph, so we need to update
|
||||
{
|
||||
m_startInternalPos.x = glyphsPositions[m_cursorPos].x;
|
||||
m_startInternalPos.y = glyphsPositions[m_cursorPos].y - m_font->getTopMargin();
|
||||
m_startRenderPos = m_cursorPos;
|
||||
} else if(m_cursorPos > m_startRenderPos || // cursor is after the previuos first rendered glyph
|
||||
(m_cursorPos == m_startRenderPos && textLength == m_cursorPos)) // cursor is at the previuos rendered element, and is the last text element
|
||||
{
|
||||
Rect virtualRect(m_startInternalPos, m_screenCoords.size()); // previous rendered virtual rect
|
||||
int pos = m_cursorPos - 1; // element before cursor
|
||||
glyph = (uchar)m_text[pos]; // glyph of the element before cursor
|
||||
Rect glyphRect(glyphsPositions[pos], glyphsSize[glyph]);
|
||||
|
||||
// if the cursor is not on the previous rendered virtual rect we need to update it
|
||||
if(!virtualRect.contains(glyphRect.topLeft()) || !virtualRect.contains(glyphRect.bottomRight())) {
|
||||
// calculate where is the first glyph visible
|
||||
Point startGlyphPos;
|
||||
startGlyphPos.y = std::max(glyphRect.bottom() - virtualRect.height(), 0);
|
||||
startGlyphPos.x = std::max(glyphRect.right() - virtualRect.width(), 0);
|
||||
|
||||
// find that glyph
|
||||
for(pos = 0; pos < textLength; ++pos) {
|
||||
glyph = (uchar)m_text[pos];
|
||||
glyphRect = Rect(glyphsPositions[pos], glyphsSize[glyph]);
|
||||
glyphRect.setTop(std::max(glyphRect.top() - m_font->getTopMargin() - m_font->getGlyphSpacing().height(), 0));
|
||||
glyphRect.setLeft(std::max(glyphRect.left() - m_font->getGlyphSpacing().width(), 0));
|
||||
|
||||
// first glyph entirely visible found
|
||||
if(glyphRect.topLeft() >= startGlyphPos) {
|
||||
m_startInternalPos.x = glyphsPositions[pos].x;
|
||||
m_startInternalPos.y = glyphsPositions[pos].y - m_font->getTopMargin();
|
||||
m_startRenderPos = pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m_startInternalPos = Point(0,0);
|
||||
}
|
||||
|
||||
m_drawArea = m_screenCoords;
|
||||
|
||||
if(m_align & AlignBottom) {
|
||||
m_drawArea.translate(0, m_screenCoords.height() - textBoxSize.height());
|
||||
} else if(m_align & AlignVerticalCenter) {
|
||||
m_drawArea.translate(0, (m_screenCoords.height() - textBoxSize.height()) / 2);
|
||||
} else { // AlignTop
|
||||
}
|
||||
|
||||
if(m_align & AlignRight) {
|
||||
m_drawArea.translate(m_screenCoords.width() - textBoxSize.width(), 0);
|
||||
} else if(m_align & AlignHorizontalCenter) {
|
||||
m_drawArea.translate((m_screenCoords.width() - textBoxSize.width()) / 2, 0);
|
||||
} else { // AlignLeft
|
||||
|
||||
}
|
||||
|
||||
for(int i = 0; i < textLength; ++i) {
|
||||
glyph = (uchar)m_text[i];
|
||||
m_glyphsCoords[i].clear();
|
||||
|
||||
// skip invalid glyphs
|
||||
if(glyph < 32)
|
||||
continue;
|
||||
|
||||
// calculate initial glyph rect and texture coords
|
||||
Rect glyphScreenCoords(glyphsPositions[i], glyphsSize[glyph]);
|
||||
Rect glyphTextureCoords = glyphsTextureCoords[glyph];
|
||||
|
||||
// first translate to align position
|
||||
if(m_align & AlignBottom) {
|
||||
glyphScreenCoords.translate(0, m_screenCoords.height() - textBoxSize.height());
|
||||
} else if(m_align & AlignVerticalCenter) {
|
||||
glyphScreenCoords.translate(0, (m_screenCoords.height() - textBoxSize.height()) / 2);
|
||||
} else { // AlignTop
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
if(m_align & AlignRight) {
|
||||
glyphScreenCoords.translate(m_screenCoords.width() - textBoxSize.width(), 0);
|
||||
} else if(m_align & AlignHorizontalCenter) {
|
||||
glyphScreenCoords.translate((m_screenCoords.width() - textBoxSize.width()) / 2, 0);
|
||||
} else { // AlignLeft
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
// only render glyphs that are after startRenderPosition
|
||||
if(glyphScreenCoords.bottom() < m_startInternalPos.y || glyphScreenCoords.right() < m_startInternalPos.x)
|
||||
continue;
|
||||
|
||||
// bound glyph topLeft to startRenderPosition
|
||||
if(glyphScreenCoords.top() < m_startInternalPos.y) {
|
||||
glyphTextureCoords.setTop(glyphTextureCoords.top() + (m_startInternalPos.y - glyphScreenCoords.top()));
|
||||
glyphScreenCoords.setTop(m_startInternalPos.y);
|
||||
}
|
||||
if(glyphScreenCoords.left() < m_startInternalPos.x) {
|
||||
glyphTextureCoords.setLeft(glyphTextureCoords.left() + (m_startInternalPos.x - glyphScreenCoords.left()));
|
||||
glyphScreenCoords.setLeft(m_startInternalPos.x);
|
||||
}
|
||||
|
||||
// subtract startInternalPos
|
||||
glyphScreenCoords.translate(-m_startInternalPos);
|
||||
|
||||
// translate rect to screen coords
|
||||
glyphScreenCoords.translate(m_screenCoords.topLeft());
|
||||
|
||||
// only render if glyph rect is visible on screenCoords
|
||||
if(!m_screenCoords.intersects(glyphScreenCoords))
|
||||
continue;
|
||||
|
||||
// bound glyph bottomRight to screenCoords bottomRight
|
||||
if(glyphScreenCoords.bottom() > m_screenCoords.bottom()) {
|
||||
glyphTextureCoords.setBottom(glyphTextureCoords.bottom() + (m_screenCoords.bottom() - glyphScreenCoords.bottom()));
|
||||
glyphScreenCoords.setBottom(m_screenCoords.bottom());
|
||||
}
|
||||
if(glyphScreenCoords.right() > m_screenCoords.right()) {
|
||||
glyphTextureCoords.setRight(glyphTextureCoords.right() + (m_screenCoords.right() - glyphScreenCoords.right()));
|
||||
glyphScreenCoords.setRight(m_screenCoords.right());
|
||||
}
|
||||
|
||||
// render glyph
|
||||
m_glyphsCoords[i] = glyphScreenCoords;
|
||||
m_glyphsTexCoords[i] = glyphTextureCoords;
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::setFont(FontPtr font)
|
||||
{
|
||||
if(m_font != font) {
|
||||
m_font = font;
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::setText(const std::string& text)
|
||||
{
|
||||
if(m_text != text) {
|
||||
m_text = text;
|
||||
if(m_cursorPos >= 0) {
|
||||
m_cursorPos = 0;
|
||||
m_cursorTicks = g_platform.getTicks();
|
||||
}
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::setScreenCoords(const Rect& screenCoords)
|
||||
{
|
||||
if(screenCoords != m_screenCoords) {
|
||||
m_screenCoords = screenCoords;
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::setAlign(AlignmentFlag align)
|
||||
{
|
||||
if(m_align != align) {
|
||||
m_align = align;
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::setCursorPos(int pos)
|
||||
{
|
||||
if(pos != m_cursorPos) {
|
||||
if(pos < 0)
|
||||
m_cursorPos = 0;
|
||||
else if((uint)pos >= m_text.length())
|
||||
m_cursorPos = m_text.length();
|
||||
else
|
||||
m_cursorPos = pos;
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::enableCursor(bool enable)
|
||||
{
|
||||
if(enable) {
|
||||
m_cursorPos = 0;
|
||||
m_cursorTicks = g_platform.getTicks();
|
||||
} else
|
||||
m_cursorPos = -1;
|
||||
recalculate();
|
||||
}
|
||||
|
||||
void TextArea::appendCharacter(char c)
|
||||
{
|
||||
if(m_cursorPos >= 0) {
|
||||
std::string tmp;
|
||||
tmp = c;
|
||||
m_text.insert(m_cursorPos, tmp);
|
||||
m_cursorPos++;
|
||||
m_cursorTicks = g_platform.getTicks();
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::removeCharacter(bool right)
|
||||
{
|
||||
if(m_cursorPos >= 0 && m_text.length() > 0) {
|
||||
if(right && (uint)m_cursorPos < m_text.length())
|
||||
m_text.erase(m_text.begin() + m_cursorPos);
|
||||
else if((uint)m_cursorPos == m_text.length()) {
|
||||
m_text.erase(m_text.begin() + (--m_cursorPos));
|
||||
m_cursorTicks = g_platform.getTicks();
|
||||
}
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
|
||||
void TextArea::moveCursor(bool right)
|
||||
{
|
||||
if(right) {
|
||||
if((uint)m_cursorPos+1 <= m_text.length()) {
|
||||
m_cursorPos++;
|
||||
m_cursorTicks = g_platform.getTicks();
|
||||
}
|
||||
} else {
|
||||
if(m_cursorPos-1 >= 0) {
|
||||
m_cursorPos--;
|
||||
m_cursorTicks = g_platform.getTicks();
|
||||
}
|
||||
}
|
||||
recalculate();
|
||||
}
|
||||
|
||||
int TextArea::getTextPos(Point pos)
|
||||
{
|
||||
int textLength = m_text.length();
|
||||
|
||||
// find any glyph that is actually on the
|
||||
int candidatePos = -1;
|
||||
for(int i=0;i<textLength;++i) {
|
||||
Rect clickGlyphRect = m_glyphsCoords[i];
|
||||
clickGlyphRect.addTop(m_font->getTopMargin() + m_font->getGlyphSpacing().height());
|
||||
clickGlyphRect.addLeft(m_font->getGlyphSpacing().width()+1);
|
||||
if(clickGlyphRect.contains(pos))
|
||||
return i;
|
||||
else if(pos.y >= clickGlyphRect.top() && pos.y <= clickGlyphRect.bottom()) {
|
||||
if(pos.x <= clickGlyphRect.left())
|
||||
candidatePos = i;
|
||||
else if(pos.x >= clickGlyphRect.right())
|
||||
candidatePos = i+1;
|
||||
}
|
||||
}
|
||||
return candidatePos;
|
||||
}
|
@@ -1,55 +0,0 @@
|
||||
#ifndef TEXTAREA_H
|
||||
#define TEXTAREA_H
|
||||
|
||||
#include "graphicsdeclarations.h"
|
||||
|
||||
class TextArea
|
||||
{
|
||||
public:
|
||||
TextArea();
|
||||
TextArea(FontPtr font,
|
||||
const std::string& text,
|
||||
const Rect& screenCoords,
|
||||
AlignmentFlag align = AlignTopLeft,
|
||||
const Color& color = Color::white);
|
||||
|
||||
void draw();
|
||||
|
||||
void setFont(FontPtr font);
|
||||
void setText(const std::string& text);
|
||||
void setScreenCoords(const Rect& screenCoords);
|
||||
void setAlign(AlignmentFlag align);
|
||||
void setColor(const Color& color) { m_color = color; }
|
||||
void setCursorPos(int pos);
|
||||
void enableCursor(bool enable = true);
|
||||
void setCursorVisible(bool visible = true) { m_cursorVisible = visible; }
|
||||
|
||||
void moveCursor(bool right);
|
||||
void appendCharacter(char c);
|
||||
void removeCharacter(bool right);
|
||||
|
||||
std::string getText() const { return m_text; }
|
||||
|
||||
FontPtr getFont() const { return m_font; }
|
||||
int getTextPos(Point pos);
|
||||
|
||||
private:
|
||||
void recalculate();
|
||||
|
||||
FontPtr m_font;
|
||||
std::string m_text;
|
||||
Rect m_screenCoords;
|
||||
Rect m_drawArea;
|
||||
AlignmentFlag m_align;
|
||||
Color m_color;
|
||||
int m_cursorPos;
|
||||
Point m_startInternalPos;
|
||||
int m_startRenderPos;
|
||||
int m_cursorTicks;
|
||||
bool m_cursorVisible;
|
||||
|
||||
std::vector<Rect> m_glyphsCoords;
|
||||
std::vector<Rect> m_glyphsTexCoords;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user