mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-02-16 07:10:34 -05:00
Improved window dragging.
Fixed issues when dragging the window, like the window stopping its movement when moving the cursor too fast, or flickering all over the place. Also, the window position will be clamped to the area of the monitor/s to prevent it from going outside of them. Also, setting VSync instead of target FPS prevents possible stuttering and probably increased CPU usage.
This commit is contained in:
parent
8a565f910a
commit
2da6a2a78f
@ -18,6 +18,7 @@ Shader shader = { 0 };
|
||||
Light light = { 0 };
|
||||
Vector3 lightPos = { -5.0f, 10.0f, 10.0f };
|
||||
Vector2 dragOffset;
|
||||
bool isDragging = false;
|
||||
std::string sohFolder = NULLSTR;
|
||||
bool extracting = false;
|
||||
bool rom_ready = false;
|
||||
@ -95,20 +96,52 @@ void OTRGame::update(){
|
||||
}
|
||||
|
||||
void OTRGame::draw() {
|
||||
Vector2 windowSize(GetScreenWidth(), GetScreenHeight());
|
||||
Rectangle titlebar = Rectangle(0, 0, windowSize.x - 50, 35);
|
||||
Vector2 mousePos = GetMousePosition();
|
||||
Vector2 mouseDelta = GetMouseDelta();
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !isDragging &&
|
||||
mousePos.x >= titlebar.x && mousePos.y >= titlebar.y && mousePos.x <= titlebar.x + titlebar.width && mousePos.y <= titlebar.y + titlebar.height) {
|
||||
isDragging = true;
|
||||
dragOffset = mousePos;
|
||||
}
|
||||
else if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && isDragging) {
|
||||
isDragging = false;
|
||||
dragOffset = Vector2(0, 0);
|
||||
}
|
||||
|
||||
if (isDragging && (mouseDelta.x != 0.0f || mouseDelta.y != 0.0f)) {
|
||||
Vector2 wndPos = GetWindowPosition();
|
||||
wndPos = Vector2(wndPos.x + (mousePos.x - dragOffset.x), wndPos.y + (mousePos.y - dragOffset.y));
|
||||
|
||||
// Calculate virtual screen total size in case there are multiple monitors
|
||||
|
||||
int vsX1 = 0, vsY1 = 0, vsX2 = 0, vsY2 = 0;
|
||||
int monitorCount = GetMonitorCount();
|
||||
|
||||
for (int m = 0; m < monitorCount; m++) {
|
||||
Vector2 monitorPos = GetMonitorPosition(m);
|
||||
Vector2 monitorSize = Vector2(GetMonitorWidth(m), GetMonitorHeight(m));
|
||||
|
||||
if (monitorPos.x < vsX1) vsX1 = monitorPos.x;
|
||||
if (monitorPos.y < vsY1) vsY1 = monitorPos.y;
|
||||
if (monitorPos.x + monitorSize.x > vsX2) vsX2 = monitorPos.x + monitorSize.x;
|
||||
if (monitorPos.y + monitorSize.y > vsY2) vsY2 = monitorPos.y + monitorSize.y;
|
||||
}
|
||||
|
||||
// Clamp the window to the borders of the monitors
|
||||
|
||||
if (wndPos.x < vsX1) wndPos.x = vsX1;
|
||||
if (wndPos.y < vsY1) wndPos.y = vsY1;
|
||||
if (wndPos.x + windowSize.x > vsX2) wndPos.x = vsX2 - windowSize.x;
|
||||
if (wndPos.y + windowSize.y > vsY2) wndPos.y = vsY2 - windowSize.y;
|
||||
|
||||
SetWindowPosition(wndPos.x, wndPos.y);
|
||||
}
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(Color(40, 40, 40, 255));
|
||||
Vector3 windowSize(GetScreenWidth(), GetScreenHeight());
|
||||
Rectangle titlebar = Rectangle(0, 0, windowSize.x - 50, 35);
|
||||
Vector2 mousePos = Vector2(GetMouseX(), GetMouseY());
|
||||
bool hoveredTitlebar = mousePos.x >= titlebar.x && mousePos.y >= titlebar.y && mousePos.x <= titlebar.x + titlebar.width && mousePos.y <= titlebar.y + titlebar.height;
|
||||
|
||||
if (hoveredTitlebar && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||
if (dragOffset.x == 0 && dragOffset.y == 0) dragOffset = mousePos;
|
||||
Vector2 wndPos = GetWindowPosition();
|
||||
|
||||
SetWindowPosition(wndPos.x + (mousePos.x - dragOffset.x), wndPos.y + (mousePos.y - dragOffset.y));
|
||||
}
|
||||
else dragOffset = Vector2(0, 0);
|
||||
|
||||
DrawTexture(Textures["Frame"], 0, 0, WHITE);
|
||||
|
||||
|
@ -18,7 +18,7 @@ void UpdateDrawFrame(void) {
|
||||
|
||||
int main() {
|
||||
constexpr Vector2 windowSize = Vector2(400, 200);
|
||||
SetTargetFPS(144);
|
||||
SetConfigFlags(FLAG_VSYNC_HINT);
|
||||
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
|
||||
SetConfigFlags(FLAG_WINDOW_UNDECORATED);
|
||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||
|
Loading…
Reference in New Issue
Block a user