home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / DELPHIX.ZIP / Source / DXFFBEdit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-06  |  12.9 KB  |  508 lines

  1. unit DXFFBEdit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Spin, DXInput, TypInfo, ExtCtrls, Menus, ComCtrls;
  8.  
  9. type
  10.   TDelphiXFFEditForm = class(TForm)
  11.     ListGroupBox: TGroupBox;
  12.     AddButton: TButton;
  13.     DelButton: TButton;
  14.     EditGroupBox: TGroupBox;
  15.     EffectTypeBox: TComboBox;
  16.     EffectTypeLabel: TLabel;
  17.     ConstantLabel: TLabel;
  18.     ConstantYEdit: TSpinEdit;
  19.     ConstantXEdit: TSpinEdit;
  20.     ConstantXLabel: TLabel;
  21.     ConstantYLabel: TLabel;
  22.     FadeTimeLabel: TLabel;
  23.     FadeTimeEdit: TSpinEdit;
  24.     PeriodLabel: TLabel;
  25.     PeriodEdit: TSpinEdit;
  26.     PeriodLabel2: TLabel;
  27.     PowerLabel: TLabel;
  28.     TimeLabel: TLabel;
  29.     TimeEdit: TSpinEdit;
  30.     TimeLabel2: TLabel;
  31.     AttackTimeEditLabel: TLabel;
  32.     AttackTimeEdit: TSpinEdit;
  33.     RunButton: TButton;
  34.     RunGroupButton: TButton;
  35.     OKButton: TButton;
  36.     CancelButton: TButton;
  37.     DXInput: TDXInput;
  38.     NameEditLabel: TLabel;
  39.     NameEdit: TEdit;
  40.     Bevel1: TBevel;
  41.     StopButton: TButton;
  42.     PopupMenu: TPopupMenu;
  43.     A1: TMenuItem;
  44.     DeleteEffectItem: TMenuItem;
  45.     PowerEdit: TSpinEdit;
  46.     Timer: TTimer;
  47.     Bevel2: TBevel;
  48.     N1: TMenuItem;
  49.     N2: TMenuItem;
  50.     SaveToFileItem: TMenuItem;
  51.     AddFromFileButton: TButton;
  52.     OpenDialog: TOpenDialog;
  53.     SaveDialog: TSaveDialog;
  54.     EffectView: TTreeView;
  55.     ConditionLabel: TLabel;
  56.     ConditionXLabel: TLabel;
  57.     ConditionXEdit: TSpinEdit;
  58.     ConditionYLabel: TLabel;
  59.     ConditionYEdit: TSpinEdit;
  60.     Label1: TLabel;
  61.     Label2: TLabel;
  62.     AttackLevelEdit: TSpinEdit;
  63.     FadeLevelEdit: TSpinEdit;
  64.     Label3: TLabel;
  65.     Label4: TLabel;
  66.     Label5: TLabel;
  67.     Label6: TLabel;
  68.     procedure OKButtonClick(Sender: TObject);
  69.     procedure CancelButtonClick(Sender: TObject);
  70.     procedure EffectViewChange(Sender: TObject; Node: TTreeNode);
  71.     procedure FormShow(Sender: TObject);
  72.     procedure FormCreate(Sender: TObject);
  73.     procedure AddButtonClick(Sender: TObject);
  74.     procedure DelButtonClick(Sender: TObject);
  75.     procedure RunButtonClick(Sender: TObject);
  76.     procedure RunGroupButtonClick(Sender: TObject);
  77.     procedure StopButtonClick(Sender: TObject);
  78.     procedure ChangeEvent(Sender: TObject);
  79.     procedure TimerTimer(Sender: TObject);
  80.     procedure NameEditChange(Sender: TObject);
  81.     procedure EffectViewDragDrop(Sender, Source: TObject; X, Y: Integer);
  82.     procedure EffectViewDragOver(Sender, Source: TObject; X, Y: Integer;
  83.       State: TDragState; var Accept: Boolean);
  84.     procedure AddFromFileButtonClick(Sender: TObject);
  85.     procedure SaveToFileItemClick(Sender: TObject);
  86.     procedure EffectViewEdited(Sender: TObject; Node: TTreeNode;
  87.       var S: String);
  88.   private
  89.     FChanged: Boolean;
  90.     FSelectEffect: TForceFeedbackEffect;
  91.     FOldStates: TDXInputStates;
  92.     FUpdating: Boolean;
  93.     function AddTree(Parent: TTreeNode; Effect: TForceFeedbackEffect): TTreeNode;
  94.     procedure Save;
  95.   public
  96.     Effects: TForceFeedbackEffects;
  97.   end;
  98.  
  99. var
  100.   DelphiXFFEditForm: TDelphiXFFEditForm;
  101.  
  102. implementation
  103.  
  104. uses DXConsts;
  105.  
  106. {$R *.DFM}
  107.  
  108. procedure TDelphiXFFEditForm.FormCreate(Sender: TObject);
  109. var
  110.   e: TForceFeedbackEffectType;
  111.   s: string;
  112. begin
  113.   for e:=Low(e) to High(e) do
  114.   begin
  115.     if e=etNone then
  116.       s := SNone
  117.     else
  118.       s := GetEnumName(TypeInfo(TForceFeedbackEffectType), Integer(e));
  119.  
  120.     EffectTypeBox.Items.Add(s);
  121.   end;
  122. end;
  123.  
  124. function TDelphiXFFEditForm.AddTree(Parent: TTreeNode; Effect: TForceFeedbackEffect): TTreeNode;
  125. var
  126.   i: Integer;
  127. begin
  128.   Result := EffectView.Items.AddChildObject(Parent, Effect.Name, Effect);
  129.   for i:=0 to Effect.Count-1 do
  130.     AddTree(Result, Effect.Effects[i]);
  131. end;
  132.  
  133. procedure TDelphiXFFEditForm.Save;
  134. begin
  135.   if FChanged then EffectViewChange(nil, nil);
  136. end;
  137.  
  138. procedure TDelphiXFFEditForm.FormShow(Sender: TObject);
  139. begin
  140.   Caption := Format(SFFBEffectEditor, [Effects.Input.ClassName]);
  141.  
  142.   Effects.Input.Enabled := True;
  143.  
  144.   EffectView.Selected := AddTree(nil, Effects);
  145.   if EffectView.Selected<>nil then
  146.     EffectView.Selected.Expanded := True;
  147. end;
  148.  
  149. procedure TDelphiXFFEditForm.OKButtonClick(Sender: TObject);
  150. begin
  151.   Save;
  152.   Tag := 1;
  153.   Close;
  154. end;
  155.  
  156. procedure TDelphiXFFEditForm.CancelButtonClick(Sender: TObject);
  157. begin
  158.   Close;
  159. end;
  160.  
  161. procedure TDelphiXFFEditForm.EffectViewChange(Sender: TObject; Node: TTreeNode);
  162. var
  163.   i: Integer;
  164.   Control: TControl;
  165. begin
  166.   StopButtonClick(nil);
  167.  
  168.   FUpdating := True;
  169.   try
  170.     if FSelectEffect<>nil then
  171.     begin
  172.       if FChanged then
  173.       begin
  174.         with FSelectEffect do
  175.         begin
  176.           Name := NameEdit.Text;
  177.  
  178.           EffectType := etNone;
  179.  
  180.           Power := PowerEdit.Value;
  181.           Time := TimeEdit.Value;
  182.           AttackTime := AttackTimeEdit.Value;
  183.           AttackLevel := AttackLevelEdit.Value;
  184.           FadeTime := FadeTimeEdit.Value;
  185.           FadeLevel := FadeLevelEdit.Value;
  186.           Constant := Point(ConstantXEdit.Value, ConstantYEdit.Value);
  187.           Period := PeriodEdit.Value;
  188.           Condition := Point(ConditionXEdit.Value, ConditionYEdit.Value);
  189.  
  190.           EffectType := TForceFeedbackEffectType(EffectTypeBox.ItemIndex);
  191.         end;
  192.       end;
  193.     end;
  194.  
  195.     FSelectEffect := nil;
  196.     if (EffectView.Selected<>nil) and (EffectView.Selected.Data<>nil) then
  197.       FSelectEffect := EffectView.Selected.Data;
  198.  
  199.     DelButton.Enabled := (FSelectEffect<>nil) and (FSelectEffect.Parent<>nil);
  200.     DeleteEffectItem.Enabled := DelButton.Enabled;
  201.     SaveToFileItem.Enabled := FSelectEffect<>nil;
  202.  
  203.     if FSelectEffect<>nil then
  204.     begin
  205.       for i:=0 to ComponentCount-1 do
  206.         if (Components[i] is TControl) then
  207.         begin
  208.           Control := TControl(Components[i]);
  209.           if Control.Parent=EditGroupBox then
  210.           begin
  211.             Control.Enabled := True;
  212.             if Control is TEdit then
  213.               TSpinEdit(Control).Color := clWindow
  214.             else if Control is TSpinEdit then
  215.               TSpinEdit(Control).Color := clWindow
  216.             else if Control is TComboBox then
  217.               TComboBox(Control).Color := clWindow;
  218.           end;
  219.         end;
  220.     end else
  221.     begin
  222.       for i:=0 to ComponentCount-1 do
  223.         if (Components[i] is TControl) then
  224.         begin
  225.           Control := TControl(Components[i]);
  226.           if Control.Parent=EditGroupBox then
  227.           begin
  228.             Control.Enabled := False;
  229.             if Control is TEdit then
  230.             begin
  231.               TEdit(Control).Color := clBtnFace;
  232.               TEdit(Control).Text := '';
  233.             end else
  234.             if Control is TSpinEdit then
  235.             begin
  236.               TSpinEdit(Control).Color := clBtnFace;
  237.               TSpinEdit(Control).Value := 0;
  238.             end else if Control is TComboBox then
  239.             begin
  240.               TComboBox(Control).Color := clBtnFace;
  241.               TComboBox(Control).ItemIndex := 0;
  242.             end else if Control is TCheckBox then
  243.             begin
  244.               TCheckBox(Control).Checked := False;
  245.             end;
  246.           end;
  247.         end;
  248.     end;
  249.  
  250.     if FSelectEffect<>nil then
  251.     begin
  252.       with FSelectEffect do
  253.       begin
  254.         NameEdit.Text := Name;
  255.  
  256.         EffectTypeBox.ItemIndex := Integer(EffectType);
  257.  
  258.         PowerEdit.Value := Power;
  259.  
  260.         TimeEdit.Value := Time;
  261.  
  262.         AttackTimeEdit.Value := AttackTime;
  263.         AttackLevelEdit.Value := AttackLevel;
  264.         FadeTimeEdit.Value := FadeTime;
  265.         FadeLevelEdit.Value := FadeLevel;
  266.  
  267.         ConstantXEdit.Value := Constant.X;
  268.         ConstantYEdit.Value := Constant.Y;
  269.  
  270.         ConditionXEdit.Value := Condition.X;
  271.         ConditionYEdit.Value := Condition.Y;
  272.  
  273.         PeriodEdit.Value := Period;
  274.       end;
  275.     end;
  276.   finally
  277.     FUpdating := False;
  278.   end;
  279.  
  280.   FChanged := False;
  281.   FOldStates := [];
  282. end;
  283.  
  284. procedure TDelphiXFFEditForm.AddButtonClick(Sender: TObject);
  285. var
  286.   Effect: TForceFeedbackEffect;
  287.   OwnerEffect: TForceFeedbackEffect;
  288.   i, j: Integer;
  289.   Flag: Boolean;
  290. begin
  291.   if FSelectEffect<>nil then
  292.     OwnerEffect := FSelectEffect
  293.   else
  294.     OwnerEffect := Effects;
  295.  
  296.   {  Unique name making  }
  297.   j := 0;
  298.   repeat
  299.     Flag := True;
  300.     Inc(j);
  301.     for i:=0 to OwnerEffect.Count-1 do
  302.       if AnsiCompareText(OwnerEffect[i].Name, Format('Effect%d', [j]))=0 then
  303.       begin
  304.         Flag := False;
  305.         Break;
  306.       end;
  307.   until Flag;
  308.  
  309.   {  Effect making  }
  310.   Effect := TForceFeedbackEffect.Create(OwnerEffect);
  311.   Effect.Name := Format('Effect%d', [j]);
  312.  
  313.   EffectView.Selected := AddTree(EffectView.Selected, Effect);
  314. end;
  315.  
  316. procedure TDelphiXFFEditForm.DelButtonClick(Sender: TObject);
  317. begin
  318.   FSelectEffect.Free;
  319.   FSelectEffect := nil;
  320.  
  321.   EffectView.Selected.Delete;
  322. end;
  323.  
  324. procedure TDelphiXFFEditForm.RunButtonClick(Sender: TObject);
  325. begin
  326.   if not RunButton.Enabled then Exit;
  327.  
  328.   Save;
  329.  
  330.   if not DXInput.UseDirectInput then
  331.   begin
  332.     Screen.Cursor := crHourGlass;
  333.     try
  334.       DXInput.UseDirectInput := True;
  335.     finally
  336.       Screen.Cursor := crDefault;
  337.     end;
  338.   end;
  339.  
  340.   FSelectEffect.Start;
  341. end;
  342.  
  343. procedure TDelphiXFFEditForm.RunGroupButtonClick(Sender: TObject);
  344. var
  345.   Effect: TForceFeedbackEffect;
  346. begin
  347.   if not RunGroupButton.Enabled then Exit;
  348.  
  349.   Save;
  350.  
  351.   if not DXInput.UseDirectInput then
  352.   begin
  353.     Screen.Cursor := crHourGlass;
  354.     try
  355.       DXInput.UseDirectInput := True;
  356.     finally
  357.       Screen.Cursor := crDefault;
  358.     end;
  359.   end;
  360.  
  361.   Effect := FSelectEffect;
  362.   while (Effect.Parent<>nil) and (Effect.Parent.Parent<>nil) do
  363.     Effect := Effect.Parent;
  364.  
  365.   Effect.Start;
  366. end;
  367.  
  368. procedure TDelphiXFFEditForm.StopButtonClick(Sender: TObject);
  369. begin
  370.   Effects.Stop;
  371.   Effects.UnLoad(True);
  372. end;
  373.  
  374. procedure TDelphiXFFEditForm.ChangeEvent(Sender: TObject);
  375. begin
  376.   FChanged := True;
  377. end;
  378.  
  379. procedure TDelphiXFFEditForm.TimerTimer(Sender: TObject);
  380. var
  381.   s: TDXInputStates;
  382. begin
  383.   if not (Effects.Input is TKeyboard) then
  384.   begin
  385.     Effects.Input.Update;
  386.     s := Effects.Input.States;
  387.  
  388.     if (isButton1 in s) and (not (isButton1 in FOldStates)) then
  389.       RunGroupButtonClick(nil)
  390.     else if (isButton2 in s) and (not (isButton2 in FOldStates)) then
  391.       RunButtonClick(nil)
  392.     else if (s*[isButton1, isButton2]=[]) and (FOldStates*[isButton1, isButton2]<>[]) then
  393.       StopButtonClick(nil);
  394.  
  395.     FOldStates := s;
  396.   end;
  397. end;
  398.  
  399. procedure TDelphiXFFEditForm.NameEditChange(Sender: TObject);
  400. begin
  401.   if not FUpdating then
  402.     if EffectView.Selected<>nil then
  403.     begin
  404.       EffectView.Selected.Text := NameEdit.Text;
  405.       FSelectEffect.Name := NameEdit.Text;
  406.     end;
  407. end;
  408.  
  409. procedure TDelphiXFFEditForm.EffectViewDragOver(Sender, Source: TObject; X,
  410.   Y: Integer; State: TDragState; var Accept: Boolean);
  411. var
  412.   Node: TTreeNode;
  413. begin
  414.   Accept := False;
  415.  
  416.   Node := EffectView.GetNodeAt(X, Y);
  417.   if Node=nil then Exit;
  418.  
  419.   if Node=EffectView.Items.GetFirstNode then Exit;
  420.  
  421.   while Node<>nil do
  422.   begin
  423.     if Node=EffectView.Selected then
  424.       Exit;
  425.     Node := Node.Parent;
  426.   end;
  427.  
  428.   Accept := True;
  429. end;
  430.  
  431. procedure TDelphiXFFEditForm.EffectViewDragDrop(Sender, Source: TObject; X,
  432.   Y: Integer);
  433. var
  434.   Node, Node2: TTreeNode;
  435.   Effect: TForceFeedbackEffect;
  436. begin
  437.   Save;
  438.  
  439.   Node := EffectView.GetNodeAt(X, Y);
  440.  
  441.   if Node<>nil then
  442.   begin
  443.     Effect := TObject(EffectView.Selected.Data) as TForceFeedbackEffect;
  444.  
  445.     Node2 := EffectView.Selected;
  446.  
  447.     if X>Node.DisplayRect(True).Right+10 then
  448.     begin
  449.       {  Effect is moved to the child.  }
  450.       Effect.Parent := TObject(Node.Data) as TForceFeedbackEffect;
  451.       EffectView.Selected.MoveTo(Node, naAddChild);
  452.       Effect.Index := EffectView.Selected.Index;
  453.     end else
  454.     begin
  455.       {  Effect is moved to the brother.  }
  456.       Effect.Parent := (TObject(Node.Data) as TForceFeedbackEffect).Parent;
  457.       EffectView.Selected.MoveTo(Node, naInsert);
  458.       Effect.Index := EffectView.Selected.Index;
  459.     end;
  460.  
  461.     EffectView.Selected := Node2;
  462.  
  463.     EffectView.Invalidate;
  464.   end;
  465. end;
  466.  
  467. procedure TDelphiXFFEditForm.AddFromFileButtonClick(Sender: TObject);
  468. var
  469.   i: Integer;
  470.   Effect: TForceFeedbackEffect;
  471. begin
  472.   if not OpenDialog.Execute then Exit;
  473.  
  474.   Save;
  475.  
  476.   for i:=0 to OpenDialog.Files.Count-1 do
  477.   begin
  478.     Effect := TForceFeedbackEffect.Create(Effects);
  479.     try
  480.       Effect.LoadFromFile(OpenDialog.Files[i]);
  481.     except
  482.       Effect.Free;
  483.       raise;
  484.     end;
  485.  
  486.     EffectView.Selected := AddTree(EffectView.Items.GetFirstNode, Effect);
  487.   end;
  488. end;
  489.  
  490. procedure TDelphiXFFEditForm.SaveToFileItemClick(Sender: TObject);
  491. begin
  492.   SaveDialog.FileName := FSelectEffect.Name+'.ffe';
  493.  
  494.   if not SaveDialog.Execute then Exit;
  495.  
  496.   Save;
  497.  
  498.   FSelectEffect.SaveToFile(SaveDialog.FileName);
  499. end;
  500.  
  501. procedure TDelphiXFFEditForm.EffectViewEdited(Sender: TObject;
  502.   Node: TTreeNode; var S: String);
  503. begin
  504.   NameEdit.Text := s;
  505. end;
  506.  
  507. end.
  508.