Delete Board, add new ButtonBase

This commit is contained in:
Kirigaya Kazuto 2017-04-06 12:32:07 +08:00
parent 0d4b0a534d
commit 49a0db30d1
2 changed files with 11 additions and 199 deletions

View File

@ -103,161 +103,7 @@ Brush::Brush(Renderer Rnd) : Renderer(Rnd)
}
Board::Board(Renderer Rnd,Rect Area) : area(Area),brush(Rnd)
{
brush.setArea(area);
}
Brush Board::getBrush()
{
return brush;
}
Rect Board::getArea()
{
return area;
}
void Board::_Control::add(Interactive* widget)
{
vec.push_back(widget);
}
int Board::_Control::size()
{
return vec.size();
}
bool Board::_Control::remove(Interactive* widget)
{
for(auto iter=vec.begin();iter!=vec.end();++iter)
{
if(*iter==widget)
{
vec.erase(iter);
return true;
}
}
return false;
}
Interactive* Board::_Control::at(int index)
{
return vec.at(index);
}
ButtonBase::ButtonBase()
{
status=1;
}
void ButtonBase::setTextureClicked(Texture Clicked)
{
t3=Clicked;
}
void ButtonBase::setTextureMouseover(Texture Mouseover)
{
t2=Mouseover;
}
void ButtonBase::setTextureNormal(Texture Normal)
{
t1=Normal;
}
void ButtonBase::setRect(Rect SensorArea)
{
rect=SensorArea;
}
void ButtonBase::draw(Brush& brush) /// virtual
{
switch(status)
{
case 1:
brush.copyTo(t1,rect);
break;
case 2:
brush.copyTo(t2,rect);
break;
case 3:
brush.copyTo(t3,rect);
break;
}
}
int ButtonBase::handle(SDL_Event e,int& running,int& update) /// virtual
{
switch(e.type)
{
case SDL_MOUSEMOTION:
{
if(Point(e.motion.x,e.motion.y).inRect(rect))
{
if(status==1)
{
if(onmouseover) onmouseover();
status=2;
update=1;
}
}
else
{
if(status==2)
{
if(onmouseout) onmouseout();
status=1;
update=1;
}
else if(status==3)
{
if(onrelease) onrelease();
if(onmouseout) onmouseout();
status=1;
update=1;
}
}
return 1;
}
break;
case SDL_MOUSEBUTTONDOWN:
{
if(Point(e.button.x,e.button.y).inRect(rect))
{
if(status==2)
{
if(onclicked) onclicked();
status=3;
update=1;
}
}
return 1;
}
break;
case SDL_MOUSEBUTTONUP:
{
if(Point(e.button.x,e.button.y).inRect(rect))
{
if(status==3)
{
if(onrelease) onrelease();
status=2;
update=1;
}
}
return 1;
}
break;
}
return 0;
}
}/// End of namespace MiniEngine::Widget

View File

@ -1,7 +1,6 @@
#pragma once
#include "MiniEngine.h"
#include <functional>
#include <vector>
#include "MiniEngine_Event.h"
namespace MiniEngine
{
@ -27,7 +26,6 @@ private:
friend class Board;
};
class Drawable
{
public:
@ -35,51 +33,19 @@ public:
virtual ~Drawable()=default;
};
class Interactive
class ButtonBase : public Drawable, public EventHandlerBase
{
public:
virtual int handle(SDL_Event e,int& running,int& update)=0;
};
protected:
bool onClick();
bool onMouseOver();
bool onMouseOut();
class ButtonBase : public Drawable, public Interactive
{
public:
ButtonBase();
void setTextureNormal(Texture Normal);
void setTextureMouseover(Texture Mouseover);
void setTextureClicked(Texture Clicked);
void setRect(Rect SensorArea);
virtual void draw(Brush& brush);
virtual int handle(SDL_Event e,int& running,int& update);
std::function<void()> onmouseover,onclicked,onrelease,onmouseout;
/// Overrides
bool onMouseDown();
bool onMouseUp();
bool onMouseMotion();
private:
int status;
Texture t1,t2,t3;
Rect rect;
};
class Board
{
public:
Board(Renderer Rnd,Rect Area);
Brush getBrush();
Rect getArea();
class _Control
{
public:
void add(Interactive* widget);
Interactive* at(int index);
int size();
bool remove(Interactive* widget);
private:
std::vector<Interactive*> vec;
}Control;
private:
Rect area;
Brush brush;
bool status;
};
}/// End of namespace MiniEngine::Widget