_windows window and tab dragging done

This commit is contained in:
2025-01-19 21:56:10 +02:00
parent 28957fea3e
commit 0aefdf4411
10 changed files with 438 additions and 160 deletions

View File

@@ -10,6 +10,7 @@
#include "Engine/Platform/BatteryInfo.h"
#include "Engine/Platform/WindowsManager.h"
#include "Engine/Platform/SDL/SDLInput.h"
#include "Engine/Engine/Engine.h"
#include <SDL3/SDL_hints.h>
#include <SDL3/SDL_init.h>
@@ -25,6 +26,7 @@
#include "Engine/Platform/MessageBox.h"
#include <SDL3/SDL_messagebox.h>
#endif
#include <SDL3/SDL_timer.h>
#define DefaultDPI 96
@@ -142,10 +144,15 @@ bool SDLPlatform::CheckWindowDragging(Window* window, WindowHitCodes hit)
return handled;
}
extern Window* draggedWindow;
extern Float2 draggedWindowStartPosition;
extern Float2 draggedWindowMousePosition;
void SDLPlatform::Tick()
{
SDLInput::Update();
#if false
if (DraggedWindowId != 0)
{
Float2 mousePos;
@@ -200,6 +207,129 @@ void SDLPlatform::Tick()
#endif
}
}
#endif
auto watch = [](void* userdata, SDL_Event* event) -> bool
{
Window* draggedWindow = *(Window**)userdata;
if (draggedWindow == nullptr)
return true;
SDLWindow* window = SDLWindow::GetWindowFromEvent(*event);
if (event->type == SDL_EVENT_WINDOW_EXPOSED)
{
/*SDL_Event mouseDownEvent{ 0 };
mouseDownEvent.button.type = SDL_EVENT_MOUSE_BUTTON_DOWN;
mouseDownEvent.button.down = true;
mouseDownEvent.button.timestamp = SDL_GetTicksNS();
mouseDownEvent.button.windowID = SDL_GetWindowID(window->GetSDLWindow());
mouseDownEvent.button.button = SDL_BUTTON_LEFT;
mouseDownEvent.button.clicks = 1;
mouseDownEvent.button.x = 0;//static_cast<float>(static_cast<LONG>(WINDOWS_GET_X_LPARAM(msg->lParam)));
mouseDownEvent.button.y = 0;//static_cast<float>(static_cast<LONG>(WINDOWS_GET_Y_LPARAM(msg->lParam)));
if (window)
window->HandleEvent(*event);*/
Engine::OnUpdate();//Scripting::Update(); // For docking updates
Engine::OnDraw();
return false;
}
else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN)
{
SDLWindow* window = SDLWindow::GetWindowFromEvent(*event);
if (window)
{
bool result = false;
window->OnLeftButtonHit(WindowHitCodes::Caption, result);
//if (result)
// return false;
window->HandleEvent(*event);
}
return false;
}
/*else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP)
{
LOG(Info, "ate SDL_EVENT_MOUSE_BUTTON_UP");
return false;
}*/
else if (event->type == SDL_EVENT_WINDOW_MOVED)
{
Float2 start = draggedWindowStartPosition;
Float2 newPos = Float2(static_cast<float>(event->window.data1), static_cast<float>(event->window.data2));
Float2 offset = newPos - start;
Float2 mousePos = draggedWindowMousePosition/* + offset*/;
SDL_Event mouseMovedEvent { 0 };
mouseMovedEvent.motion.type = SDL_EVENT_MOUSE_MOTION;
mouseMovedEvent.motion.windowID = SDL_GetWindowID(draggedWindow->GetSDLWindow());
mouseMovedEvent.motion.timestamp = SDL_GetTicksNS();
mouseMovedEvent.motion.state = SDL_BUTTON_LEFT;
mouseMovedEvent.motion.x = mousePos.X;
mouseMovedEvent.motion.y = mousePos.Y;
if (window)
window->HandleEvent(mouseMovedEvent);
if (window)
window->HandleEvent(*event);
return false;
}
if (window)
window->HandleEvent(*event);
/*bool* dragging = (bool*)userdata;
if (event->type == SDL_EVENT_WINDOW_EXPOSED)
{
// This event is usually sent when the window is focused and requires redrawing,
// but we are more interested in these events when the window is being dragged,
// blocking the main thread during the drag operation. Assume the window is being
// dragged when we have received the event more than once per cycle.
bool result = true;
if (*dragging)
{
// Send a simulated mouse button down event, we can't tell when
// the real button down event arrives...
SDLWindow* window = SDLWindow::GetWindowFromEvent(*event);
SDL_Event mouseDownEvent{ 0 };
mouseDownEvent.button.type = SDL_EVENT_MOUSE_BUTTON_DOWN;
mouseDownEvent.button.down = true;
mouseDownEvent.button.timestamp = SDL_GetTicksNS();
mouseDownEvent.button.windowID = SDL_GetWindowID(window->GetSDLWindow());
mouseDownEvent.button.button = SDL_BUTTON_LEFT;
mouseDownEvent.button.clicks = 1;
mouseDownEvent.button.x = 0;//static_cast<float>(static_cast<LONG>(WINDOWS_GET_X_LPARAM(msg->lParam)));
mouseDownEvent.button.y = 0;//static_cast<float>(static_cast<LONG>(WINDOWS_GET_Y_LPARAM(msg->lParam)));
if (window)
window->HandleEvent(*event);
Engine::OnUpdate();//Scripting::Update(); // For docking updates
Engine::OnDraw();
return false;
}
else
*dragging = true;
return true;
}*/
/*else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN && *dragging)
{
SDLWindow* window = SDLWindow::GetWindowFromEvent(*event);
if (window)
window->HandleEvent(*event);
return false;
}*/
/*if (dragging)
{
LOG(Info, "events during dragging: {}", event->type); // usually SDL_EVENT_WINDOW_MOVED
}*/
return true;
};
bool suc = SDL_AddEventWatch(watch, &draggedWindow);
if (!suc)
suc = suc;
SDL_PumpEvents();
SDL_Event events[32];
@@ -214,6 +344,24 @@ void SDLPlatform::Tick()
else
SDLPlatform::HandleEvent(events[i]);
}
SDL_RemoveEventWatch(watch, &draggedWindow);
if (draggedWindow != nullptr)
{
// We are no longer dragging since event loop is no longer blocked
SDL_Event event{ 0 };
event.button.type = SDL_EVENT_MOUSE_BUTTON_UP;
event.button.down = false;
event.button.timestamp = SDL_GetTicksNS();
event.button.windowID = SDL_GetWindowID(draggedWindow->GetSDLWindow());
event.button.button = SDL_BUTTON_LEFT;
event.button.clicks = 1;
event.button.x = 0;//static_cast<float>(static_cast<LONG>(WINDOWS_GET_X_LPARAM(msg->lParam)));
event.button.y = 0;//static_cast<float>(static_cast<LONG>(WINDOWS_GET_Y_LPARAM(msg->lParam)));
SDL_PushEvent(&event);
draggedWindow = nullptr;
}
}
bool SDLPlatform::HandleEvent(SDL_Event& event)
@@ -275,8 +423,10 @@ Float2 SDLPlatform::GetMousePosition()
// Use the last known reported position we got from window events.
pos = Input::GetMouseScreenPosition();
}
else
SDL_GetGlobalMouseState(&pos.X, &pos.Y);
//else
// SDL_GetGlobalMouseState(&pos.X, &pos.Y);
#else
pos = Input::GetMouseScreenPosition();
#endif
return pos;
}