VrJoypad.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:6k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrJoypad;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Dialogs,
  14.   VrConst, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrJoypadDirection = (jdUp, jdDown, jdLeft, jdRight);
  17.   TVrJoypadDirections = set of TVrJoypadDirection;
  18.   TVrVisibleArrow = (vaUp, vaDown, vaLeft, vaRight);
  19.   TVrVisibleArrows = set of TVrVisibleArrow;
  20.   TVrJoypad = class(TVrGraphicImageControl)
  21.   private
  22.     FSpacing: Integer;
  23.     FDirections: TVrJoypadDirections;
  24.     FVisibleArrows: TVrVisibleArrows;
  25.     FPalette: TVrPalette;
  26.     ImageWidth: Integer;
  27.     ImageHeight: Integer;
  28.     Bitmaps: array[0..1] of TBitmap;
  29.     procedure SetSpacing(Value: Integer);
  30.     procedure SetDirections(Value: TVrJoypadDirections);
  31.     procedure SetVisibleArrows(Value: TVrVisibleArrows);
  32.     procedure SetPalette(Value: TVrPalette);
  33.     procedure PaletteModified(Sender: TObject);
  34.   protected
  35.     procedure LoadBitmaps; virtual;
  36.     procedure DestroyBitmaps;
  37.     procedure UpdateLed(Index: TVrVisibleArrow; Active: Boolean);
  38.     procedure UpdateLeds;
  39.     procedure Paint; override;
  40.     function GetPalette: HPalette; override;
  41.     procedure GetImageRect(Index: TVrVisibleArrow; var R: TRect);
  42.   public
  43.     constructor Create(AOwner: TComponent); override;
  44.     destructor Destroy; override;
  45.   published
  46.     property Spacing: Integer read FSpacing write SetSpacing default 4;
  47.     property Directions: TVrJoypadDirections read FDirections write SetDirections default [];
  48.     property VisibleArrows: TVrVisibleArrows read FVisibleArrows write SetVisibleArrows default [vaUp, vaDown, vaLeft, vaRight];
  49.     property Palette: TVrPalette read FPalette write SetPalette;
  50.     property Transparent default false;
  51.     property Align;
  52. {$IFDEF VER110}
  53.     property Anchors;
  54.     property Constraints;
  55. {$ENDIF}
  56.     property Color default clBlack;
  57.     property DragCursor;
  58. {$IFDEF VER110}
  59.     property DragKind;
  60. {$ENDIF}
  61.     property DragMode;
  62.     property ParentColor default false;
  63.     property ParentShowHint;
  64.     property PopupMenu;
  65.     property ShowHint;
  66.     property Visible;
  67.     property OnClick;
  68. {$IFDEF VER130}
  69.     property OnContextPopup;
  70. {$ENDIF}    
  71.     property OnDblClick;
  72.     property OnDragDrop;
  73.     property OnDragOver;
  74. {$IFDEF VER110}
  75.     property OnEndDock;
  76. {$ENDIF}
  77.     property OnEndDrag;
  78.     property OnMouseDown;
  79.     property OnMouseMove;
  80.     property OnMouseUp;
  81. {$IFDEF VER110}
  82.     property OnStartDock;
  83. {$ENDIF}
  84.     property OnStartDrag;
  85.   end;
  86. implementation
  87. {$R VRJOYPAD.D32}
  88. const
  89.   ResName: array[0..1] of PChar = ('IMAGESOFF', 'IMAGESON');
  90. {TVrJoypad}
  91. constructor TVrJoypad.Create(AOwner: TComponent);
  92. begin
  93.   inherited Create(AOwner);
  94.   ControlStyle := ControlStyle + [csOpaque, csReplicatable];
  95.   Width := 55;
  96.   Height := 55;
  97.   ParentColor := false;
  98.   Color := clBlack;
  99.   FSpacing := 4;
  100.   FDirections := [];
  101.   FVisibleArrows := [vaLeft, vaRight, vaUp, vaDown];
  102.   FPalette := TVrPalette.Create;
  103.   FPalette.OnChange := PaletteModified;
  104.   LoadBitmaps;
  105. end;
  106. destructor TVrJoypad.Destroy;
  107. begin
  108.   FPalette.Free;
  109.   DestroyBitmaps;
  110.   inherited Destroy;
  111. end;
  112. procedure TVrJoypad.LoadBitmaps;
  113. var
  114.   I: Integer;
  115. begin
  116.   for I := 0 to 1 do
  117.   begin
  118.     if not Assigned(Bitmaps[I]) then
  119.       Bitmaps[I] := TBitmap.Create;
  120.     Bitmaps[I].Handle := LoadBitmap(hInstance, ResName[I]);
  121.     FPalette.ToBMP(Bitmaps[I], ResColorLow, ResColorHigh);
  122.   end;
  123.   ImageWidth := Bitmaps[0].Width div 4;
  124.   ImageHeight := Bitmaps[0].Height;
  125. end;
  126. procedure TVrJoypad.DestroyBitmaps;
  127. var
  128.   I: Integer;
  129. begin
  130.   for I := 0 to 1 do Bitmaps[I].Free;
  131. end;
  132. function TVrJoypad.GetPalette: HPalette;
  133. begin
  134.   Result := BitmapImage.Palette;
  135. end;
  136. procedure TVrJoypad.SetPalette(Value: TVrPalette);
  137. begin
  138.   FPalette.Assign(Value);
  139. end;
  140. procedure TVrJoypad.SetSpacing(Value: Integer);
  141. begin
  142.   if (FSpacing <> Value) and (Value >= 0) then
  143.   begin
  144.     FSpacing := Value;
  145.     UpdateControlCanvas;
  146.   end;
  147. end;
  148. procedure TVrJoypad.SetDirections(Value: TVrJoypadDirections);
  149. begin
  150.   if FDirections <> Value then
  151.   begin
  152.     FDirections := Value;
  153.     UpdateControlCanvas;
  154.   end;
  155. end;
  156. procedure TVrJoypad.SetVisibleArrows(Value: TVrVisibleArrows);
  157. begin
  158.   if FVisibleArrows <> Value then
  159.   begin
  160.     FVisibleArrows := Value;
  161.     UpdateControlCanvas;
  162.   end;
  163. end;
  164. procedure TVrJoypad.PaletteModified(Sender: TObject);
  165. begin
  166.   LoadBitmaps;
  167.   UpdateControlCanvas;
  168. end;
  169. procedure TVrJoypad.UpdateLed(Index: TVrVisibleArrow; Active: Boolean);
  170. var
  171.   R, Source: TRect;
  172. begin
  173.   with DestCanvas do
  174.   begin
  175.     GetImageRect(Index, R);
  176.     Source := Bounds(ord(Index) * ImageWidth, 0, ImageWidth, ImageHeight);
  177.     Brush.Style := bsClear;
  178.     BrushCopy(R, Bitmaps[ord(Active)], Source, clBlack);
  179.   end;
  180. end;
  181. procedure TVrJoyPad.UpdateLeds;
  182. var
  183.   I: TVrVisibleArrow;
  184.   Dir: TVrJoypadDirection;
  185. begin
  186.   Dir := jdUp;
  187.   for I := Low(TVrVisibleArrow) to High(TVrVisibleArrow) do
  188.   begin
  189.     if I in VisibleArrows then
  190.       UpdateLed(I, Dir in FDirections);
  191.     Inc(Dir);
  192.   end;
  193. end;
  194. procedure TVrJoypad.Paint;
  195. begin
  196.   ClearBitmapCanvas;
  197.   DestCanvas := BitmapCanvas;
  198.   try
  199.     UpdateLeds;
  200.   finally
  201.     DestCanvas := Self.Canvas;
  202.   end;
  203.   inherited Paint;
  204. end;
  205. procedure TVrJoypad.GetImageRect(Index: TVrVisibleArrow; var R: TRect);
  206. var
  207.   X, Y: Integer;
  208. begin
  209.   X := (Width - ImageWidth) div 2;
  210.   Y := (Height - ImageHeight) div 2;
  211.   case Index of
  212.     vaUp: Y := Y - (ImageHeight div 2) - 3 - FSpacing;
  213.     vaDown: Y := Y + (ImageHeight div 2) + 3 + FSpacing;
  214.     vaLeft: X := X - (ImageWidth div 2) - 3 - FSpacing;
  215.     vaRight: X := X + (ImageWidth div 2) + 3 + FSpacing;
  216.   end;
  217.   R := Bounds(X, Y, ImageWidth, ImageHeight);
  218. end;
  219. end.