From 333bec0a122a962d0b7c55948a73ead6052ae9d2 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 10 Jun 2025 01:31:18 +0800 Subject: [PATCH] =?UTF-8?q?InputEvent=E4=B8=8D=E5=86=8D=E5=88=86=E5=A4=9A?= =?UTF-8?q?=E7=B1=BB=E5=BB=BA=E5=A4=9A=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/io/event/InputEvent.h | 40 +++++++++++++++++------------ inc/hgl/io/event/InputEventSource.h | 2 +- inc/hgl/io/event/KeyboardEvent.h | 17 +++++++----- inc/hgl/io/event/MouseEvent.h | 39 +++++++++++++++------------- inc/hgl/io/event/WindowEvent.h | 17 +++++++----- 5 files changed, 66 insertions(+), 49 deletions(-) diff --git a/inc/hgl/io/event/InputEvent.h b/inc/hgl/io/event/InputEvent.h index b1c24a7..1541a2e 100644 --- a/inc/hgl/io/event/InputEvent.h +++ b/inc/hgl/io/event/InputEvent.h @@ -6,7 +6,7 @@ namespace hgl::io { struct EventHeader { - uint8 type; ///<输入源类型 + InputEventSource type; ///<输入源类型 uint8 index; ///<输入源索引(比如同一设备有多个) uint16 id; ///<事件id }; @@ -28,7 +28,11 @@ namespace hgl::io InputEventSource source_type; - SortedSet sub_event_proc[size_t(InputEventSource::RANGE_SIZE)]; + InputEvent *parent_input_event; + + SortedSet sub_event_proc; + + void SetParent(InputEvent *ie){parent_input_event=ie;} public: @@ -36,20 +40,12 @@ namespace hgl::io virtual EventProcResult OnEvent(const EventHeader &header,const uint64 data) { - if(!RangeCheck((InputEventSource)header.type)) + if(!RangeCheck(header.type)) return(EventProcResult::Break); - if(sub_event_proc[header.type].GetCount()>0) + if(!sub_event_proc.IsEmpty()) { - for(InputEvent *ie:sub_event_proc[header.type]) - if(ie->OnEvent(header,data)==EventProcResult::Break) - return EventProcResult::Break; - } - - 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)]) + for(InputEvent *ie:sub_event_proc) if(ie->OnEvent(header,data)==EventProcResult::Break) return EventProcResult::Break; } @@ -62,14 +58,20 @@ namespace hgl::io InputEvent() { source_type=InputEventSource::Root; + parent_input_event=nullptr; } InputEvent(InputEventSource ies) { source_type=ies; + parent_input_event=nullptr; } - virtual ~InputEvent()=default; + virtual ~InputEvent() + { + if(parent_input_event) + parent_input_event->Unjoin(this); + } virtual bool Join(InputEvent *ie) { @@ -81,7 +83,9 @@ namespace hgl::io if(!RangeCheck(ies)) return(false); - return(sub_event_proc[size_t(ies)].Add(ie)!=-1); + ie->SetParent(this); + + return(sub_event_proc.Add(ie)!=-1); } bool Unjoin(InputEvent *ie) @@ -93,7 +97,11 @@ namespace hgl::io if(!RangeCheck(ies)) return(false); - return sub_event_proc[size_t(ies)].Delete(ie); + ie->SetParent(nullptr); + + return sub_event_proc.Delete(ie); } + + virtual bool Update(){return true;} };//class InputEvent }//namespace hgl::io diff --git a/inc/hgl/io/event/InputEventSource.h b/inc/hgl/io/event/InputEventSource.h index ed1f05a..f17068e 100644 --- a/inc/hgl/io/event/InputEventSource.h +++ b/inc/hgl/io/event/InputEventSource.h @@ -3,7 +3,7 @@ #include namespace hgl::io { - enum class InputEventSource + enum class InputEventSource:uint8 { Root=0, diff --git a/inc/hgl/io/event/KeyboardEvent.h b/inc/hgl/io/event/KeyboardEvent.h index 6b0f15a..734bb21 100644 --- a/inc/hgl/io/event/KeyboardEvent.h +++ b/inc/hgl/io/event/KeyboardEvent.h @@ -167,16 +167,19 @@ namespace hgl EventProcResult OnEvent(const EventHeader &header,const uint64 data) override { + if(header.type==InputEventSource::Keyboard) + { + switch(KeyboardEventID(header.id)) + { + case KeyboardEventID::Pressed: if(OnPressed (KeyboardButton(((KeyboardEventData *)&data)->key)))return EventProcResult::Break;break; + case KeyboardEventID::Released: if(OnReleased (KeyboardButton(((KeyboardEventData *)&data)->key)))return EventProcResult::Break;break; + case KeyboardEventID::Char: if(OnChar ( ((KeyboardEventData *)&data)->ch ) )return EventProcResult::Break;break; + } + } + if(InputEvent::OnEvent(header,data)==EventProcResult::Break) return EventProcResult::Break; - switch(KeyboardEventID(header.id)) - { - case KeyboardEventID::Pressed: if(OnPressed (KeyboardButton(((KeyboardEventData *)&data)->key)))return EventProcResult::Break;break; - case KeyboardEventID::Released: if(OnReleased (KeyboardButton(((KeyboardEventData *)&data)->key)))return EventProcResult::Break;break; - case KeyboardEventID::Char: if(OnChar ( ((KeyboardEventData *)&data)->ch ) )return EventProcResult::Break;break; - } - return EventProcResult::Continue; } diff --git a/inc/hgl/io/event/MouseEvent.h b/inc/hgl/io/event/MouseEvent.h index 25cb2de..7512b4a 100644 --- a/inc/hgl/io/event/MouseEvent.h +++ b/inc/hgl/io/event/MouseEvent.h @@ -64,30 +64,33 @@ namespace hgl EventProcResult OnEvent(const EventHeader &header,const uint64 data) override { - if(InputEvent::OnEvent(header,data)==EventProcResult::Break) - return EventProcResult::Break; - - med=(MouseEventData *)&data; - - if(MouseEventID(header.id)==MouseEventID::Wheel) - { - if(OnWheel (med->x,med->y) )return EventProcResult::Break; - } - else + if(header.type==InputEventSource::Mouse) { - x=med->x;y=med->y; + med=(MouseEventData *)&data; - switch(MouseEventID(header.id)) + if(MouseEventID(header.id)==MouseEventID::Wheel) + { + if(OnWheel (med->x,med->y) )return EventProcResult::Break; + } + else { - case MouseEventID::Move: if(OnMove (med->x,med->y) )return EventProcResult::Break;break; - case MouseEventID::Pressed: pressed_statues[med->button]=true; - if(OnPressed (med->x,med->y,MouseButton(med->button)))return EventProcResult::Break;break; - case MouseEventID::Released: pressed_statues[med->button]=false; - if(OnReleased (med->x,med->y,MouseButton(med->button)))return EventProcResult::Break;break; - case MouseEventID::DblClicked: if(OnDblClicked (med->x,med->y,MouseButton(med->button)))return EventProcResult::Break;break; + x=med->x;y=med->y; + + switch(MouseEventID(header.id)) + { + case MouseEventID::Move: if(OnMove (med->x,med->y) )return EventProcResult::Break;break; + case MouseEventID::Pressed: pressed_statues[med->button]=true; + if(OnPressed (med->x,med->y,MouseButton(med->button)))return EventProcResult::Break;break; + case MouseEventID::Released: pressed_statues[med->button]=false; + if(OnReleased (med->x,med->y,MouseButton(med->button)))return EventProcResult::Break;break; + case MouseEventID::DblClicked: if(OnDblClicked (med->x,med->y,MouseButton(med->button)))return EventProcResult::Break;break; + } } } + if(InputEvent::OnEvent(header,data)==EventProcResult::Break) + return EventProcResult::Break; + return EventProcResult::Continue; } diff --git a/inc/hgl/io/event/WindowEvent.h b/inc/hgl/io/event/WindowEvent.h index 04c2fec..9b0b36b 100644 --- a/inc/hgl/io/event/WindowEvent.h +++ b/inc/hgl/io/event/WindowEvent.h @@ -33,18 +33,21 @@ namespace hgl public: - WindowEvent():InputEvent(InputEventSource::Window){} + WindowEvent():InputEvent(InputEventSource::Window){wed=nullptr;} virtual ~WindowEvent()=default; virtual EventProcResult OnEvent(const EventHeader &header,const uint64 data) override { - wed=(WindowEventData *)&data; - - switch(WindowEventID(header.id)) + if(header.type==InputEventSource::Window) { - case WindowEventID::Active:OnActive (wed->active) ;break; - case WindowEventID::Resize:OnResize (wed->width,wed->height);break; - case WindowEventID::Close: OnClose () ;break; + wed=(WindowEventData *)&data; + + switch(WindowEventID(header.id)) + { + case WindowEventID::Active:OnActive (wed->active) ;break; + case WindowEventID::Resize:OnResize (wed->width,wed->height);break; + case WindowEventID::Close: OnClose () ;break; + } } if(InputEvent::OnEvent(header,data)==EventProcResult::Break)