CUI类实现了监听事件和事件派发处理。这个没什么难的,所以就直接贴代码了!
老规矩下面贴出来完整代码:
CUI.h代码实现
#pragma once #include "UI_Manage.h" namespace U2D { struct Event { enum TYPE { MOUSE_UP, }type; event_selector listener_event; //事件监听 }; class CUI :public CNode { //friend class CTitleScene; friend class CUI_Manage; protected: CPicture texture; char name[50]; Vector2 cornerPoint[4]; list<Event> event_list; //事件监听列表 void sendEvent(Event::TYPE e);//分发消息 public: CUI(); void setName(char *name) { strcpy(this->name, name); } char *getName() { return this->name; } virtual void draw() {}; virtual void drawDebug() {}; void addEventListener(Event::TYPE e, event_selector fun_event); virtual bool MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return false; } virtual ~CUI(); }; }
CUI.cpp 代码实现
这个没什么难的,所以就直接贴代码了,这个类因为是个基类,只有.h没有.cpp文件。 Ui.h代码实现 #pragma once #include "UI_Manage.h" namespace U2D { struct Event { enum TYPE { MOUSE_UP, }type; event_selector listener_event; //事件监听 }; class CUI :public CNode { //friend class CTitleScene; friend class CUI_Manage; protected: CPicture texture; char name[50]; Vector2 cornerPoint[4]; list<Event> event_list; //事件监听列表 void sendEvent(Event::TYPE e);//分发消息 public: CUI(); void setName(char *name) { strcpy(this->name, name); } char *getName() { return this->name; } virtual void draw() {}; virtual void drawDebug() {}; void addEventListener(Event::TYPE e, event_selector fun_event); virtual bool MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return false; } virtual ~CUI(); }; } [点击并拖拽以移动] Ui.cpp 代码实现 阿 #include "Engine.h" namespace U2D { CUI::CUI() { memset(name, 0, 50); memset(cornerPoint, 0, sizeof(Vector2) * 4); } void CUI::addEventListener(Event::TYPE e, event_selector fun_event) { Event eve; eve.type = e; eve.listener_event = fun_event; event_list.push_back(eve); } void CUI::sendEvent(Event::TYPE e) { list<Event>::iterator iter; for (iter = event_list.begin(); iter != event_list.end(); iter++) { if (iter->type == e)//如果发来的消息和我们想要的消息一致就执行回调函数,比如传入的消息是mouse_down,我们传入的(也就是我们想要的状态)也为down,这个时候就会执行这个回调函数里面的内容 { if (parent == NULL) (this->*(iter->listener_event))(this); else (parent->*(iter->listener_event))(this); } } } CUI::~CUI() { } }