text edit improvments (not finished)
This commit is contained in:
parent
e01bd17510
commit
f1475c0569
|
@ -52,7 +52,7 @@ IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
ADD_DEFINITIONS(-D_DEBUG)
|
ADD_DEFINITIONS(-D_DEBUG)
|
||||||
ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
|
||||||
SET(SOURCES src/framework/ui/uilinedecorationskin.cpp src/framework/ui/uicheckboxskin.cpp src/framework/ui/uilinedecoration.cpp src/framework/ui/uicheckbox.cpp
|
SET(SOURCES
|
||||||
# game sources
|
# game sources
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/menustate.cpp
|
src/menustate.cpp
|
||||||
|
@ -79,11 +79,11 @@ SET(SOURCES src/framework/ui/uilinedecorationskin.cpp src/framework/ui/uicheckbo
|
||||||
src/framework/graphics/framebuffer.cpp
|
src/framework/graphics/framebuffer.cpp
|
||||||
src/framework/graphics/font.cpp
|
src/framework/graphics/font.cpp
|
||||||
src/framework/graphics/fonts.cpp
|
src/framework/graphics/fonts.cpp
|
||||||
src/framework/graphics/fonttext.cpp
|
|
||||||
src/framework/graphics/textureloader.cpp
|
src/framework/graphics/textureloader.cpp
|
||||||
src/framework/graphics/texture.cpp
|
src/framework/graphics/texture.cpp
|
||||||
src/framework/graphics/textures.cpp
|
src/framework/graphics/textures.cpp
|
||||||
src/framework/graphics/graphics.cpp
|
src/framework/graphics/graphics.cpp
|
||||||
|
src/framework/graphics/textarea.cpp
|
||||||
|
|
||||||
# framework ui
|
# framework ui
|
||||||
src/framework/ui/uilayout.cpp
|
src/framework/ui/uilayout.cpp
|
||||||
|
@ -101,6 +101,10 @@ SET(SOURCES src/framework/ui/uilinedecorationskin.cpp src/framework/ui/uicheckbo
|
||||||
src/framework/ui/uiwindowskin.cpp
|
src/framework/ui/uiwindowskin.cpp
|
||||||
src/framework/ui/uitextedit.cpp
|
src/framework/ui/uitextedit.cpp
|
||||||
src/framework/ui/uitexteditskin.cpp
|
src/framework/ui/uitexteditskin.cpp
|
||||||
|
src/framework/ui/uilinedecorationskin.cpp
|
||||||
|
src/framework/ui/uicheckboxskin.cpp
|
||||||
|
src/framework/ui/uilinedecoration.cpp
|
||||||
|
src/framework/ui/uicheckbox.cpp
|
||||||
|
|
||||||
# framework net
|
# framework net
|
||||||
src/framework/net/connection.cpp
|
src/framework/net/connection.cpp
|
||||||
|
|
|
@ -84,7 +84,7 @@ text edits:
|
||||||
default:
|
default:
|
||||||
default size: [86, 16]
|
default size: [86, 16]
|
||||||
font: tibia-10px-antialised
|
font: tibia-10px-antialised
|
||||||
text margin: 2
|
text margin: 3
|
||||||
bordered image:
|
bordered image:
|
||||||
left border: [308,97,1,1]
|
left border: [308,97,1,1]
|
||||||
right border: [319,97,1,10]
|
right border: [319,97,1,10]
|
||||||
|
|
|
@ -24,14 +24,15 @@
|
||||||
|
|
||||||
#include "dispatcher.h"
|
#include "dispatcher.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
#include "engine.h"
|
||||||
|
|
||||||
Dispatcher g_dispatcher;
|
Dispatcher g_dispatcher;
|
||||||
|
|
||||||
void Dispatcher::poll(int ticks)
|
void Dispatcher::poll()
|
||||||
{
|
{
|
||||||
while(!m_taskList.empty()) {
|
while(!m_taskList.empty()) {
|
||||||
Task *task = m_taskList.top();
|
Task *task = m_taskList.top();
|
||||||
if(ticks < task->ticks)
|
if(g_engine.getLastFrameTicks() < task->ticks)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
task->callback();
|
task->callback();
|
||||||
|
|
|
@ -49,7 +49,7 @@ public:
|
||||||
Dispatcher() { }
|
Dispatcher() { }
|
||||||
|
|
||||||
/// Execute scheduled events
|
/// Execute scheduled events
|
||||||
void poll(int ticks);
|
void poll();
|
||||||
|
|
||||||
/// Add an event
|
/// Add an event
|
||||||
void addTask(const Callback& callback);
|
void addTask(const Callback& callback);
|
||||||
|
|
|
@ -53,8 +53,8 @@ void Engine::terminate()
|
||||||
void Engine::run()
|
void Engine::run()
|
||||||
{
|
{
|
||||||
Font *defaultFont = g_fonts.getDefaultFont();
|
Font *defaultFont = g_fonts.getDefaultFont();
|
||||||
int ticks = Platform::getTicks();
|
m_lastFrameTicks = Platform::getTicks();
|
||||||
int lastFpsTicks = ticks;
|
int lastFpsTicks = m_lastFrameTicks;
|
||||||
int frameCount = 0;
|
int frameCount = 0;
|
||||||
int fps = 0;
|
int fps = 0;
|
||||||
m_running = true;
|
m_running = true;
|
||||||
|
@ -66,18 +66,18 @@ void Engine::run()
|
||||||
// poll network events
|
// poll network events
|
||||||
g_connections.poll();
|
g_connections.poll();
|
||||||
|
|
||||||
ticks = Platform::getTicks();
|
m_lastFrameTicks = Platform::getTicks();
|
||||||
|
|
||||||
// poll diaptcher tasks
|
// poll diaptcher tasks
|
||||||
g_dispatcher.poll(ticks);
|
g_dispatcher.poll();
|
||||||
|
|
||||||
// render only when visible
|
// render only when visible
|
||||||
if(Platform::isWindowVisible()) {
|
if(Platform::isWindowVisible()) {
|
||||||
// calculate and fps
|
// calculate and fps
|
||||||
if(m_calculateFps) {
|
if(m_calculateFps) {
|
||||||
frameCount++;
|
frameCount++;
|
||||||
if(ticks - lastFpsTicks >= 1000) {
|
if(m_lastFrameTicks - lastFpsTicks >= 1000) {
|
||||||
lastFpsTicks = ticks;
|
lastFpsTicks = m_lastFrameTicks;
|
||||||
fps = frameCount;
|
fps = frameCount;
|
||||||
frameCount = 0;
|
frameCount = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,8 @@ public:
|
||||||
/// Enable FPS counter on screen
|
/// Enable FPS counter on screen
|
||||||
void enableFpsCounter(bool enable = true) { m_calculateFps = enable; };
|
void enableFpsCounter(bool enable = true) { m_calculateFps = enable; };
|
||||||
|
|
||||||
|
int getLastFrameTicks() const { return m_lastFrameTicks; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Called to render every frame
|
/// Called to render every frame
|
||||||
void render();
|
void render();
|
||||||
|
@ -70,6 +72,7 @@ private:
|
||||||
bool m_calculateFps;
|
bool m_calculateFps;
|
||||||
|
|
||||||
GameState *m_currentState;
|
GameState *m_currentState;
|
||||||
|
int m_lastFrameTicks;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Engine g_engine;
|
extern Engine g_engine;
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "core/resources.h"
|
#include "core/resources.h"
|
||||||
#include "textures.h"
|
#include "textures.h"
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
|
#include "textarea.h"
|
||||||
|
|
||||||
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
|
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
|
||||||
{
|
{
|
||||||
|
@ -138,116 +139,32 @@ void Font::renderText(const std::string& text,
|
||||||
Font::renderText(text, screenCoords, ALIGN_TOP_LEFT, color);
|
Font::renderText(text, screenCoords, ALIGN_TOP_LEFT, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Font::renderText(const std::string& text,
|
void Font::renderText(const std::string& text,
|
||||||
const Rect& screenCoords,
|
const Rect& screenCoords,
|
||||||
int align,
|
int align,
|
||||||
const Color& color,
|
const Color& color)
|
||||||
const Point& startInternalPos,
|
|
||||||
int cursorPos,
|
|
||||||
const Color& cursorColor)
|
|
||||||
{
|
{
|
||||||
// prevent glitches from invalid rects
|
TextArea textArea(this, text, screenCoords, align, color);
|
||||||
if(!screenCoords.isValid())
|
textArea.draw();
|
||||||
return;
|
|
||||||
|
|
||||||
int textLenght = text.length();
|
|
||||||
|
|
||||||
// map glyphs positions
|
|
||||||
Size textBoxSize;
|
|
||||||
Point *glyphsPositions = calculateGlyphsPositions(text, align, &textBoxSize);
|
|
||||||
|
|
||||||
for(int i = 0; i < textLenght; ++i) {
|
|
||||||
int glyph = (uchar)text[i];
|
|
||||||
|
|
||||||
// skip invalid glyphs
|
|
||||||
if(glyph < 32)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// calculate initial glyph rect and texture coords
|
|
||||||
Rect glyphScreenCoords(glyphsPositions[i], m_glyphsSize[glyph]);
|
|
||||||
Rect glyphTextureCoords = m_glyphsTextureCoords[glyph];
|
|
||||||
|
|
||||||
// first translate to align position
|
|
||||||
if(align & ALIGN_BOTTOM) {
|
|
||||||
glyphScreenCoords.translate(0, screenCoords.height() - textBoxSize.height());
|
|
||||||
} else if(align & ALIGN_VERTICAL_CENTER) {
|
|
||||||
glyphScreenCoords.translate(0, (screenCoords.height() - textBoxSize.height()) / 2);
|
|
||||||
} else { // ALIGN_TOP
|
|
||||||
// nothing to do
|
|
||||||
}
|
|
||||||
|
|
||||||
if(align & ALIGN_RIGHT) {
|
|
||||||
glyphScreenCoords.translate(screenCoords.width() - textBoxSize.width(), 0);
|
|
||||||
} else if(align & ALIGN_HORIZONTAL_CENTER) {
|
|
||||||
glyphScreenCoords.translate((screenCoords.width() - textBoxSize.width()) / 2, 0);
|
|
||||||
} else { // ALIGN_TOP
|
|
||||||
// nothing to do
|
|
||||||
}
|
|
||||||
|
|
||||||
// only render glyphs that are after startRenderPosition
|
|
||||||
if(glyphScreenCoords.bottom() < startInternalPos.y || glyphScreenCoords.right() < startInternalPos.x)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// bound glyph topLeft to startRenderPosition
|
|
||||||
if(glyphScreenCoords.top() < startInternalPos.y) {
|
|
||||||
glyphTextureCoords.setTop(glyphTextureCoords.top() + (startInternalPos.y - glyphScreenCoords.top()));
|
|
||||||
glyphScreenCoords.setTop(startInternalPos.y);
|
|
||||||
}
|
|
||||||
if(glyphScreenCoords.left() < startInternalPos.x) {
|
|
||||||
glyphTextureCoords.setLeft(glyphTextureCoords.left() + (startInternalPos.x - glyphScreenCoords.left()));
|
|
||||||
glyphScreenCoords.setLeft(startInternalPos.x);
|
|
||||||
}
|
|
||||||
|
|
||||||
// subtract startInternalPos
|
|
||||||
glyphScreenCoords.translate(-startInternalPos);
|
|
||||||
|
|
||||||
// translate rect to screen coords
|
|
||||||
glyphScreenCoords.translate(screenCoords.topLeft());
|
|
||||||
|
|
||||||
// only render if glyph rect is visible on screenCoords
|
|
||||||
if(!screenCoords.intersects(glyphScreenCoords))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// bound glyph bottomRight to screenCoords bottomRight
|
|
||||||
if(glyphScreenCoords.bottom() > screenCoords.bottom()) {
|
|
||||||
glyphTextureCoords.setBottom(glyphTextureCoords.bottom() + (screenCoords.bottom() - glyphScreenCoords.bottom()));
|
|
||||||
glyphScreenCoords.setBottom(screenCoords.bottom());
|
|
||||||
}
|
|
||||||
if(glyphScreenCoords.right() > screenCoords.right()) {
|
|
||||||
glyphTextureCoords.setRight(glyphTextureCoords.right() + (screenCoords.right() - glyphScreenCoords.right()));
|
|
||||||
glyphScreenCoords.setRight(screenCoords.right());
|
|
||||||
}
|
|
||||||
|
|
||||||
// render glyph
|
|
||||||
g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords, color);
|
|
||||||
|
|
||||||
// render cursor
|
|
||||||
if(i == cursorPos) {
|
|
||||||
Rect cursorRect(glyphScreenCoords.left()-1, glyphScreenCoords.top(), 1, m_glyphHeight);
|
|
||||||
g_graphics.drawFilledRect(cursorRect, cursorColor);
|
|
||||||
}
|
|
||||||
// render cursor after last element
|
|
||||||
else if(cursorPos == textLenght && i == textLenght - 1) {
|
|
||||||
Rect cursorRect(glyphScreenCoords.right()+1, glyphScreenCoords.top(), 1, m_glyphHeight);
|
|
||||||
g_graphics.drawFilledRect(cursorRect, cursorColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point* Font::calculateGlyphsPositions(const std::string& text, int align, Size *textBoxSize)
|
std::vector<Point> Font::calculateGlyphsPositions(const std::string& text, int align, Size *textBoxSize)
|
||||||
{
|
{
|
||||||
static Point glyphsPositions[8192];
|
int numGlyphs = text.length();
|
||||||
static int lineWidths[512];
|
std::vector<Point> glyphsPositions(numGlyphs);
|
||||||
|
if(numGlyphs == 0) {
|
||||||
|
if(textBoxSize)
|
||||||
|
textBoxSize->setSize(0,0);
|
||||||
|
return glyphsPositions;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> lineWidths(numGlyphs);
|
||||||
int maxLineWidth = 0;
|
int maxLineWidth = 0;
|
||||||
int lines = 0;
|
int lines = 0;
|
||||||
int glyph;
|
int glyph;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// protect buffer overflow on glyphsPostions
|
|
||||||
int numGlyphs = text.length();
|
|
||||||
if(numGlyphs > 8192)
|
|
||||||
logFatal("could not calculate glyphs positions, text length is > 8192!");
|
|
||||||
|
|
||||||
// calculate lines width
|
// calculate lines width
|
||||||
if((align & ALIGN_RIGHT || align & ALIGN_HORIZONTAL_CENTER) || textBoxSize) {
|
if((align & ALIGN_RIGHT || align & ALIGN_HORIZONTAL_CENTER) || textBoxSize) {
|
||||||
lineWidths[0] = 0;
|
lineWidths[0] = 0;
|
||||||
|
@ -299,13 +216,13 @@ Point* Font::calculateGlyphsPositions(const std::string& text, int align, Size *
|
||||||
textBoxSize->setHeight(virtualPos.y + m_glyphHeight);
|
textBoxSize->setHeight(virtualPos.y + m_glyphHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (Point *)glyphsPositions;
|
return glyphsPositions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Size Font::calculateTextRectSize(const std::string& text)
|
Size Font::calculateTextRectSize(const std::string& text)
|
||||||
{
|
{
|
||||||
Size size;
|
Size size;
|
||||||
calculateGlyphsPositions(text, ALIGN_TOP_LEFT, &size);
|
calculateGlyphsPositions(text, ALIGN_TOP_LEFT, &size);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,8 @@ enum EAlign {
|
||||||
ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT
|
ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class TextArea;
|
||||||
|
|
||||||
class Font
|
class Font
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -65,21 +67,21 @@ public:
|
||||||
void renderText(const std::string& text,
|
void renderText(const std::string& text,
|
||||||
const Rect& screenCoords,
|
const Rect& screenCoords,
|
||||||
int align = ALIGN_TOP_LEFT,
|
int align = ALIGN_TOP_LEFT,
|
||||||
const Color& color = Color::white,
|
const Color& color = Color::white);
|
||||||
const Point& startInternalPos = Point(),
|
|
||||||
int cursorPos = -1,
|
|
||||||
const Color& cursorColor = Color::white);
|
|
||||||
|
|
||||||
|
|
||||||
/// Calculate glyphs positions to use on render, also calculates textBoxSize if wanted
|
/// Calculate glyphs positions to use on render, also calculates textBoxSize if wanted
|
||||||
Point *calculateGlyphsPositions(const std::string& text, int align = ALIGN_TOP_LEFT, Size *textBoxSize = NULL);
|
std::vector<Point> calculateGlyphsPositions(const std::string& text, int align = ALIGN_TOP_LEFT, Size *textBoxSize = NULL);
|
||||||
|
|
||||||
/// Simulate render and calculate text size
|
/// Simulate render and calculate text size
|
||||||
Size calculateTextRectSize(const std::string& text);
|
Size calculateTextRectSize(const std::string& text);
|
||||||
|
|
||||||
const std::string& getName() const { return m_name; }
|
const std::string& getName() const { return m_name; }
|
||||||
int getGlyphHeight() const { return m_glyphHeight; }
|
int getGlyphHeight() const { return m_glyphHeight; }
|
||||||
|
const Rect *getGlyphsTextureCoords() const { return m_glyphsTextureCoords; }
|
||||||
|
const Size *getGlyphsSize() const { return m_glyphsSize; }
|
||||||
|
const TexturePtr& getTexture() const { return m_texture; }
|
||||||
|
int getTopMargin() const { return m_topMargin; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void calculateGlyphsWidthsAutomatically(const Size& glyphSize);
|
void calculateGlyphsWidthsAutomatically(const Size& glyphSize);
|
||||||
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
/* The MIT License
|
|
||||||
*
|
|
||||||
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include "fonttext.h"
|
|
||||||
|
|
|
@ -0,0 +1,256 @@
|
||||||
|
/* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "textarea.h"
|
||||||
|
#include "graphics.h"
|
||||||
|
#include "core/engine.h"
|
||||||
|
|
||||||
|
TextArea::TextArea() :
|
||||||
|
m_font(0),
|
||||||
|
m_align(ALIGN_TOP_LEFT),
|
||||||
|
m_color(Color::white),
|
||||||
|
m_cursorPos(-1),
|
||||||
|
m_cursorVisible(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TextArea::TextArea(Font* font,
|
||||||
|
const std::string& text,
|
||||||
|
const Rect& screenCoords,
|
||||||
|
int align,
|
||||||
|
const Color& color) :
|
||||||
|
m_font(font),
|
||||||
|
m_text(text),
|
||||||
|
m_screenCoords(screenCoords),
|
||||||
|
m_align(align),
|
||||||
|
m_color(color),
|
||||||
|
m_cursorPos(-1),
|
||||||
|
m_cursorVisible(false)
|
||||||
|
{
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::draw()
|
||||||
|
{
|
||||||
|
int numGlyphs = m_text.length();
|
||||||
|
const TexturePtr& texture = m_font->getTexture();
|
||||||
|
for(int i=0;i<numGlyphs;++i) {
|
||||||
|
g_graphics.drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i], m_color);
|
||||||
|
}
|
||||||
|
|
||||||
|
// render cursor
|
||||||
|
if(m_cursorVisible && m_cursorPos >= 0 && m_cursorPos <= numGlyphs) {
|
||||||
|
const int delay = 500;
|
||||||
|
int ticks = g_engine.getLastFrameTicks();
|
||||||
|
// draw every 500ms
|
||||||
|
if(ticks - m_cursorTicks <= delay) {
|
||||||
|
Rect cursorRect;
|
||||||
|
if(m_cursorPos == 0)
|
||||||
|
cursorRect = Rect(m_drawArea.left()-1, m_drawArea.top(), 1, m_font->getGlyphHeight());
|
||||||
|
else if(m_cursorPos == numGlyphs)
|
||||||
|
cursorRect = Rect(m_glyphsCoords[m_cursorPos-1].right(), m_glyphsCoords[m_cursorPos-1].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 = g_engine.getLastFrameTicks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::recalculate()
|
||||||
|
{
|
||||||
|
// prevent glitches from invalid rects
|
||||||
|
if(!m_screenCoords.isValid())
|
||||||
|
return;
|
||||||
|
|
||||||
|
int textLenght = m_text.length();
|
||||||
|
|
||||||
|
m_glyphsCoords.clear();
|
||||||
|
m_glyphsTexCoords.clear();
|
||||||
|
|
||||||
|
m_glyphsCoords.resize(textLenght);
|
||||||
|
m_glyphsTexCoords.resize(textLenght);
|
||||||
|
|
||||||
|
// map glyphs positions
|
||||||
|
Size textBoxSize;
|
||||||
|
std::vector<Point> glyphsPositions = m_font->calculateGlyphsPositions(m_text, m_align, &textBoxSize);
|
||||||
|
const Rect *glyphsTextureCoords = m_font->getGlyphsTextureCoords();
|
||||||
|
const Size *glyphsSize = m_font->getGlyphsSize();
|
||||||
|
|
||||||
|
m_drawArea.setLeft(m_screenCoords.left());
|
||||||
|
m_drawArea.setTop(m_screenCoords.top()+m_font->getTopMargin());
|
||||||
|
m_drawArea.setRight(m_screenCoords.right());
|
||||||
|
m_drawArea.setBottom(m_screenCoords.bottom());
|
||||||
|
|
||||||
|
for(int i = 0; i < textLenght; ++i) {
|
||||||
|
int glyph = (uchar)m_text[i];
|
||||||
|
|
||||||
|
// 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 & ALIGN_BOTTOM) {
|
||||||
|
glyphScreenCoords.translate(0, m_screenCoords.height() - textBoxSize.height());
|
||||||
|
} else if(m_align & ALIGN_VERTICAL_CENTER) {
|
||||||
|
glyphScreenCoords.translate(0, (m_screenCoords.height() - textBoxSize.height()) / 2);
|
||||||
|
} else { // ALIGN_TOP
|
||||||
|
// nothing to do
|
||||||
|
}
|
||||||
|
|
||||||
|
if(m_align & ALIGN_RIGHT) {
|
||||||
|
glyphScreenCoords.translate(m_screenCoords.width() - textBoxSize.width(), 0);
|
||||||
|
} else if(m_align & ALIGN_HORIZONTAL_CENTER) {
|
||||||
|
glyphScreenCoords.translate((m_screenCoords.width() - textBoxSize.width()) / 2, 0);
|
||||||
|
} else { // ALIGN_TOP
|
||||||
|
// 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(Font* font)
|
||||||
|
{
|
||||||
|
m_font = font;
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::setText(const std::string& text)
|
||||||
|
{
|
||||||
|
m_text = text;
|
||||||
|
if(m_cursorPos >= 0) {
|
||||||
|
m_cursorPos = 0;
|
||||||
|
m_cursorTicks = g_engine.getLastFrameTicks();
|
||||||
|
}
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::setScreenCoords(Rect screenCoords)
|
||||||
|
{
|
||||||
|
m_screenCoords = screenCoords;
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::setAlign(int align)
|
||||||
|
{
|
||||||
|
m_align = align;
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::setStartInternalPos(Point startPos)
|
||||||
|
{
|
||||||
|
m_startInternalPos = startPos;
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::enableCursor(bool enable)
|
||||||
|
{
|
||||||
|
if(enable) {
|
||||||
|
m_cursorPos = 0;
|
||||||
|
m_cursorTicks = g_engine.getLastFrameTicks();
|
||||||
|
} else
|
||||||
|
m_cursorPos = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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_engine.getLastFrameTicks();
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::removeCharacter(bool right)
|
||||||
|
{
|
||||||
|
if(m_cursorPos >= 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_cursorPos > 0) {
|
||||||
|
m_text.erase(m_text.begin() + (--m_cursorPos));
|
||||||
|
m_cursorTicks = g_engine.getLastFrameTicks();
|
||||||
|
}
|
||||||
|
recalculate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextArea::moveCursor(bool right)
|
||||||
|
{
|
||||||
|
if(right) {
|
||||||
|
if((uint)m_cursorPos+1 <= m_text.length()) {
|
||||||
|
m_cursorPos++;
|
||||||
|
m_cursorTicks = g_engine.getLastFrameTicks();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(m_cursorPos-1 >= 0) {
|
||||||
|
m_cursorPos--;
|
||||||
|
m_cursorTicks = g_engine.getLastFrameTicks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,32 +22,55 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef FONTTEXT_H
|
#ifndef TEXTAREA_H
|
||||||
#define FONTTEXT_H
|
#define TEXTAREA_H
|
||||||
|
|
||||||
#include "prerequisites.h"
|
#include "prerequisites.h"
|
||||||
#include "font.h"
|
#include "font.h"
|
||||||
|
|
||||||
class FontText
|
class TextArea
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FontText() { }
|
TextArea();
|
||||||
|
TextArea(Font *font,
|
||||||
|
const std::string& text,
|
||||||
|
const Rect& screenCoords,
|
||||||
|
int align = ALIGN_TOP_LEFT,
|
||||||
|
const Color& color = Color::white);
|
||||||
|
|
||||||
|
void draw();
|
||||||
|
|
||||||
|
void setFont(Font *font);
|
||||||
|
void setText(const std::string& text);
|
||||||
|
void setScreenCoords(Rect screenCoords);
|
||||||
|
void setAlign(int align);
|
||||||
|
void setColor(const Color& color) { m_color = color; }
|
||||||
|
void setStartInternalPos(Point startPos);
|
||||||
|
void enableCursor(bool enable = true);
|
||||||
|
void setCursorVisible(bool visible = true) { m_cursorVisible = visible; }
|
||||||
|
|
||||||
|
void moveCursor(bool right);
|
||||||
void appendCharacter(char c);
|
void appendCharacter(char c);
|
||||||
void appendText(const std::string &text);
|
void removeCharacter(bool right);
|
||||||
void erase(bool left);
|
|
||||||
|
|
||||||
void setText(const std::string &text);
|
const std::string& getText() const { return m_text; }
|
||||||
void setCursorPos(int pos);
|
|
||||||
void setSelection(int start, int end);
|
|
||||||
void setColor(const Color& color);
|
|
||||||
void setSize(const Size& size);
|
|
||||||
void setStartPos();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_cursorPos;
|
void recalculate();
|
||||||
std::string m_text;
|
|
||||||
Font *m_font;
|
Font *m_font;
|
||||||
|
std::string m_text;
|
||||||
|
Rect m_screenCoords;
|
||||||
|
Rect m_drawArea;
|
||||||
|
int m_align;
|
||||||
|
Color m_color;
|
||||||
|
Point m_startInternalPos;
|
||||||
|
int m_cursorPos;
|
||||||
|
int m_cursorTicks;
|
||||||
|
bool m_cursorVisible;
|
||||||
|
|
||||||
|
std::vector<Rect> m_glyphsCoords;
|
||||||
|
std::vector<Rect> m_glyphsTexCoords;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FONTTEXT_H
|
#endif // TEXTAREA_H
|
|
@ -111,7 +111,7 @@ void UIContainer::onInputEvent(const InputEvent& event)
|
||||||
}
|
}
|
||||||
// mouse events
|
// mouse events
|
||||||
} else if(event.type & EV_MOUSE) {
|
} 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(event.type & EV_DOWN || event.type & EV_MOUSE_WHEEL) {
|
||||||
if(child->getRect().contains(event.mousePos)) {
|
if(child->getRect().contains(event.mousePos)) {
|
||||||
// focus it
|
// focus it
|
||||||
|
@ -134,7 +134,11 @@ void UIContainer::onInputEvent(const InputEvent& event)
|
||||||
|
|
||||||
void UIContainer::setFocusedElement(UIElementPtr focusedElement)
|
void UIContainer::setFocusedElement(UIElementPtr focusedElement)
|
||||||
{
|
{
|
||||||
if(m_focusedElement)
|
if(m_focusedElement) {
|
||||||
m_focusedElement->setFocused(false);
|
m_focusedElement->setFocused(false);
|
||||||
|
m_focusedElement->onFocusChange();
|
||||||
|
}
|
||||||
m_focusedElement = focusedElement;
|
m_focusedElement = focusedElement;
|
||||||
|
m_focusedElement->setFocused(true);
|
||||||
|
m_focusedElement->onFocusChange();
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ public:
|
||||||
|
|
||||||
virtual void render();
|
virtual void render();
|
||||||
virtual void onInputEvent(const InputEvent& event) { }
|
virtual void onInputEvent(const InputEvent& event) { }
|
||||||
|
virtual void onFocusChange() { }
|
||||||
|
|
||||||
UIElementPtr backwardsGetElementById(const std::string& id);
|
UIElementPtr backwardsGetElementById(const std::string& id);
|
||||||
|
|
||||||
|
|
|
@ -23,74 +23,45 @@
|
||||||
|
|
||||||
|
|
||||||
#include "uitextedit.h"
|
#include "uitextedit.h"
|
||||||
|
#include "uitexteditskin.h"
|
||||||
#include "graphics/fonts.h"
|
#include "graphics/fonts.h"
|
||||||
|
|
||||||
UITextEdit::UITextEdit() :
|
UITextEdit::UITextEdit() :
|
||||||
UIElement(UI::TextEdit),
|
UIElement(UI::TextEdit)
|
||||||
m_cursorPos(0),
|
|
||||||
m_startRenderPos(0)
|
|
||||||
{
|
{
|
||||||
|
UITextEditSkin *skin = static_cast<UITextEditSkin*>(getSkin());
|
||||||
|
m_textArea.setFont(skin->getFont());
|
||||||
|
m_textArea.enableCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UITextEdit::onInputEvent(const InputEvent& event)
|
void UITextEdit::onInputEvent(const InputEvent& event)
|
||||||
{
|
{
|
||||||
if(event.type == EV_TEXT_ENTER) {
|
if(event.type == EV_TEXT_ENTER) {
|
||||||
appendCharacter(event.keychar);
|
m_textArea.appendCharacter(event.keychar);
|
||||||
} else if(event.type == EV_KEY_DOWN) {
|
} else if(event.type == EV_KEY_DOWN) {
|
||||||
if(event.keycode == KC_DELETE)
|
if(event.keycode == KC_DELETE)
|
||||||
removeCharacter(true);
|
m_textArea.removeCharacter(true);
|
||||||
else if(event.keycode == KC_BACK)
|
else if(event.keycode == KC_BACK)
|
||||||
removeCharacter(false);
|
m_textArea.removeCharacter(false);
|
||||||
else if(event.keycode == KC_RIGHT) {
|
else if(event.keycode == KC_RIGHT)
|
||||||
if(m_cursorPos < m_text.length())
|
m_textArea.moveCursor(true);
|
||||||
m_cursorPos++;
|
else if(event.keycode == KC_LEFT)
|
||||||
} else if(event.keycode == KC_LEFT) {
|
m_textArea.moveCursor(false);
|
||||||
if(m_cursorPos > 0)
|
|
||||||
m_cursorPos--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
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 "prerequisites.h"
|
||||||
#include "uielement.h"
|
#include "uielement.h"
|
||||||
|
#include "graphics/textarea.h"
|
||||||
|
|
||||||
class Font;
|
class Font;
|
||||||
|
|
||||||
|
@ -37,24 +38,16 @@ public:
|
||||||
|
|
||||||
void onInputEvent(const InputEvent& event);
|
void onInputEvent(const InputEvent& event);
|
||||||
|
|
||||||
void clearText();
|
|
||||||
void setText(const std::string& text);
|
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);
|
TextArea& getTextArea() { return m_textArea; }
|
||||||
uint getCursorPos() { return m_cursorPos; }
|
|
||||||
|
|
||||||
void onLayoutRectChange(const Rect& rect);
|
void onLayoutRectChange(const Rect& rect);
|
||||||
|
void onFocusChange();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void appendCharacter(char c);
|
TextArea m_textArea;
|
||||||
void removeCharacter(bool right);
|
|
||||||
void recalculate();
|
|
||||||
|
|
||||||
Rect m_textRect;
|
|
||||||
uint m_cursorPos;
|
|
||||||
int m_startRenderPos;
|
|
||||||
std::string m_text;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<UITextEdit> UITextEditPtr;
|
typedef boost::shared_ptr<UITextEdit> UITextEditPtr;
|
||||||
|
|
|
@ -51,5 +51,5 @@ void UITextEditSkin::draw(UIElement* element)
|
||||||
UIElementSkin::draw(element);
|
UIElementSkin::draw(element);
|
||||||
|
|
||||||
UITextEdit *textEdit = static_cast<UITextEdit*>(element);
|
UITextEdit *textEdit = static_cast<UITextEdit*>(element);
|
||||||
m_font->renderText(textEdit->getText(), textEdit->getRect(), ALIGN_TOP_LEFT, m_textColor);
|
textEdit->getTextArea().draw();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue