Skip to content

Commit

Permalink
feat(house): layouts are now saved per user
Browse files Browse the repository at this point in the history
You will "lose" your would layout unless you move the old file and rename it with your player index.
"lose" as in, it won't be accessible through the form, the config file is not deleted.

Improved free of the house loader
  • Loading branch information
Torwent committed Dec 24, 2024
1 parent d943b59 commit b3db692
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
23 changes: 21 additions & 2 deletions optional/handlers/house/house.simba
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ begin
Result.Create(owner, 'House teleport:', 'Choose your method of house teleportation.', topleft, [170, 40], offset, True);
Result.AddItemArray(['Spell', 'Tablet', 'Contruction/Max cape']);

Self.Loader.SetupConfig();
if Self.Loader.Config.Has('teleport') then
i := Self.Loader.Config.GetInt('teleport');

Expand Down Expand Up @@ -1264,7 +1265,23 @@ begin
tmp.Free();
end;

procedure TScriptForm.POHBuilderOnShow(sender: TObject);
procedure TScriptForm.POHBuilderLeftOnShow(sender: TObject);
var
combobox: TComboBox;
imgbox: TSimbaImageBox;
begin
if Login.PlayerIndex <> House.Loader.LayoutIndex then
begin
WriteLn 'Refreshing layout.';
House.Loader.Free();
combobox := TPanel(sender).GetChild('decoration_combobox');
Self.POHDecorationUpdate(combobox);
combobox := TPanel(sender).GetChild('room_combobox');
Self.RoomUpdate(combobox);
end;
end;

procedure TScriptForm.POHBuilderClientOnShow(sender: TObject);
var
imgbox: TSimbaImageBox;
begin
Expand Down Expand Up @@ -1301,9 +1318,11 @@ begin
panel.Create(Result);
panel.setAlign(TAlign.alLeft);
panel.setBevelWidth(0);
panel.setOnPaint(@Self.POHBuilderLeftOnShow);

decoration.Create(panel, 'Decoration:', 'Change your house decoration', [0, 10], [166,0], True);
decoration.AddItemArray(['Basic wood', 'Basic stone', 'Whitewashed stone', 'Fremennik-style wood', 'Tropical wood', 'Fancy stone', 'Deathly mansion', 'Twisted theme', 'Hosidius house', 'Cosy cabin']);
decoration.SetName('decoration');
decoration.SetItemIndex(0);
decoration.ComboBox.setOnChange(@Self.POHDecorationUpdate);

Expand Down Expand Up @@ -1334,7 +1353,7 @@ begin
panel.setAlign(TAlign.alClient);
panel.setBevelWidth(0);

panel.setOnPaint(@Self.POHBuilderOnShow);
panel.setOnPaint(@Self.POHBuilderClientOnShow);

caption.Create(panel,'LEFT CLICK to select a room, RIGHT CLICK to pan around. CTRL + WHEEL to zoom.', '', [0,0], [0,0], True);
caption.setAlign(TAlign.alTop);
Expand Down
34 changes: 30 additions & 4 deletions optional/handlers/house/housemap.simba
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Helper record used by the {ref}`THouseHandler`.
All `THouseLoader` methods are helper methods for the {ref}`THouseHandler` and you shouldn't have to call them for anything.
*)
THouseLoader = record
AMOUNT, SIZE, Downscale: Int32;
AMOUNT, SIZE, Downscale, LayoutIndex: Int32;
Map, Collision, DownscaledMap: TMufasaBitmap;

Rooms: array of array of THouseRoom;
Expand Down Expand Up @@ -81,14 +81,30 @@ procedure THouseLoader.Free();
var
room: EHouseRoom;
begin
Self.Config.Free();
Self.Map.Free();
Self.DownscaledMap.Free();
Self.RoomsBitmap.Free();
Self.IconBitmap.Free();

Self.Map := nil;
Self.DownscaledMap := nil;
Self.RoomsBitmap := nil;
Self.IconBitmap := nil;

for room := Low(EHouseRoom) to High(EHouseRoom) do
begin
if Self.RoomBitmaps[room] <> nil then Self.RoomBitmaps[room].Free();
if Self.IconBitmaps[room] <> nil then Self.IconBitmaps[room].Free();
if Self.RoomBitmaps[room] <> nil then
begin
Self.RoomBitmaps[room].Free();
Self.RoomBitmaps[room] := nil;
end;

if Self.IconBitmaps[room] <> nil then
begin
Self.IconBitmaps[room].Free();
Self.IconBitmaps[room] := nil;
end;
end;

Self.Rooms := [];
Expand All @@ -105,6 +121,16 @@ begin
Self.Colors := decoration.GetColors();
end;

procedure THouseLoader.SetupConfig();
begin
if Self.Config.IsSetup and (Self.LayoutIndex = Login.PlayerIndex) then Exit;
Self.LayoutIndex := Login.PlayerIndex;
Self.Config.Setup(
AppPath + 'Configs' + DirectorySeparator + 'house' +
DirectorySeparator + ToStr(Self.LayoutIndex)
);
end;

(*
## THouseLoader.Init()
```pascal
Expand All @@ -115,7 +141,7 @@ You don't usually have to call it yourself.
*)
procedure THouseLoader.Init(size, amount: Int32);
begin
Self.Config.Setup('pohhandler');
Self.SetupConfig();

if not Self.Config.Has('size') then Self.Config.Put('size', size);
if not Self.Config.Has('amount') then Self.Config.Put('amount', amount);
Expand Down
3 changes: 1 addition & 2 deletions templates/househandler_example.simba
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ begin
SetTop(TControl.AdjustToDPI(190));
end;

Self.CreateHouseBuilder();

House.CreateHouseTeleportCombobox(tab, [40, 210], [0,0]);

Self.CreateHouseBuilder();
Self.CreateAntibanManager();
Self.CreateWaspLibSettings();
Self.CreateAPISettings();
Expand Down
3 changes: 1 addition & 2 deletions utils/config.simba
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ you wish to unlock the ram used by this (which should be minimal).
*)
procedure TConfigJSON.Free();
begin
if not Self.IsSetup then
Exit;
if not Self.IsSetup then Exit;

Self.JSON.Free();
Self.JSON := nil;
Expand Down

0 comments on commit b3db692

Please sign in to comment.