2017-03-02 12:19:13 +08:00
|
|
|
#include "MiniEngine_Widget.h"
|
|
|
|
using namespace MiniEngine;
|
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
namespace MiniEngine
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace Widget
|
|
|
|
{
|
|
|
|
|
2017-03-02 12:19:13 +08:00
|
|
|
void Brush::setArea(Rect Area)
|
|
|
|
{
|
|
|
|
area=Area;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Brush::copy(Texture t,Rect src,Rect dst)
|
|
|
|
{
|
|
|
|
dst.x+=area.x;
|
|
|
|
dst.y+=area.y;
|
2017-03-02 13:12:41 +08:00
|
|
|
|
|
|
|
if(dst.x>area.x+area.w||dst.y>area.y+area.h)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
/// Not Printed.
|
|
|
|
return 1;
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
|
|
|
if(dst.x+dst.w>area.x+area.w)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
/// Some parts will not be printed.
|
|
|
|
src.w=src.w*(area.w-dst.x+area.x)/dst.w;
|
|
|
|
|
|
|
|
dst.w=area.w-(dst.x-area.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dst.y+dst.h>area.y+area.h)
|
|
|
|
{
|
|
|
|
/// Some parts will not be printed
|
|
|
|
src.h=src.h*(area.h-dst.y+area.y)/dst.h;
|
|
|
|
|
|
|
|
dst.h=area.h-(dst.y-area.y);
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-03-02 12:19:13 +08:00
|
|
|
return Renderer::copy(t,src,dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Brush::copyTo(Texture t,Rect dst)
|
|
|
|
{
|
|
|
|
dst.x+=area.x;
|
|
|
|
dst.y+=area.y;
|
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
if(dst.x>area.x+area.w||dst.y>area.y+area.h)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
/// Not Printed.
|
|
|
|
return 1;
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
|
|
|
if(dst.x+dst.w>area.x+area.w||dst.y+dst.h>area.y+area.h)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
/// Some parts will not be printed.
|
2017-03-02 12:19:13 +08:00
|
|
|
Rect src=t.getSize();
|
|
|
|
src.x=src.y=0;
|
2017-03-02 13:12:41 +08:00
|
|
|
|
|
|
|
if(dst.x+dst.w>area.x+area.w)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
src.w=src.w*(area.w-dst.x+area.x)/dst.w;
|
|
|
|
|
|
|
|
dst.w=area.w-(dst.x-area.x);
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
|
|
|
if(dst.y+dst.h>area.y+area.h)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
src.h=src.h*(area.h-dst.y+area.y)/dst.h;
|
|
|
|
|
|
|
|
dst.h=area.h-(dst.y-area.y);
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Renderer::copy(t,src,dst);
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return Renderer::copyTo(t,dst);
|
|
|
|
}
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Brush::copyTo(Texture t,Point lupoint)
|
|
|
|
{
|
|
|
|
return copyTo(t,Rect(lupoint.x,lupoint.y,t.getw(),t.geth()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Brush::copyFill(Texture t,Rect src)
|
|
|
|
{
|
|
|
|
Rect dst=area;
|
|
|
|
return Renderer::copy(t,src,dst);
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
int Brush::copyFullFill(Texture t)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
|
|
|
Rect dst=area;
|
|
|
|
return Renderer::copyTo(t,dst);
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:00:53 +08:00
|
|
|
Brush::Brush(Renderer Rnd) : Renderer(Rnd)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 21:00:53 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Board::Board(Renderer Rnd,Rect Area) : area(Area),brush(Rnd)
|
|
|
|
{
|
|
|
|
brush.setArea(area);
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Brush Board::getBrush()
|
|
|
|
{
|
2017-03-02 21:00:53 +08:00
|
|
|
return brush;
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
Rect Board::getArea()
|
|
|
|
{
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:00:01 +08:00
|
|
|
void Board::_Control::add(Interactive* widget)
|
|
|
|
{
|
|
|
|
vec.push_back(widget);
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-03-02 22:00:01 +08:00
|
|
|
int Board::_Control::size()
|
|
|
|
{
|
|
|
|
return vec.size();
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-03-02 22:00:01 +08:00
|
|
|
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;
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-03-02 22:00:01 +08:00
|
|
|
Interactive* Board::_Control::at(int index)
|
|
|
|
{
|
|
|
|
return vec.at(index);
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-02 12:19:13 +08:00
|
|
|
ButtonBase::ButtonBase()
|
|
|
|
{
|
2017-03-02 21:00:53 +08:00
|
|
|
status=1;
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
void ButtonBase::draw(Brush& brush) /// virtual
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-03-02 21:00:53 +08:00
|
|
|
int ret=-1;
|
2017-03-02 12:19:13 +08:00
|
|
|
switch(status)
|
|
|
|
{
|
2017-03-02 13:12:41 +08:00
|
|
|
case 1:
|
2017-03-02 21:00:53 +08:00
|
|
|
ret=brush.copyTo(t1,rect);
|
2017-03-02 13:12:41 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2017-03-02 21:00:53 +08:00
|
|
|
ret=brush.copyTo(t2,rect);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ret=brush.copyTo(t3,rect);
|
2017-03-02 13:12:41 +08:00
|
|
|
break;
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-03-02 21:00:53 +08:00
|
|
|
int ButtonBase::handle(SDL_Event e,int& running,int& update) /// virtual
|
2017-03-02 13:12:41 +08:00
|
|
|
{
|
2017-03-02 21:00:53 +08:00
|
|
|
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;
|
|
|
|
}
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-03-02 21:00:53 +08:00
|
|
|
return 0;
|
2017-03-02 13:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}/// End of namespace MiniEngine::Widget
|
|
|
|
|
|
|
|
}/// End of namespace MiniEngine::Widget
|