📄 adstmach.pas
字号:
{ we're activating a state, get the parameters for the packets }
for I := 0 to pred(NewState.Conditions.Count) do begin
if Assigned (LiveDataSource) then
LiveDataSource.StateMachineActivate (NewState,
NewState.Conditions[I], I);
{PacketList.Add(TApdDataPacket.Create(Self)); }
{with TApdDataPacket(PacketList[I]) do begin }
{ Name := NewState.Name + '_StatePacket' + IntToStr(I); }
{ Tag := I; }
{ AutoEnable := False; }
{ Enabled := False; }
{ assign the port and OnPacket event handler }
{ ComPort := FComPort; }
{ OnPacket := PacketEvent; }
{ OnTimeout := PacketTimeout; }
{ set up the Start and End conditions }
{ StartString := NewState.Conditions[I].StartString; }
{ if StartString = '' then }
{ StartCond := scAnyData }
{ else }
{ StartCond := scString; }
{ EndCond := []; }
{ EndString := NewState.Conditions[I].EndString; }
{ if EndString <> '' then }
{ EndCond := EndCond + [ecString]; }
{ PacketSize := NewState.Conditions[I].PacketSize; }
{ if PacketSize > 0 then }
{ EndCond := EndCond + [ecPacketSize]; }
{ Timeout := NewState.Conditions[I].Timeout; }
{ Enabled := True; }
{ InternalManager.KeepAlive := True; }
{end; }
end;
FCurrentState := NewState;
NewState.Activate;
FActive := True; {!!.02}
end;
end;
procedure TApdCustomStateMachine.DoDeactivate;
{var }
{ I : Integer; }
begin
if Paused then
Exit;
if Assigned (LiveDataSource) then
LiveDataSource.StateMachineDeactivate (FCurrentState);
{ disable and free our Condition's data packets }
{for I := 0 to pred(PacketList.Count) do begin }
{ if FComPort.Open then }
{ FComPort.AddStringToLog ('Deactivate_Data_Packet:' + }
{ IntToStr (i)); }
{ TApdDataPacket(PacketList[I]).Enabled := False; }
{ TApdDataPacket(PacketList[I]).Free; }
{end; }
FCurrentState.Deactivate;
end;
procedure TApdCustomStateMachine.DoStateChange(var M: TMessage);
var
NextState : TApdCustomState;
begin
if Paused then
Exit;
NextState := FCurrentState.Conditions[M.WParam].NextState;
if Assigned (LiveDataSource) then
LiveDataSource.StateChange (FCurrentState,
FCurrentState.Conditions[M.WParam].NextState);
if Assigned(FCurrentState.FOnStateFinish) then begin
FCurrentState.FOnStateFinish(FCurrentState,
FCurrentState.FConditions[M.WParam], NextState);
end;
if Assigned (LiveDataSource) then
LiveDataSource.StateChange (FCurrentState,
FCurrentState.Conditions[M.WParam].NextState);
{ tell the state to deactivate }
DoDeactivate;
{PacketList.Clear;}
{ generate the appropriate events }
if Assigned(FOnStateChange) then
FOnStateChange(Self, FCurrentState, NextState);
if (Assigned (LiveDataSource)) and
(FCurrentState.Conditions[M.wParam].OutputOnActivate <> '') then
LiveDataSource.Output (FCurrentState.Conditions[M.wParam].OutputOnActivate);
{ activate the next state }
DoActivate(NextState);
end;
function TApdCustomStateMachine.GetComPort : TApdCustomComPort;
begin
if LiveDataSource is TApdStateComPortSource then
Result := TApdStateComPortSource (LiveDataSource).ComPort
else if Assigned (FDefaultDataSource) then
Result := FDefaultDataSource.ComPort
else
Result := nil;
end;
function TApdCustomStateMachine.GetData: Pointer;
begin
Result := FData;
end;
function TApdCustomStateMachine.GetDataSource : TApdStateCustomDataSource;
begin
if Assigned (FDataSource) then
Result := FDataSource
else
Result := nil;
end;
function TApdCustomStateMachine.GetDataSize: Integer;
begin
Result := FDataSize;
end;
function TApdCustomStateMachine.GetDataString: string;
begin
Result := FDataString;
end;
function TApdCustomStateMachine.GetLiveDataSource : TApdStateCustomDataSource;
begin
if Assigned (FDataSource) then
Result := FDataSource
else
Result := FDefaultDataSource;
end;
function TApdCustomStateMachine.GetPaused;
begin
if Assigned (LiveDataSource) then
Result := LiveDataSource.Paused
else
Result := FDefaultDataSource.Paused;
end;
function TApdCustomStateMachine.GetStateNames: TStringList;
var
I : Integer;
begin
Result := TStringList.Create;
for I := 0 to pred(Owner.ComponentCount) do
if Owner.Components[I] is TApdCustomState then
if TApdCustomState(Owner.Components[I]).FStateMachine = Self then
Result.Add(TApdCustomState(Owner.Components[I]).Name);
end;
procedure TApdCustomStateMachine.Loaded;
begin
inherited;
end;
procedure TApdCustomStateMachine.MouseDown (Button : TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i : Integer;
Point : TPoint;
State : TApdCustomState;
AddType : TApdConnectAddType;
begin
if Button = mbLeft then begin
Point.x := X;
Point.y := Y;
if (Point.x < Left) or (Point.x > Left + Width) or
(Point.y < Top) or (Point.y > Top + Height) then
Exit;
for i := 0 to pred (ControlCount) do begin
if Controls[i] is TApdCustomState then begin
State := TApdCustomState(Controls[i]);
if (Point.x >= State.Left) and
(Point.x <= State.Left + State.Width) and
(Point.y >= State.Top) and
(Point.y <= State.Top + State.Height) then
Exit;
end;
end;
if (ssShift in Shift) then
AddType := atAdd
else if (ssCtrl in Shift) then
AddType := atSub
else
AddType := atNone;
ConnectoidAtPoint (AddType, Point);
end;
inherited MouseDown (Button, Shift, X, Y);
end;
procedure TApdCustomStateMachine.Notification (AComponent : TComponent;
Operation : TOperation);
begin
inherited Notification (AComponent, Operation);
if (Operation = opRemove) then begin
{See if our com port is going away}
if (AComponent = ComPort) then
ComPort := nil
else if (AComponent = FDataSource) then
FDataSource := nil
else if (AComponent = FStartState) then
FStartState := nil
else if (AComponent = FTerminalState) then
FTerminalState := nil;
end else if (Operation = opInsert) then begin
{Check for a com port being installed}
if not Assigned (ComPort) and
(AComponent is TApdCustomComPort) then
ComPort := TApdCustomComPort (AComponent);
// if not Assigned (FDataSource) and
// (AComponent is TApdStateCustomDataSource) and
// (not (AComponent = FDefaultDataSource)) then
// FDataSource := TApdStateCustomDataSource (AComponent);
end;
end;
{procedure TApdCustomStateMachine.PacketEvent(Sender: TObject; }
{ Data: Pointer; Size: Integer); }
{var }
{ Index : Integer; }
{begin }
{ Index := TApdDataPacket(Sender).Tag; }
{ {.$IFOPT H-}
{ if Size > 255 then }
{ raise EStringSizeError.Create(ecPacketTooLong, False); }
{ {.$ENDIF}
{ SetLength(FDataString, Size); }
{ Move(Data^, FDataString[1], Size); }
{ FData := Data; }
{ FDataSize := Size; }
{ PostMessage(Handle, apw_StateChange, Index, 0); }
{end; }
{procedure TApdCustomStateMachine.PacketTimeout(Sender: TObject); }
{begin }
{ PostMessage (Handle, apw_StateChange, }
{ TApdDataPacket(Sender).Tag, 1); }
{end; }
procedure TApdCustomStateMachine.StateMachinePaint;
var
R : TRect;
I, C : Integer;
State : TApdCustomState;
HintText : string;
begin
HintText := '';
{get the display dimensions}
R := GetClientRect;
{ clear the existing display }
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := Color;
Canvas.FillRect(R);
Canvas.Brush.Style := bsClear;
Canvas.Pen.Color := clActiveBorder;
Canvas.TextRect(R, 2, 2, Caption);
{ draw the connectoids }
for I := 0 to pred(ControlCount) do begin
if Controls[I] is TApdCustomState then begin
State := TApdCustomState(Controls[I]);
if State.Conditions.Count > 0 then begin
for C := 0 to pred(State.Conditions.Count) do
if State.Conditions[C].NextState <> nil then begin
RenderConnectoid (State.Conditions[C].Connectoid,
State, State.Conditions[C].NextState);
if (State.Conditions[C].Connectoid.FSelected) and
((csDesigning in ComponentState) or
(ccsHint in ConnectoidClickStyle)) then
HintText := HintText + State.Name + ' -> ' +
State.Conditions[C].NextState.Name + ' ';
end;
end;
end;
end;
{ tell the states to draw themselves }
for I := 0 to pred(ControlCount){ComponentCount)} do begin
if Controls[I]{Components[I]} is TApdCustomState then
TApdCustomState(Controls{Components}[I]).Paint;
end;
{ Display Hint Text }
if HintText <> '' then begin
Canvas.Pen.Color := clBlack;
Canvas.Pen.Width := 1;
Canvas.Brush.Color := $00bfffff;
Canvas.Font.Color := clBlack;
Canvas.Rectangle (3, 3,
Canvas.TextWidth (HintText) + 7,
Canvas.TextHeight (HintText) + 7);
Canvas.TextOut (5, 5, HintText);
Canvas.Brush.Color := clWhite;
end;
end;
procedure TApdCustomStateMachine.PaintWindow(DC: HDC);
var
R : TRect;
begin
Canvas.Handle := DC;
try
R := ClientRect;
InvalidateRect(Canvas.Handle, @R, True);
StateMachinePaint;
finally
Canvas.Handle := 0;
end;
end;
procedure TApdCustomStateMachine.Pause;
begin
if Assigned (LiveDataSource) then
LiveDataSource.Pause;
FDefaultDataSource.Pause;
end;
procedure TApdCustomStateMachine.RenderConnectoid (
Connectoid : TApdStateConnectoid;
State, DestState : TApdCustomState);
var
StartAt : TPoint;
EndAt : TPoint;
MidPoint : TPoint;
MinPoint : TPoint;
MaxPoint : TPoint;
OldColor : TColor;
{HintText : string;}
begin
if Assigned(Connectoid) and Assigned(DestState) and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -