Skip to content

Commit

Permalink
Feat/movable card (#4)
Browse files Browse the repository at this point in the history
feat: a lot of different things to move the card and reset it's position
  • Loading branch information
Mouchoir authored Nov 7, 2023
1 parent ba02a75 commit 8205996
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 17 deletions.
6 changes: 6 additions & 0 deletions HDTAnomalyDisplay.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Debug|x86.ActiveCfg = Debug|x86
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Debug|x86.Build.0 = Debug|x86
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Release|Any CPU.Build.0 = Release|Any CPU
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Release|x86.ActiveCfg = Release|x86
{9151F41C-A5D4-406A-A35E-5FC828019E88}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
44 changes: 33 additions & 11 deletions HDTAnomalyDisplay/AnomalyDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
using Hearthstone_Deck_Tracker.API;
using System.Windows.Controls;
using Hearthstone_Deck_Tracker.Controls;
using System.Windows.Media;
using ControlzEx.Standard;

namespace HDTAnomalyDisplay
{
public class AnomalyDisplay
{
public CardImage cardImage;
public CardImage CardImage;
public static MoveCardManager MoveManager;

public AnomalyDisplay()
{
Expand Down Expand Up @@ -43,17 +46,30 @@ public async Task AwaitGameEntity()

public void InitializeView(int cardDbfId)
{
if (cardImage == null)
// Do not recreate card if it already exists via a double call to HandleGameStart() (cf OnLoad)
if (CardImage == null)
{
cardImage = new CardImage();
CardImage = new CardImage();

Core.OverlayCanvas.Children.Add(cardImage);
Canvas.SetBottom(cardImage, 50);
Canvas.SetLeft(cardImage, 0);
cardImage.Visibility = System.Windows.Visibility.Visible;
Core.OverlayCanvas.Children.Add(CardImage);
Canvas.SetTop(CardImage, Settings.Default.AnomalyCardTop);
Canvas.SetLeft(CardImage, Settings.Default.AnomalyCardLeft);
CardImage.Visibility = System.Windows.Visibility.Visible;

MoveManager = new MoveCardManager(CardImage);
Settings.Default.PropertyChanged += SettingsChanged;
SettingsChanged(null, null);
}

cardImage.SetCardIdFromCard(Database.GetCardFromDbfId(cardDbfId, false));
CardImage.SetCardIdFromCard(Database.GetCardFromDbfId(cardDbfId, false));
}

// On scaling change update the card
private void SettingsChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
CardImage.RenderTransform = new ScaleTransform(Settings.Default.AnomalyCardScale / 100, Settings.Default.AnomalyCardScale / 100);
Canvas.SetTop(CardImage, Settings.Default.AnomalyCardTop);
Canvas.SetLeft(CardImage, Settings.Default.AnomalyCardLeft);
}

public async void HandleGameStart()
Expand Down Expand Up @@ -82,9 +98,15 @@ public async void HandleGameStart()

public void ClearCard()
{
cardImage.SetCardIdFromCard(null);
Core.OverlayCanvas.Children.Remove(cardImage);
cardImage = null;
CardImage.SetCardIdFromCard(null);
Core.OverlayCanvas.Children.Remove(CardImage);

MoveManager.Dispose();
Settings.Default.PropertyChanged -= SettingsChanged;

CardImage = null;
MoveManager = null;

}
}
}
11 changes: 5 additions & 6 deletions HDTAnomalyDisplay/AnomalyDisplayPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,32 @@ public class AnomalyDisplayPlugin : IPlugin

public string Description => "Displays the current Battlegrounds anomaly on your overlay";

public string ButtonText => "NO SETTINGS";
public string ButtonText => "SETTINGS";
// public string ButtonText => Strings.GetLocalized("");

public string Author => "Mouchoir & Tignus";

public Version Version => new Version(1, 0);
public Version Version => new Version(1, 1, 0);

public MenuItem MenuItem => null;

public AnomalyDisplay anomalyDisplay;

public void OnButtonPress()
{
}
public void OnButtonPress() => SettingsView.Flyout.IsOpen = true;

public void OnLoad()
{
anomalyDisplay = new AnomalyDisplay();
GameEvents.OnGameStart.Add(anomalyDisplay.HandleGameStart);
GameEvents.OnGameEnd.Add(anomalyDisplay.ClearCard);

// Processing GameStart logic in case plugin was loaded after starting a game
// Processing GameStart logic in case plugin was loaded/unloaded after starting a game without restarting HDT
anomalyDisplay.HandleGameStart();
}

public void OnUnload()
{
Settings.Default.Save();
anomalyDisplay.ClearCard();
anomalyDisplay = null;
}
Expand Down
45 changes: 45 additions & 0 deletions HDTAnomalyDisplay/HDTAnomalyDisplay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="HearthDb">
<HintPath>references\HearthDb.dll</HintPath>
Expand All @@ -41,6 +58,7 @@
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -53,7 +71,34 @@
<ItemGroup>
<Compile Include="AnomalyDisplay.cs" />
<Compile Include="AnomalyDisplayPlugin.cs" />
<Compile Include="MoveCardManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="SettingsView.xaml.cs">
<DependentUpon>SettingsView.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="SettingsView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="MahApps.Metro">
<Version>1.6.5</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
104 changes: 104 additions & 0 deletions HDTAnomalyDisplay/MoveCardManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Hearthstone_Deck_Tracker;
using Core = Hearthstone_Deck_Tracker.API.Core;
using System;
using System.Windows.Controls;
using System.Windows;
using Hearthstone_Deck_Tracker.Controls;
using Hearthstone_Deck_Tracker.Utility;

namespace HDTAnomalyDisplay
{
public class MoveCardManager
{
private User32.MouseInput _mouseInput;
private CardImage _card;

private bool _selected;

public MoveCardManager(CardImage cardImageToMove)
{
_card = cardImageToMove;
}

public bool ToggleUILockState()
{
if (Hearthstone_Deck_Tracker.Core.Game.IsRunning && _mouseInput == null)
{
_mouseInput = new User32.MouseInput();
_mouseInput.LmbDown += MouseInputOnLmbDown;
_mouseInput.LmbUp += MouseInputOnLmbUp;
_mouseInput.MouseMoved += MouseInputOnMouseMoved;
return true;
}
Dispose();
return false;
}

public bool isUILocked()
{
return _mouseInput == null;
}

public void Dispose()
{
_mouseInput?.Dispose();
_mouseInput = null;
_selected = false;
}

private void MouseInputOnLmbDown(object sender, EventArgs eventArgs)
{
var pos = User32.GetMousePos();
var _mousePos = new Point(pos.X, pos.Y);
if (PointInsideControl(_mousePos, _card))
{
_selected = true;
}
}

private void MouseInputOnLmbUp(object sender, EventArgs eventArgs)
{
_selected = false;
}

private void MouseInputOnMouseMoved(object sender, EventArgs eventArgs)
{
if (!_selected)
{
return;
}

var pos = User32.GetMousePos();
var p = Core.OverlayCanvas.PointFromScreen(new Point(pos.X, pos.Y));

// TODO check max height and width, does not work yet
if (p.Y < 0)
{
p.Y = 0;
}
else if (p.Y > Core.OverlayCanvas.Height)
{

p.Y = Core.OverlayCanvas.Height;
}

if (p.X < 0)
{
p.X = 0;
}
else if (p.X > Core.OverlayCanvas.Width)
{
p.X = Core.OverlayCanvas.Width;
}

Settings.Default.AnomalyCardTop = p.Y;
Settings.Default.AnomalyCardLeft = p.X;
}

private bool PointInsideControl(Point p, FrameworkElement control)
{
var pos = control.PointFromScreen(p);
return pos.X > 0 && pos.X < control.ActualWidth && pos.Y > 0 && pos.Y < control.ActualHeight;
}
}
}
62 changes: 62 additions & 0 deletions HDTAnomalyDisplay/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8205996

Please sign in to comment.