CMCore/inc/hgl/io/event/InputEvent.h

100 lines
2.5 KiB
C
Raw Normal View History

2025-03-06 01:20:37 +08:00
#pragma once
2022-01-24 18:56:05 +08:00
#include<hgl/type/SortedSet.h>
2022-01-24 18:56:05 +08:00
#include<hgl/io/event/InputEventSource.h>
2025-03-06 01:20:37 +08:00
namespace hgl::io
2022-01-24 18:56:05 +08:00
{
2025-03-06 01:20:37 +08:00
struct EventHeader
2022-01-24 18:56:05 +08:00
{
2025-03-06 01:20:37 +08:00
uint8 type; ///<输入源类型
uint8 index; ///<输入源索引(比如同一设备有多个)
uint16 id; ///<事件id
};
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
constexpr size_t EventHeaderBytes=sizeof(EventHeader);
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
/**
2025-06-08 22:09:22 +08:00
*
*/
2025-03-06 01:20:37 +08:00
enum class EventProcResult
{
Continue,
Break,
};
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
class InputEvent
{
protected:
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
InputEventSource source_type;
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
SortedSet<InputEvent *> sub_event_proc[size_t(InputEventSource::RANGE_SIZE)];
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
public:
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
const InputEventSource GetInputEventSource()const{return source_type;}
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
virtual EventProcResult OnEvent(const EventHeader &header,const uint64 data)
{
if(!RangeCheck((InputEventSource)header.type))
return(EventProcResult::Break);
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
if(sub_event_proc[header.type].GetCount()>0)
2022-01-24 18:56:05 +08:00
{
2025-03-06 01:20:37 +08:00
for(InputEvent *ie:sub_event_proc[header.type])
if(ie->OnEvent(header,data)==EventProcResult::Break)
return EventProcResult::Break;
2022-01-24 18:56:05 +08:00
}
2025-03-06 01:20:37 +08:00
if(sub_event_proc[size_t(InputEventSource::Root)].GetCount()>0
&&InputEventSource(header.type)!=InputEventSource::Root)
2022-01-24 18:56:05 +08:00
{
2025-03-06 01:20:37 +08:00
for(InputEvent *ie:sub_event_proc[size_t(InputEventSource::Root)])
if(ie->OnEvent(header,data)==EventProcResult::Break)
return EventProcResult::Break;
2022-01-24 18:56:05 +08:00
}
2025-03-06 01:20:37 +08:00
return(EventProcResult::Continue);
}
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
public:
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
InputEvent()
{
source_type=InputEventSource::Root;
}
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
InputEvent(InputEventSource ies)
{
source_type=ies;
}
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
virtual ~InputEvent()=default;
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
virtual bool Join(InputEvent *ie)
{
if(!ie)
return(false);
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
const InputEventSource ies=ie->GetInputEventSource();
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
if(!RangeCheck(ies))
return(false);
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
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 hgl::io