Commit 3cc2c48a authored by gerstrong@gmail.com's avatar gerstrong@gmail.com

Merging GUI Text code

parent 1cb6fb99
......@@ -20,6 +20,14 @@ CGUIText::CGUIText(const std::string& text)
void CGUIText::setText(const std::string& text)
{
#if defined(USE_SDL_TTF)
const SDL_Color textColor = { 0xff, 0, 0, 0 };
mTrueTypeFont.open("lazy.ttf", 28);
mTrueTypeFont.render(mTextSfc, text, textColor);
#else
if(!mTextList.empty())
mTextList.clear();
......@@ -56,6 +64,8 @@ void CGUIText::setText(const std::string& text)
mTextDim.w=buf.size();
mTextDim.h = mTextList.size();
#endif
}
......@@ -91,17 +101,37 @@ void CGUIText::processLogic()
void CGUIText::processRender(const GsRect<float> &RectDispCoordFloat)
{
// Transform to the display coordinates
GsRect<float> displayRect = mRect;
displayRect.transform(RectDispCoordFloat);
SDL_Rect lRect = displayRect.SDLRect();
// Transform to the display coordinates
GsRect<float> displayRect = mRect;
displayRect.transform(RectDispCoordFloat);
SDL_Rect lRect = displayRect.SDLRect();
#if defined(USE_SDL_TTF)
auto *blit = gVideoDriver.getBlitSurface();
BlitSurface(mTextSfc.getSDLSurface(), nullptr,
blit, &lRect);
BlitSurface(mTextSfc.getSDLSurface(), nullptr,
blit, nullptr);
#endif
// Now lets draw the text of the list control
/*auto *renderer = &gVideoDriver.getRendererRef();
//Render to screen
SDL_RenderCopyEx( renderer,
mTexture.getPtr(), nullptr,
&lRect,
0, nullptr, SDL_FLIP_NONE );*/
// Now lets draw the text of the list control
auto &Font = gGraphics.getFont(mFontID);
std::list<std::string>::iterator textIt = mTextList.begin();
for( size_t i=0 ; textIt != mTextList.end() ; textIt++, i++ )
{
{
auto &theText = *textIt;
const int textWidth = Font.calcPixelTextWidth(theText);
......@@ -128,7 +158,5 @@ void CGUIText::processRender(const GsRect<float> &RectDispCoordFloat)
Font.drawFontCentered(gVideoDriver.getBlitSurface(), theText, lRect.x, lRect.w, lRect.y+i*8, false);
mScrollPosMax = 0;
}
}
}
}
......@@ -16,6 +16,11 @@
#include "GsControl.h"
#include <graphics/GsTexture.h>
#include <graphics/GsSurface.h>
#include <graphics/GsTrueTypeFont.h>
class CGUIText : public GsControl
{
public:
......@@ -25,8 +30,7 @@ public:
virtual void setText(const std::string& text);
virtual void processLogic();
//void drawGalaxyStyle(SDL_Rect& lRect);
virtual void processLogic();
virtual void processRender(const GsRect<float> &RectDispCoordFloat);
// Defines in character units how tall and wide the entire text is.
......@@ -42,9 +46,18 @@ private:
/// Automatic horizontal scrolling in case the text is too big
float mScrollPosMax = 0;
float mScrollPos = 0;
float mScrollVel = 0.3;
float mScrollPosMax = 0.0f;
float mScrollPos = 0.0f;
float mScrollVel = 0.3f;
#if defined(USE_SDL_TTF)
GsTrueTypeFont mTrueTypeFont;
GsTexture mTexture;
GsSurface mTextSfc;
#endif
enum class ScrollDir
{
......
/*
* CGUIText.cpp
*
* Created on: 30.10.2011
* Author: gerstrong
*/
#include <graphics/GsGraphics.h>
#include <base/video/CVideoDriver.h>
#include <base/utils/StringUtils.h>
#include "GsText.h"
CGUIText::CGUIText(const std::string& text)
{
setText(text);
}
void CGUIText::setText(const std::string& text)
{
const SDL_Color textColor = { 0xff, 0, 0, 0 };
//const SDL_Color textColor =
//gVideoDriver.getScreenSurface().mapRGB(0x0, 0xFF, 0xFF);
#if defined(USE_SDL_TTF)
mTrueTypeFont.open("lazy.ttf", 28);
mTrueTypeFont.render(mTextSfc, text, textColor);
#else
if(!mTextList.empty())
mTextList.clear();
// Split up the text in lines
mTextDim.w = 0;
// TODO: I think there is a more elegant way to achieve this!
std::string buf = "";
for( size_t i=0 ; i<text.size() ; i++ )
{
if( endofText(text.substr(i)) )
{
if( mTextDim.w<buf.size() )
mTextDim.w=buf.size();
mTextList.push_back(buf);
buf.clear();
}
else
{
buf += text[i];
}
}
size_t pos = 0;
if(!buf.empty())
{
while( (pos = buf.find('\n')) != std::string::npos )
buf.erase(pos,1);
}
mTextList.push_back(buf);
if( mTextDim.w<buf.size() )
mTextDim.w=buf.size();
mTextDim.h = mTextList.size();
#endif
}
void CGUIText::processLogic()
{
// Horizontal scrolling.
// If Max is zero, nothing need to be scrolled
if(mScrollPosMax <= 0)
return;
// Check if scroll position touches the edges
if(mScrollPos <= 0)
{
mScrollDir = ScrollDir::RIGHT;
mScrollPos = 0;
}
else if(mScrollPos >= mScrollPosMax)
{
mScrollDir = ScrollDir::LEFT;
mScrollPos = mScrollPosMax;
}
// Scroll into the direction
if(mScrollDir == ScrollDir::LEFT)
{
mScrollPos -= mScrollVel;
}
else if(mScrollDir == ScrollDir::RIGHT)
{
mScrollPos += mScrollVel;
}
}
void CGUIText::processRender(const GsRect<float> &RectDispCoordFloat)
{
// Transform to the display coordinates
GsRect<float> displayRect = mRect;
displayRect.transform(RectDispCoordFloat);
SDL_Rect lRect = displayRect.SDLRect();
auto *blit = gVideoDriver.getBlitSurface();
#if defined(USE_SDL_TTF)
BlitSurface(mTextSfc.getSDLSurface(), nullptr,
blit, &lRect);
BlitSurface(mTextSfc.getSDLSurface(), nullptr,
blit, nullptr);
#endif
/*auto *renderer = &gVideoDriver.getRendererRef();
//Render to screen
SDL_RenderCopyEx( renderer,
mTexture.getPtr(), nullptr,
&lRect,
0, nullptr, SDL_FLIP_NONE );*/
// Now lets draw the text of the list control
auto &Font = gGraphics.getFont(mFontID);
std::list<std::string>::iterator textIt = mTextList.begin();
for( size_t i=0 ; textIt != mTextList.end() ; textIt++, i++ )
{
auto &theText = *textIt;
const int textWidth = Font.calcPixelTextWidth(theText);
// The tolerance is the amount of pixels at least of difference to consider
// for scrolling. We consider a tolerance so strange jittery are avoided for text
// that nearly fits
const int tol = 8;
// The first text item decides wheter scrolling takes place
if(textWidth > lRect.w + tol) // tolerance
{
const auto diff = textWidth - lRect.w;
mScrollPosMax = diff;
Font.drawFont(gVideoDriver.getBlitSurface(),
theText,
lRect.x-int(mScrollPos),
lRect.y+i*8,
false);
}
else
{
Font.drawFontCentered(gVideoDriver.getBlitSurface(), theText, lRect.x, lRect.w, lRect.y+i*8, false);
mScrollPosMax = 0;
}
}
}
/*
* CGUIText.h
*
* Created on: 30.10.2011
* Author: gerstrong
*
* This is the text control for our new GUI
*/
#ifndef CGUITEXT_H_
#define CGUITEXT_H_
#include <string>
#include <list>
#include <memory>
#include "GsControl.h"
#include <graphics/GsTexture.h>
#include <graphics/GsSurface.h>
#include <graphics/GsTrueTypeFont.h>
class CGUIText : public GsControl
{
public:
CGUIText(const std::string& text);
void setupButtonSurface(const std::string& text = "");
virtual void setText(const std::string& text);
virtual void processLogic();
virtual void processRender(const GsRect<float> &RectDispCoordFloat);
// Defines in character units how tall and wide the entire text is.
GsRect<unsigned int> mTextDim;
protected:
std::list<std::string> mTextList;
private:
std::unique_ptr<SDL_Surface> mpTextDarkSfc;
std::unique_ptr<SDL_Surface> mpTextLightSfc;
std::unique_ptr<SDL_Surface> mpTextDisabledSfc;
/// Automatic horizontal scrolling in case the text is too big
float mScrollPosMax = 0.0f;
float mScrollPos = 0.0f;
float mScrollVel = 0.3f;
#if defined(USE_SDL_TTF)
GsTrueTypeFont mTrueTypeFont;
#endif
GsTexture mTexture;
GsSurface mTextSfc;
enum class ScrollDir
{
NONE,
LEFT,
RIGHT
} mScrollDir = ScrollDir::NONE;
};
#endif /* CGUITEXT_H_ */
......@@ -541,6 +541,7 @@ Chrisfischtopher
Commander Spleen
Dreams
Casey Bird
Raymond Larabie
Alexander 'z33ky' Hirsch
hasufell
Alexandre Detiste
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment