;
; Functions for stopwatch
;

; The stopwatch gui should be called "StopwatchGui",
; and have a text parameter labeled "StopwatchText".
; Window should be initialized in the main script;
; this library assumes the window already exists.

; Global Vars
StopwatchTime = 0
StopwatchHidden = 0
StopwatchOn = 0
SetTimer, StopwatchUpdate, 1000
SetTimer, StopwatchUpdate, Off

StopwatchStop() {
    Global StopwatchOn
    SetTimer, StopwatchUpdate, Off
    StopwatchOn = 0
}

StopwatchStart() {
    Global StopwatchOn
    SetTimer, StopwatchUpdate, On
    StopwatchOn = 1
}

StopwatchToggle() {
    Global StopwatchOn
    If StopwatchOn {
        StopwatchStop()
    } Else {
        StopwatchStart()
    }
}

StopwatchReset() {
    Global StopwatchTime
    StopwatchTime = 0
    StopwatchShow()
}

StopwatchShow() {
    Global StopwatchTime, StopwatchHidden
    Minutes := Format("{:02}", StopwatchTime // 60)
    Seconds := Format("{:02}", Mod(StopwatchTime, 60))
    GuiControl, Text, StopwatchText, %Minutes%:%Seconds%
    Gui, Show, NoActivate, StopwatchGui
    StopwatchHidden = 0
}

StopwatchHide() {
    Global StopwatchHidden
    StopwatchStop()
    Gui, Hide
    StopwatchHidden = 1
}

StopwatchToggleHidden() {
    Global StopwatchHidden
    If StopwatchHidden {
        StopwatchShow()
    } Else {
        StopwatchHide()
    }
}

StopwatchUpdate() {
    Global StopwatchTime
    StopwatchTime++
    StopwatchShow()
}

StopwatchFastforward(Seconds:=1) {
    Global StopwatchTime
    StopwatchTime += Seconds
    StopwatchShow()
}

StopwatchRewind(Seconds:=1) {
    Global StopwatchTime
    StopwatchTime -= Seconds
    StopwatchShow()
}