Skip to content

Commit

Permalink
Added MVP overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilPepper committed Dec 24, 2020
1 parent eb1366a commit 53ff474
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 12 deletions.
5 changes: 5 additions & 0 deletions MHWItemBoxTracker/Config/ConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public static ItemBoxTrackerConfig loadConfig()
{
return PathFinder.loadJson<ItemBoxTrackerConfig>("plugin.settings.json");
}

public static void saveConfig(ItemBoxTrackerConfig config)
{
PathFinder.saveJson("plugin.settings.json", config);
}
}
}
2 changes: 2 additions & 0 deletions MHWItemBoxTracker/Config/ItemBoxTrackerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ namespace MHWItemBoxTracker.Config
class ItemBoxTrackerConfig : PluginSettings
{
public List<ItemConfig> tracking { get; set; }
public int[] overlayPosition { get; set; }
public double opacity { get; set; }
}
}
34 changes: 34 additions & 0 deletions MHWItemBoxTracker/GUI/ItemBoxTracker.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<src:Widget x:Class="MHWItemBoxTracker.GUI.ItemBoxTracker"
xmlns:src="clr-namespace:HunterPie.GUI;assembly=HunterPie.UI"
xmlns:Custom="clr-namespace:HunterPie.GUIControls.Custom_Controls;assembly=HunterPie.UI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
ResizeMode="NoResize" WindowStyle="None" AllowsTransparency="True" ShowInTaskbar="False" Topmost="True"
SizeToContent="Height" Width="350" WidgetActive="True"
MouseDown="OnMouseDown">
<Grid>
<ListBox Name="theList" HorizontalContentAlignment="Stretch" IsEnabled="False">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<!--<Image />-->
<TextBlock Grid.Column="0" Text="{Binding name}" FontSize="14" HorizontalAlignment="Left" Padding="0,0,10,0" />
<TextBlock Grid.Column="1" Text="{Binding ratio}" FontSize="14" HorizontalAlignment="Right" Padding="0,0,10,0" />
<ProgressBar Grid.Column="2" Minimum="0" Maximum="100" Value="{Binding progress}" />
<!--<Custom:MinimalHealthBar Style="{StaticResource OVERLAY_MONSTER_PART_BAR_STYLE}" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Top" />-->

<Grid.ColumnDefinitions>
<!--<ColumnDefinition Width="50" />-->
<ColumnDefinition Width="*" />
<ColumnDefinition Width="75" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</src:Widget>
96 changes: 96 additions & 0 deletions MHWItemBoxTracker/GUI/ItemBoxTracker.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using HunterPie.GUI;
using HunterPie.Core;
using System.Collections.Generic;
using System.Windows;
using MHWItemBoxTracker.Config;
using System.Linq;
using Debugger = HunterPie.Logger.Debugger;

namespace MHWItemBoxTracker.GUI
{
public partial class ItemBoxTracker : Widget
{
public ItemBoxTracker()
{
InitializeComponent();
BaseWidth = Width;
BaseHeight = Height;
SetWindowFlags();
ApplySettings();
}

public void setItemsToDisplay(List<ItemBoxRow> itemBoxRows)
{
Dispatch(() =>
{
theList.ItemsSource = itemBoxRows;
Debugger.Log(itemBoxRows.Count > 0 ? "Loading Box!!!" : "Hiding Box!!!");
WidgetHasContent = (itemBoxRows.Count > 0);
ChangeVisibility();
}
);

}


public override void EnterWidgetDesignMode()
{
base.EnterWidgetDesignMode();
RemoveWindowTransparencyFlag();
}

public override void LeaveWidgetDesignMode()
{
base.LeaveWidgetDesignMode();
ApplyWindowTransparencyFlag();
SaveSettings();
}

private void SaveSettings()
{
var config = ConfigLoader.loadConfig();
if (config.overlayPosition == null || config.overlayPosition.Length != 2)
{
config.overlayPosition = new int[] { 0, 0 };
}
config.overlayPosition[0] = (int)Left; // - UserSettings.PlayerConfig.Overlay.Position[0];
config.overlayPosition[1] = (int)Top; // - UserSettings.PlayerConfig.Overlay.Position[1];

ConfigLoader.saveConfig(config);
}

public override void ApplySettings(bool FocusTrigger = false)
{
Dispatch(() =>
{
if (!FocusTrigger)
{
var config = ConfigLoader.loadConfig();
Left = config?.overlayPosition?[0] ?? 0; // + UserSettings.PlayerConfig.Overlay.Position[0];
Top = config?.overlayPosition?[1] ?? 0; // + UserSettings.PlayerConfig.Overlay.Position[1];
WidgetActive = config?.IsEnabled ?? true;
Opacity = 0.6;
}
base.ApplySettings();
});
}
private void Dispatch(System.Action function) => Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, function);

private void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
{
MoveWidget();
//SaveSettings();
}
}
}

public class ItemBoxRow
{
public string name { get; set; }
public string ratio { get; set; }
public double progress { get; set; }
}
}
18 changes: 14 additions & 4 deletions MHWItemBoxTracker/MHWItemBoxTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<Reference Include="..\HunterPie.Core.dll" Condition="Exists('..\HunterPie.Core.dll')" />
<PackageReference Include="HunterPie.Core" Version="1.0.3-99r5-1.0" Condition="!Exists('..\HunterPie.Core.dll')" />
<Reference Include="..\HunterPie.UI.dll" Condition="Exists('..\HunterPie.UI.dll')" />
<PackageReference Include="HunterPie.UI" Version="1.0.3-99r5-1.0" Condition="!Exists('..\HunterPie.UI.dll')" />
<Reference Include="..\HunterPie.Core.dll" Condition="Exists('..\HunterPie.Core.dll')" />
<PackageReference Include="HunterPie.Core" Version="1.0.3-99r5-1.0" Condition="!Exists('..\HunterPie.Core.dll')" />
<Reference Include="..\HunterPie.UI.dll" Condition="Exists('..\HunterPie.UI.dll')" />
<PackageReference Include="HunterPie.UI" Version="1.0.3-99r5-1.0" Condition="!Exists('..\HunterPie.UI.dll')" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config\ConfigLoader.cs" />
<Compile Include="Config\ItemConfig.cs" />
<Compile Include="GUI\ItemBoxTracker.xaml.cs">
<DependentUpon>ItemBoxTracker.xaml</DependentUpon>
</Compile>
<Compile Include="helper\ItemBoxTracker.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
Expand All @@ -95,5 +98,12 @@
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Page Include="GUI\ItemBoxTracker.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions MHWItemBoxTracker/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Main : IPlugin
public string Description { get; set; }
public Game Context { get; set; }
private ItemBoxTracker tracker { get; set; }

public void Initialize(Game context)
{
var module = PathFinder.loadJson<PluginInformation>("module.json");
Expand Down
7 changes: 7 additions & 0 deletions MHWItemBoxTracker/Utils/PathFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@ public static T loadJson<T>(string json)
var jsonPath = Path.Combine(PathFinder.getPluginPath(), json);
return JsonConvert.DeserializeObject<T>(File.ReadAllText(jsonPath));
}

public static void saveJson<T>(string json, T config)
{
var jsonPath = Path.Combine(PathFinder.getPluginPath(), json);
var jsonStr = JsonConvert.SerializeObject(config, Formatting.Indented);
File.WriteAllText(jsonPath, jsonStr);
}
}
}
30 changes: 22 additions & 8 deletions MHWItemBoxTracker/helper/ItemBoxTracker.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using HunterPie.Core;
using HunterPie.GUI;
using MHWItemBoxTracker.Config;
using Debugger = HunterPie.Logger.Debugger;

namespace MHWItemBoxTracker.helper
{
class ItemBoxTracker
{
private Player player { get; }
private GUI.ItemBoxTracker gui;

public ItemBoxTracker(Player player)
{
this.player = player;
Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
new Action(() =>
{
gui = new GUI.ItemBoxTracker();
Overlay.RegisterWidget(gui);
}));
}

public void loadItemBox(object source = null, EventArgs e = null)
{
if (!player.InHarvestZone) return;

Debugger.Log("Loading Box!!!");

var items = ConfigLoader.loadConfig().tracking;
var box = player.ItemBox;
var ids = items.Select(ic => ic.itemId).ToHashSet();

Debugger.Log(String.Join(",", ids));
var itemsHeld = box.FindItemsInBox(ids);
Debugger.Log(String.Join(",", itemsHeld));
foreach (ItemConfig item in items){
var itemBoxRows = new List<GUI.ItemBoxRow>();
foreach (ItemConfig item in items)
{
int amountHeld = 0;
itemsHeld.TryGetValue(item.itemId, out amountHeld);
Debugger.Log($"{item.name}: {amountHeld}/{item.amount}");

itemBoxRows.Add(new GUI.ItemBoxRow
{
name = item.name,
ratio = $"{amountHeld}/{item.amount}",
progress = 100.0 * amountHeld / item.amount,
});
}
gui?.setItemsToDisplay(itemBoxRows);
}

public void unloadItemBox(object source = null, EventArgs e = null)
{
Debugger.Log("Hiding Box!!!");
gui?.setItemsToDisplay(new List<GUI.ItemBoxRow>());
}
}
}

0 comments on commit 53ff474

Please sign in to comment.