Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// DRAFT
// [Internal]
enum ImGuiIoEventType
{
ImGuiIoEventType_Unknown = 0,
ImGuiIoEventType_MousePos,
ImGuiIoEventType_MouseButton,
ImGuiIoEventType_MouseWheel,
};
// [Internal]
struct ImGuiIoEvent
{
ImGuiIoEventType Type;
union
{
struct { ImVec2 Pos; } MousePos; // Parameters, if Type == ImGuiIoEventType_MousePos
struct { int Button; bool IsDown; } MouseButton; // Parameters, if Type == ImGuiIoEventType_MouseButton
struct { ImVec2 Value; } MouseWheel; // Parameters, if Type == ImGuiIoEventType_MouseWheel
};
ImGuiIoEvent() { memset(this, 0, sizeof(*this)); Type = ImGuiIoEventType_Unknown; }
};
// Dummy wip class (will be moved to ImGui's internal and ImGuiIO)
struct ImGuiIoEventQueue
{
// Internal
ImVector<ImGuiIoEvent> Events;
public:
// This would be in ImGuiIO, functions declared publicly.
void AddMousePosEvent(float x, float y)
{
ImGuiIoEvent evt;
evt.Type = ImGuiIoEventType_MousePos;
evt.MousePos.Pos = ImVec2(x, y);
Events.push_back(evt);
}
void AddMouseButtonEvent(int mouse_button, bool is_down)
{
IM_ASSERT(mouse_button >= 0 && mouse_button < IM_ARRAYSIZE(ImGui::GetIO().MouseDown));
ImGuiIoEvent evt;
evt.Type = ImGuiIoEventType_MouseButton;
evt.MouseButton.Button = mouse_button;
evt.MouseButton.IsDown = is_down;
Events.push_back(evt);
}
void AddMouseWheelEvent(float wheel_x, float wheel_y)
{
ImGuiIoEvent evt;
evt.Type = ImGuiIoEventType_MouseWheel;
evt.MouseWheel.Value = ImVec2(wheel_x, wheel_y);
Events.push_back(evt);
}
// [Internal]
void PreNewFrame()
{
ImGuiIO& io = ImGui::GetIO();
int mouse_moved = false;
int mouse_button_changed_mask = 0x00;
int mouse_wheeled = false;
int event_n = 0;
while (event_n < Events.Size)
{
ImGuiIoEvent& evt = Events[event_n];
if (evt.Type == ImGuiIoEventType_MousePos)
{
// Rule: Stop processing queued events if we already handled a mouse button change
if (mouse_button_changed_mask != 0)
break;
io.MousePos = evt.MousePos.Pos;
mouse_moved = true;
//printf("[%05d] Processed MousePos %.f %.f\n", ImGui::GetFrameCount(), io.MousePos.x, io.MousePos.y);
event_n++;
continue;
}
if (evt.Type == ImGuiIoEventType_MouseButton)
{
int button = evt.MouseButton.Button;
IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(io.MouseDown));
if (io.MouseDown[button] != evt.MouseButton.IsDown)
{
// Rule: Stop processing queued events if we got multiple action on the same button
if (mouse_button_changed_mask & (1 << button))
break;
mouse_button_changed_mask |= (1 << button);
io.MouseDown[button] = evt.MouseButton.IsDown;
}
//printf("[%05d] Processed MouseButton %d %d (io.MousePos %.0f %.0f)\n", ImGui::GetFrameCount(), evt.MouseButton.Button, evt.MouseButton.Down, io.MousePos.x, io.MousePos.y);
event_n++;
continue;
}
if (evt.Type == ImGuiIoEventType_MouseWheel)
{
io.MouseWheelH += evt.MouseWheel.Value.x;
io.MouseWheel += evt.MouseWheel.Value.y;
mouse_wheeled = true;
//printf("[%05d] Processed MouseWheel %.f %.f\n", ImGui::GetFrameCount(), evt.MouseWheel.Value.x, evt.MouseWheel.Value.y);
event_n++;
continue;
}
}
// Remaining events will be processed on the next frame
if (event_n == Events.Size)
Events.resize(0);
else
Events.erase(Events.Data, Events.Data + event_n);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.