Seite 1 von 1

DoItLater - Delayed SetGadgetText, etc

Verfasst: 09.05.2019 19:33
von mk-soft
Bevor ich es wieder verbummle... :wink:

Code: Alles auswählen

;-TOP
; Comment : DoItLater
; Author  : mk-soft
; Version : v0.02
; Create  : 27.03.2019
; OS      : All

Enumeration
  #SetGadgetText
  #SetGadgetState
  #SetGadgetColor
EndEnumeration

Structure udtDoItLater
  Command.i
  Gadget.i
  Text.s
  Param.i
  Param2.i
  Time.i
  Start.i
EndStructure

Global NewList DoItLater.udtDoItLater()
Global MutexDoItLater = CreateMutex()

Procedure SetGadgetTextDelay(Gadget, Text.s, Time)
  LockMutex(MutexDoItLater)
  LastElement(DoItLater())
  AddElement(DoItLater())
  With DoItLater()
    \Command = #SetGadgetText
    \Gadget = Gadget
    \Text = Text
    \Time = Time
    \Start = ElapsedMilliseconds()
  EndWith
  UnlockMutex(MutexDoItLater)
EndProcedure

Procedure SetGadgetStateDelay(Gadget, State, Time)
  LockMutex(MutexDoItLater)
  LastElement(DoItLater())
  AddElement(DoItLater())
  With DoItLater()
    \Command = #SetGadgetState
    \Gadget = Gadget
    \Param = State
    \Time = Time
    \Start = ElapsedMilliseconds()
  EndWith
  UnlockMutex(MutexDoItLater)
EndProcedure

Procedure SetGadgetColorDelay(Gadget, ColorType, Color, Time)
  LockMutex(MutexDoItLater)
  LastElement(DoItLater())
  AddElement(DoItLater())
  With DoItLater()
    \Command = #SetGadgetColor
    \Gadget = Gadget
    \Param = ColorType
    \Param2 = Color
    \Time = Time
    \Start = ElapsedMilliseconds()
  EndWith
  UnlockMutex(MutexDoItLater)
EndProcedure

Procedure DoEventDoItLater()
  Protected Time = ElapsedMilliseconds()
  With DoItLater()
    LockMutex(MutexDoItLater)
    ForEach DoItLater()
      If (Time - \Start) >= \Time
        If IsGadget(\Gadget)
          Select \Command
            Case #SetGadgetText
              SetGadgetText(\Gadget, \Text)
            Case #SetGadgetState
              SetGadgetState(\Gadget, \Param)
            Case #SetGadgetColor
              SetGadgetColor(\Gadget, \Param, \Param2)
          EndSelect
        EndIf
        DeleteElement(DoItLater())
      EndIf
    Next
    UnlockMutex(MutexDoItLater)
  EndWith
EndProcedure

;- Example

Procedure Main()
  Protected event, index, x, y
  
  If OpenWindow(0 , #PB_Ignore, #PB_Ignore, 620, 80, "Do It Later", #PB_Window_SystemMenu)
    x = 10
    y = 10
    For index = 0 To 9
      ButtonGadget(index, x, y, 55, 25, "---")
      x + 60
    Next
    StringGadget(10, 10, 45, 120, 25, "")
    
    BindEvent(#PB_Event_Timer, @DoEventDoItLater(), 0, 999)
    AddWindowTimer(0, 999, 50)
    
    Repeat
      event = WaitWindowEvent()
      Select event
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          index = EventGadget()
          Select index
            Case 0 To 9
              SetGadgetText(index, "-" + index + "-")
              SetGadgetTextDelay(index, "---", 800)
            Case 10
              If EventType() = #PB_EventType_Focus
                SetGadgetColor(10, #PB_Gadget_BackColor, #Red)
                SetGadgetColorDelay(10, #PB_Gadget_BackColor, #PB_Default, 150)
              EndIf
          EndSelect
      EndSelect
    ForEver
  EndIf
EndProcedure : Main()