From b78d31d8a01ff4e7e86bf433f9046a3f783d63fa Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 6 Mar 2025 01:20:37 +0800 Subject: [PATCH] Layout codes of InputEvent/WindowEvent --- inc/hgl/io/event/InputEvent.h | 163 ++++++++++++++-------------- inc/hgl/io/event/InputEventSource.h | 29 ++--- inc/hgl/io/event/WindowEvent.h | 4 +- 3 files changed, 93 insertions(+), 103 deletions(-) diff --git a/inc/hgl/io/event/InputEvent.h b/inc/hgl/io/event/InputEvent.h index e22f53b..51aea9f 100644 --- a/inc/hgl/io/event/InputEvent.h +++ b/inc/hgl/io/event/InputEvent.h @@ -1,104 +1,99 @@ -#ifndef HGL_IO_INPUT_EVENT_INCLUDE -#define HGL_IO_INPUT_EVENT_INCLUDE +#pragma once #include #include -namespace hgl +namespace hgl::io { - namespace io + struct EventHeader { - struct EventHeader + uint8 type; ///<输入源类型 + uint8 index; ///<输入源索引(比如同一设备有多个) + uint16 id; ///<事件id + }; + + constexpr size_t EventHeaderBytes=sizeof(EventHeader); + + /** + * 事件处理结果 + */ + enum class EventProcResult + { + Continue, + Break, + }; + + class InputEvent + { + protected: + + InputEventSource source_type; + + SortedSet sub_event_proc[size_t(InputEventSource::RANGE_SIZE)]; + + public: + + const InputEventSource GetInputEventSource()const{return source_type;} + + virtual EventProcResult OnEvent(const EventHeader &header,const uint64 data) { - uint8 type; ///<输入源类型 - uint8 index; ///<输入源索引(比如同一设备有多个) - uint16 id; ///<事件id - }; + if(!RangeCheck((InputEventSource)header.type)) + return(EventProcResult::Break); - constexpr size_t EventHeaderBytes=sizeof(EventHeader); + if(sub_event_proc[header.type].GetCount()>0) + { + for(InputEvent *ie:sub_event_proc[header.type]) + if(ie->OnEvent(header,data)==EventProcResult::Break) + return EventProcResult::Break; + } - /** - * 事件处理结果 - */ - enum class EventProcResult + if(sub_event_proc[size_t(InputEventSource::Root)].GetCount()>0 + &&InputEventSource(header.type)!=InputEventSource::Root) + { + for(InputEvent *ie:sub_event_proc[size_t(InputEventSource::Root)]) + if(ie->OnEvent(header,data)==EventProcResult::Break) + return EventProcResult::Break; + } + + return(EventProcResult::Continue); + } + + public: + + InputEvent() { - Continue, - Break, - }; + source_type=InputEventSource::Root; + } - class InputEvent + InputEvent(InputEventSource ies) { - protected: + source_type=ies; + } - InputEventSource source_type; + virtual ~InputEvent()=default; - SortedSet sub_event_proc[size_t(InputEventSource::RANGE_SIZE)]; + virtual bool Join(InputEvent *ie) + { + if(!ie) + return(false); - public: + const InputEventSource ies=ie->GetInputEventSource(); - const InputEventSource GetInputEventSource()const{return source_type;} + if(!RangeCheck(ies)) + return(false); - virtual EventProcResult OnEvent(const EventHeader &header,const uint64 data) - { - if(!RangeCheck((InputEventSource)header.type)) - return(EventProcResult::Break); + return(sub_event_proc[size_t(ies)].Add(ie)!=-1); + } - if(sub_event_proc[header.type].GetCount()>0) - { - for(InputEvent *ie:sub_event_proc[header.type]) - if(ie->OnEvent(header,data)==EventProcResult::Break) - return EventProcResult::Break; - } + bool Unjoin(InputEvent *ie) + { + if(!ie)return(false); - if(sub_event_proc[size_t(InputEventSource::Root)].GetCount()>0 - &&InputEventSource(header.type)!=InputEventSource::Root) - { - for(InputEvent *ie:sub_event_proc[size_t(InputEventSource::Root)]) - if(ie->OnEvent(header,data)==EventProcResult::Break) - return EventProcResult::Break; - } + const InputEventSource ies=ie->GetInputEventSource(); - return(EventProcResult::Continue); - } + if(!RangeCheck(ies)) + return(false); - public: - - InputEvent() - { - source_type=InputEventSource::Root; - } - - InputEvent(InputEventSource ies) - { - source_type=ies; - } - - virtual ~InputEvent()=default; - - virtual bool Join(InputEvent *ie) - { - if(!ie) - return(false); - - const InputEventSource ies=ie->GetInputEventSource(); - - if(!RangeCheck(ies)) - return(false); - - return(sub_event_proc[size_t(ies)].Add(ie)!=-1); - } - - bool Unjoin(InputEvent *ie) - { - if(!ie)return(false); - - const InputEventSource ies=ie->GetInputEventSource(); - - if(!RangeCheck(ies)) - return(false); - - return sub_event_proc[size_t(ies)].Delete(ie); - } - };//class InputEvent - }//namespace io -}//namespace hgl -#endif//HGL_IO_INPUT_EVENT_INCLUDE + return sub_event_proc[size_t(ies)].Delete(ie); + } + };//class InputEvent +}//namespace hgl::io diff --git a/inc/hgl/io/event/InputEventSource.h b/inc/hgl/io/event/InputEventSource.h index 62480fe..ed1f05a 100644 --- a/inc/hgl/io/event/InputEventSource.h +++ b/inc/hgl/io/event/InputEventSource.h @@ -1,24 +1,19 @@ -#ifndef HGL_IO_INPUT_DEVICE_SOURCE_INCLUDE -#define HGL_IO_INPUT_DEVICE_SOURCE_INCLUDE +#pragma once #include -namespace hgl +namespace hgl::io { - namespace io + enum class InputEventSource { - enum class InputEventSource - { - Root=0, + Root=0, - OS, - Window, + OS, + Window, - Keyboard, - Mouse, - Joystick, + Keyboard, + Mouse, + Joystick, - ENUM_CLASS_RANGE(Root,Joystick) - }; - }//namespace io -}//namespace hgl -#endif//HGL_IO_INPUT_DEVICE_SOURCE_INCLUDE \ No newline at end of file + ENUM_CLASS_RANGE(Root,Joystick) + }; +}//namespace hgl::io diff --git a/inc/hgl/io/event/WindowEvent.h b/inc/hgl/io/event/WindowEvent.h index 255b5ff..04c2fec 100644 --- a/inc/hgl/io/event/WindowEvent.h +++ b/inc/hgl/io/event/WindowEvent.h @@ -36,7 +36,7 @@ namespace hgl WindowEvent():InputEvent(InputEventSource::Window){} virtual ~WindowEvent()=default; - EventProcResult OnEvent(const EventHeader &header,const uint64 data) override + virtual EventProcResult OnEvent(const EventHeader &header,const uint64 data) override { wed=(WindowEventData *)&data; @@ -61,4 +61,4 @@ namespace hgl };//class WindowEvent:public InputEvent }//namespace io }//namespace hgl -#endif//HGL_IO_WINDOW_EVENT_INCLUDE \ No newline at end of file +#endif//HGL_IO_WINDOW_EVENT_INCLUDE