[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.
This commit is contained in:
Kirigaya Kazuto 2017-04-06 21:53:53 +08:00
parent 6ec7a5c77a
commit 4630a157dc
4 changed files with 280 additions and 23 deletions

View File

@ -24,6 +24,11 @@ namespace _MiniEngine_Internal
} }
}/// End of namespace _MiniEngine_Internal }/// End of namespace _MiniEngine_Internal
EventBase::EventBase(SDL_Event Event)
{
e=Event;
}
void EventBase::update(const EventBase& ev) void EventBase::update(const EventBase& ev)
{ {
e=ev.e; e=ev.e;

View File

@ -3,6 +3,9 @@
class EventBase class EventBase
{ {
public:
EventBase()=default;
EventBase(SDL_Event);
protected: protected:
SDL_Event e; SDL_Event e;
virtual void update(const EventBase&); virtual void update(const EventBase&);

View File

@ -1,5 +1,4 @@
#include "MiniEngine_Widget.h" #include "MiniEngine_Widget.h"
using namespace MiniEngine;
namespace MiniEngine namespace MiniEngine
{ {
@ -7,13 +6,28 @@ namespace MiniEngine
namespace Widget 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) void Brush::setArea(Rect Area)
{ {
area=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.x+=area.x;
dst.y+=area.y; dst.y+=area.y;
@ -42,8 +56,9 @@ int Brush::copy(Texture t,Rect src,Rect dst)
return Renderer::copy(t,src,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.x+=area.x;
dst.y+=area.y; 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) 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
}/// End of namespace MiniEngine::Widget }/// End of namespace MiniEngine::Widget

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "MiniEngine.h" #include "MiniEngine.h"
#include "MiniEngine_Event.h" #include "MiniEngine_Event.h"
#include <string>
#include <list>
namespace MiniEngine namespace MiniEngine
{ {
@ -8,44 +10,112 @@ namespace MiniEngine
namespace Widget 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 class Brush : public Renderer
{ {
public: public:
void setArea(Rect Area); int copy(Texture t, Rect src, PosInfo dst);
void setFullArea(Rect FullArea); int copyTo(Texture t, PosInfo dst);
int copyTo(Texture t, PointInfo lupoint);
int copy(Texture t, Rect src, Rect dst);
int copyTo(Texture t, Rect dst);
int copyTo(Texture t, Point lupoint);
int copyFill(Texture t,Rect src); int copyFill(Texture t,Rect src);
int copyFullFill(Texture t); int copyFullFill(Texture t);
Rect getArea();
protected: protected:
Brush(Renderer Rnd); Brush(Renderer Rnd);
void setArea(Rect Area);
private: private:
Rect area,fullarea; Rect area;
friend class Board; friend class Frame;
}; };
class Drawable
class BoardBase : public EventHandlerBase
{ {
public: public:
virtual void draw(Brush& brush)=0; virtual void draw(const Brush&)=0;
virtual ~Drawable()=default;
}; };
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<Board*> _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<BoardBase*> _lst;
PosInfo info;
};
class WidgetBase : public BoardBase
{
public:
Board* getBoard();
protected:
PosInfo info;
};
class ButtonBase : public WidgetBase
{ {
protected: protected:
bool onClick(); bool onClick();
bool onMouseOver(); bool onMouseOver();
bool onMouseOut(); bool onMouseOut();
/// Overrides
bool onMouseDown();
bool onMouseUp();
bool onMouseMotion();
private: 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 }/// End of namespace MiniEngine::Widget