Added [optional] 'Clicked' state and GetInteractionState function

This commit is contained in:
Oliver 'kfsone' Smith 2021-02-24 12:18:45 -08:00
parent 8e6dcc45e1
commit eff176bb13

View file

@ -2,6 +2,10 @@
package raygui package raygui
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
// GUI controls states // GUI controls states
type ControlState int type ControlState int
@ -13,4 +17,31 @@ const (
Focused Focused
// Pressed indicates the mouse is hovering over the GUI element and LMB is pressed down. // Pressed indicates the mouse is hovering over the GUI element and LMB is pressed down.
Pressed 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
}
}