home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d3456 / KBMWABD.ZIP / WABD_HotSpotEditor.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-15  |  9KB  |  336 lines

  1. unit WABD_HotSpotEditor;
  2.  
  3. {$I kbmWABD.inc}
  4.  
  5. interface
  6.  
  7. uses
  8. {$ifdef LEVEL6}
  9.   DesignIntf,
  10. {$else}
  11.   DsgnIntf,
  12. {$endif}
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   WABD_Objects, StdCtrls, ExtCtrls, TypInfo;
  15.  
  16. type
  17.   THSEditForm = class(TForm)
  18.     Panel1: TPanel;
  19.     Panel2: TPanel;
  20.     HotList: TListBox;
  21.     AddBut: TButton;
  22.     DeleteBut: TButton;
  23.     ScrollBox1: TScrollBox;
  24.     Image1: TImage;
  25.     Shape: TShape;
  26.     procedure AddButClick(Sender: TObject);
  27.     procedure HotListClick(Sender: TObject);
  28.     procedure DeleteButClick(Sender: TObject);
  29.     procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton;
  30.       Shift: TShiftState; X, Y: Integer);
  31.     procedure ShapeMouseMove(Sender: TObject; Shift: TShiftState; X,
  32.       Y: Integer);
  33.     procedure ShapeMouseUp(Sender: TObject; Button: TMouseButton;
  34.       Shift: TShiftState; X, Y: Integer);
  35.     procedure FormCreate(Sender: TObject);
  36.   private
  37.     { Private declarations }
  38.   public
  39.     { Public declarations }
  40. {$ifdef LEVEL6}
  41.     MyDesigner   : IDesigner;
  42. {$else}
  43.  {$ifdef LEVEL5}
  44.     MyDesigner   : IFormDesigner;
  45.  {$else}
  46.   {$ifdef LEVEL4}
  47.     MyDesigner   : IFormDesigner;
  48.   {$else}
  49.    {$ifdef LEVEL3}
  50.     MyDesigner   : TFormDesigner; // Delphi 3, C++ Builder 3
  51.    {$endif}
  52.   {$endif}
  53.  {$endif}
  54. {$endif}
  55.  
  56.     ParImage   : TWABD_Base_Image;
  57.     Moving     : boolean;
  58.     CurShape   : TShape;
  59.     DownX      : integer;
  60.     DownY      : integer;
  61.     LastDown   : DWORD;
  62.     CurReg     : integer;
  63.     DetectChanges : boolean;
  64.     procedure  Init;
  65.     procedure  HotSpotChange(Sender: TObject);
  66.   end;
  67.  
  68. var
  69.   HSEditForm: THSEditForm;
  70.  
  71. implementation
  72.  
  73. {$R *.DFM}
  74.  
  75. procedure THSEditForm.HotSpotChange(Sender: TObject);
  76. begin
  77.    if not DetectChanges then exit;
  78.    Init;
  79. end;
  80.  
  81. procedure THSEditForm.Init;
  82. var
  83.    i  : integer;
  84.    c  : TComponent;
  85.    h  : TWABD_HotSpot;
  86.    bm : TBitmap;
  87.    s  : TShape;
  88. begin
  89.    Assert(ParImage<>nil, 'ParImage = nil');
  90.    HotList.Items.Clear;
  91.  
  92.    // Clear out the old shapes
  93.    for i := ScrollBox1.ControlCount-1 downto 0 do begin
  94.       if ScrollBox1.Controls[i] is TShape then
  95.          ScrollBox1.Controls[i].Free;
  96.    end;
  97.    CurShape := nil;
  98.  
  99.    for i := 0 to ParImage.Owner.ComponentCount-1 do begin
  100.       c := ParImage.Owner.Components[i];
  101.       if c is TWABD_HotSpot then begin
  102.          h := c as TWABD_HotSpot;
  103.          if h.ImageParent <> ParImage then continue;
  104.          h.OnChange     := HotSpotChange;
  105.          HotList.Items.Add(c.Name);
  106.          s := TShape.Create(ScrollBox1);
  107.          s.Name         := h.Name;     // so we can find it easily
  108.          s.Left         := h.X1;
  109.          s.Top          := h.Y1;
  110.          s.Width        := h.X2 - h.X1;
  111.          s.Height       := h.Y2 - h.Y1;
  112.          s.OnMouseDown  := ShapeMouseDown;
  113.          s.OnMouseMove  := ShapeMouseMove;
  114.          s.OnMouseUp    := ShapeMouseUp;
  115.          s.Parent       := ScrollBox1;
  116.          s.Tag          := integer(h);
  117.          s.Brush.Color  := clInactiveCaption;
  118.          s.Brush.Style  := bsDiagCross;
  119.          s.Pen.Color    := clBlack;
  120.       end;
  121.    end;
  122.  
  123.    Image1.Width := ParImage.ImageWidth;
  124.    Image1.Height := ParImage.ImageHeight;
  125.    try
  126.       Image1.Picture.LoadFromFile(ParImage.LocalImagePath);
  127.    except
  128.       on e: Exception do begin
  129.          bm := TBitmap.Create;
  130.          bm.Width := Image1.Width;
  131.          bm.Height := Image1.Height;
  132.          bm.handletype:=bmDDB;
  133.          bm.Canvas.TextOut(5, 5, 'Unable to load: ' + ParImage.LocalImagePath);
  134.          Image1.Picture.Bitmap := bm;
  135.          bm.Free;
  136.       end;
  137.    end;
  138.  
  139.    DeleteBut.Enabled := False;
  140. end;
  141.  
  142. procedure THSEditForm.AddButClick(Sender: TObject);
  143. var
  144.    newc : TComponentClass;
  145.    c    : TWABD_HotSpot;
  146. begin
  147.    newc := TWABD_HotSpot;
  148.    c := MyDesigner.CreateComponent(newc, ParImage.Owner, 0, 0, 0, 0) as TWABD_HotSpot;
  149.    Assert(c<>nil, 'c = nil');
  150.    c.ImageParent := ParImage;
  151.    c.X2 := 50;
  152.    c.Y2 := 50;
  153.    MyDesigner.Modified;
  154.  
  155.    Init;
  156.  
  157.    HotList.ItemIndex := HotList.Items.IndexOf(c.Name);
  158.    HotListClick(Self);
  159. end;
  160.  
  161. procedure THSEditForm.HotListClick(Sender: TObject);
  162. var
  163.    i : integer;
  164.    n : string;
  165. begin
  166.    i := HotList.ItemIndex;
  167.    if i = -1 then exit;
  168.  
  169.    DeleteBut.Enabled := True;
  170.  
  171.    n := HotList.Items[i];
  172.    MyDesigner.SelectComponent(ParImage.Owner.FindComponent(n));
  173.  
  174.    if CurShape<>nil then CurShape.Brush.Color := clInactiveCaption;
  175.    CurShape := ScrollBox1.FindComponent(n) as TShape;
  176.    if CurShape<>nil then CurShape.Brush.Color := clActiveCaption;
  177. end;
  178.  
  179. procedure THSEditForm.DeleteButClick(Sender: TObject);
  180. var
  181.    i : integer;
  182.    c : TComponent;
  183. begin
  184.    i := HotList.ItemIndex;
  185.    if i = -1 then exit;
  186.  
  187.    c := ParImage.Owner.FindComponent(HotList.Items[i]);
  188.    c.Free;
  189.    MyDesigner.SelectComponent(nil);
  190.    MyDesigner.Modified;
  191.  
  192.    HotList.Items.Delete(i);
  193.    if i >= HotList.Items.Count then i := HotList.Items.Count-1;
  194.  
  195.    Init;
  196.  
  197.    HotList.ItemIndex := i;
  198.    HotListClick(Self);
  199. end;
  200.  
  201. function GetRegion(Shape: TControl; X, Y: integer): integer;
  202. const
  203.    BORDER = 5;
  204. var
  205.    px, py : integer;
  206. begin
  207.    px := 1;
  208.    if X < BORDER then px := 0;
  209.    if X > Shape.Width-BORDER then px := 2;
  210.  
  211.    py := 1;
  212.    if Y < BORDER then py := 0;
  213.    if Y > Shape.Height-BORDER then py := 2;
  214.  
  215.    Result := py * 3 + px + 1;
  216. end;
  217.  
  218. function GetRegCursor(Reg: integer): TCursor;
  219. begin
  220.    Result := crDefault;
  221.    case Reg of
  222.       1, 9  : Result := crSizeNWSE;
  223.       3, 7  : Result := crSizeNESW;
  224.       2, 8  : Result := crSizeNS;
  225.       4, 6  : Result := crSizeWE;
  226.       5     : Result := crDefault;
  227.    end;
  228. end;
  229.  
  230. procedure THSEditForm.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
  231.   Shift: TShiftState; X, Y: Integer);
  232. var
  233.    s        : TShape;
  234.    h        : TWABD_HotSpot;
  235.    MethName : string;
  236.    nm       : TMethod;
  237.    np       : TPoint;
  238. begin
  239.    s := Sender as TShape;
  240.    np := s.ClientToScreen(Point(X, Y));
  241.  
  242.    Moving := True;
  243.    DownX  := np.x;
  244.    DownY  := np.y;
  245.  
  246.    if CurShape<>nil then CurShape.Brush.Color := clInactiveCaption;
  247.  
  248.    CurShape := Sender as TShape;
  249.    CurShape.BringToFront;
  250.    CurShape.Brush.Color := clActiveCaption;
  251.  
  252.    CurReg := GetRegion(CurShape, X, Y);
  253.    CurShape.Cursor := GetRegCursor(CurReg);
  254.  
  255.    h := TWABD_HotSpot(CurShape.Tag);
  256.    MyDesigner.SelectComponent(h);
  257.    HotList.ItemIndex := HotList.Items.IndexOf(h.Name);
  258.    DeleteBut.Enabled := HotList.ItemIndex <> -1;
  259.  
  260.    if (GetTickCount - LastDown) < 300 then begin
  261.       // Simulate a double click
  262.       MethName := h.Name + 'UserClick';
  263.       nm := MyDesigner.CreateMethod(MethName, GetTypeData(TypeInfo(TNotifyEvent)));
  264.       h.OnUserClick := TNotifyEvent(nm);
  265.       MyDesigner.ShowMethod(MethName);
  266.    end;
  267.  
  268.    LastDown := GetTickCount;
  269. end;
  270.  
  271. procedure THSEditForm.ShapeMouseMove(Sender: TObject; Shift: TShiftState;
  272.   X, Y: Integer);
  273. var
  274.    nx, ny    : integer;
  275.    s         : TShape;
  276.    h         : TWABD_HotSpot;
  277.    Reg       : integer;
  278.    np        : TPoint;
  279.    NewLeft   : integer;
  280.    NewTop    : integer;
  281.    NewWidth  : integer;
  282.    NewHeight : integer;
  283. begin
  284.    s := Sender as TShape;
  285.    Reg := GetRegion(s, X, Y);
  286.    s.Cursor := GetRegCursor(Reg);
  287.  
  288.    if not Moving then exit;
  289.  
  290.    np := s.ClientToScreen(Point(x,y));
  291.    nx := np.X - DownX;
  292.    ny := np.Y - DownY;
  293.    DownX := np.X;
  294.    DownY := np.Y;
  295.  
  296.    NewLeft   := s.Left;
  297.    NewTop    := s.Top;
  298.    NewWidth  := s.Width;
  299.    NewHeight := s.Height;
  300.    case CurReg of
  301.       1, 2, 3  : begin NewTop := NewTop + ny; NewHeight := NewHeight - ny; end;
  302.       5        : begin NewTop := NewTop + ny; end;
  303.       7, 8, 9  : begin NewHeight := NewHeight + ny; end;
  304.    end;
  305.    case CurReg of
  306.       1, 4, 7  : begin NewLeft := NewLeft + nx; NewWidth := NewWidth - nx; end;
  307.       5        : begin NewLeft := NewLeft + nx; end;
  308.       3, 6, 9  : begin NewWidth := NewWidth + nx; end;
  309.    end;
  310.    if NewHeight < 3 then NewHeight := 3;
  311.    if NewWidth  < 3 then NewWidth  := 3;
  312.    s.SetBounds(NewLeft, NewTop, NewWidth, NewHeight);
  313.  
  314.    DetectChanges := False;
  315.    h := TWABD_HotSpot(s.Tag);
  316.    h.X1 := s.Left;
  317.    h.Y1 := s.Top;
  318.    h.X2 := s.Left + s.Width;
  319.    h.Y2 := s.Top  + s.Height;
  320.    DetectChanges := True;
  321.    MyDesigner.Modified;
  322. end;
  323.  
  324. procedure THSEditForm.ShapeMouseUp(Sender: TObject; Button: TMouseButton;
  325.   Shift: TShiftState; X, Y: Integer);
  326. begin
  327.    Moving := False;
  328. end;
  329.  
  330. procedure THSEditForm.FormCreate(Sender: TObject);
  331. begin
  332.    DetectChanges := True;
  333. end;
  334.  
  335. end.
  336.