2017-03-02 12:19:13 +08:00
|
|
|
#include "MiniEngine_Widget.h"
|
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
namespace MiniEngine
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace Widget
|
|
|
|
{
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
Rect PosInfo::getRect(Rect Area)
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
Area.x*=x;
|
|
|
|
Area.y*=y;
|
|
|
|
Area.w*=w;
|
|
|
|
Area.h*=h;
|
|
|
|
return Area;
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
|
2017-03-02 12:19:13 +08:00
|
|
|
void Brush::setArea(Rect Area)
|
|
|
|
{
|
|
|
|
area=Area;
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
Rect Brush::getArea()
|
|
|
|
{
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Brush::copy(Texture t,Rect src,PosInfo dst_posinfo)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-04-06 21:53:53 +08:00
|
|
|
Rect dst=dst_posinfo.getRect(area);
|
2017-03-02 12:19:13 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
int Brush::copyTo(Texture t,PosInfo dst_posinfo)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-04-06 21:53:53 +08:00
|
|
|
Rect dst=dst_posinfo.getRect(area);
|
2017-03-02 12:19:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
int Brush::copyTo(Texture t,PointInfo lupoint)
|
2017-03-02 12:19:13 +08:00
|
|
|
{
|
2017-04-06 21:53:53 +08:00
|
|
|
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);
|
2017-03-02 12:19:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:32:00 +08:00
|
|
|
int Brush::fillRect(PosInfo info)
|
|
|
|
{
|
|
|
|
return Renderer::fillRect(info.getRect(area));
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:00:53 +08:00
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
|
|
|
|
Frame::Frame(Renderer rnd,Rect Area) : brush(rnd)
|
|
|
|
{
|
|
|
|
brush.setArea(Area);
|
|
|
|
update=running=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::add(Board* p)
|
|
|
|
{
|
|
|
|
_lst.push_back(p);
|
2017-04-07 16:00:40 +08:00
|
|
|
p->_frame=this;
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Frame::remove(Board* p)
|
|
|
|
{
|
|
|
|
_lst.remove(p);
|
2017-04-07 16:00:40 +08:00
|
|
|
p->_frame=nullptr;
|
2017-04-06 21:53:53 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
void Board::add(Board* p)
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
_blst.push_back(p);
|
|
|
|
p->_parent=this;
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
int Board::remove(Board* p)
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
_blst.remove(p);
|
|
|
|
p->_parent=nullptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Board::add(WidgetBase* p)
|
|
|
|
{
|
|
|
|
_wlst.push_back(p);
|
|
|
|
p->_parent=this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Board::remove(WidgetBase* p)
|
|
|
|
{
|
|
|
|
_wlst.remove(p);
|
|
|
|
p->_parent=nullptr;
|
2017-04-06 21:53:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PosInfo Board::getPosInfo()
|
|
|
|
{
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:32:00 +08:00
|
|
|
void Board::draw(Brush& b)
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
|
|
|
/// FIXME: Bug Found while trying to draw a Board in Board.
|
2017-04-07 16:00:40 +08:00
|
|
|
for(auto& p:_wlst)
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
|
|
|
p->draw(b);
|
|
|
|
}
|
2017-04-07 16:00:40 +08:00
|
|
|
|
|
|
|
Brush nb=b;
|
|
|
|
nb.setArea(info.getRect(nb.getArea()));
|
|
|
|
for(auto& p:_blst)
|
|
|
|
{
|
|
|
|
p->draw(nb);
|
|
|
|
}
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Board::event(const EventBase& ev) /// virtual override
|
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
for(auto& p:_wlst)
|
|
|
|
{
|
|
|
|
if(p->event(ev)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto& p:_blst)
|
|
|
|
{
|
|
|
|
if(p->event(ev)) return true;
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
ButtonBase::ButtonBase()
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
_status=0;
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
void ButtonBase::onPressed()
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 16:00:40 +08:00
|
|
|
void ButtonBase::onClick()
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonBase::onMouseOver()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonBase::onMouseOut()
|
|
|
|
{
|
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ButtonBase::onMouseDown(const MouseButtonEvent& ev)
|
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:32:00 +08:00
|
|
|
void TextButton::draw(Brush&)
|
2017-04-06 21:53:53 +08:00
|
|
|
{
|
2017-04-07 16:00:40 +08:00
|
|
|
/// FIXME: Unfinished TextButton::draw() due to Font loading in Frame.
|
2017-04-06 21:53:53 +08:00
|
|
|
printf("TextButton::draw()\n");
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:32:00 +08:00
|
|
|
void ColorButton::draw(Brush& b)
|
2017-04-07 16:00:40 +08:00
|
|
|
{
|
2017-04-24 10:32:00 +08:00
|
|
|
RGBA td=b.getColor();
|
2017-04-07 16:00:40 +08:00
|
|
|
switch(_colorstatus)
|
|
|
|
{
|
|
|
|
case 0:/// Normal
|
|
|
|
/// How to fill rect with PosInfo and Brush...
|
2017-04-24 10:32:00 +08:00
|
|
|
b.setColor(normal);
|
|
|
|
b.fillRect(info);
|
2017-04-07 16:00:40 +08:00
|
|
|
break;
|
|
|
|
case 1:/// Active (MouseOver)
|
2017-04-24 10:32:00 +08:00
|
|
|
b.setColor(active);
|
|
|
|
b.fillRect(info);
|
2017-04-07 16:00:40 +08:00
|
|
|
break;
|
|
|
|
case 2:/// Pressed Down (MouseUp)
|
2017-04-24 10:32:00 +08:00
|
|
|
b.setColor(active);
|
|
|
|
b.fillRect(info);
|
2017-04-07 16:00:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-04-24 10:32:00 +08:00
|
|
|
b.setColor(td);
|
2017-04-07 16:00:40 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 21:53:53 +08:00
|
|
|
|
2017-03-02 13:12:41 +08:00
|
|
|
}/// End of namespace MiniEngine::Widget
|
|
|
|
|
|
|
|
}/// End of namespace MiniEngine::Widget
|