1
0
Files
applications
bindings
components
Comba_Animation
aboutcomponent
acs
beepfp
callite
captcha
chelper
chemtext
cmdline
cmdlinecfg
colorpalette
cryptini
csvdocument
epiktimer
everettrandom
examplecomponent
exctrls
extrasyn
fpexif
fpsound
fpspreadsheet
fractions
freetypepascal
geckoport
gradcontrols
grid_semaphor
industrialstuff
iosdesigner
iphonelazext
jujiboutils
jvcllaz
kcontrols
lazautoupdate
lazbarcodes
lazmapviewer
lclextensions
longtimer
manualdock
mbColorLib
mplayer
multithreadprocs
nvidia-widgets
onguard
orpheus
playsoundpackage
poweredby
powerpdf
rgbgraphics
richmemo
richview
rtfview
rx
delphi doc file
trunk
version
2.1
v2.6_laz_1.2
Demos
docs
images
languages
lib
RxMDICloseIcon.lrs
WinXP.manifest
WindowsXP.RES
WindowsXP.rc
autopanel.pas
boxprocs.pas
curredit.pas
dateutil.pas
dbcurredit.pas
dbdateedit.pas
dbutils.pas
dcl_rx_ctrl.lpk
dcl_rx_ctrl.pas
duallist.pas
ex_rx_bin_datapacket.pas
ex_rx_datapacket.pas
ex_rx_xml_datapacket.pas
exsortfb.pas
exsortmds.pas
exsortsql.pas
exsortzeos.pas
fduallst.lfm
fduallst.lrs
fduallst.lrt
fduallst.pas
folderlister.pas
pagemngr.pas
pickdate.lrs
pickdate.pas
register_rxctrl.pas
registerrx.pas
registerrxdb.pas
registerrxtools.pas
rx.inc
rx.lrs
rx_lcl.res
rx_sort_fbdataset.lpk
rx_sort_fbdataset.pas
rx_sort_sqldb.lpk
rx_sort_sqldb.pas
rx_sort_zeos.lpk
rx_sort_zeos.pas
rxaboutdialog.pas
rxaboutformunit.lfm
rxaboutformunit.pas
rxappicon.pas
rxapputils.pas
rxceeditlookupfields.pas
rxclock.pas
rxcloseformvalidator.pas
rxconfigvalues.pas
rxconst.pas
rxctrls.pas
rxcustomchartpanel.pas
rxdaterangeeditunit.pas
rxdbcolorbox.pas
rxdbcomb.pas
rxdbctrls.pas
rxdbgrid.lrs
rxdbgrid.pas
rxdbgrid_columsunit.lfm
rxdbgrid_columsunit.lrt
rxdbgrid_columsunit.pas
rxdbgrid_export_spreadsheet.lpk
rxdbgrid_export_spreadsheet.pas
rxdbgrid_findunit.lfm
rxdbgrid_findunit.pas
rxdbgrid_print.lpk
rxdbgrid_print.pas
rxdbgridexportspreadsheet.pas
rxdbgridexportspreadsheet.res
rxdbgridexportspreadsheet_paramsunit.lfm
rxdbgridexportspreadsheet_paramsunit.pas
rxdbgridprintgrid.pas
rxdbgridprintgrid.res
rxdbgridprintgrid_setupunit.lfm
rxdbgridprintgrid_setupunit.pas
rxdbspinedit.pas
rxdbtimeedit.pas
rxdconst.pas
rxdice.inc
rxdice.pas
rxfileutils.pas
rxfilterby.lfm
rxfilterby.lrt
rxfilterby.pas
rxhistory.pas
rxhistorynavigator.pas
rxiconv.pas
rxinipropstorage.pas
rxlogin.lfm
rxlogin.lrs
rxlogin.pas
rxlookup.pas
rxmdi.pas
rxmemds.pas
rxnew.lpk
rxnew.pas
rxpopupunit.pas
rxsortby.lfm
rxsortby.lrt
rxsortby.pas
rxsortfbdataset.pas
rxsortmemds.pas
rxsortsqldb.pas
rxsortzeos.pas
rxspin.lrs
rxspin.pas
rxstrutils.pas
rxswitch.inc
rxswitch.pas
rxsystemservices.pas
rxtbrsetup.lfm
rxtbrsetup.lrt
rxtbrsetup.pas
rxtimeedit.pas
rxtoolbar.pas
rxversinfo.pas
rxviewspanel.pas
rxxpman.pas
seldsfrm.lfm
seldsfrm.lrt
seldsfrm.pas
tooledit.lrs
tooledit.pas
vclutils.pas
scrolltext
smnetgradient
spktoolbar
splashabout
svn
systools
tdi
thtmlport
tparadoxdataset
tvplanit
xdev_toolkit
zlibar
zmsql
examples
image_sources
lclbindings
wst
lazarus-ccr/components/rx/version/v2.6_laz_1.2/rxdice.pas

383 lines
9.9 KiB
ObjectPascal
Raw Normal View History

{ rxdice unit
Copyright (C) 2005-2010 Lagunov Aleksey alexs@yandex.ru and Lazarus team
original conception from rx library for Delphi (c)
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit rxdice;
interface
{$I rx.inc}
uses SysUtils, LCLType, LCLProc, LCLIntf, LMessages, Classes, Graphics,
Controls, Forms, StdCtrls, ExtCtrls, Menus, VCLUtils;
type
TRxDiceValue = 1..6;
{ TRxDice }
TRxDice = class(TCustomControl)
private
{ Private declarations }
FActive: Boolean;
FAutoSize: Boolean;
FBitmap: TBitmap;
FInterval: Cardinal;
FAutoStopInterval: Cardinal;
FOnChange: TNotifyEvent;
FRotate: Boolean;
FShowFocus: Boolean;
FTimer: TTimer;
FTickCount: Longint;
FValue: TRxDiceValue;
FOnStart: TNotifyEvent;
FOnStop: TNotifyEvent;
procedure CMFocusChanged(var Message: TLMessage); message CM_FOCUSCHANGED;
procedure WMSize(var Message: TLMSize); message LM_SIZE;
procedure CreateBitmap;
procedure SetAutoSize(Value: Boolean);
procedure SetInterval(Value: Cardinal);
procedure SetRotate(AValue: Boolean);
procedure SetShowFocus(AValue: Boolean);
procedure SetValue(Value: TRxDiceValue);
procedure TimerExpired(Sender: TObject);
protected
{ Protected declarations }
function GetPalette: HPALETTE; override;
procedure AdjustSize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure Paint; override;
procedure Change; dynamic;
procedure DoStart; dynamic;
procedure DoStop; dynamic;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure RandomValue;
published
{ Published declarations }
property Align;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
property AutoStopInterval: Cardinal read FAutoStopInterval write FAutoStopInterval default 0;
property Color;
property Cursor;
property DragMode;
property DragCursor;
property Enabled;
property Interval: Cardinal read FInterval write SetInterval default 60;
property ParentColor;
property ParentShowHint;
property PopupMenu;
property Rotate: Boolean read FRotate write SetRotate;
property ShowFocus: Boolean read FShowFocus write SetShowFocus;
property ShowHint;
property Anchors;
property Constraints;
property DragKind;
property TabOrder;
property TabStop;
property Value: TRxDiceValue read FValue write SetValue default 1;
property Visible;
property OnClick;
property OnDblClick;
property OnEnter;
property OnExit;
property OnMouseMove;
property OnMouseDown;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnKeyDown;
property OnKeyUp;
property OnKeyPress;
property OnDragOver;
property OnDragDrop;
property OnEndDrag;
property OnStartDrag;
property OnContextPopup;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnStart: TNotifyEvent read FOnStart write FOnStart;
property OnStop: TNotifyEvent read FOnStop write FOnStop;
property OnEndDock;
property OnStartDock;
end;
{$I RXDICE.INC}
implementation
{ TRxDice }
constructor TRxDice.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Randomize;
ControlStyle := [csClickEvents, csSetCaption, csCaptureMouse,
csOpaque, csDoubleClicks];
FValue := 1;
FInterval := 60;
CreateBitmap;
FAutoSize := True;
Width := FBitmap.Width + 2;
Height := FBitmap.Height + 2;
end;
destructor TRxDice.Destroy;
begin
FOnChange := nil;
if FBitmap <> nil then FBitmap.Free;
inherited Destroy;
end;
function TRxDice.GetPalette: HPALETTE;
begin
if FBitmap <> nil then Result := FBitmap.Palette
else Result := 0;
end;
procedure TRxDice.RandomValue;
var
Val: Byte;
begin
Val := Random(6) + 1;
if Val = Byte(FValue) then
begin
if Val = 1 then Inc(Val)
else Dec(Val);
end;
SetValue(TRxDiceValue(Val));
end;
procedure TRxDice.DoStart;
begin
if Assigned(FOnStart) then FOnStart(Self);
end;
procedure TRxDice.DoStop;
begin
if Assigned(FOnStop) then FOnStop(Self);
end;
procedure TRxDice.CMFocusChanged(var Message: TLMessage);
var
Active: Boolean;
begin
{ with Message do Active := (Sender = Self);
if Active <> FActive then begin
FActive := Active;
if FShowFocus then Invalidate;
end;}
inherited;
end;
procedure TRxDice.WMSize(var Message: TLMSize);
begin
inherited;
AdjustSize;
end;
procedure TRxDice.CreateBitmap;
begin
if FBitmap = nil then FBitmap := TBitmap.Create;
case FValue of
1:FBitmap.Handle := CreatePixmapIndirect(@DICE1[0], GetSysColor(COLOR_BTNFACE));
2:FBitmap.Handle := CreatePixmapIndirect(@DICE2[0], GetSysColor(COLOR_BTNFACE));
3:FBitmap.Handle := CreatePixmapIndirect(@DICE3[0], GetSysColor(COLOR_BTNFACE));
4:FBitmap.Handle := CreatePixmapIndirect(@DICE4[0], GetSysColor(COLOR_BTNFACE));
5:FBitmap.Handle := CreatePixmapIndirect(@DICE5[0], GetSysColor(COLOR_BTNFACE));
6:FBitmap.Handle := CreatePixmapIndirect(@DICE6[0], GetSysColor(COLOR_BTNFACE));
end;
end;
procedure TRxDice.AdjustSize;
var
MinSide: Integer;
begin
if not (csReading in ComponentState) then
begin
if AutoSize and Assigned(FBitmap) and (FBitmap.Width > 0) and
(FBitmap.Height > 0) then
SetBounds(Left, Top, FBitmap.Width + 2, FBitmap.Height + 2)
else
begin
{ Adjust aspect ratio if control size changed }
MinSide := Width;
if Height < Width then MinSide := Height;
SetBounds(Left, Top, MinSide, MinSide);
end;
end;
end;
procedure TRxDice.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Button = mbLeft) and TabStop and CanFocus then SetFocus;
inherited MouseDown(Button, Shift, X, Y);
end;
procedure TRxDice.Paint;
var
ARect: TRect;
procedure DrawBitmap;
var
TmpImage: TBitmap;
IWidth, IHeight: Integer;
IRect: TRect;
begin
IWidth := FBitmap.Width;
IHeight := FBitmap.Height;
IRect := Rect(0, 0, IWidth, IHeight);
TmpImage := TBitmap.Create;
try
TmpImage.Width := IWidth;
TmpImage.Height := IHeight;
TmpImage.Canvas.Brush.Color := Self.Brush.Color;
// TmpImage.Canvas.BrushCopy(IRect, FBitmap, IRect, FBitmap.TransparentColor);
InflateRect(ARect, -1, -1);
// Canvas.StretchDraw(ARect, TmpImage);
Canvas.StretchDraw(ARect, FBitmap);
finally
TmpImage.Free;
end;
end;
begin
ARect := ClientRect;
if FBitmap <> nil then DrawBitmap;
{ if Focused and FShowFocus and TabStop and not (csDesigning in ComponentState) then
begin
Canvas.DrawFocusRect(ARect);
end;}
end;
procedure TRxDice.TimerExpired(Sender: TObject);
var
ParentForm: TCustomForm;
Now: Longint;
begin
RandomValue;
if not FRotate then
begin
FTimer.Free;
FTimer := nil;
if (csDesigning in ComponentState) then
begin
ParentForm := GetParentForm(Self);
if ParentForm <> nil then ParentForm.Designer.Modified;
end;
DoStop;
end
else
if AutoStopInterval > 0 then
begin
Now := GetTickCount;
if (Now - FTickCount >= AutoStopInterval) or (Now < FTickCount) then
Rotate := False;
end;
end;
procedure TRxDice.Change;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure TRxDice.SetValue(Value: TRxDiceValue);
begin
if FValue <> Value then
begin
FValue := Value;
CreateBitmap;
Invalidate;
Change;
end;
end;
procedure TRxDice.SetAutoSize(Value: Boolean);
begin
if Value <> FAutoSize then
begin
FAutoSize := Value;
AdjustSize;
Invalidate;
end;
end;
procedure TRxDice.SetInterval(Value: Cardinal);
begin
if FInterval <> Value then
begin
FInterval := Value;
if FTimer <> nil then FTimer.Interval := FInterval;
end;
end;
procedure TRxDice.SetRotate(AValue: Boolean);
begin
if FRotate <> AValue then
begin
if AValue then
begin
if FTimer = nil then FTimer := TTimer.Create(Self);
try
with FTimer do
begin
OnTimer := @TimerExpired;
Interval := FInterval;
Enabled := True;
end;
FRotate := AValue;
FTickCount := GetTickCount;
DoStart;
except
FTimer.Free;
FTimer := nil;
raise;
end;
end
else
FRotate := AValue;
end;
end;
procedure TRxDice.SetShowFocus(AValue: Boolean);
begin
if FShowFocus <> AValue then
begin
FShowFocus := AValue;
if not (csDesigning in ComponentState) then Invalidate;
end;
end;
end.