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

108 lines
2.4 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-06-10 01:31:18 +08:00
InputEventSource type; ///<输入源类型
2025-03-06 01:20:37 +08:00
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-06-10 01:31:18 +08:00
InputEvent *parent_input_event;
SortedSet<InputEvent *> sub_event_proc;
void SetParent(InputEvent *ie){parent_input_event=ie;}
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)
{
2025-06-10 01:31:18 +08:00
if(!RangeCheck(header.type))
2025-03-06 01:20:37 +08:00
return(EventProcResult::Break);
2022-01-24 18:56:05 +08:00
2025-06-10 01:31:18 +08:00
if(!sub_event_proc.IsEmpty())
2022-01-24 18:56:05 +08:00
{
2025-06-10 01:31:18 +08:00
for(InputEvent *ie:sub_event_proc)
2025-03-06 01:20:37 +08:00
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;
2025-06-10 01:31:18 +08:00
parent_input_event=nullptr;
2025-03-06 01:20:37 +08:00
}
2022-01-24 18:56:05 +08:00
2025-03-06 01:20:37 +08:00
InputEvent(InputEventSource ies)
{
source_type=ies;
2025-06-10 01:31:18 +08:00
parent_input_event=nullptr;
2025-03-06 01:20:37 +08:00
}
2022-01-24 18:56:05 +08:00
2025-06-10 01:31:18 +08:00
virtual ~InputEvent()
{
if(parent_input_event)
parent_input_event->Unjoin(this);
}
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-06-10 01:31:18 +08:00
ie->SetParent(this);
return(sub_event_proc.Add(ie)!=-1);
2025-03-06 01:20:37 +08:00
}
bool Unjoin(InputEvent *ie)
{
if(!ie)return(false);
const InputEventSource ies=ie->GetInputEventSource();
if(!RangeCheck(ies))
return(false);
2025-06-10 01:31:18 +08:00
ie->SetParent(nullptr);
return sub_event_proc.Delete(ie);
2025-03-06 01:20:37 +08:00
}
2025-06-10 01:31:18 +08:00
virtual bool Update(){return true;}
2025-03-06 01:20:37 +08:00
};//class InputEvent
}//namespace hgl::io