Skip to content

Commit

Permalink
Shapebox: Add copy button
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 6, 2023
1 parent 38901f9 commit 2560876
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 10 deletions.
19 changes: 10 additions & 9 deletions Source/forms/simba.main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,15 @@ implementation
simba.ide_mainstatusbar, simba.ide_mainmenubar, simba.ide_maintoolbar,

simba.shapeboxform, simba.openexampleform, simba.colorpickerhistoryform,
simba.debugimageform, simba.imagetostringform, simba.aboutform, simba.findinfilesform,
simba.outputform, simba.filebrowserform, simba.notesform, simba.settingsform,
simba.functionlistform, simba.scripttab, simba.scripttabsform,
simba.associate, simba.aca, simba.dtmeditor, simba.env,
simba.dockinghelpers, simba.nativeinterface,
simba.scriptformatter, simba.theme,
simba.scriptbackup, simba.backupsform, simba.threading,
simba.downloadsimbaform, simba.editor;
simba.debugimageform, simba.imagetostringform, simba.aboutform,
simba.findinfilesform, simba.outputform, simba.filebrowserform,
simba.notesform, simba.settingsform, simba.scripttabsform,
simba.functionlistform, simba.downloadsimbaform, simba.backupsform,

simba.scripttab, simba.associate,
simba.aca, simba.dtmeditor, simba.env, simba.dockinghelpers, simba.nativeinterface,
simba.scriptformatter, simba.theme, simba.scriptbackup,
simba.threading, simba.editor, simba.component_menubar;

procedure TSimbaForm.HandleException(Sender: TObject; E: Exception);

Expand Down Expand Up @@ -596,7 +597,7 @@ procedure TSimbaForm.DoApplicationKeyDown(Sender: TObject; var Key: Word; Shift:
var
Msg: TLMKey;
begin
if (Screen.ActiveControl is TSimbaEditor) then
if (Screen.ActiveControl is TSimbaEditor) or (Screen.ActiveControl is TSimbaMenuBar) then
begin
Msg := Default(TLMKey);
Msg.CharCode := Key;
Expand Down
12 changes: 12 additions & 0 deletions Source/script/imports/simba.import_shapebox.pas
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ procedure _LapeSimbaShapeBox_NameButton(const Params: PParamArray; const Result:
PButton(Result)^ := PSimbaShapeBox(Params^[0])^.NameButton;
end;

procedure _LapeSimbaShapeBox_CopyButton(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PButton(Result)^ := PSimbaShapeBox(Params^[0])^.CopyButton;
end;

procedure _LapeSimbaShapeBox_DeleteButton(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PButton(Result)^ := PSimbaShapeBox(Params^[0])^.DeleteButton;
Expand Down Expand Up @@ -156,6 +161,11 @@ procedure _LapeSimbaShapeBox_QueryNameOnNew_WRite(const Params: PParamArray); LA
PSimbaShapeBox(Params^[0])^.QueryNameOnNew := PBoolean(Params^[1])^;
end;

procedure _LapeSimbaShapeBox_CopyShape(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PInteger(Result)^ := PSimbaShapeBox(Params^[0])^.CopyShape(PInteger(Params^[1])^);
end;

procedure _LapeSimbaShapeBox_DeleteShape(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
begin
PSimbaShapeBox(Params^[0])^.DeleteShape(PInteger(Params^[1])^);
Expand Down Expand Up @@ -199,6 +209,7 @@ procedure ImportSimbaShapeBox(Compiler: TSimbaScript_Compiler);
addClassVar('TShapeBox', 'UserDataSize', 'Integer', @_LapeSimbaShapeBox_UserDataSize_Read, @_LapeSimbaShapeBox_UserDataSize_Write);
addClassVar('TShapeBox', 'QueryNameOnNew', 'Boolean', @_LapeSimbaShapeBox_QueryNameOnNew_Read, @_LapeSimbaShapeBox_QueryNameOnNew_Write);

addGlobalFunc('function TShapeBox.CopyShape(Index: Integer): Integer;', @_LapeSimbaShapeBox_CopyShape);
addGlobalFunc('procedure TShapeBox.DeleteShape(Index: Integer);', @_LapeSimbaShapeBox_DeleteShape);
addGlobalFunc('procedure TShapeBox.DeleteAllShapes;', @_LapeSimbaShapeBox_DeleteAllShapes);

Expand All @@ -211,6 +222,7 @@ procedure ImportSimbaShapeBox(Compiler: TSimbaScript_Compiler);
addGlobalFunc('function TShapeBox.PathButton: TLazButton;', @_LapeSimbaShapeBox_PathButton);
addGlobalFunc('function TShapeBox.PrintButton: TLazButton;', @_LapeSimbaShapeBox_PrintButton);
addGlobalFunc('function TShapeBox.NameButton: TLazButton;', @_LapeSimbaShapeBox_NameButton);
addGlobalFunc('function TShapeBox.CopyButton: TLazButton;', @_LapeSimbaShapeBox_CopyButton);
addGlobalFunc('function TShapeBox.DeleteButton: TLazButton;', @_LapeSimbaShapeBox_DeleteButton);
addGlobalFunc('function TShapeBox.DeleteAllButton: TLazButton;', @_LapeSimbaShapeBox_DeleteAllButton);

Expand Down
82 changes: 81 additions & 1 deletion Source/simba.shapebox.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TSimbaShapeBoxShape = class
function GetLineColor(const Flags: EPaintShapeFlags): TColor; inline;
function GetConnectorColor(const Flags: EPaintShapeFlags): TColor; inline;
public
FShapeBox: TSimbaShapeBox;
FDragStart: TPoint;
FName: String;
FUserData: Pointer;
Expand All @@ -51,6 +52,8 @@ TSimbaShapeBoxShape = class

function ToStr: String; virtual; abstract;
procedure FromStr(Str: String); virtual; abstract;

function CreateCopy: TSimbaShapeBoxShape; virtual;
end;

TSimbaShapeBoxShape_Point = class(TSimbaShapeBoxShape)
Expand Down Expand Up @@ -78,6 +81,8 @@ TSimbaShapeBoxShape_Point = class(TSimbaShapeBoxShape)

function ToStr: String; override;
procedure FromStr(Str: String); override;

function CreateCopy: TSimbaShapeBoxShape; override;
end;

TSimbaShapeBoxShape_Box = class(TSimbaShapeBoxShape)
Expand Down Expand Up @@ -110,6 +115,8 @@ TSimbaShapeBoxShape_Box = class(TSimbaShapeBoxShape)

function ToStr: String; override;
procedure FromStr(Str: String); override;

function CreateCopy: TSimbaShapeBoxShape; override;
end;

TSimbaShapeBoxShape_Poly = class(TSimbaShapeBoxShape)
Expand Down Expand Up @@ -140,6 +147,8 @@ TSimbaShapeBoxShape_Poly = class(TSimbaShapeBoxShape)

function ToStr: String; override;
procedure FromStr(Str: String); override;

function CreateCopy: TSimbaShapeBoxShape; override;
end;

TSimbaShapeBoxShape_Path = class(TSimbaShapeBoxShape_Poly)
Expand Down Expand Up @@ -189,6 +198,7 @@ TSimbaShapeBox = class(TSimbaImageBox)
FDeleteButton: TButton;
FDeleteAllButton: TButton;
FPrintButton: TButton;
FCopyButton: TButton;

FUserDataSize: Integer;
FQueryNameOnNew: Boolean;
Expand Down Expand Up @@ -222,6 +232,7 @@ TSimbaShapeBox = class(TSimbaImageBox)
procedure DoShapeDeleteAllClick(Sender: TObject);
procedure DoShapeNameClick(Sender: TObject);
procedure DoShapePrintClick(Sender: TObject);
procedure DoShapeCopyClick(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Expand All @@ -231,6 +242,8 @@ TSimbaShapeBox = class(TSimbaImageBox)
procedure SaveToFile(FileName: String);
procedure LoadFromFile(FileName: String);

function CopyShape(Index: Integer): Integer;

procedure DeleteShape(Index: Integer);
procedure DeleteAllShapes;

Expand All @@ -255,6 +268,7 @@ TSimbaShapeBox = class(TSimbaImageBox)
property DeleteButton: TButton read FDeleteButton;
property DeleteAllButton: TButton read FDeleteAllButton;
property PrintButton: TButton read FPrintButton;
property CopyButton: TButton read FCopyButton;

property ShapeIndex: Integer read GetShapeIndex;
property ShapeCount: Integer read GetShapeCount;
Expand All @@ -273,6 +287,7 @@ constructor TSimbaShapeBoxShape.Create(ShapeBox: TSimbaShapeBox);
begin
inherited Create();

FShapeBox := ShapeBox;
FShapeType := String(ClassName).After('_');
FName := FShapeType;
FUserData := AllocMem(ShapeBox.UserDataSize);
Expand All @@ -286,6 +301,12 @@ destructor TSimbaShapeBoxShape.Destroy;
inherited Destroy();
end;

function TSimbaShapeBoxShape.CreateCopy: TSimbaShapeBoxShape;
begin
Result := TSimbaShapeBoxShapeClass(Self.ClassType).Create(FShapeBox);
Result.FName := FName;
end;

function TSimbaShapeBoxShape.GetLineColor(const Flags: EPaintShapeFlags): TColor;
begin
if (EPaintShapeFlag.SELECTED in Flags) or (EPaintShapeFlag.SELECTING in Flags) then
Expand Down Expand Up @@ -382,6 +403,13 @@ procedure TSimbaShapeBoxShape_Point.FromStr(Str: String);
SScanf(Str, '%d,%d', [@FPoint.X, @FPoint.Y]);
end;

function TSimbaShapeBoxShape_Point.CreateCopy: TSimbaShapeBoxShape;
begin
Result := inherited CreateCopy();

TSimbaShapeBoxShape_Point(Result).FPoint := FPoint;
end;

function TSimbaShapeBoxShape_Path.DistToEdge(P: TPoint): Integer;
var
i: Integer;
Expand Down Expand Up @@ -588,6 +616,13 @@ procedure TSimbaShapeBoxShape_Poly.FromStr(Str: String);
SScanf(Elements[I], '%d,%d', [@FPoly[I].X, @FPoly[I].Y]);
end;

function TSimbaShapeBoxShape_Poly.CreateCopy: TSimbaShapeBoxShape;
begin
Result := inherited CreateCopy();

TSimbaShapeBoxShape_Poly(Result).FPoly := Copy(FPoly);
end;

function TSimbaShapeBoxShape_Box.GetBox: TBox;
begin
Result := FBox;
Expand All @@ -604,6 +639,13 @@ procedure TSimbaShapeBoxShape_Box.FromStr(Str: String);
SScanf(Str, '%d,%d,%d,%d', [@FBox.X1, @FBox.Y1, @FBox.X2, @FBox.Y2]);
end;

function TSimbaShapeBoxShape_Box.CreateCopy: TSimbaShapeBoxShape;
begin
Result := inherited CreateCopy();

TSimbaShapeBoxShape_Box(Result).FBox := FBox;
end;

constructor TSimbaShapeBoxShape_Box.Create(ShapeBox: TSimbaShapeBox);
begin
inherited Create(ShapeBox);
Expand Down Expand Up @@ -853,6 +895,11 @@ procedure TSimbaShapeBox.DoShapePrintClick(Sender: TObject);
SimbaDebugLn([EDebugLn.FOCUS], FShapes[FListBox.ItemIndex].ToStr);
end;

procedure TSimbaShapeBox.DoShapeCopyClick(Sender: TObject);
begin
CopyShape(FListBox.ItemIndex);
end;

function TSimbaShapeBox.GetShapeAt(P: TPoint): TSimbaShapeBoxShape;
var
I: Integer;
Expand All @@ -861,7 +908,7 @@ function TSimbaShapeBox.GetShapeAt(P: TPoint): TSimbaShapeBoxShape;
Result := nil;

BestDist := Integer.MaxValue;
for I := 0 to FShapes.Count - 1 do
for I := FShapes.Count - 1 downto 0 do
begin
Dist := FShapes[I].DistToEdge(P);

Expand Down Expand Up @@ -1222,6 +1269,28 @@ procedure TSimbaShapeBox.LoadFromFile(FileName: String);
end;
end;

function TSimbaShapeBox.CopyShape(Index: Integer): Integer;
var
NewName: String;
begin
Result := -1;

if CheckIndex(Index) then
begin
NewName := FShapes[Index].FName;
if FQueryNameOnNew and (not InputQuery('Simba', 'Enter name', NewName)) then
Exit;
InternalAddShape(FShapes[Index].CreateCopy());
if FQueryNameOnNew then
InternalNameShape(FListBox.ItemIndex, NewName);

if (FUserDataSize > 0) then
Move(FShapes[Index].FUserData^, FShapes[FListBox.ItemIndex].FUserData^, FUserDataSize);

Result := FListBox.ItemIndex;
end;
end;

procedure TSimbaShapeBox.DeleteShape(Index: Integer);
begin
InternalDeleteShape(Index);
Expand Down Expand Up @@ -1294,6 +1363,17 @@ constructor TSimbaShapeBox.Create(AOwner: TComponent);
BorderSpacing.Around := 5;
end;

FCopyButton := TButton.Create(FPanel);
with FCopyButton do
begin
Parent := FPanel;
Align := alBottom;
Caption := 'Copy Shape';
OnClick := @DoShapeCopyClick;
AutoSize := True;
BorderSpacing.Around := 5;
end;

FPrintButton := TButton.Create(FPanel);
with FPrintButton do
begin
Expand Down

0 comments on commit 2560876

Please sign in to comment.