From cdf6dcad239256cc2884544905854ea2c5820c56 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Wed, 26 Apr 2017 08:32:32 +0800 Subject: [PATCH] Add FontManager Remove debug printf. Fix bugs in TextButton. --- MiniEngine_Widget.cpp | 93 +++++++++++++++++++++++++++++++++++++++---- MiniEngine_Widget.h | 23 ++++++++++- 2 files changed, 107 insertions(+), 9 deletions(-) diff --git a/MiniEngine_Widget.cpp b/MiniEngine_Widget.cpp index 1473a69..bcd075c 100644 --- a/MiniEngine_Widget.cpp +++ b/MiniEngine_Widget.cpp @@ -131,6 +131,11 @@ int Brush::fillRect(PosInfo info) return Renderer::fillRect(info.getRect(area)); } +int Brush::drawRect(PosInfo info) +{ + return Renderer::drawRect(info.getRect(area)); +} + SDL_Event _WidgetEventBase::getRealEvent() { @@ -139,10 +144,8 @@ SDL_Event _WidgetEventBase::getRealEvent() void _PositionEvent::updatePos(int X,int Y,Rect Area) { - printf("PositionEvent::updatePos(%d,%d,Rect:%d %d %d %d)\n",X,Y,Area.x,Area.y,Area.w,Area.h); x=(double)(X-Area.x)/Area.w; y=(double)(Y-Area.y)/Area.h; - printf("Position: %.2f %.2f\n",x,y); } void MouseButtonEvent::update(const MiniEngine::EventSystem::MouseButtonEvent& ev,Rect Area) @@ -444,11 +447,79 @@ bool ButtonBase::onMouseMotion(const MouseMotionEvent& ev) return false; } -void TextButton::setText(std::string Text) +//private +FontManager::FontManager() +{ + _id=0; +} + +FONTHANDLE FontManager::loadFont(const std::string& FontFilename,int Size) +{ + Font t; + int ret=t.use(FontFilename,Size); + if(ret!=0) + { + /// Error + return nullptr; + } + else + { + ++_id; + _vec.push_back(std::make_pair(_id,t)); + return (FONTHANDLE)(_id); + } +} + +Font FontManager::getFont(FONTHANDLE ID) +{ + Font t; + for(auto& x:_vec) + { + if(x.first==(int)ID) + { + return x.second; + } + } + return t; +} +void FontManager::freeFont(FONTHANDLE ID) +{ + for(auto iter=_vec.begin();iter!=_vec.end();iter++) + { + if(iter->first==(int)ID) + { + _vec.erase(iter); + return; + } + } +} +FontManager* FontManager::getInstance() +{ + if(_this==nullptr) + { + _this=new FontManager(); + _this->loadFont("msyh.ttf",18); + return _this; + } + else + { + return _this; + } +} + +//static +FontManager* FontManager::_this=nullptr; + +TextButton::TextButton() +{ + _changed=false; +} + +void TextButton::setText(std::string Text,FONTHANDLE FontStyle) { if(Text==_word) return; _word=Text; - /// TODO: How to Render... + _changed=true; } std::string TextButton::getText() @@ -456,10 +527,16 @@ std::string TextButton::getText() return _word; } -void TextButton::draw(Brush&) +void TextButton::draw(Brush& b) { - /// FIXME: Unfinished TextButton::draw() due to Font loading in Frame. - printf("TextButton::draw()\n"); + if(_changed) + { + _text=FontManager::getInstance()->getFont(_style).renderUTF8(b,_word,RGBA(255,255,255,0)); + _changed=false; + } + + b.drawRect(info); + b.copyTo(_text,info); } void ColorButton::onRelease() @@ -502,4 +579,4 @@ void ColorButton::draw(Brush& b) }/// End of namespace MiniEngine::Widget -}/// End of namespace MiniEngine::Widget +}/// End of namespace MiniEngine diff --git a/MiniEngine_Widget.h b/MiniEngine_Widget.h index 7674776..8c9e9b6 100644 --- a/MiniEngine_Widget.h +++ b/MiniEngine_Widget.h @@ -3,6 +3,7 @@ #include "MiniEngine_Event.h" #include #include +#include namespace MiniEngine { @@ -42,6 +43,7 @@ public: Rect getArea(); void setArea(Rect Area); int fillRect(PosInfo info); + int drawRect(PosInfo info); protected: Brush(Renderer Rnd); private: @@ -170,15 +172,34 @@ private: virtual bool onMouseMotion(const MouseMotionEvent&) override; }; +typedef void* FONTHANDLE; +class FontManager +{ +public: + FONTHANDLE loadFont(const std::string& FontFilename,int Size); + Font getFont(FONTHANDLE ID); + void freeFont(FONTHANDLE ID); + + static FontManager* getInstance(); +private: + FontManager(); + std::vector> _vec; + int _id; + static FontManager* _this; +}; + class TextButton : public ButtonBase { public: - void setText(std::string Text); + TextButton(); + void setText(std::string Text,FONTHANDLE FontStyle=(FONTHANDLE)1); std::string getText(); virtual void draw(Brush&) override; private: std::string _word; Texture _text; + bool _changed; + FONTHANDLE _style; }; class ColorButton : public ButtonBase