Skip to content

Commit

Permalink
Fixed priority highlighting (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
KYPremco authored Aug 12, 2023
1 parent f23caf6 commit 7e9f0fa
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Core/TimberApi/BottomBarSystem/Patchers/ToolButtonPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using HarmonyLib;
using TimberApi.HarmonyPatcherSystem;
using TimberApi.SceneSystem;
using Timberborn.BuilderPrioritySystemUI;
using Timberborn.ToolSystem;
using UnityEngine;

namespace TimberApi.BottomBarSystem.Patchers
{
Expand All @@ -14,7 +16,7 @@ public class ToolButtonPatcher : BaseHarmonyPatcher
public override void Apply(Harmony harmony)
{
harmony.Patch(
GetMethodInfo<ToolButton>("OnButtonClicked"),
GetMethodInfo<ToolButton>(nameof(ToolButton.OnButtonClicked)),
prefix: GetHarmonyMethod(nameof(OnButtonClickedPatch))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static bool OnToolGroupEntered(ToolGroupEnteredEvent toolGroupEnteredEven

public static bool OnToolGroupExited(ToolGroupExitedEvent toolGroupExitedEvent, ToolGroupButton __instance, VisualElement ____toolGroupButtonWrapper)
{
if(toolGroupExitedEvent.ToolGroup is not ExitingTool) return false;
if(toolGroupExitedEvent.ToolGroup is not ExitingToolGroup) return false;

__instance.ToolButtonsElement.ToggleDisplayStyle(false);
____toolGroupButtonWrapper.RemoveFromClassList(ActiveClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using TimberApi.HarmonyPatcherSystem;
using TimberApi.SceneSystem;
using TimberApi.ToolGroupSystem;
using Timberborn.SingletonSystem;
using Timberborn.ToolSystem;

namespace TimberApi.BottomBarSystem.Patchers
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class ToolGroupManagerPatcher : BaseHarmonyPatcher
{
private static readonly ToolGroup ExitingToolGroup = new ExitingTool();
private static readonly ToolGroup ExitingToolGroup = new ExitingToolGroup();

public override string UniqueId => "TimberApi.ToolGroupManager";

Expand All @@ -27,7 +28,7 @@ public override bool ShouldApply(SceneEntrypoint? sceneEntrypoint)
return sceneEntrypoint == SceneEntrypoint.InGame;
}

public static void SwitchToolGroupPatch(ToolGroup? toolGroup, ToolGroupManager __instance)
public static void SwitchToolGroupPatch(ToolGroup? toolGroup, ToolGroupManager __instance, EventBus ____eventBus)
{
ToolButtonPatcher.ActiveToolButton?.Root.EnableInClassList("button--active", false);

Expand All @@ -36,6 +37,8 @@ public static void SwitchToolGroupPatch(ToolGroup? toolGroup, ToolGroupManager _
return;
}

____eventBus.Post(new ToolGroupExitedEvent(__instance.ActiveToolGroup));

__instance.ActiveToolGroup = ExitingToolGroup;
}
}
Expand Down
7 changes: 3 additions & 4 deletions Core/TimberApi/ToolGroupSystem/ExitingToolGroup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Timberborn.ConstructionMode;
using Timberborn.ForestryUI;
using Timberborn.PlantingUI;
using Timberborn.ToolSystem;

namespace TimberApi.ToolGroupSystem
{
/// <summary>
/// Add all mode enablers in this class to deactivate them on exiting a group
/// Requires IConstructionModeEnabler to disable construction mode even when it's in the other ToolGroups, reason is unknown.
/// </summary>
public class ExitingTool : TreeCuttingAreaToolGroup, IConstructionModeEnabler, IPlantingToolGroup
public class ExitingToolGroup : ToolGroup, IConstructionModeEnabler
{
}
}
2 changes: 2 additions & 0 deletions Core/TimberApi/ToolGroupSystem/ToolGroupConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using TimberApi.ConfiguratorSystem;
using TimberApi.SceneSystem;
using TimberApi.SpecificationSystem;
using TimberApi.ToolGroupSystem.ToolGroups.BuilderPriority;
using TimberApi.ToolGroupSystem.ToolGroups.ConstructionMode;
using TimberApi.ToolGroupSystem.ToolGroups.Default;
using TimberApi.ToolGroupSystem.ToolGroups.PlantingMode;
Expand All @@ -22,6 +23,7 @@ public void Configure(IContainerDefinition containerDefinition)
containerDefinition.MultiBind<IToolGroupFactory>().To<DefaultToolGroupFactory>().AsSingleton();
containerDefinition.MultiBind<IToolGroupFactory>().To<PlantingModeToolGroupFactory>().AsSingleton();
containerDefinition.MultiBind<IToolGroupFactory>().To<TreeCuttingAreaToolGroupFactory>().AsSingleton();
containerDefinition.MultiBind<IToolGroupFactory>().To<BuilderPriorityToolGroupFactory>().AsSingleton();
containerDefinition.MultiBind<ISpecificationGenerator>().To<TimberbornGroupGenerator>().AsSingleton();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnityEngine;

namespace TimberApi.ToolGroupSystem.ToolGroups.BuilderPriority
{
public sealed class BuilderPriorityToolGroup : Timberborn.BuilderPrioritySystemUI.BuilderPriorityToolGroup, IToolGroup
{
public BuilderPriorityToolGroup(string id, string? groupId, int order, string section, string displayNameLocKey, bool devMode, Sprite icon)
{
Id = id;
DisplayNameLocKey = displayNameLocKey;
Order = order;
Icon = icon;
DevMode = devMode;
Section = section;
GroupId = groupId;
}

public string Id { get; }

public string? GroupId { get; }

public int Order { get; }

public string Section { get; }

public bool DevMode { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace TimberApi.ToolGroupSystem.ToolGroups.BuilderPriority
{
public class BuilderPriorityToolGroupFactory : IToolGroupFactory
{
public string Id => "BuilderPriorityToolGroup";

public IToolGroup Create(ToolGroupSpecification toolGroupSpecification)
{
return new BuilderPriorityToolGroup(
toolGroupSpecification.Id,
toolGroupSpecification.GroupId,
toolGroupSpecification.Order,
toolGroupSpecification.Section,
toolGroupSpecification.NameLocKey,
toolGroupSpecification.DevMode,
toolGroupSpecification.Icon
);
}
}
}
1 change: 1 addition & 0 deletions Core/TimberApi/ToolSystem/ToolService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void LateLoad()
var toolFactory = _toolFactoryService.Get(specification.Type);

var tool = specification.GroupId is null ? toolFactory.Create(specification) : toolFactory.Create(specification, (ToolGroup) _toolGroupService.GetToolGroup(specification.GroupId));

tools.Add(specification.Id.ToLower(), tool);

var toolButton = _toolButtonFactoryService.Get(specification.Layout).Create(tool, specification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static GeneratedSpecification CreatePriorityToolGroup()
Id = "Priority",
Layout = "Blue",
Order = 50,
Type = "DefaultToolGroup",
Type = "BuilderPriorityToolGroup",
NameLocKey = "ToolGroups.Priority",
Icon = "Sprites/BottomBar/PriorityToolGroupIcon",
Section = "BottomBar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SettingBoxTool(ToolGroup? toolGroup, EventBus eventBus, ToolGroupManager

public override void Enter()
{
_toolGroupManager.SwitchToolGroup(new ExitingTool());
_toolGroupManager.SwitchToolGroup(new ExitingToolGroup());
_eventBus.Post(new ShowOptionsEvent());
}

Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Unreleased

### Changes
- Updated `ExitToolGroup`, does not require most group types to disable them after closing
- Changed `SwitchToolGroupPatch` so it will trigger the `ExitToolGroup` and the original group
- Added `BuilderPriorityToolGroup`

## TimberAPI v0.6.0.0

Expand Down

0 comments on commit 7e9f0fa

Please sign in to comment.