From 4630a157dc21663e28e1d0b6e1f938c9de06bbdf Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 6 Apr 2017 21:53:53 +0800 Subject: [PATCH] [Big Update] New Widget with new event framework New Event Framework Events from SDL2 will be dispatched in Frame::run(). In class Frame, events are dispatched to Board::event(), and Board with dispatched them to inner Board or Widget. If an event is not solved, the Frame will try to solve it. --- MiniEngine_Event.cpp | 5 ++ MiniEngine_Event.h | 3 + MiniEngine_Widget.cpp | 189 ++++++++++++++++++++++++++++++++++++++++-- MiniEngine_Widget.h | 106 +++++++++++++++++++---- 4 files changed, 280 insertions(+), 23 deletions(-) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 973041a..ab9767e 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -24,6 +24,11 @@ namespace _MiniEngine_Internal } }/// End of namespace _MiniEngine_Internal +EventBase::EventBase(SDL_Event Event) +{ + e=Event; +} + void EventBase::update(const EventBase& ev) { e=ev.e; diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 09444e5..3784d94 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -3,6 +3,9 @@ class EventBase { +public: + EventBase()=default; + EventBase(SDL_Event); protected: SDL_Event e; virtual void update(const EventBase&); diff --git a/MiniEngine_Widget.cpp b/MiniEngine_Widget.cpp index 1627802..fb9b32b 100644 --- a/MiniEngine_Widget.cpp +++ b/MiniEngine_Widget.cpp @@ -1,5 +1,4 @@ #include "MiniEngine_Widget.h" -using namespace MiniEngine; namespace MiniEngine { @@ -7,13 +6,28 @@ namespace MiniEngine namespace Widget { +Rect PosInfo::getRect(Rect SizeRect) +{ + SizeRect.x*=x; + SizeRect.y*=y; + SizeRect.w*=w; + SizeRect.h*=h; + return SizeRect; +} + void Brush::setArea(Rect Area) { area=Area; } -int Brush::copy(Texture t,Rect src,Rect dst) +Rect Brush::getArea() { + return area; +} + +int Brush::copy(Texture t,Rect src,PosInfo dst_posinfo) +{ + Rect dst=dst_posinfo.getRect(area); dst.x+=area.x; dst.y+=area.y; @@ -42,8 +56,9 @@ int Brush::copy(Texture t,Rect src,Rect dst) return Renderer::copy(t,src,dst); } -int Brush::copyTo(Texture t,Rect dst) +int Brush::copyTo(Texture t,PosInfo dst_posinfo) { + Rect dst=dst_posinfo.getRect(area); dst.x+=area.x; dst.y+=area.y; @@ -81,9 +96,14 @@ int Brush::copyTo(Texture t,Rect dst) } } -int Brush::copyTo(Texture t,Point lupoint) +int Brush::copyTo(Texture t,PointInfo lupoint) { - return copyTo(t,Rect(lupoint.x,lupoint.y,t.getw(),t.geth())); + PosInfo pos; + pos.x=lupoint.x; + pos.y=lupoint.y; + pos.w=(double)t.getw()/area.w; + pos.h=(double)t.geth()/area.h; + return copyTo(t,pos); } int Brush::copyFill(Texture t,Rect src) @@ -105,6 +125,165 @@ Brush::Brush(Renderer Rnd) : Renderer(Rnd) + +Frame::Frame(Renderer rnd,Rect Area) : brush(rnd) +{ + brush.setArea(Area); + update=running=true; +} + +void Frame::add(Board* p) +{ + _lst.push_back(p); +} + +int Frame::remove(Board* p) +{ + _lst.remove(p); + return 0; +} + +void Frame::run() +{ + while(running) + { + while(!update&&SDL_WaitEvent(&e)) + { + EventBase x(e); + bool dealed=false; + if(!_lst.empty()) + { + for(auto iter=_lst.rbegin();iter!=_lst.rend();++iter) + { + if((dealed=(*iter)->event(x))) break; + } + } + + /// If Message Not Solved, Try to Solve it here. (Maybe System Message) + if(!dealed) + { + switch(e.type) + { + case SDL_QUIT: + stop(); + needUpdate(); + break; + } + } + } + + brush.clear(); + + if(!_lst.empty()) + { + for(auto iter=_lst.rbegin();iter!=_lst.rend();++iter) + { + Brush nbrush=brush; + nbrush.setArea((*iter)->getPosInfo().getRect(brush.getArea())); + (*iter)->draw(nbrush); + } + } + + brush.update(); + + update=false; + + printf("Looped\n"); + } +} + +void Frame::stop() +{ + running=false; +} + +void Frame::needUpdate() +{ + update=true; +} + + + + + +void Board::add(BoardBase* p) +{ + _lst.push_back(p); +} + +int Board::remove(BoardBase* p) +{ + _lst.remove(p); + return 0; +} + +PosInfo Board::getPosInfo() +{ + return info; +} + +void Board::draw(const Brush& b) +{ + /// FIXME: Bug Found while trying to draw a Board in Board. + for(auto& p:_lst) + { + p->draw(b); + } +} + +bool Board::event(const EventBase& ev) /// virtual override +{ + return false; +} + +bool ButtonBase::onClick() +{ + return false; +} + +bool ButtonBase::onMouseOver() +{ + return false; +} + +bool ButtonBase::onMouseOut() +{ + return false; +} + +bool ButtonBase::onMouseDown(const MouseButtonEvent& ev) +{ + return false; +} + +bool ButtonBase::onMouseUp(const MouseButtonEvent& ev) +{ + return false; +} + +bool ButtonBase::onMouseMotion(const MouseMotionEvent& ev) +{ + return false; +} + +void TextButton::setText(std::string Text) +{ + if(Text==_word) return; + _word=Text; + /// TODO: How to Render... +} + +std::string TextButton::getText() +{ + return _word; +} + +void TextButton::draw(const Brush&) +{ + printf("TextButton::draw()\n"); +} + + }/// End of namespace MiniEngine::Widget }/// End of namespace MiniEngine::Widget diff --git a/MiniEngine_Widget.h b/MiniEngine_Widget.h index a6bd46d..c662708 100644 --- a/MiniEngine_Widget.h +++ b/MiniEngine_Widget.h @@ -1,6 +1,8 @@ #pragma once #include "MiniEngine.h" #include "MiniEngine_Event.h" +#include +#include namespace MiniEngine { @@ -8,44 +10,112 @@ namespace MiniEngine namespace Widget { +class PosInfo +{ +public: + double x,y; + double w,h; + Rect getRect(Rect SizeRect); +}; + +class PointInfo +{ +public: + double x,y; +}; + + +/// Forward Declaration +class Frame; +class Board; + class Brush : public Renderer { public: - void setArea(Rect Area); - void setFullArea(Rect FullArea); - - int copy(Texture t, Rect src, Rect dst); - int copyTo(Texture t, Rect dst); - int copyTo(Texture t, Point lupoint); + int copy(Texture t, Rect src, PosInfo dst); + int copyTo(Texture t, PosInfo dst); + int copyTo(Texture t, PointInfo lupoint); int copyFill(Texture t,Rect src); int copyFullFill(Texture t); + Rect getArea(); protected: Brush(Renderer Rnd); + void setArea(Rect Area); private: - Rect area,fullarea; - friend class Board; + Rect area; + friend class Frame; }; -class Drawable + +class BoardBase : public EventHandlerBase { public: - virtual void draw(Brush& brush)=0; - virtual ~Drawable()=default; + virtual void draw(const Brush&)=0; }; -class ButtonBase : public Drawable, public EventHandlerBase +class Frame +{ +public: + Frame(Renderer,Rect); + void run(); + void stop(); + void needUpdate(); + void add(Board*); + int remove(Board*); +private: + bool running; + bool update; + SDL_Event e; + std::list _lst; + Brush brush; +}; + +class Board : public BoardBase +{ +public: + void add(BoardBase*); + int remove(BoardBase*); + virtual void draw(const Brush&); + virtual bool event(const EventBase& ev) override; + Frame* getFrame(); + PosInfo getPosInfo(); +private: + std::list _lst; + PosInfo info; +}; + +class WidgetBase : public BoardBase +{ +public: + Board* getBoard(); +protected: + PosInfo info; +}; + +class ButtonBase : public WidgetBase { protected: bool onClick(); bool onMouseOver(); bool onMouseOut(); - - /// Overrides - bool onMouseDown(); - bool onMouseUp(); - bool onMouseMotion(); private: - bool status; + /// Overrides + virtual bool onMouseDown(const MouseButtonEvent&) override; + virtual bool onMouseUp(const MouseButtonEvent&) override; + virtual bool onMouseMotion(const MouseMotionEvent&) override; + + int _status; +}; + +class TextButton : public ButtonBase +{ +public: + void setText(std::string Text); + std::string getText(); + virtual void draw(const Brush&) override; +private: + std::string _word; + Texture _text; }; }/// End of namespace MiniEngine::Widget