From eff176bb13d58eda0444b57104229e22625b5af6 Mon Sep 17 00:00:00 2001 From: Oliver 'kfsone' Smith Date: Wed, 24 Feb 2021 12:18:45 -0800 Subject: [PATCH] Added [optional] 'Clicked' state and GetInteractionState function --- raygui/raygui.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/raygui/raygui.go b/raygui/raygui.go index 4e6c2c8..9ea0b17 100644 --- a/raygui/raygui.go +++ b/raygui/raygui.go @@ -2,6 +2,10 @@ package raygui +import ( + rl "github.com/gen2brain/raylib-go/raylib" +) + // GUI controls states type ControlState int @@ -13,4 +17,31 @@ const ( Focused // Pressed indicates the mouse is hovering over the GUI element and LMB is pressed down. Pressed + // Clicked indicates the mouse is hovering over the GUI element and LMB has just been released. + Clicked ) + +// IsColliding will return true if 'point' is within any of the given rectangles. +func IsInAny(point rl.Vector2, rectangles ...rl.Rectangle) bool { + for _, rect := range rectangles { + if rl.CheckCollisionPointRec(point, rect) { + return true + } + } + return false +} + +// GetInteractionState determines the current state of a control based on mouse position and +// button states. +func GetInteractionState(rectangles ...rl.Rectangle) ControlState { + switch { + case !IsInAny(rl.GetMousePosition(), rectangles...): + return Normal + case rl.IsMouseButtonDown(rl.MouseLeftButton): + return Pressed + case rl.IsMouseButtonReleased(rl.MouseLeftButton) || rl.IsMouseButtonPressed(rl.MouseLeftButton): + return Clicked + default: + return Focused + } +}