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 >
Wrap
Pascal/Delphi Source File
|
2001-07-15
|
9KB
|
336 lines
unit WABD_HotSpotEditor;
{$I kbmWABD.inc}
interface
uses
{$ifdef LEVEL6}
DesignIntf,
{$else}
DsgnIntf,
{$endif}
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
WABD_Objects, StdCtrls, ExtCtrls, TypInfo;
type
THSEditForm = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
HotList: TListBox;
AddBut: TButton;
DeleteBut: TButton;
ScrollBox1: TScrollBox;
Image1: TImage;
Shape: TShape;
procedure AddButClick(Sender: TObject);
procedure HotListClick(Sender: TObject);
procedure DeleteButClick(Sender: TObject);
procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ShapeMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ShapeMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
{$ifdef LEVEL6}
MyDesigner : IDesigner;
{$else}
{$ifdef LEVEL5}
MyDesigner : IFormDesigner;
{$else}
{$ifdef LEVEL4}
MyDesigner : IFormDesigner;
{$else}
{$ifdef LEVEL3}
MyDesigner : TFormDesigner; // Delphi 3, C++ Builder 3
{$endif}
{$endif}
{$endif}
{$endif}
ParImage : TWABD_Base_Image;
Moving : boolean;
CurShape : TShape;
DownX : integer;
DownY : integer;
LastDown : DWORD;
CurReg : integer;
DetectChanges : boolean;
procedure Init;
procedure HotSpotChange(Sender: TObject);
end;
var
HSEditForm: THSEditForm;
implementation
{$R *.DFM}
procedure THSEditForm.HotSpotChange(Sender: TObject);
begin
if not DetectChanges then exit;
Init;
end;
procedure THSEditForm.Init;
var
i : integer;
c : TComponent;
h : TWABD_HotSpot;
bm : TBitmap;
s : TShape;
begin
Assert(ParImage<>nil, 'ParImage = nil');
HotList.Items.Clear;
// Clear out the old shapes
for i := ScrollBox1.ControlCount-1 downto 0 do begin
if ScrollBox1.Controls[i] is TShape then
ScrollBox1.Controls[i].Free;
end;
CurShape := nil;
for i := 0 to ParImage.Owner.ComponentCount-1 do begin
c := ParImage.Owner.Components[i];
if c is TWABD_HotSpot then begin
h := c as TWABD_HotSpot;
if h.ImageParent <> ParImage then continue;
h.OnChange := HotSpotChange;
HotList.Items.Add(c.Name);
s := TShape.Create(ScrollBox1);
s.Name := h.Name; // so we can find it easily
s.Left := h.X1;
s.Top := h.Y1;
s.Width := h.X2 - h.X1;
s.Height := h.Y2 - h.Y1;
s.OnMouseDown := ShapeMouseDown;
s.OnMouseMove := ShapeMouseMove;
s.OnMouseUp := ShapeMouseUp;
s.Parent := ScrollBox1;
s.Tag := integer(h);
s.Brush.Color := clInactiveCaption;
s.Brush.Style := bsDiagCross;
s.Pen.Color := clBlack;
end;
end;
Image1.Width := ParImage.ImageWidth;
Image1.Height := ParImage.ImageHeight;
try
Image1.Picture.LoadFromFile(ParImage.LocalImagePath);
except
on e: Exception do begin
bm := TBitmap.Create;
bm.Width := Image1.Width;
bm.Height := Image1.Height;
bm.handletype:=bmDDB;
bm.Canvas.TextOut(5, 5, 'Unable to load: ' + ParImage.LocalImagePath);
Image1.Picture.Bitmap := bm;
bm.Free;
end;
end;
DeleteBut.Enabled := False;
end;
procedure THSEditForm.AddButClick(Sender: TObject);
var
newc : TComponentClass;
c : TWABD_HotSpot;
begin
newc := TWABD_HotSpot;
c := MyDesigner.CreateComponent(newc, ParImage.Owner, 0, 0, 0, 0) as TWABD_HotSpot;
Assert(c<>nil, 'c = nil');
c.ImageParent := ParImage;
c.X2 := 50;
c.Y2 := 50;
MyDesigner.Modified;
Init;
HotList.ItemIndex := HotList.Items.IndexOf(c.Name);
HotListClick(Self);
end;
procedure THSEditForm.HotListClick(Sender: TObject);
var
i : integer;
n : string;
begin
i := HotList.ItemIndex;
if i = -1 then exit;
DeleteBut.Enabled := True;
n := HotList.Items[i];
MyDesigner.SelectComponent(ParImage.Owner.FindComponent(n));
if CurShape<>nil then CurShape.Brush.Color := clInactiveCaption;
CurShape := ScrollBox1.FindComponent(n) as TShape;
if CurShape<>nil then CurShape.Brush.Color := clActiveCaption;
end;
procedure THSEditForm.DeleteButClick(Sender: TObject);
var
i : integer;
c : TComponent;
begin
i := HotList.ItemIndex;
if i = -1 then exit;
c := ParImage.Owner.FindComponent(HotList.Items[i]);
c.Free;
MyDesigner.SelectComponent(nil);
MyDesigner.Modified;
HotList.Items.Delete(i);
if i >= HotList.Items.Count then i := HotList.Items.Count-1;
Init;
HotList.ItemIndex := i;
HotListClick(Self);
end;
function GetRegion(Shape: TControl; X, Y: integer): integer;
const
BORDER = 5;
var
px, py : integer;
begin
px := 1;
if X < BORDER then px := 0;
if X > Shape.Width-BORDER then px := 2;
py := 1;
if Y < BORDER then py := 0;
if Y > Shape.Height-BORDER then py := 2;
Result := py * 3 + px + 1;
end;
function GetRegCursor(Reg: integer): TCursor;
begin
Result := crDefault;
case Reg of
1, 9 : Result := crSizeNWSE;
3, 7 : Result := crSizeNESW;
2, 8 : Result := crSizeNS;
4, 6 : Result := crSizeWE;
5 : Result := crDefault;
end;
end;
procedure THSEditForm.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
s : TShape;
h : TWABD_HotSpot;
MethName : string;
nm : TMethod;
np : TPoint;
begin
s := Sender as TShape;
np := s.ClientToScreen(Point(X, Y));
Moving := True;
DownX := np.x;
DownY := np.y;
if CurShape<>nil then CurShape.Brush.Color := clInactiveCaption;
CurShape := Sender as TShape;
CurShape.BringToFront;
CurShape.Brush.Color := clActiveCaption;
CurReg := GetRegion(CurShape, X, Y);
CurShape.Cursor := GetRegCursor(CurReg);
h := TWABD_HotSpot(CurShape.Tag);
MyDesigner.SelectComponent(h);
HotList.ItemIndex := HotList.Items.IndexOf(h.Name);
DeleteBut.Enabled := HotList.ItemIndex <> -1;
if (GetTickCount - LastDown) < 300 then begin
// Simulate a double click
MethName := h.Name + 'UserClick';
nm := MyDesigner.CreateMethod(MethName, GetTypeData(TypeInfo(TNotifyEvent)));
h.OnUserClick := TNotifyEvent(nm);
MyDesigner.ShowMethod(MethName);
end;
LastDown := GetTickCount;
end;
procedure THSEditForm.ShapeMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
nx, ny : integer;
s : TShape;
h : TWABD_HotSpot;
Reg : integer;
np : TPoint;
NewLeft : integer;
NewTop : integer;
NewWidth : integer;
NewHeight : integer;
begin
s := Sender as TShape;
Reg := GetRegion(s, X, Y);
s.Cursor := GetRegCursor(Reg);
if not Moving then exit;
np := s.ClientToScreen(Point(x,y));
nx := np.X - DownX;
ny := np.Y - DownY;
DownX := np.X;
DownY := np.Y;
NewLeft := s.Left;
NewTop := s.Top;
NewWidth := s.Width;
NewHeight := s.Height;
case CurReg of
1, 2, 3 : begin NewTop := NewTop + ny; NewHeight := NewHeight - ny; end;
5 : begin NewTop := NewTop + ny; end;
7, 8, 9 : begin NewHeight := NewHeight + ny; end;
end;
case CurReg of
1, 4, 7 : begin NewLeft := NewLeft + nx; NewWidth := NewWidth - nx; end;
5 : begin NewLeft := NewLeft + nx; end;
3, 6, 9 : begin NewWidth := NewWidth + nx; end;
end;
if NewHeight < 3 then NewHeight := 3;
if NewWidth < 3 then NewWidth := 3;
s.SetBounds(NewLeft, NewTop, NewWidth, NewHeight);
DetectChanges := False;
h := TWABD_HotSpot(s.Tag);
h.X1 := s.Left;
h.Y1 := s.Top;
h.X2 := s.Left + s.Width;
h.Y2 := s.Top + s.Height;
DetectChanges := True;
MyDesigner.Modified;
end;
procedure THSEditForm.ShapeMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Moving := False;
end;
procedure THSEditForm.FormCreate(Sender: TObject);
begin
DetectChanges := True;
end;
end.