Reference
  Util\Color.txt
End Reference

Import StdWindow
  Wnd = Window
End Import

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Visual Effects
'
'Terop
'-DoTerop
'
'Label
'-(PutLabelProc)
'-PutLabel
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Terop(Scrolling Message)
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure DoTerop()
  Dim msgs = GetArguments()

  Dim lineHeight = CInt([F_MSG].Size * 1.5F)

  Dim ss = GetScreenSize()

  Dim spacingLines = ss.Y / lineHeight
  If ss.Y % lineHeight > 0 Then spacingLines += 1

  Dim lines = CreateArrayList(spacingLines * 2 + msgs.Count)
  For i = 1 To msgs.Count
    lines[spacingLines + i - 1] = msgs[i - 1]
  Next i

  Dim panel = Wnd.CreatePanel()
  Dim startIndex = 0
  Dim shiftY = 0

  Dim t0 = GetClock()
  Do
    Dim dt = GetClock() - t0
    Dim si = CInt(dt / 15000000L) + 1
    If startIndex < si Then
      If si <= spacingLines + msgs.Count Then
        startIndex = si
        panel.Dispose()
        panel = Wnd.CreatePanel()
        For i = startIndex To startIndex + spacingLines
          If lines[i - 1] <> Nothing AndAlso lines[i - 1] <> "" Then
            panel.Add(Wnd.CreateLabel(0, (i - startIndex) * lineHeight, _
              [F_MSG], Color.LightGray, lines[i - 1]))
          End If
        Next i
      Else
        panel.Dispose()
        Return
      End If
    End If
    panel.Y = -CInt((dt - (si - 1) * 15000000L) * lineHeight / 15000000L)
    Sleep(0)
  Loop

End Procedure

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Label(with fading in/out)
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure PutLabelProc(pnl, t0, t1, t2)
  Dim ct0 = t0 * 10000L
  If ct0 = 0 Then ct0 = 1L
  Dim s = GetClock()
  Dim t = s + ct0
  Dim x = s
  Do
    pnl.Color = SetAlpha(pnl.Color, 255L - (t - x) * 255L / ct0)
    Sleep(0)
    x = GetClock()
  Loop While x < t
  pnl.Color = SetAlpha(pnl.Color, 255)
  Sleep(t1)
  Dim ct2 = t2 * 10000L
  If ct2 = 0 Then ct2 = 1L
  s = GetClock()
  t = s + ct2
  x = s
  Do
    pnl.Color = SetAlpha(pnl.Color, (t - x) * 255L / ct2)
    Sleep(0)
    x = GetClock()
  Loop While x < t
  pnl.Dispose()
End Procedure

Procedure PutLabel(x, y, f, c, t, t0, t1, t2)
  Dim pnl = Wnd.CreatePanel()
  pnl.Color = 0x00FFFFFF
  pnl.Add(Wnd.CreateLabel(x, y, f, c, t))
  RunThread(PutLabelProc, pnl, t0, t1, t2)
End Procedure

Procedure BlinkScreen()
  Dim fx = [ColorFillShader]
  SetShaderParam(fx, "red", 1.0)
  SetShaderParam(fx, "green", 1.0)
  SetShaderParam(fx, "blue", 1.0)
  AddAreaEffect(fx)
  Sleep(100)
  RemoveAreaEffect(fx)
  Sleep(100)
  AddAreaEffect(fx)
  Sleep(100)
  RemoveAreaEffect(fx)
  Sleep(0)
End Procedure

Procedure AlphaFadeInProc(a, r, g, b, t)
  Dim fx = [ColorBlendShader]
  Dim af = a / 255.0f
  SetShaderParam(fx, "red", r / 255.0f)
  SetShaderParam(fx, "green", g / 255.0f)
  SetShaderParam(fx, "blue", b / 255.0f)
  AddAreaEffect(fx)
  Dim t0 = GetClock()
  Dim tt = t * 10000.0
  Do
    Dim dt = GetClock() - t0
    If dt < tt Then
      SetShaderParam(fx, "power", af * (dt / tt))
    Else
      SetShaderParam(fx, "power", af)
      Sleep(0)
      Return
    End If
    Sleep(0)
  Loop
End Procedure

Procedure AlphaFadeIn(a, r, g, b, t)
  RunThread(AlphaFadeInProc, a, r, g, b, t)
End Procedure

Procedure FadeInProc(r, g, b, t)
  Dim fx = [ColorBlendShader]
  SetShaderParam(fx, "red", r / 255.0f)
  SetShaderParam(fx, "green", g / 255.0f)
  SetShaderParam(fx, "blue", b / 255.0f)
  AddAreaEffect(fx)
  Dim t0 = GetClock()
  Dim tt = t * 10000.0
  Do
    Dim dt = GetClock() - t0
    If dt < tt Then
      SetShaderParam(fx, "power", dt / tt)
    Else
      SetShaderParam(fx, "power", 1.0f)
      Sleep(0)
      Return
    End If
    Sleep(0)
  Loop
End Procedure

Procedure FadeIn(r, g, b, t)
  RunThread(FadeInProc, r, g, b, t)
End Procedure

Procedure AlphaFadeOutProc(a, r, g, b, t)
  Dim fx = [ColorBlendShader]
  Dim af = a / 255.0f
  SetShaderParam(fx, "red", r / 255.0f)
  SetShaderParam(fx, "green", g / 255.0f)
  SetShaderParam(fx, "blue", b / 255.0f)
  Dim t0 = GetClock()
  Dim tt = t * 10000.0
  Do
    Dim dt = GetClock() - t0
    If dt < tt Then
      SetShaderParam(fx, "power", af * (1.0 - dt / tt))
    Else
      SetShaderParam(fx, "power", 0.0f)
      RemoveAreaEffect(fx)
      Sleep(0)
      Return
    End If
    Sleep(0)
  Loop  
End Procedure

Procedure AlphaFadeOut(a, r, g, b, t)
  RunThread(AlphaFadeOutProc, a, r, g, b, t)
End Procedure

Procedure FadeOutProc(r, g, b, t)
  Dim fx = [ColorBlendShader]
  SetShaderParam(fx, "red", r / 255.0f)
  SetShaderParam(fx, "green", g / 255.0f)
  SetShaderParam(fx, "blue", b / 255.0f)
  Dim t0 = GetClock()
  Dim tt = t * 10000.0
  Do
    Dim dt = GetClock() - t0
    If dt < tt Then
      SetShaderParam(fx, "power", 1.0f - dt / tt)
    Else
      SetShaderParam(fx, "power", 0.0f)
      RemoveAreaEffect(fx)
      Sleep(0)
      Return
    End If
    Sleep(0)
  Loop
End Procedure

Procedure FadeOut(r, g, b, t)
  RunThread(FadeOutProc, r, g, b, t)
End Procedure
