Commit 15e513db authored by gerstrong@gmail.com's avatar gerstrong@gmail.com

More GsKit classes

parent 1dfc8c87
#include "GsControlsManager.h"
GsControlsManager::GsControlsManager()
{
}
#ifndef GSCONTROLSMANAGER_H
#define GSCONTROLSMANAGER_H
class GsControlsManager
{
public:
GsControlsManager();
};
#endif // GSCONTROLSMANAGER_H
\ No newline at end of file
#include "GsFrame.h"
#include <base/video/CVideoDriver.h>
GsFrame::GsFrame()
{
}
void GsFrame::processLogic()
{
for(auto &obj : mControlList)
{
obj->processLogic();
}
}
void GsFrame::processRender(const GsRect<float> &rectDispCoordFloat)
{
GsWeakSurface blitsfc(gVideoDriver.getBlitSurface());
// Transform to the display coordinates
auto displayRect = mRect;
displayRect.transform(rectDispCoordFloat);
auto lRect = displayRect.SDLRect();
blitsfc.fill(lRect, mBackgroundColor);
for(auto &obj : mControlList)
{
obj->processRender(rectDispCoordFloat);
}
}
void GsFrame::setBackgroundColor(const GsColor &color)
{
mBackgroundColor = color;
}
#ifndef GSFRAME_H
#define GSFRAME_H
#include "GsControl.h"
#include <base/utils/Color.h>
#include <list>
/**
* @brief The GsFrame class A frame is container holding different widgets.
* The coordinates are all defined in relation to the Framesize
*/
class GsFrame : public GsControl
{
public:
GsFrame();
void processLogic() override;
void processRender(const GsRect<float> &rectDispCoordFloat) override;
void setBackgroundColor(const GsColor &color);
private:
std::list<GsControl*> mControlList;
GsColor mBackgroundColor;
};
#endif // GSFRAME_H
/*
* 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_ */
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