Skip to content

Commit

Permalink
* implement PlayerSwapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes-Schneider committed Oct 26, 2020
1 parent a589e7e commit 9c20731
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 65 deletions.
14 changes: 12 additions & 2 deletions GoldDiff/View/ControlElement/PlayerGoldDifferenceView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@
<Image Grid.Column="1"
Margin="0, 0, 12, 0"
VerticalAlignment="Center"
AllowDrop="{Binding ElementName=This, Path=CanSwapPlayers}"
DataContext="{Binding ElementName=This, Path=PlayerBlueSide}"
Source="{Binding Path=Champion.SmallTileImage}" />
Source="{Binding Path=Champion.SmallTileImage}"
MouseLeftButtonDown="ChampionTileBlueSide_OnMouseLeftButtonDown"
DragEnter="ChampionTileBlueSide_OnDragOver"
DragOver="ChampionTileBlueSide_OnDragOver"
Drop="ChampionTileBlueSide_OnDrop" />

<!-- /Champion Tile Blue Side -->

Expand Down Expand Up @@ -122,8 +127,13 @@
<Image Grid.Column="5"
Margin="12, 0, 0, 0"
VerticalAlignment="Center"
AllowDrop="{Binding ElementName=This, Path=CanSwapPlayers}"
DataContext="{Binding ElementName=This, Path=PlayerRedSide}"
Source="{Binding Path=Champion.SmallTileImage}" />
Source="{Binding Path=Champion.SmallTileImage}"
MouseLeftButtonDown="ChampionTileRedSide_OnMouseLeftButtonDown"
DragEnter="ChampionTileRedSide_OnDragOver"
DragOver="ChampionTileRedSide_OnDragOver"
Drop="ChampionTileRedSide_OnDrop" />

<!-- /Champion Tile Red Side -->

Expand Down
126 changes: 125 additions & 1 deletion GoldDiff/View/ControlElement/PlayerGoldDifferenceView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using GoldDiff.LeagueOfLegends.Game;
using GoldDiff.Shared.LeagueOfLegends;

namespace GoldDiff.View.ControlElement
{
public partial class PlayerGoldDifferenceView : UserControl
{
private class DragDropData
{
public LoLTeamType Team;
public LoLPositionType Position;
}

public class SwapPlayersEventArguments
{
public LoLTeamType Team { get; }

public LoLPositionType PositionA { get; }

public LoLPositionType PositionB { get; }

public SwapPlayersEventArguments(LoLTeamType team, LoLPositionType positionA, LoLPositionType positionB)
{
Team = team;
PositionA = positionA;
PositionB = positionB;
}
}

public event EventHandler<SwapPlayersEventArguments>? SwapPlayers;

public static readonly DependencyProperty PositionProperty = DependencyProperty.Register(nameof(Position), typeof(LoLPositionType), MethodBase.GetCurrentMethod().DeclaringType);

public static readonly DependencyProperty PlayerBlueSideProperty = DependencyProperty.Register(nameof(PlayerBlueSide), typeof(LoLPlayer), MethodBase.GetCurrentMethod().DeclaringType);

public static readonly DependencyProperty PlayerRedSideProperty = DependencyProperty.Register(nameof(PlayerRedSide), typeof(LoLPlayer), MethodBase.GetCurrentMethod().DeclaringType);

public static readonly DependencyProperty CanSwapPlayersProperty = DependencyProperty.Register(nameof(CanSwapPlayers), typeof(bool), MethodBase.GetCurrentMethod().DeclaringType);

public LoLPositionType Position
{
get => (LoLPositionType) GetValue(PositionProperty);
Expand All @@ -32,9 +60,105 @@ public LoLPlayer? PlayerRedSide
set => SetValue(PlayerRedSideProperty, value);
}

public bool CanSwapPlayers
{
get => (bool) GetValue(CanSwapPlayersProperty);
set => SetValue(CanSwapPlayersProperty, value);
}

public PlayerGoldDifferenceView()
{
InitializeComponent();
}

private void ChampionTileBlueSide_OnMouseLeftButtonDown(object sender, MouseEventArgs e)
{
StartDragging(PlayerBlueSide, e);
}

private void ChampionTileRedSide_OnMouseLeftButtonDown(object sender, MouseEventArgs e)
{
StartDragging(PlayerRedSide, e);
}

private void StartDragging(LoLPlayer? player, MouseEventArgs e)
{
if (!CanSwapPlayers)
{
return;
}

if (player == null)
{
return;
}

if (e.LeftButton != MouseButtonState.Pressed)
{
return;
}

DragDrop.DoDragDrop(this, new DataObject(typeof(DragDropData), new DragDropData{Team = player.Team, Position = Position}), DragDropEffects.Move);
}

private void ChampionTileBlueSide_OnDragOver(object sender, DragEventArgs e)
{
HandleDragOver(LoLTeamType.BlueSide, e);
}

private void ChampionTileRedSide_OnDragOver(object sender, DragEventArgs e)
{
HandleDragOver(LoLTeamType.RedSide, e);
}

private void HandleDragOver(LoLTeamType acceptedTeam, DragEventArgs e)
{
e.Effects = AcceptDrop(acceptedTeam, e, out _) ? DragDropEffects.Move : DragDropEffects.None;
e.Handled = true;
}

private void ChampionTileBlueSide_OnDrop(object sender, DragEventArgs e)
{
HandleDrop(LoLTeamType.BlueSide, e);
}

private void ChampionTileRedSide_OnDrop(object sender, DragEventArgs e)
{
HandleDrop(LoLTeamType.RedSide, e);
}

private void HandleDrop(LoLTeamType acceptedTeam, DragEventArgs e)
{
if (!AcceptDrop(acceptedTeam, e, out var dragDropData))
{
return;
}

if (dragDropData.Position == Position)
{
return;
}

SwapPlayers?.Invoke(this, new SwapPlayersEventArguments(dragDropData.Team, dragDropData.Position, Position));
}

private bool AcceptDrop(LoLTeamType acceptedTeam, DragEventArgs e, out DragDropData dragDropData)
{
dragDropData = null!;

if (!e.Data.GetDataPresent(typeof(DragDropData)))
{
return false;
}

dragDropData = (e.Data.GetData(typeof(DragDropData)) as DragDropData)!;

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (ReferenceEquals(dragDropData, null) || dragDropData.Team != acceptedTeam)
{
return false;
}
return true;
}
}
}
4 changes: 1 addition & 3 deletions GoldDiff/View/Controller/GoldDifferenceWindowController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public GoldDifferenceWindowController(GoldDifferenceWindowViewModel? model, LoLG

private void ModelOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (PlayerPropertyNames.Contains(e.PropertyName))
if (string.IsNullOrEmpty(e.PropertyName) || PlayerPropertyNames.Contains(e.PropertyName))
{
UpdateActivePlayerBackground();
}
Expand Down Expand Up @@ -207,7 +207,5 @@ private Dictionary<LoLPositionType, LoLPlayer> OrderPlayers(LoLTeam team, LoLCli

return result;
}


}
}
16 changes: 10 additions & 6 deletions GoldDiff/View/GoldDifferenceWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
x:Name="This"
Style="{DynamicResource {x:Static view:GoldDiffSharedResourceKeys.DefaultWindowStyle}}"

Title="{x:Static properties:GoldDifferenceWindowResources.Title}"
MinWidth="500"
MinHeight="350"
Height="400"
Expand Down Expand Up @@ -53,7 +52,8 @@

<controlElement:PlayerGoldDifferenceView Position="Top"
PlayerBlueSide="{Binding TopPlayerBlueSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
PlayerRedSide="{Binding TopPlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
PlayerRedSide="{Binding TopPlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SwapPlayers="PlayerGoldDifferenceView_OnSwapPlayers" />
</Border>

<Border Grid.Row="1"
Expand All @@ -64,7 +64,8 @@

<controlElement:PlayerGoldDifferenceView Position="Jungle"
PlayerBlueSide="{Binding JunglePlayerBlueSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
PlayerRedSide="{Binding JunglePlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
PlayerRedSide="{Binding JunglePlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SwapPlayers="PlayerGoldDifferenceView_OnSwapPlayers" />

</Border>

Expand All @@ -76,7 +77,8 @@

<controlElement:PlayerGoldDifferenceView Position="Middle"
PlayerBlueSide="{Binding MiddlePlayerBlueSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
PlayerRedSide="{Binding MiddlePlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
PlayerRedSide="{Binding MiddlePlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SwapPlayers="PlayerGoldDifferenceView_OnSwapPlayers" />

</Border>

Expand All @@ -88,7 +90,8 @@

<controlElement:PlayerGoldDifferenceView Position="Bottom"
PlayerBlueSide="{Binding BottomPlayerBlueSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
PlayerRedSide="{Binding BottomPlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
PlayerRedSide="{Binding BottomPlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SwapPlayers="PlayerGoldDifferenceView_OnSwapPlayers" />

</Border>

Expand All @@ -100,7 +103,8 @@

<controlElement:PlayerGoldDifferenceView Position="Support"
PlayerBlueSide="{Binding SupportPlayerBlueSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
PlayerRedSide="{Binding SupportPlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
PlayerRedSide="{Binding SupportPlayerRedSide, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SwapPlayers="PlayerGoldDifferenceView_OnSwapPlayers" />

</Border>
</Grid>
Expand Down
12 changes: 12 additions & 0 deletions GoldDiff/View/GoldDifferenceWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Windows;
using System.Windows.Media;
using GoldDiff.LeagueOfLegends.Game;
using GoldDiff.Shared.LeagueOfLegends;
using GoldDiff.View.ControlElement;
using GoldDiff.View.Controller;
using GoldDiff.View.Model;

Expand Down Expand Up @@ -40,5 +42,15 @@ public GoldDifferenceWindow(LoLGame? game)

PrivateModel = Model;
}

private void PlayerGoldDifferenceView_OnSwapPlayers(object sender, PlayerGoldDifferenceView.SwapPlayersEventArguments e)
{
if (e.PositionA == e.PositionB)
{
return;
}

Model.SwapPlayers(e.Team, e.PositionA, e.PositionB);
}
}
}
Loading

0 comments on commit 9c20731

Please sign in to comment.