Skip to content

Commit

Permalink
feat(webber): added downscalling option (read notes)
Browse files Browse the repository at this point in the history
- The world map WaspLib has is too big and causes problems with some of the developer tools.
This will probably have to be changed someday but for now, this gives you the option to use webber with good performance with a tiny bit loss of accuracy and a pretty large loss in visual quality.
the scaling can be toggled with the `SCALE` boolean contant at the top and if used, I would recommend once you are done, turn it off and see how the changes appear while not scaled down.
- added webgraph stuff from Student
  • Loading branch information
Torwent committed Jan 17, 2024
1 parent 3d26809 commit c893424
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 59 deletions.
8 changes: 4 additions & 4 deletions osr/walker/waspweb.graph

Large diffs are not rendered by default.

193 changes: 138 additions & 55 deletions tools/webber.simba
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const OFFSET_POINT
Optional offset for the webgraph. Can be handy.
*)
OFFSET_POINT: TPoint = [0, 0];
SCALE: Boolean = False;

type
TWebberForm = record
Expand Down Expand Up @@ -75,6 +76,12 @@ var
best, bestIndex: Int32;
off: TPoint;
begin
if SCALE then
begin
X := X*4;
Y := Y*4;
end;

if Self.Dragging and Self.DragMode then
begin
if Self.DraggingBox then
Expand Down Expand Up @@ -135,6 +142,7 @@ procedure TWebberForm.AddNode(p: TPoint);
var
c: Int32;
begin

if (SelectedNode > -1) and (Graph.InvalidConnection(p,Graph.Nodes[Self.SelectedNode])) then
begin
WriteLn('Error: Path crosses another path');
Expand All @@ -155,67 +163,90 @@ begin
end;


procedure TWebberForm.OnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Int32);
procedure TWebberForm.OnMouseDown(Sender: TObject; button: TMouseButton; Shift: TShiftState; X, Y: Int32);
var
I: Int32;
T: UInt64;
i, n: Int32;
t: UInt64;
p: TPoint;
begin
if Button = mbLeft then
if button <> mbLeft then
Exit;

if SCALE then
begin
if Self.DragMode then
X := X*4;
Y := Y*4;
n := 6;
end
else
n := 3;

if Self.DragMode then
begin
Self.Dragging := True;
if Self.BoxIsValid() and Self.SelectBox.Contains([X, Y]) then
begin
Self.Dragging := True;
if Self.BoxIsValid() and Self.SelectBox.Contains([X, Y]) then
begin
Self.DraggingBox := True;
Self.ClickPoint := [X, Y];
Exit;
end;
Self.DraggingBox := True;
Self.ClickPoint := [X, Y];
Exit;
end;

Self.SelectBox.X1 := X;
Self.SelectBox.Y1 := Y;
Self.SelectBox.X2 := X;
Self.SelectBox.Y2 := Y;
Self.SelectBox.X1 := X;
Self.SelectBox.Y1 := Y;
Self.SelectBox.X2 := X;
Self.SelectBox.Y2 := Y;

Self.ImageBox.Update();
Self.ImageBox.Update();
Exit;
end;

if (ssShift in Shift) then
begin
if Self.SelectedNode = -1 then
Exit;
end;

if (ssShift in Shift) then
for i := 0 to High(Self.Graph.Nodes) do
begin
if Self.SelectedNode = -1 then
p := Self.Graph.Nodes[i];
if Distance(p.X, p.Y, X, Y) <= n then
begin
if Self.SelectedNode <> i then
Self.ConnectNodes(Self.SelectedNode, i);
Self.ImageBox.Update();
Exit;
end;
end;

for I := 0 to High(Self.Graph.Nodes) do
if Distance(Self.Graph.Nodes[I].X, Self.Graph.Nodes[I].Y, X, Y) <= 3 then
begin
if Self.SelectedNode <> I then
Self.ConnectNodes(self.SelectedNode, I);
Self.ImageBox.Update();
Exit;
end;
end else
begin
Self.Dragging := True;
Exit;
end;

for I := 0 to High(Self.Graph.Nodes) do
if Distance(Self.Graph.Nodes[I].X, Self.Graph.Nodes[I].Y, X, Y) <= 3 then
begin
Self.SelectedNode := I;
Self.ImageBox.GetStatusPanel().SetText(Self.Graph.Names[I]);
Self.ImageBox.Update();
Exit;
end;
Self.Dragging := True;

AddNode([X, Y]);
for i := 0 to High(Self.Graph.Nodes) do
begin
p := Self.Graph.Nodes[i];
if Distance(p.X, p.Y, X, Y) <= n then
begin
Self.SelectedNode := i;
Self.ImageBox.GetStatusPanel().SetText(Self.Graph.Names[i]);
Self.ImageBox.Update();
Exit;
end;
end;

Self.AddNode([X, Y]);
end;

procedure TWebberForm.OnMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Int32);
procedure TWebberForm.OnMouseUp(Sender: TObject; button: TMouseButton; Shift: TShiftState; X, Y: Int32);
begin
if Button = mbLeft then
if button = mbLeft then
begin
if SCALE then
begin
X := X*4;
Y := Y*4;
end;

Self.Dragging := False;
Self.DraggingBox := False;
if Self.DragMode then
Expand Down Expand Up @@ -270,13 +301,12 @@ begin
Self.DeleteNode(Self.SelectedNode);
end;

procedure TWebberForm.DrawWeb(Area: TBox);
procedure TWebberForm.DrawWeb(area: TBox);
var
W,H,i,j,n,color,add: Int32;
i,j,n,color: Int32;
p,q: TPoint;
nodes,line: TPointArray;
canvas: TCanvas;
B: TBox;
begin
nodes := Self.Graph.Nodes;
if Length(nodes) = 0 then
Expand All @@ -285,15 +315,26 @@ begin
Canvas := Self.ImageBox.GetOverlay.GetCanvas();
Canvas.GetPen.SetColor($00FFFF);

if SCALE then
n := 1
else
n := 3;

for i := 0 to High(Graph.Paths) do
begin
p := nodes[i];

if SCALE then
p := [Floor(p.X/4), Floor(p.Y/4)];

for j:=0 to High(Graph.Paths[i]) do
begin
q := nodes[Graph.Paths[i][j]];

if p.InBox(Area) or q.InBox(Area) then
if SCALE then
q := [Floor(q.X/4), Floor(q.Y/4)];

if p.InBox(area) or q.InBox(area) then
begin
Canvas.MoveTo(p.x, p.y);
Canvas.LineTo(q.x, q.y);
Expand All @@ -307,21 +348,29 @@ begin
if Length(Graph.Names[i]) <> 0 then
color := $77FF00;

if nodes[i].InBox(Area) then
p := nodes[i];
if SCALE then
p := [Floor(p.X/4), Floor(p.Y/4)];

if p.InBox(area) then
begin
Canvas.GetBrush().SetColor(Color);
Canvas.FillRect(nodes[i].X-3, nodes[i].Y-3, nodes[i].X+3, nodes[i].Y+3);
Canvas.FillRect(p.X-n, p.Y-n, p.X+n, p.Y+n);
end;
end;

if InRange(SelectedNode, Low(nodes), High(nodes)) then
begin
Canvas.GetBrush().SetColor($FF0000);
Canvas.FillRect(Nodes[SelectedNode].X-3, Nodes[SelectedNode].Y-3, Nodes[SelectedNode].X+3, Nodes[SelectedNode].Y+3);
p := Nodes[SelectedNode];
if SCALE then
p := [Floor(p.X/4), Floor(p.Y/4)];

Canvas.FillRect(p.X-n, p.Y-n, p.X+n, p.Y+n);
end;
end;

procedure TWebberForm.DrawPath(Area: TBox);
procedure TWebberForm.DrawPath(area: TBox);
var
i,j,W,H: Int32;
p,q: TPoint;
Expand All @@ -333,22 +382,24 @@ begin
if Length(Nodes) = 0 then
Exit;

p := Nodes[Path[0]];
q := Nodes[Path[High(Path)]];

Canvas := Self.ImageBox.GetOverlay.GetCanvas();
Canvas.GetPen().SetColor(clNavy);
Canvas.GetPen().setWidth(3);

for i := 0 to High(Path) do
begin
p := Nodes[Path[i]];
if SCALE then
p := [Floor(p.X/4), Floor(p.Y/4)];

if i < High(Path) then
begin
q := Nodes[Path[i+1]];

if p.InBox(Area) or q.InBox(Area) then
if SCALE then
q := [Floor(q.X/4), Floor(q.Y/4)];

if p.InBox(area) or q.InBox(area) then
begin
Canvas.MoveTo(p.x, p.y);
Canvas.LineTo(q.x, q.y);
Expand Down Expand Up @@ -378,6 +429,12 @@ begin
p := corners[i];
q := corners[i+1];

if SCALE then
begin
p := [Floor(p.X/4), Floor(p.Y/4)];
q := [Floor(q.X/4), Floor(q.Y/4)];
end;

if p.InBox(area) or q.InBox(area) then
begin
canvas.MoveTo(p.x, p.y);
Expand Down Expand Up @@ -573,6 +630,8 @@ var
panel: TPanel;
button: TButton;
check: TLabeledCheckBox;
bmp, tmp: TMufasaBitmap;
path, fileName: String;
begin
try
Self.Name := 'Graph';
Expand Down Expand Up @@ -623,6 +682,25 @@ begin
button.SetOnClick(@Self.OnPrintGraph);
button.setAlign(alLeft);


if SCALE then
begin
fileName := SHA1File(Self.Map) + '.bmp';
path := WL_DATAPATH + 'assets' + DIRECTORYSEPARATOR;

bmp.Init();
if FileExists(path + fileName) then
bmp.LoadFromFile(path + fileName)
else
begin
tmp := TRSWalkerMap.InternalLoadMap(Self.Map);
tmp.Downsample(4, bmp);
tmp.Free();
if ForceDirectories(path) then
bmp.SaveToFile(path + fileName);
end;
end;

Self.ImageBox.Init(Self.Form);
Self.ImageBox.SetParent(Self.Form);
Self.ImageBox.SetAlign(alClient);
Expand All @@ -631,7 +709,12 @@ begin
Self.ImageBox.SetOnMouseDown(@Self.OnMouseDown);
Self.ImageBox.SetOnMouseUp(@Self.OnMouseUp);
Self.ImageBox.SetOnKeyDown(@Self.OnKeyDown);
Self.ImageBox.GetBackground().LoadFromFile(Self.Map);

if SCALE then
Self.ImageBox.GetBackground().LoadFromMufasaBitmap(bmp)
else
Self.ImageBox.getBackground().LoadFromFile(Self.Map);

Self.ImageBox.BackgroundChanged();

WriteLn('Click: Add or select node' + LineEnding +
Expand Down

0 comments on commit c893424

Please sign in to comment.