
unit delayer
{
	link memory linka {} = #.linker.linka;
	public link unit io {}
	
	//---------------------------------------------------
	//[i] var_uint8 OS_EventFlag;
	public uint8 OS_EventFlag;
	
	//[i] event ValueChangedEvent;
	//[i] event CompleteAllEvent;
	//[i] event CompleteOneEvent;
	
	//---------------------
	//[i] var_uint8 OS_time;
	public uint8 OS_time;
	
	int32 tick;
	//---------------------
	//[i] var_bool isOK;
	bool isOK;
	
	int32 Start;
	int32 End;
	->linka int32 CValue;
	int32 C;
	bool isChanged;
	int32 FullTime;
	int32 Times;
	bool isForever;
	bool isPause;
	bool isSwap;
	
	//------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		OS_time = 10;
		
		FullTime = 0;
		tick = 0;
		isOK = true;
		isChanged = false;
		isForever = false;
		isSwap = false;
		isPause = false;
		OS_EventFlag = 0;
	}
	//------------------------------------------
	//[i] function_void OS_run;
	public void OS_run()
	{
		if( !isPause && !isOK ) {
			int32 LC = C;
			tick + 1;
			C = Start + (End - Start) * tick / FullTime;
			
			if( tick == FullTime ) {
				
				if( isSwap ) {
					int32 ttt = Start;
					Start = End;
					End = ttt;
				}
				
				if( !isForever ) {
					Times - 1;
					if( Times == 0 ) {
						isOK = true;
						OS_EventFlag.1(bit) = 1;
						OS_EventFlag.2(bit) = 1;
					}
					else {
						C = Start;
						tick = 0;
						OS_EventFlag.2(bit) = 1;
					}
				}
				else {
					C = Start;
					tick = 0;
					OS_EventFlag.2(bit) = 1;
				}
				
			}
			if( C != LC ) {
				isChanged = true;
			}
		}
	}
	//------------------------------------------
	//[i] function_void OS_thread;
	public void OS_thread()
	{
		if( isChanged ) {
			CValue = C;
			isChanged = false;
			OS_EventFlag.0(bit) = 1;
		}
	}
	//---------------------------------------------------
	//[i] function_void Pause;
	public void Pause()
	{
		isPause = true;
	}
	//---------------------------------------------------
	//[i] function_void Continue;
	public void Continue()
	{
		isPause = false;
	}
	//---------------------------------------------------
	//[i] function_void XsRunTo float int32 int32;
	public void XsRunTo( int32 ft, linka int32 v*, int32 end )
	{
		SetTime( ft );
		CValue -> v;
		Start = v;
		C = Start;
		End = end;
		tick = 0;
		Times = 1;
		isForever = false;
		isSwap = false;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XsRunFromTo float int32 int32 int32;
	public void XsRunFromTo( int32 ft, linka int32 v*, int32 start, int32 end )
	{
		SetTime( ft );
		CValue -> v;
		Start = start;
		C = Start;
		End = end;
		tick = 0;
		Times = 1;
		isForever = false;
		isSwap = false;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XsRunFromToTimes float int32 int32 int32 int32;
	public void XsRunFromToTimes( int32 ft, linka int32 v*, int32 start, int32 end, int32 time )
	{
		SetTime( ft );
		CValue -> v;
		Start = start;
		C = Start;
		End = end;
		tick = 0;
		Times = time;
		isForever = false;
		isSwap = false;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XsRunFromToForever float int32 int32 int32;
	public void XsRunFromToForever( int32 ft, linka int32 v*, int32 start, int32 end )
	{
		SetTime( ft );
		CValue -> v;
		Start = start;
		C = Start;
		End = end;
		tick = 0;
		isForever = true;
		isSwap = false;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XsRunBetweenTimes float int32 int32 int32 int32;
	public void XsRunBetweenTimes( int32 ft, linka int32 v*, int32 start, int32 end, int32 time )
	{
		SetTime( ft );
		CValue -> v;
		Start = start;
		C = Start;
		End = end;
		tick = 0;
		Times = time;
		isForever = false;
		isSwap = true;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XsRunBetweenForever float int32 int32 int32;
	public void XsRunBetweenForever( int32 ft, linka int32 v*, int32 start, int32 end )
	{
		SetTime( ft );
		CValue -> v;
		Start = start;
		C = Start;
		End = end;
		tick = 0;
		isForever = true;
		isSwap = true;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XchTimes int32;
	public void XchTimes( int32 time )
	{
		isForever = false;
		isSwap = false;
		Times = time;
		int32 t = Start;
		Start = End;
		End = t;
		tick = 0;
		isOK = false;
	}
	//---------------------------------------------------
	//[i] function_void XchForever;
	public void XchForever()
	{
		isForever = true;
		isSwap = false;
		int32 t = Start;
		Start = End;
		End = t;
		tick = 0;
		isOK = false;
	}
	
	//=================================================================================
	
	//ÉèÖÃÊ±¼ä
	void SetTime( int32 ft )
	{
		uint32 ftt = (uint)ft;
		if( ftt.30(bit) == 1 ) {
			ftt.30(bit) = 0;
			FullTime = (int)ftt / 10;
		}
		else {
			FullTime = ft * 100;
		}
	}
}












