STNAV

Top  Previous  Next

unit Stnav;

 

interface

 

uses

SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,

Forms, Dialogs, ExtCtrls, Buttons, StMap16w, StMap16, Almtype;

 

type

TNavButtons = (nvLeft,nvRight,nvUp,nvDown,nvTotal);

TDirectChangeEvent = procedure(Sender: TObject; Button: integer) of object;

 

TStNavigator = class(TCustomPanel)

private

  FStellaMap       : TStMapW;

  FSize            : integer;

  FOnUp            : TDirectChangeEvent;

  FOnDown          : TDirectChangeEvent;

  FOnLeft          : TDirectChangeEvent;

  FOnRight         : TDirectChangeEvent;

  FOnTotal         : TDirectChangeEvent;

  procedure SeTStMapW(Value:TStMapW);

  procedure SetSize(Value:integer);

  procedure WMSize(var Msg: TWMSize); message WM_SIZE;

protected

  w,h      : integer;

  FButtons : array[TNavButtons] of TSpeedButton;

  procedure Resize; override;

  procedure Click(Sender:TObject);

  procedure Notification(AComponent: TComponent;

    Operation: TOperation); override;

public

  constructor Create(AOwner:TComponent);override;

  destructor Destroy;override;

published

  Property StellaMapSource : TStMapW read FStellaMap write SeTStMapW ;

  Property Size: integer read FSize write SetSize default 80;

  Property OnUp: TDirectChangeEvent read FOnUp write FOnUp;

  Property OnDown: TDirectChangeEvent read FOnDown write FOnDown;

  Property OnLeft: TDirectChangeEvent read FOnLeft write FOnLeft;

  Property OnRight: TDirectChangeEvent read FOnRight write FOnRight;

  Property OnTotal: TDirectChangeEvent read FOnTotal write FOnTotal;

  property Align;

  Property BevelInner;

  Property BevelOuter;

  Property BevelWidth;

  Property BorderWidth;

  Property BorderStyle;

  Property Color;

  Property Ctl3D;

  Property ShowHint;

  Property Tag;

  Property Visible;

end;

 

implementation

 

{$R STNAV}

 

Const

BtnTypeName : array[TNavButtons] of PChar = ('NAV_LEFT', 'NAV_RIGHT',

               'NAV_UP','NAV_DOWN','NAV_TOTAL');

 

constructor TStNavigator.Create(AOwner:TComponent);

var i:TNavButtons;

  ResName: array[0..40] of Char;

  j: integer;

begin

   inherited Create(AOwner);

   j:=1;

   For i:=Low(FButtons) to High(FButtons) do begin

       FButtons[i]:=TSpeedButton.Create(Self);

       FButtons[i].Parent:=Self;

       FButtons[i].Tag:= j;

       FButtons[i].Caption := '';

       FButtons[i].Enabled := False;

       FButtons[i].GroupIndex := 0;

       FButtons[i].AllowAllUp := True;

       FButtons[i].OnClick := Click;

       FButtons[i].Glyph.Handle := LoadBitmap(HInstance,

           StrFmt(ResName, '%s', [BtnTypeName[I]]));

       FButtons[i].NumGlyphs := 1;

       Inc(j);

   end;

   Caption:='';

   Size:=40;

end;

 

destructor TStNavigator.Destroy;

var i: TNavButtons;

begin

   For i:=Low(FButtons) to High(FButtons) do FButtons[i].Free;

   inherited Destroy;

end;

 

procedure TStNavigator.SeTStMapW(Value:TStMapW);

var i: TNavButtons;

begin

   FStellaMap:=Value;

   For i:=Low(FButtons) to High(FButtons) do

       FButtons[i].Enabled := Value<>nil;

   Resize;

end;

 

procedure TStNavigator.SetSize(Value:integer);

begin

   If Value>10 then FSize:=Value;

   SetBounds(Left,Top,FSize,FSize);

   Caption:='';

   Resize;

   Invalidate;

end;

 

procedure TStNavigator.WMSize(var Msg: TWMSize);

begin

  inherited;

  If Msg.Width > Msg.Height then FSize := Msg.Width;

  If Msg.Height> Msg.Width then FSize := Msg.Height;

  Size:=FSize;

  Resize;

  Invalidate;

end;

 

procedure TStNavigator.Resize;

var i: TNavButtons;

  bw,bh: integer;   {ButtonWidth/height}

  bs : integer;

begin

 if (csLoading in ComponentState) then Exit;

 bs:=BevelWidth+BorderWidth;

 If BevelInner<>bvNone then bs:=bs+BevelWidth;

 bw := (Size - 2*bs) div 3;

 FButtons[nvLeft].SetBounds(bs,bs+bw,bw,bw);

 FButtons[nvRight].SetBounds(bs+2*bw,bs+bw,bw,bw);

 FButtons[nvUp].SetBounds(bs+bw,bs,bw,bw);

 FButtons[nvDown].SetBounds(bs+bw,bs+2*bw,bw,bw);

 FButtons[nvTotal].SetBounds(bs+bw,bs+bw,bw,bw);

 FButtons[nvTotal].Hint:='Total';

 Invalidate;

 Inherited Resize;

end;

 

procedure TStNavigator.Click(Sender:TObject);

var i: TNavButtons;

  t: integer;

begin

 If StellaMapSource<>nil then begin

    t := (Sender as TSpeedButton).Tag;

    Case t of

    1: begin

            StellaMapSource.eltolas(elRight);

            If Assigned(FOnRight) then FOnRight(Self,t);

       end;

    2: begin

            StellaMapSource.eltolas(elLeft);

            If Assigned(FOnLeft) then FOnLeft(Self,t);

       end;

    3: begin

            StellaMapSource.eltolas(elDown);

            If Assigned(FOnUp) then FOnUp(Self,t);

       end;

    4: begin

            StellaMapSource.eltolas(elUp);

            If Assigned(FOnDown) then FOnDown(Self,t);

       end;

    5: begin

            StellaMapSource.MinMaxKeres;

            If Assigned(FOnTotal) then FOnTotal(Self,t);

       end;

    end;

 end;

end;

 

procedure TStNavigator.Notification(AComponent: TComponent;

Operation: TOperation);

begin

inherited Notification(AComponent, Operation);

if (Operation = opRemove) and (AComponent = StellaMapSource)

then StellaMapSource := nil;

end;

 

end.