

__all__ = ('Delay', )

import random
import time
from kivy.clock import Clock
from kivy.properties import (BooleanProperty, NumericProperty, StringProperty,
    OptionProperty, BoundedNumericProperty, ReferenceListProperty,
    ObjectProperty)
from moa.stage.base import MoaStage


class Delay(MoaStage):

    def on_paused(self, instance, value, **kwargs):
        super(Delay, self).on_paused(instance, value, **kwargs)

        if self.disabled or not self.started or self.finished:
            return

        if value:
            self.delay = max(0, self.delay - (time.clock() - self.start_time))
            Clock.unschedule(self.increment_loop)
        else:
            Clock.schedule_once(self.increment_loop, self.delay)

    def on_stop(self, **kwargs):
        if super(Delay, self).on_stop(**kwargs):
            return True
        Clock.unschedule(self.increment_loop)
        return False

    def increment_loop(self, *largs, **kwargs):
        if not super(Delay, self).increment_loop(**kwargs):
            return False

        if self.delay_type == 'random':
            self.delay = random.uniform(self.min, self.max)
        Clock.schedule_once(self.increment_loop, self.delay)
        return True

    min = BoundedNumericProperty(0., min=0.)

    max = BoundedNumericProperty(1., min=0.)

    range = ReferenceListProperty(min, max)

    delay = BoundedNumericProperty(0.5, min=0.)

    delay_type = OptionProperty('constant', options=['constant', 'random'])

@@ -106,62 +106,8 @@
 rt (
-BooleanProperty, NumericProperty, StringProperty,%0A    
 Opti
@@ -141,16 +141,20 @@
 roperty,
+%0A   
  Referen
@@ -171,28 +171,8 @@
 erty
-,%0A    ObjectProperty
 )%0Afr
@@ -187,13 +187,8 @@
 tage
-.base
  imp
@@ -238,39 +238,26 @@
 def 
-on_
 pause
-d
 (self, 
-instance, value
+*largs
 , **
@@ -273,16 +273,19 @@
         
+if 
 super(De
@@ -299,354 +299,407 @@
 lf).
-on_
 pause
-d(instance, value, **kwargs)%0A%0A        if self.disabled or not self.started or self.finished:%0A            return%0A%0A        if value:%0A            self.delay = max(0, self.delay - (time.clock() - self.start_time))%0A            Clock.unschedule(self.increment_loop)%0A        else:%0A            Clock.schedule_once(self.increment_loop, self.delay)
+(*largs, **kwargs):%0A            self.delay = max(0, self.delay - (time.clock() - self.start_time))%0A            Clock.unschedule(self.step_stage)%0A            return True%0A        return False%0A%0A    def unpause(self, *largs, **kwargs):%0A        if super(Delay, self).unpause(*largs, **kwargs):%0A            Clock.schedule_once(self.step_stage, self.delay)%0A            return True%0A        return False
 %0A%0A  
@@ -704,19 +704,16 @@
     def 
-on_
 stop(sel
@@ -714,16 +714,24 @@
 op(self,
+ *largs,
  **kwarg
@@ -768,16 +768,21 @@
 lf).
-on_
 stop(
+*largs, 
 **kw
@@ -792,36 +792,16 @@
 s):%0A    
-        return True%0A
         
@@ -826,23 +826,43 @@
 elf.
-increment_loop)
+step_stage)%0A            return True
 %0A   
@@ -888,30 +888,26 @@
     def 
-increment_loop
+step_stage
 (self, *
@@ -962,23 +962,27 @@
 lf).
-increment_loop(
+step_stage(*largs, 
 **kw
@@ -1151,22 +1151,18 @@
 elf.
-increment_loop
+step_stage
 , se

Update delay stage to reflect base stage changes.