Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions UI/Controls/NodeTree/PathingCategoryNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using BhModule.Community.Pathing.Behavior.Modifier;
Expand Down Expand Up @@ -39,7 +39,8 @@ public bool Active
public PathingCategory PathingCategory { get; }
private PathingCategory PackCategory { get; }

private readonly IList<IPathingEntity> _entities;
private IEnumerable<IPathingEntity> _entities => this.TreeView?.EntityLookup?[this.PathingCategory]
?? CategoryUtil.GetAssociatedPathingEntities(this.PathingCategory, _packState.Entities);

private readonly bool _forceShowAll = false;

Expand All @@ -53,7 +54,6 @@ public bool Active
public PathingCategoryNode(IPackState packState, PathingCategory pathingCategory, bool showForceAll) : base(pathingCategory.DisplayName) {
_packState = packState;
this.PathingCategory = pathingCategory;
_entities = CategoryUtil.GetAssociatedPathingEntities(pathingCategory, packState.Entities).ToList();
_forceShowAll = showForceAll;

if (pathingCategory.IsSeparator) {
Expand All @@ -63,13 +63,6 @@ public PathingCategoryNode(IPackState packState, PathingCategory pathingCategory
this.ShowIconTooltip = false;
} else {
this.Checkable = true;

if (_entities.Count <= 0 && (this.PathingCategory.IsSeparator || this.PathingCategory.Count > 0)) {
BackgroundOpacity = 0.3f;
} else {
BackgroundOpacity = 0.05f;
BackgroundOpaqueColor = Color.LightYellow;
}
}

DetectAndBuildContexts();
Expand Down Expand Up @@ -195,7 +188,7 @@ private void BuildAchievementTexture() {
}

private void BuildEntityCount() {
if (_entities.Count <= 0) {
if (!_entities.Any()) {
if (!this.PathingCategory.IsSeparator && this.PathingCategory.Count <= 0) {
_ = new Label {
Parent = _propertiesPanel,
Expand Down Expand Up @@ -365,6 +358,15 @@ private void UpdateChildrenActiveState() {
protected override void OnParentChanged() {
base.OnParentChanged();

if (this.TreeView != null && !this.PathingCategory.IsSeparator) {
if (!_entities.Any() && (this.PathingCategory.IsSeparator || this.PathingCategory.Count > 0)) {
BackgroundOpacity = 0.3f;
} else {
BackgroundOpacity = 0.05f;
BackgroundOpaqueColor = Color.LightYellow;
}
}

if(Checkable)
UpdateActiveState(Checked);
}
Expand All @@ -380,7 +382,7 @@ public void UpdateLabelActiveState() {
public int AddSubNodes(bool forceShowAll) {
if (this.PathingCategory.Count <= 0) return 0;

(IEnumerable<PathingCategory> subCategories, int skipped) = this.PathingCategory.FilterCategories(_packState, forceShowAll);
(IEnumerable<PathingCategory> subCategories, int skipped) = this.PathingCategory.FilterCategories(_packState, forceShowAll, this.TreeView?.EntityLookup);

foreach (var subCategory in subCategories) {
if (subCategory == null) continue;
Expand Down
44 changes: 27 additions & 17 deletions UI/Controls/NodeTree/TreeView.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BhModule.Community.Pathing.Entity;
using BhModule.Community.Pathing.State;
using BhModule.Community.Pathing.UI.Controls.TreeNodes;
using BhModule.Community.Pathing.Utility;
Expand All @@ -24,12 +25,26 @@ public class TreeView : Container

public PackInitiator PackInitiator { get; private set; }
public IList<TreeNodeBase> AllBaseNodes { get; } = new List<TreeNodeBase>();
public IList<TreeNodeBase> ChildBaseNodes { get; } = new List<TreeNodeBase>();
private IList<PathingCategory> AllCategories { get; set; } = new List<PathingCategory>();
private IList<TreeNodeBase> ChildBaseNodes { get; } = new List<TreeNodeBase>();
private List<CategorySearchRecord> _searchRecords { get; set; } = new List<CategorySearchRecord>();

private class CategorySearchRecord {
public PathingCategory Category { get; }
public string NormalizedDisplayName { get; }
public string NormalizedName { get; }

public CategorySearchRecord(PathingCategory category) {
this.Category = category;
this.NormalizedDisplayName = category.DisplayName?.Replace(" ", "") ?? "";
this.NormalizedName = category.Name?.Replace(" ", "") ?? "";
}
}

public event EventHandler<EventArgs> NodeLoadingStarted;
public event EventHandler<EventArgs> NodesLoadedFinished;

public ILookup<PathingCategory, IPathingEntity> EntityLookup { get; private set; }

public TreeView(PackInitiator packInitiator) {
PackInitiator = packInitiator;
}
Expand Down Expand Up @@ -123,6 +138,8 @@ public void LoadNodes() {
ClearChildNodes();
AllBaseNodes.Clear();

this.EntityLookup = PackInitiator.PackState.Entities.ToArray().ToLookup(e => e.Category);

var rootCategory = PackInitiator.GetAllMarkersCategories();

if (rootCategory == null) return;
Expand All @@ -147,7 +164,7 @@ public void LoadNodes() {

_rootNode.Expand();

AllCategories = CategoryUtil.FlattenCategories(rootCategory).ToList();
_searchRecords = CategoryUtil.FlattenCategories(rootCategory).Select(c => new CategorySearchRecord(c)).ToList();

this.NodesLoadedFinished?.Invoke(this, EventArgs.Empty);
}
Expand All @@ -173,23 +190,16 @@ private void GlobalPathablesEnabledOnSettingChanged(object sender, ValueChangedE

string normalizedInput = input.Replace(" ", "");

var results = AllCategories
.AsParallel()
.WithCancellation(cancellationToken)
var results = _searchRecords
.Where(c =>
{
if (string.IsNullOrWhiteSpace(c.DisplayName) || string.IsNullOrWhiteSpace(c.Name)) return false;

string normalizedDisplayName = c.DisplayName?.Replace(" ", "");
string normalizedName = c.Name.Replace(" ", "");

cancellationToken.ThrowIfCancellationRequested();
if (string.IsNullOrWhiteSpace(c.Category.DisplayName) || string.IsNullOrWhiteSpace(c.Category.Name)) return false;

return (normalizedDisplayName != null && normalizedDisplayName.IndexOf(normalizedInput, StringComparison.OrdinalIgnoreCase) >= 0) ||
normalizedName.IndexOf(normalizedInput, StringComparison.OrdinalIgnoreCase) >= 0;
}).ToList();
return (c.NormalizedDisplayName.IndexOf(normalizedInput, StringComparison.OrdinalIgnoreCase) >= 0) ||
c.NormalizedName.IndexOf(normalizedInput, StringComparison.OrdinalIgnoreCase) >= 0;
}).Select(c => c.Category).ToList();

(filteredResults, skipped) = results.FilterCategories(PackInitiator.PackState, forceShowAll);
(filteredResults, skipped) = results.FilterCategories(PackInitiator.PackState, forceShowAll, this.EntityLookup);

return (filteredResults.ToList(), skipped);
}
Expand Down
18 changes: 10 additions & 8 deletions Utility/CategoryUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using BhModule.Community.Pathing.Entity;
Expand Down Expand Up @@ -39,7 +39,7 @@ public static string GetPath(this PathingCategory category) {
return $".{category.Namespace}";
}

public static (IEnumerable<PathingCategory>, int skipped) FilterCategories(this IEnumerable<PathingCategory> categories, IPackState packState, bool forceShowAll = false) {
public static (IEnumerable<PathingCategory>, int skipped) FilterCategories(this IEnumerable<PathingCategory> categories, IPackState packState, bool forceShowAll = false, ILookup<PathingCategory, IPathingEntity> entityLookup = null) {
if (categories == null || packState == null) return (null, 0);

var subCategories = categories.Where(cat => cat.LoadedFromPack && cat.DisplayName != "" && !cat.IsHidden);
Expand Down Expand Up @@ -67,7 +67,7 @@ public static (IEnumerable<PathingCategory>, int skipped) FilterCategories(this
lastIsSeparator = true;
}
//Check if the category has any loaded/visible children recursively
else if (subCategory.HasVisibleChildren(packState, true))
else if (subCategory.HasVisibleChildren(packState, true, entityLookup))
{
// If category has visible children, we include it
filteredSubCategories.Add(subCategory);
Expand All @@ -86,24 +86,26 @@ public static (IEnumerable<PathingCategory>, int skipped) FilterCategories(this
return (Enumerable.Reverse(filteredSubCategories), skipped);
}

public static bool HasVisibleChildren(this PathingCategory category, IPackState packState, bool recursively = false) {
public static bool HasVisibleChildren(this PathingCategory category, IPackState packState, bool recursively = false, ILookup<PathingCategory, IPathingEntity> entityLookup = null) {

if (packState == null ||
string.IsNullOrWhiteSpace(category.DisplayName) ||
!category.LoadedFromPack) return false;

var mapId = GameService.Gw2Mumble.CurrentMap.Id;

var hasVisibleEntities = packState.Entities
.ToArray()
.Any(e => e.MapId == mapId && e.Category == category);
var hasVisibleEntities = entityLookup != null
? entityLookup[category].Any(e => e.MapId == mapId)
: packState.Entities
.ToArray()
.Any(e => e.MapId == mapId && e.Category == category);

if (hasVisibleEntities) return true;

if (recursively) {
foreach (var childCategory in category)
{
if (childCategory.HasVisibleChildren(packState, true))
if (childCategory.HasVisibleChildren(packState, true, entityLookup))
{
return true;
}
Expand Down