From 0a2efcf8feb340ca4f0a2257ac3dd291c253b56b Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Thu, 2 Mar 2017 21:00:53 +0800 Subject: [PATCH] Update Brush,Board and BaseButton. --- MiniEngine_Widget.cpp | 92 ++++++++++++++++++++++++++++++++++++++----- MiniEngine_Widget.h | 12 ++++-- 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/MiniEngine_Widget.cpp b/MiniEngine_Widget.cpp index 5d5c937..e43ed68 100644 --- a/MiniEngine_Widget.cpp +++ b/MiniEngine_Widget.cpp @@ -1,4 +1,6 @@ #include "MiniEngine_Widget.h" +#include +using namespace std; using namespace MiniEngine; namespace MiniEngine @@ -98,16 +100,19 @@ int Brush::copyFullFill(Texture t) return Renderer::copyTo(t,dst); } -Board::Board(Rect Area) +Brush::Brush(Renderer Rnd) : Renderer(Rnd) { - area=Area; + +} + +Board::Board(Renderer Rnd,Rect Area) : area(Area),brush(Rnd) +{ + brush.setArea(area); } Brush Board::getBrush() { - Brush b; - b.setArea(area); - return b; + return brush; } Rect Board::getArea() @@ -123,7 +128,7 @@ Rect Board::getArea() ButtonBase::ButtonBase() { - status=0; + status=1; } void ButtonBase::setTextureClicked(Texture Clicked) @@ -148,20 +153,89 @@ void ButtonBase::setRect(Rect SensorArea) void ButtonBase::draw(Brush& brush) /// virtual { + int ret=-1; switch(status) { - case 0: - break; case 1: + ret=brush.copyTo(t1,rect); break; case 2: + ret=brush.copyTo(t2,rect); + break; + case 3: + ret=brush.copyTo(t3,rect); break; } } -void ButtonBase::handle(SDL_Event e,int& running,int& update) /// virtual +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 diff --git a/MiniEngine_Widget.h b/MiniEngine_Widget.h index 8610a8b..4a5bc44 100644 --- a/MiniEngine_Widget.h +++ b/MiniEngine_Widget.h @@ -19,18 +19,22 @@ public: int copyTo(Texture t, Point lupoint); int copyFill(Texture t,Rect src); int copyFullFill(Texture t); +protected: + Brush(Renderer Rnd); private: Rect area,fullarea; + friend class Board; }; class Board { public: - Board(Rect Area); + Board(Renderer Rnd,Rect Area); Brush getBrush(); Rect getArea(); private: Rect area; + Brush brush; }; class Drawable @@ -43,7 +47,7 @@ public: class Interactive { public: - virtual void handle(SDL_Event e,int& running,int& update)=0; + virtual int handle(SDL_Event e,int& running,int& update)=0; }; class ButtonBase : public Drawable, public Interactive @@ -55,7 +59,9 @@ public: void setTextureClicked(Texture Clicked); void setRect(Rect SensorArea); virtual void draw(Brush& brush); - virtual void handle(SDL_Event e,int& running,int& update); + virtual int handle(SDL_Event e,int& running,int& update); + + std::function onmouseover,onclicked,onrelease,onmouseout; private: int status; Texture t1,t2,t3;