Layout codes of InputEvent/WindowEvent

This commit is contained in:
hyzboy 2025-03-06 01:20:37 +08:00
parent 3f5baa99d0
commit b78d31d8a0
3 changed files with 93 additions and 103 deletions

View File

@ -1,104 +1,99 @@
#ifndef HGL_IO_INPUT_EVENT_INCLUDE #pragma once
#define HGL_IO_INPUT_EVENT_INCLUDE
#include<hgl/type/SortedSet.h> #include<hgl/type/SortedSet.h>
#include<hgl/io/event/InputEventSource.h> #include<hgl/io/event/InputEventSource.h>
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<InputEvent *> 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; ///<输入源类型 if(!RangeCheck((InputEventSource)header.type))
uint8 index; ///<输入源索引(比如同一设备有多个) return(EventProcResult::Break);
uint16 id; ///<事件id
};
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;
}
/** if(sub_event_proc[size_t(InputEventSource::Root)].GetCount()>0
* &&InputEventSource(header.type)!=InputEventSource::Root)
*/ {
enum class EventProcResult 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, source_type=InputEventSource::Root;
Break, }
};
class InputEvent InputEvent(InputEventSource ies)
{ {
protected: source_type=ies;
}
InputEventSource source_type; virtual ~InputEvent()=default;
SortedSet<InputEvent *> 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) return(sub_event_proc[size_t(ies)].Add(ie)!=-1);
{ }
if(!RangeCheck((InputEventSource)header.type))
return(EventProcResult::Break);
if(sub_event_proc[header.type].GetCount()>0) bool Unjoin(InputEvent *ie)
{ {
for(InputEvent *ie:sub_event_proc[header.type]) if(!ie)return(false);
if(ie->OnEvent(header,data)==EventProcResult::Break)
return EventProcResult::Break;
}
if(sub_event_proc[size_t(InputEventSource::Root)].GetCount()>0 const InputEventSource ies=ie->GetInputEventSource();
&&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); if(!RangeCheck(ies))
} return(false);
public: return sub_event_proc[size_t(ies)].Delete(ie);
}
InputEvent() };//class InputEvent
{ }//namespace hgl::io
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

View File

@ -1,24 +1,19 @@
#ifndef HGL_IO_INPUT_DEVICE_SOURCE_INCLUDE #pragma once
#define HGL_IO_INPUT_DEVICE_SOURCE_INCLUDE
#include<hgl/TypeFunc.h> #include<hgl/TypeFunc.h>
namespace hgl namespace hgl::io
{ {
namespace io enum class InputEventSource
{ {
enum class InputEventSource Root=0,
{
Root=0,
OS, OS,
Window, Window,
Keyboard, Keyboard,
Mouse, Mouse,
Joystick, Joystick,
ENUM_CLASS_RANGE(Root,Joystick) ENUM_CLASS_RANGE(Root,Joystick)
}; };
}//namespace io }//namespace hgl::io
}//namespace hgl
#endif//HGL_IO_INPUT_DEVICE_SOURCE_INCLUDE

View File

@ -36,7 +36,7 @@ namespace hgl
WindowEvent():InputEvent(InputEventSource::Window){} WindowEvent():InputEvent(InputEventSource::Window){}
virtual ~WindowEvent()=default; 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; wed=(WindowEventData *)&data;
@ -61,4 +61,4 @@ namespace hgl
};//class WindowEvent:public InputEvent };//class WindowEvent:public InputEvent
}//namespace io }//namespace io
}//namespace hgl }//namespace hgl
#endif//HGL_IO_WINDOW_EVENT_INCLUDE #endif//HGL_IO_WINDOW_EVENT_INCLUDE