from ca.modules.sequencer import *
from ca.modules import sequencer
import popen2
import cspace
from cspace import csVector3
import traceback

class IsTrue(ActionNode):
    """
    Checks if input value is True.
    """
    start = Property(bang)
    signals = [Signal("true",int),Signal("false",int)]

    def on_start(self,val):
        #print self.__class__.__name__,val
        if val:
            self.SendSignal("true",self.number)
        else:
            self.SendSignal("false",self.number)


class Compare(ActionNode):
    """
    Checks if the input value is bigger than the specified value.
    """
    number = Property(int)
    start = Property(bang)
    signals = [Signal("smaller",int),Signal("bigger",int)]

    def on_start(self,val):
        #print self.__class__.__name__,val
        if val < self.number:
            self.SendSignal("smaller",self.number)
        else:
            self.SendSignal("bigger",self.number)


class Number(ActionNode):
    """
    Output an specified number
    """
    number = Property(int)
    start = Property(bang)
    signals = [Signal("number",int)]

    def on_start(self,val):
        #print self.__class__.__name__,val
        self.SendSignal("number",self.number)


class Eval(ActionNode):
    """
    Do an arbitrary python operation evaluated with eval (on the parameter "var").
    """
    start = Property(bang)
    command = Property(str)
    signals = [Signal("object",anytype)]

    def on_start(self,val):
        #print self.__class__.__name__,val
        obj = eval(self.command)
        self.SendSignal("object",obj)


class Mesh(ActionNode):
    """
    Outputs an specified mesh.
    """
    start = Property(str)
    signals = [Signal("mesh",int)]
    position = Getter(csVector3)

    def on_start(self,val):
        #print self.__class__.__name__,val
        oreg = cspace.object_reg
        engine = oreg.Get(cspace.iEngine)
        mesh = engine.Meshes[val]
        self.mesh = mesh
        if mesh:
            self.SendSignal("mesh",mesh)

    def get_position(self,val):
        return self.mesh.Movable.Position


class Position(ActionNode):
    """
    Outputs position for objects having a movable.
    """
    pos = Property(cspace.csVector3)
    start = Property(bang)

    def on_start(self,val):
        self.mesh = val
        self._mov = val.Movable
        val.Movable.Position = csVector3(self.pos)
        val.Movable.UpdateMove()
    def on_pos(self,val):
        try:
            self._mov.Position = csVector3(val)
            self._mov.Movable.UpdateMove()
        except:
            pass

class MovePosition(Position):
     """
     Move the mesh a relative amount
     """
     def on_pos(self,val):
        try:
            self._mov.Position = csVector3(val) + self._mov.Position
            self._mov.Movable.UpdateMove()
        except:
            pass

class String(ActionNode):
    """
    Outputs an specified string.
    """
    message = Property(str)
    start = Property(bang)
    signals = [Signal("string",int)]
    def __init__(self):
        self.message = ""
        ActionNode.__init__(self)

    def on_start(self,val):
        #print self.__class__.__name__,val
        self.SendSignal("string",self.message)


class Printer(ActionNode):
    """
    Prints the input value.
    """
    signals = [Signal("finished",int)]
    start = Property(int)
    def __init__(self):
        ActionNode.__init__(self)

    def on_start(self,val):
        print " * Printer: ",val
        self.SendSignal("finished",val)

class Adder(ActionNode):
    """
    Adds the input value and the specified one.
    """
    number = Property(int)
    signals = [Signal("number",int)]
    start = Property(int)
    def __init__(self):
        ActionNode.__init__(self)
        self.number = 0

    def on_start(self,val):
        self.SendSignal("number",val+1)
        print " * Printer: ",val

import math
import random
class SinusGenerator(TimedAction):
    """
    Generates a sinusoidal function.
    """
    signals = [Signal("number",float)]
    max = Property(float)
    def __init__(self):
        self.message = ""
        self.max = 1.0
        TimedAction.__init__(self)

    def process(self,elapsed):
        self.SendSignal("number",math.sin(elapsed*self.max))
        return TimedAction.process(self,elapsed)

class RandomGenerator(TimedAction):
    """
    Generates random values from 0 to 1.
    """
    signals = [Signal("number",float)]
    start = Property(bang)
    def __init__(self):
        TimedAction.__init__(self)

    def process(self,elapsed):
        self.SendSignal("number",random.random())
        return TimedAction.process(self,elapsed)

class Wait(TimedAction):
    """
    Wait the specified time.
    """
    message = Property(str)
    action_time = Property(float)
    signals = [Signal("bang",bang)]
    def __init__(self):
        self.message = ""
        TimedAction.__init__(self,self.execute)
    
    def execute(self):
        self.SendSignal("bang",1)
        return self.Finished()

class Sound(TimedAction):
    """
    Play a sound with mplayer :-).
    """
    message = Property(str)
    action_time = Property(float)
    def __init__(self):
        self.message = ""
        TimedAction.__init__(self,self.execute)

    def execute(self):
        popen2.Popen3("mplayer /usr/share/sounds/KDE_Click.wav")
        print self.message

