Skip to content

Commit

Permalink
Tweak some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Aug 15, 2023
1 parent 38f3cd7 commit b77f9bb
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 63 deletions.
33 changes: 16 additions & 17 deletions Examples/array.simba
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
var
Arr: array of Integer;
Index, Value: Integer;
myArray: array of Int32;
begin
Arr.SetLength(3);
Arr[0] := 200;
Arr[1] := 400;
Arr[2] := 600;

WriteLn('Index of 400 is ', Arr.IndexOf(400));
myArray := [1,2,3,4,5,4,3,2,1];
WriteLn myArray;

Arr := [5,6,7,8];
Arr.Append(100);
// Length of array, or how many items it contains.
WriteLn Length(myArray);

WriteLn('For loop:');
for Index := 0 to Arr.Length() - 1 do
WriteLn('Arr[', Index, '] is ', Arr[Index]);
// Index of last item which is Length(myArray) - 1
WriteLn High(myArray);

WriteLn('For in loop')
for Value in Arr.Reversed() do
WriteLn(Value);
end.
// access first element:
WriteLn myArray[0];

// access second element:
WriteLn myArray[1];

// access last item:
WriteLn myArray[High(myArray)];
end;
15 changes: 15 additions & 0 deletions Examples/cluster_points.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var
TPA: TPointArray;
ATPA: T2DPointArray;
I: Integer;
begin
// Create a random array to test on
TPA := RandomTPA(100, [100,100,300,300]);
TPA := TPA.Grow(3);

// Cluster by 10 distance, if a point is not within 10 distance of any other
// points in a group, add it to a new group.
ATPA := TPA.Cluster(20);

Show(ATPA);
end;
17 changes: 17 additions & 0 deletions Examples/image.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var
Img: TImage;
begin
Img := TImage.Create(500, 500); // Images must explictly created and freed!
Img.DrawBox([100,100,400,400], $0000FF);
Img.DrawLine([50, 50], [450, 50], 5, $00FFFF);
Img.SetPixel(250, 250, $FFFFFF);
Img.SetFontSize(25);
Img.SetFontAntialiasing(False);
Img.DrawText('Hello world', [125, 125], $00FF00);

WriteLn('Color at 250,250 is ', '$' + IntToHex(Img.GetPixel(250, 250)));
WriteLn('Available fonts: ', Img.LoadedFontNames());

Img.Show(); // Show on Simba's debug image
Img.Free(); // Images must be freed
end.
21 changes: 21 additions & 0 deletions Examples/mouse_teleport_event.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var
TPA: TPointArray; // stores the teleport events so we can view at the end

procedure MouseTeleportEvent(var Sender: TInput; X, Y: Integer);
begin
WriteLn('Mouse teleported to: ', X, ', ', Y);
TPA += [X, Y];
end;

var
Event: TMouseTeleportEvent;
begin
Event := Input.AddOnMouseTeleportEvent(@MouseTeleportEvent);
Input.MouseTeleport([200,200]);
Input.MouseMove([600,600]);
Input.RemoveOnMouseTeleportEvent(Event); // Remove the event

Input.MouseMove([200, 600]); // The event has been removed so this wont be "recorded" when we show the path

Show(TPA); // Now we can see what actually happened
end;
21 changes: 0 additions & 21 deletions Examples/procedure.simba

This file was deleted.

14 changes: 7 additions & 7 deletions Examples/timing.simba → Examples/stopwatch.simba
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
program TimingExample;
program StopWatchExample;

procedure SomethingToTime;
var
Expand All @@ -12,17 +12,17 @@ begin
end;

var
Timer: TSimbaTimer;
StopWatch: TStopWatch;
Milliseconds: Integer;
Formatted: String;
begin
Timer.Start();
StopWatch.Start();
SomethingToTime();
Timer.Stop();
StopWatch.Stop();

Milliseconds := Timer.Elapsed();
Formatted := Timer.ElapsedFmt('s');
Milliseconds := StopWatch.Elapsed();
Formatted := StopWatch.ElapsedFmt('s');

WriteLn('Milliseconds: ', Milliseconds);
WriteLN('Seconds: ', Formatted);
end.
end.
13 changes: 7 additions & 6 deletions Source/Simba.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
<DpiAware Value="True/PM"/>
</XPManifest>
<Icon Value="0"/>
<Resources Count="6">
<Resources Count="7">
<Resource_0 FileName="..\Examples\array.simba" Type="RCDATA" ResourceName="EXAMPLE_ARRAY"/>
<Resource_1 FileName="..\Examples\bitmap.simba" Type="RCDATA" ResourceName="EXAMPLE_BITMAP"/>
<Resource_2 FileName="..\Examples\function.simba" Type="RCDATA" ResourceName="EXAMPLE_FUNCTION"/>
<Resource_3 FileName="..\Examples\loop.simba" Type="RCDATA" ResourceName="EXAMPLE_LOOP"/>
<Resource_4 FileName="..\Examples\procedure.simba" Type="RCDATA" ResourceName="EXAMPLE_PROCEDURE"/>
<Resource_5 FileName="..\Examples\timing.simba" Type="RCDATA" ResourceName="EXAMPLE_TIMING"/>
<Resource_1 FileName="..\Examples\function.simba" Type="RCDATA" ResourceName="EXAMPLE_FUNCTION"/>
<Resource_2 FileName="..\Examples\loop.simba" Type="RCDATA" ResourceName="EXAMPLE_LOOP"/>
<Resource_3 FileName="..\Examples\stopwatch.simba" Type="RCDATA" ResourceName="EXAMPLE_STOPWATCH"/>
<Resource_4 FileName="..\Examples\image.simba" Type="RCDATA" ResourceName="EXAMPLE_IMAGE"/>
<Resource_5 FileName="..\Examples\cluster_points.simba" Type="RCDATA" ResourceName="EXAMPLE_CLUSTER_POINTS"/>
<Resource_6 FileName="..\Examples\mouse_teleport_event.simba" Type="RCDATA" ResourceName="EXAMPLE_MOUSE_TELEPORT_EVENT"/>
</Resources>
</General>
<VersionInfo>
Expand Down
Binary file modified Source/Simba.res
Binary file not shown.
3 changes: 3 additions & 0 deletions Source/components/simba.component_treeview.pas
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ TSimbaTreeView = class(TCustomControl)
property ScrolledTop: Integer read GetScrolledTop write SetScrolledTop;
property TopLevelCount: Integer read GetTopLevelCount;

property ScrollbarHorz: TSimbaScrollBar read FScrollbarHorz;
property ScrollbarVert: TSimbaScrollBar read FScrollbarVert;

function AddNode(const NodeText: String; const ImageIndex: Integer = -1): TTreeNode; overload;
function AddNode(const ParentNode: TTreeNode; const NodeText: String; const ImageIndex: Integer = -1): TTreeNode; overload;
end;
Expand Down
4 changes: 2 additions & 2 deletions Source/forms/simba.main.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ object SimbaForm: TSimbaForm
end
object MenuItemCloseTab: TMenuItem
Caption = '&Close Tab'
ImageIndex = 52
ImageIndex = 51
ShortCut = 16471
OnClick = MenuCloseTabClick
end
object MenuItemCloseTabs: TMenuItem
Caption = 'Close All Tabs'
ImageIndex = 53
ImageIndex = 52
OnClick = MenuCloseAllTabsClick
end
object MenuItemDivider6: TMenuItem
Expand Down
2 changes: 1 addition & 1 deletion Source/forms/simba.openexampleform.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object SimbaOpenExampleForm: TSimbaOpenExampleForm
OnCreate = FormCreate
OnShow = FormShow
Position = poMainFormCenter
LCLVersion = '2.2.4.0'
LCLVersion = '3.0.0.1'
object LeftPanel: TPanel
Left = 0
Height = 750
Expand Down
28 changes: 20 additions & 8 deletions Source/forms/simba.openexampleform.pas
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ procedure TSimbaOpenExampleForm.AddPackageExamples;
end;

var
I: Integer;
ParentNode: TTreeNode;
I, MaxWidth: Integer;
Node, ParentNode: TTreeNode;
FileName: String;
Files: TStringArray;
Packages: TSimbaPackageArray;
Expand Down Expand Up @@ -106,6 +106,17 @@ procedure TSimbaOpenExampleForm.AddPackageExamples;
FreePackages(Packages);

TreeView.EndUpdate();

MaxWidth := 0;
Node := TreeView.Items.GetFirstNode();
while Assigned(Node) do
begin
MaxWidth := Max(MaxWidth, Node.DisplayTextRight);

Node := Node.GetNext();
end;

LeftPanel.Width := MaxWidth + TreeView.ScrollbarVert.Width+1;
end;

procedure TSimbaOpenExampleForm.FormShow(Sender: TObject);
Expand Down Expand Up @@ -185,12 +196,13 @@ procedure TSimbaOpenExampleForm.FormCreate(Sender: TObject);

SimbaNode := TExampleNode(TreeView.AddNode('Simba', IMG_PACKAGE));

AddNode('Array', 'EXAMPLE_ARRAY' );
AddNode('Bitmap', 'EXAMPLE_BITMAP' );
AddNode('Function', 'EXAMPLE_FUNCTION' );
AddNode('Loop', 'EXAMPLE_LOOP' );
AddNode('Procedure', 'EXAMPLE_PROCEDURE');
AddNode('Timing', 'EXAMPLE_TIMING' );
AddNode('Array', 'EXAMPLE_ARRAY' );
AddNode('Image', 'EXAMPLE_IMAGE' );
AddNode('Function', 'EXAMPLE_FUNCTION' );
AddNode('Loop', 'EXAMPLE_LOOP' );
AddNode('Stopwatch', 'EXAMPLE_STOPWATCH' );
AddNode('Point Cluster', 'EXAMPLE_CLUSTER_POINTS' );
AddNode('Mouse Teleport Event', 'EXAMPLE_MOUSE_TELEPORT_EVENT');
end;

procedure TSimbaOpenExampleForm.DoTreeViewSelectionChanged(Sender: TObject);
Expand Down
5 changes: 4 additions & 1 deletion Source/script/simba.compiler_dump.pas
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ procedure TSimbaCompilerDump.AddType(Name, Str: String);

procedure TSimbaCompilerDump.AddCode(Str: String);
begin
if not Str.EndsWith(';') then
Str := Str + ';';

Add(ImportingSection, Str);
end;

Expand Down Expand Up @@ -223,7 +226,7 @@ function TSimbaCompilerDump.addGlobalVar(Typ: lpString; Value: Pointer; AName: l
begin
Result := inherited addGlobalVar(Typ, Value, AName);

AddCode('var ' + AName + ': ' + Typ + ';');
AddCode('var ' + AName + ': ' + Typ);
end;

function TSimbaCompilerDump.addGlobalVar(AVar: TLapeGlobalVar; AName: lpString): TLapeGlobalVar;
Expand Down

0 comments on commit b77f9bb

Please sign in to comment.