[BMX] Tooltip Simulator For Your Games/Gui.

Started by Filax, April 13, 2025, 10:45:16

Previous topic - Next topic

Filax

Code: BASIC
' ------------------------------------------------
' Name : Simulator tooltip
' Date : (C)2025
' Site:https://github.com/BlackCreepyCat
' -----------------------------------------------
Graphics 800, 600 ' Example resolution

' -----------------------
' Global tooltip storage
' -----------------------
Global ToolTips:TList = New TList ' List of all tooltips

' -------------
' Tooltip class
' -------------
Type ToolTip
    Field ZoneX:Float, ZoneY:Float      ' Position of the hover zone
    Field ZoneWidth:Float, ZoneHeight:Float ' Size of the zone
    Field Caption:String                ' Tooltip text
    Field ShowStart:Int                 ' Start time for appearance timer (ms)
    Field HideStart:Int                 ' Start time for disappearance timer (ms)
    Field IsActive:Int                  ' 1 if displayed, 0 otherwise
    Field IsHovering:Int                ' 1 if hovering, 0 otherwise
    Field IsLocked:Int                  ' 1 if locked after disappearance, 0 otherwise
    Field LastMouseX:Float              ' Last mouse X position
    Field LastMouseY:Float              ' Last mouse Y position
    Field Px:Float, Py:Float            ' Tooltip position
    Field Tx:Float, Ty:Float            ' Tooltip size
    
    ' Constants
    Const SHOW_DELAY:Int = 1000  ' Delay before appearance (ms)
    Const HIDE_DELAY:Int = 3000  ' Display duration (ms)
End Type

' ----------------------------------
' Creates a tooltip for a screen zone
' ----------------------------------
Function CreateToolTip:ToolTip(x:Float, y:Float, w:Float, h:Float, caption:String)
    If w <= 0 Or h <= 0 Or caption = "" Then Return Null
    
    Local tooltip:ToolTip = New ToolTip
    tooltip.ZoneX = x
    tooltip.ZoneY = y
    tooltip.ZoneWidth = w
    tooltip.ZoneHeight = h
    tooltip.Caption = caption
    tooltip.IsActive = False
    tooltip.IsHovering = False
    tooltip.IsLocked = False
    tooltip.LastMouseX = 0
    tooltip.LastMouseY = 0
    tooltip.ShowStart = 0
    tooltip.HideStart = 0
    
    ToolTips.AddLast(tooltip)
    Return tooltip
End Function

' ----------------------------------
' Refreshes all tooltips
' ----------------------------------
Function RefreshToolTip()
    For Local tooltip:ToolTip = EachIn ToolTips
        If tooltip = Null Or tooltip.ZoneWidth <= 0 Or tooltip.ZoneHeight <= 0 Then
            DeleteToolTip(tooltip)
            Continue
        EndIf
        
        ' Check if mouse is in the zone
        Local mx:Float = MouseX()
        Local my:Float = MouseY()
        Local isMouseOver:Int = (mx >= tooltip.ZoneX And mx < tooltip.ZoneX + tooltip.ZoneWidth And ..
                                 my >= tooltip.ZoneY And my < tooltip.ZoneY + tooltip.ZoneHeight)
        
        If isMouseOver Then
            ' Check if mouse moved
            Local mouseMoved:Int = (Abs(mx - tooltip.LastMouseX) > 1 Or Abs(my - tooltip.LastMouseY) > 1)
            tooltip.LastMouseX = mx
            tooltip.LastMouseY = my
            
            If Not tooltip.IsHovering And Not tooltip.IsLocked Then
                ' Start hovering
                tooltip.IsHovering = True
                tooltip.ShowStart = MilliSecs()
            ElseIf tooltip.IsLocked And mouseMoved Then
                ' Unlock if mouse moves
                tooltip.IsLocked = False
                tooltip.IsHovering = True
                tooltip.ShowStart = MilliSecs()
            EndIf
        ElseIf Not isMouseOver And tooltip.IsHovering Then
            ' End hovering
            tooltip.IsHovering = False
            tooltip.IsActive = False
            tooltip.IsLocked = False
            tooltip.ShowStart = 0
            tooltip.HideStart = 0
        EndIf
        
        ' Handle appearance
        If tooltip.IsHovering And Not tooltip.IsActive And tooltip.ShowStart > 0 Then
            If MilliSecs() - tooltip.ShowStart >= ToolTip.SHOW_DELAY Then
                tooltip.IsActive = True
                tooltip.ShowStart = 0
                tooltip.HideStart = MilliSecs()
                ' Calculate position and size
                tooltip.Tx = TextWidth(tooltip.Caption) + 10
                tooltip.Ty = TextHeight(tooltip.Caption) + 6
                ' Position bottom-right of mouse cursor
                tooltip.Px = MouseX() + 20
                tooltip.Py = MouseY() + 20
                ' Adjust if out of screen
                If tooltip.Px + tooltip.Tx > GraphicsWidth() Then
                    tooltip.Px = GraphicsWidth() - tooltip.Tx
                EndIf
                If tooltip.Py + tooltip.Ty > GraphicsHeight() Then
                    tooltip.Py = MouseY() - tooltip.Ty - 5
                EndIf
            EndIf
        EndIf
        
        ' Handle disappearance
        If tooltip.IsActive And tooltip.HideStart > 0 Then
            If MilliSecs() - tooltip.HideStart >= ToolTip.HIDE_DELAY Then
                tooltip.IsActive = False
                tooltip.IsHovering = False
                tooltip.IsLocked = True
                tooltip.HideStart = 0
            EndIf
        EndIf
    Next
End Function

' ----------------------------------
' Draws all active tooltips
' ----------------------------------
Function DrawToolTip()
    For Local tooltip:ToolTip = EachIn ToolTips
        If tooltip = Null Or Not tooltip.IsActive Then Continue
        
        ' Check zone validity
        If tooltip.ZoneWidth <= 0 Or tooltip.ZoneHeight <= 0 Then
            DeleteToolTip(tooltip)
            Continue
        EndIf
        
        ' Draw background (gray)
        SetColor 100, 100, 100
        DrawRect tooltip.Px, tooltip.Py, tooltip.Tx, tooltip.Ty
        
        ' Draw border (black)
        SetColor 0, 0, 0
        DrawLine tooltip.Px, tooltip.Py, tooltip.Px + tooltip.Tx - 1, tooltip.Py
        DrawLine tooltip.Px, tooltip.Py, tooltip.Px, tooltip.Py + tooltip.Ty - 1
        DrawLine tooltip.Px + tooltip.Tx - 1, tooltip.Py, tooltip.Px + tooltip.Tx - 1, tooltip.Py + tooltip.Ty - 1
        DrawLine tooltip.Px, tooltip.Py + tooltip.Ty - 1, tooltip.Px + tooltip.Tx - 1, tooltip.Py + tooltip.Ty - 1
        
        ' Draw text (white, left-aligned)
        SetColor 255, 255, 255
        DrawText tooltip.Caption, tooltip.Px + 5, tooltip.Py + 3
    Next
End Function

' ----------------------------------
' Deletes a tooltip
' ----------------------------------
Function DeleteToolTip(tooltip:ToolTip)
    If tooltip = Null Then Return
    
    tooltip.ZoneWidth = 0
    tooltip.ZoneHeight = 0
    tooltip.Caption = ""
    ToolTips.Remove(tooltip)
    tooltip = Null
End Function

' ----------------------------------
' Example program
' ----------------------------------
' Create three rectangular zones with tooltips
CreateToolTip(100, 100, 120, 40, "Zone 1: Button")
CreateToolTip(300, 200, 150, 60, "Zone 2: Panel")
CreateToolTip(500, 300, 100, 30, "Zone 3: Label")

While Not KeyHit(KEY_ESCAPE)
    Cls
    
    ' Draw zones for visualization
    SetColor 0, 150, 0
    DrawRect 100, 100, 120, 40
    SetColor 0, 0, 150
    DrawRect 300, 200, 150, 60
    SetColor 150, 0, 0
    DrawRect 500, 300, 100, 30
    
    ' Update and draw tooltips
    RefreshToolTip()
    DrawToolTip()
    
    Flip
Wend