home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d6 / RX275D6.ZIP / Units / RXDICE.PAS < prev    next >
Pascal/Delphi Source File  |  1999-10-12  |  9KB  |  358 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 1995 AO ROSNO                   }
  6. {         Copyright (c) 1997, 1998 Master-Bank          }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit RXDice;
  11.  
  12. interface
  13.  
  14. {$I RX.INC}
  15.  
  16. uses SysUtils, {$IFDEF WIN32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  17.   Classes, Graphics, Messages, Controls, Forms, StdCtrls, ExtCtrls, Menus,
  18.   RxTimer, VCLUtils;
  19.  
  20. type
  21.   TRxDiceValue = 1..6;
  22.  
  23. { TRxDice }
  24.  
  25.   TRxDice = class(TCustomControl)
  26.   private
  27.     { Private declarations }
  28.     FActive: Boolean;
  29.     FAutoSize: Boolean;
  30.     FBitmap: TBitmap;
  31.     FInterval: Cardinal;
  32.     FAutoStopInterval: Cardinal;
  33.     FOnChange: TNotifyEvent;
  34.     FRotate: Boolean;
  35.     FShowFocus: Boolean;
  36.     FTimer: TRxTimer;
  37.     FTickCount: Longint;
  38.     FValue: TRxDiceValue;
  39.     FOnStart: TNotifyEvent;
  40.     FOnStop: TNotifyEvent;
  41.     procedure CMFocusChanged(var Message: TCMFocusChanged); message CM_FOCUSCHANGED;
  42.     procedure WMSize(var Message: TWMSize); message WM_SIZE;
  43.     procedure CreateBitmap;
  44.     procedure SetAutoSize(Value: Boolean);
  45.     procedure SetInterval(Value: Cardinal);
  46.     procedure SetRotate(Value: Boolean);
  47.     procedure SetShowFocus(Value: Boolean);
  48.     procedure SetValue(Value: TRxDiceValue);
  49.     procedure TimerExpired(Sender: TObject);
  50.   protected
  51.     { Protected declarations }
  52.     function GetPalette: HPALETTE; override;
  53.     procedure AdjustSize; {$IFDEF RX_D4} override; {$ENDIF}
  54.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  55.       X, Y: Integer); override;
  56.     procedure Paint; override;
  57.     procedure Change; dynamic;
  58.     procedure DoStart; dynamic;
  59.     procedure DoStop; dynamic;
  60.   public
  61.     { Public declarations }
  62.     constructor Create(AOwner: TComponent); override;
  63.     destructor Destroy; override;
  64.     procedure RandomValue;
  65.   published
  66.     { Published declarations }
  67.     property Align;
  68.     property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
  69.     property AutoStopInterval: Cardinal read FAutoStopInterval write FAutoStopInterval default 0;
  70.     property Color;
  71.     property Cursor;
  72.     property DragMode;
  73.     property DragCursor;
  74.     property Enabled;
  75.     property Interval: Cardinal read FInterval write SetInterval default 60;
  76.     property ParentColor;
  77.     property ParentShowHint;
  78.     property PopupMenu;
  79.     property Rotate: Boolean read FRotate write SetRotate;
  80.     property ShowFocus: Boolean read FShowFocus write SetShowFocus;
  81.     property ShowHint;
  82. {$IFDEF RX_D4}
  83.     property Anchors;
  84.     property Constraints;
  85.     property DragKind;
  86. {$ENDIF}
  87.     property TabOrder;
  88.     property TabStop;
  89.     property Value: TRxDiceValue read FValue write SetValue default 1;
  90.     property Visible;
  91.     property OnClick;
  92.     property OnDblClick;
  93.     property OnEnter;
  94.     property OnExit;
  95.     property OnMouseMove;
  96.     property OnMouseDown;
  97.     property OnMouseUp;
  98.     property OnKeyDown;
  99.     property OnKeyUp;
  100.     property OnKeyPress;
  101.     property OnDragOver;
  102.     property OnDragDrop;
  103.     property OnEndDrag;
  104. {$IFDEF WIN32}
  105.     property OnStartDrag;
  106. {$ENDIF}
  107. {$IFDEF RX_D5}
  108.     property OnContextPopup;
  109. {$ENDIF}
  110.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  111.     property OnStart: TNotifyEvent read FOnStart write FOnStart;
  112.     property OnStop: TNotifyEvent read FOnStop write FOnStop;
  113. {$IFDEF RX_D4}
  114.     property OnEndDock;
  115.     property OnStartDock;
  116. {$ENDIF}
  117.   end;
  118.  
  119. implementation
  120.  
  121. {$IFDEF WIN32}
  122.  {$R *.R32}
  123. {$ELSE}
  124.  {$R *.R16}
  125. {$ENDIF}
  126.  
  127. const
  128.   ResName: array [TRxDiceValue] of PChar =
  129.    ('DICE1', 'DICE2', 'DICE3', 'DICE4', 'DICE5', 'DICE6');
  130.  
  131. { TRxDice }
  132.  
  133. constructor TRxDice.Create(AOwner: TComponent);
  134. begin
  135.   inherited Create(AOwner);
  136.   Randomize;
  137.   ControlStyle := [csClickEvents, csSetCaption, csCaptureMouse,
  138.     csOpaque, csDoubleClicks];
  139.   FValue := 1;
  140.   FInterval := 60;
  141.   CreateBitmap;
  142.   FAutoSize := True;
  143.   Width := FBitmap.Width + 2;
  144.   Height := FBitmap.Height + 2;
  145. end;
  146.  
  147. destructor TRxDice.Destroy;
  148. begin
  149.   FOnChange := nil;
  150.   if FBitmap <> nil then FBitmap.Free;
  151.   inherited Destroy;
  152. end;
  153.  
  154. function TRxDice.GetPalette: HPALETTE;
  155. begin
  156.   if FBitmap <> nil then Result := FBitmap.Palette
  157.   else Result := 0;
  158. end;
  159.  
  160. procedure TRxDice.RandomValue;
  161. var
  162.   Val: Byte;
  163. begin
  164.   Val := Random(6) + 1;
  165.   if Val = Byte(FValue) then begin
  166.     if Val = 1 then Inc(Val)
  167.     else Dec(Val);
  168.   end;
  169.   SetValue(TRxDiceValue(Val));
  170. end;
  171.  
  172. procedure TRxDice.DoStart;
  173. begin
  174.   if Assigned(FOnStart) then FOnStart(Self);
  175. end;
  176.  
  177. procedure TRxDice.DoStop;
  178. begin
  179.   if Assigned(FOnStop) then FOnStop(Self);
  180. end;
  181.  
  182. procedure TRxDice.CMFocusChanged(var Message: TCMFocusChanged);
  183. var
  184.   Active: Boolean;
  185. begin
  186.   with Message do Active := (Sender = Self);
  187.   if Active <> FActive then begin
  188.     FActive := Active;
  189.     if FShowFocus then Invalidate;
  190.   end;
  191.   inherited;
  192. end;
  193.  
  194. procedure TRxDice.WMSize(var Message: TWMSize);
  195. begin
  196.   inherited;
  197. {$IFNDEF RX_D4}
  198.   AdjustSize;
  199. {$ENDIF}
  200. end;
  201.  
  202. procedure TRxDice.CreateBitmap;
  203. begin
  204.   if FBitmap = nil then FBitmap := TBitmap.Create;
  205.   FBitmap.Handle := LoadBitmap(HInstance, ResName[FValue]);
  206. end;
  207.  
  208. procedure TRxDice.AdjustSize;
  209. var
  210.   MinSide: Integer;
  211. begin
  212.   if not (csReading in ComponentState) then begin
  213.     if AutoSize and Assigned(FBitmap) and (FBitmap.Width > 0) and
  214.       (FBitmap.Height > 0) then
  215.         SetBounds(Left, Top, FBitmap.Width + 2, FBitmap.Height + 2)
  216.     else begin
  217.       { Adjust aspect ratio if control size changed }
  218.       MinSide := Width;
  219.       if Height < Width then MinSide := Height;
  220.       SetBounds(Left, Top, MinSide, MinSide);
  221.     end;
  222.   end;
  223. end;
  224.  
  225. procedure TRxDice.MouseDown(Button: TMouseButton;
  226.   Shift: TShiftState; X, Y: Integer);
  227. begin
  228.   if (Button = mbLeft) and TabStop and CanFocus then SetFocus;
  229.   inherited MouseDown(Button, Shift, X, Y);
  230. end;
  231.  
  232. procedure TRxDice.Paint;
  233. var
  234.   ARect: TRect;
  235.  
  236.   procedure DrawBitmap;
  237.   var
  238.     TmpImage: TBitmap;
  239.     IWidth, IHeight: Integer;
  240.     IRect: TRect;
  241.   begin
  242.     IWidth := FBitmap.Width;
  243.     IHeight := FBitmap.Height;
  244.     IRect := Rect(0, 0, IWidth, IHeight);
  245.     TmpImage := TBitmap.Create;
  246.     try
  247.       TmpImage.Width := IWidth;
  248.       TmpImage.Height := IHeight;
  249.       TmpImage.Canvas.Brush.Color := Self.Brush.Color;
  250.       TmpImage.Canvas.BrushCopy(IRect, FBitmap, IRect, FBitmap.TransparentColor);
  251.       InflateRect(ARect, -1, -1);
  252.       Canvas.StretchDraw(ARect, TmpImage);
  253.     finally
  254.       TmpImage.Free;
  255.     end;
  256.   end;
  257.  
  258. begin
  259.   ARect := ClientRect;
  260.   if FBitmap <> nil then DrawBitmap;
  261.   if Focused and FShowFocus and TabStop and not (csDesigning in ComponentState) then
  262.   begin
  263.     Canvas.DrawFocusRect(ARect);
  264.   end;
  265. end;
  266.  
  267. procedure TRxDice.TimerExpired(Sender: TObject);
  268. var
  269.   ParentForm: TCustomForm;
  270.   Now: Longint;
  271. begin
  272.   RandomValue;
  273.   if not FRotate then begin
  274.     FTimer.Free;
  275.     FTimer := nil;
  276.     if (csDesigning in ComponentState) then begin
  277.       ParentForm := GetParentForm(Self);
  278.       if ParentForm <> nil then ParentForm.Designer.Modified;
  279.     end;
  280.     DoStop;
  281.   end
  282.   else if AutoStopInterval > 0 then begin
  283.     Now := GetTickCount;
  284. {$IFDEF RX_D4}
  285.     if (Now - FTickCount >= Integer(AutoStopInterval))
  286. {$ELSE}
  287.     if (Now - FTickCount >= AutoStopInterval)
  288. {$ENDIF}
  289.       or (Now < FTickCount) then Rotate := False;
  290.   end;
  291. end;
  292.  
  293. procedure TRxDice.Change;
  294. begin
  295.   if Assigned(FOnChange) then FOnChange(Self);
  296. end;
  297.  
  298. procedure TRxDice.SetValue(Value: TRxDiceValue);
  299. begin
  300.   if FValue <> Value then begin
  301.     FValue := Value;
  302.     CreateBitmap;
  303.     Invalidate;
  304.     Change;
  305.   end;
  306. end;
  307.  
  308. procedure TRxDice.SetAutoSize(Value: Boolean);
  309. begin
  310.   if Value <> FAutoSize then begin
  311.     FAutoSize := Value;
  312.     AdjustSize;
  313.     Invalidate;
  314.   end;
  315. end;
  316.  
  317. procedure TRxDice.SetInterval(Value: Cardinal);
  318. begin
  319.   if FInterval <> Value then begin
  320.     FInterval := Value;
  321.     if FTimer <> nil then FTimer.Interval := FInterval;
  322.   end;
  323. end;
  324.  
  325. procedure TRxDice.SetRotate(Value: Boolean);
  326. begin
  327.   if FRotate <> Value then begin
  328.     if Value then begin
  329.       if FTimer = nil then FTimer := TRxTimer.Create(Self);
  330.       try
  331.         with FTimer do begin
  332.           OnTimer := TimerExpired;
  333.           Interval := FInterval;
  334.           Enabled := True;
  335.         end;
  336.         FRotate := Value;
  337.         FTickCount := GetTickCount;
  338.         DoStart;
  339.       except
  340.         FTimer.Free;
  341.         FTimer := nil;
  342.         raise;
  343.       end;
  344.     end
  345.     else FRotate := Value;
  346.   end;
  347. end;
  348.  
  349. procedure TRxDice.SetShowFocus(Value: Boolean);
  350. begin
  351.   if FShowFocus <> Value then begin
  352.     FShowFocus := Value;
  353.     if not (csDesigning in ComponentState) then Invalidate;
  354.   end;
  355. end;
  356.  
  357. end.
  358.