Skip to content

Commit

Permalink
Added file browser for adding images to ranking ladder
Browse files Browse the repository at this point in the history
  • Loading branch information
dRoskar committed Jan 21, 2015
1 parent a2bf4ea commit 9a9c7dd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BGCRanker/LadderEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Grid>
<Button x:Name="cancelBtn" Content="Cancel" Margin="0,0,10,10" Click="cancelBtn_Click" HorizontalAlignment="Right" Width="75" Height="22" VerticalAlignment="Bottom"/>
<Button x:Name="saveBtn" Content="Save" Margin="0,0,90,10" Click="saveBtn_Click" Height="22" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75"/>
<DataGrid x:Name="ranksGrid" Margin="10,0,10,37" Height="350" VerticalAlignment="Bottom" Loaded="DataGrid_Loaded"/>
<DataGrid x:Name="ranksGrid" Margin="10,0,10,37" Height="350" VerticalAlignment="Bottom" Loaded="DataGrid_Loaded" BeginningEdit="ranksGrid_BeginningEdit"/>
<Button x:Name="refreshBtn" Content="Refresh" HorizontalAlignment="Right" Margin="0,0,170,10" Width="75" Height="22" VerticalAlignment="Bottom" Click="refreshBtn_Click"/>
<Label Content="Number of levels:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="103"/>
<TextBox x:Name="levelsTextBox" HorizontalAlignment="Left" Height="23" Margin="118,13,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="60" KeyDown="levelsTextBox_KeyDown"/>
Expand Down
114 changes: 113 additions & 1 deletion BGCRanker/LadderEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.IO;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls.Primitives;

namespace BGCRanker
{
Expand All @@ -23,7 +24,6 @@ namespace BGCRanker
/// </summary>
public partial class LadderEditor : Window
{

private String path;
private int levels;
private float formula;
Expand Down Expand Up @@ -254,6 +254,7 @@ private void saveBtn_Click(object sender, RoutedEventArgs e)
writeIsCustom(isCustom);
writeFormula(formula);
writeLevels(levels);
writeHasData(hasData);

// erase old level data from file
eraseGridDataFromFile();
Expand Down Expand Up @@ -420,6 +421,59 @@ public String getPlayerImage(int victories)
int level = getPlayerLevel(victories);
return ladder[level - 1].ImageUri;
}

private void ranksGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
if (e.Column.Header.ToString().Equals("ImageUri"))
{
e.Cancel = true;

// show dialog for image selection
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();

// set file dialog filters
ofd.Filter = "Image files|*.jpeg;*.png;*.jpg";

// display open file dialog
Nullable<bool> result = ofd.ShowDialog();

// get file name
if (result == true)
{
String originalFilePath = ofd.FileName;

// trim the file name
String fileName = originalFilePath.Substring(originalFilePath.LastIndexOf("\\") + 1);
String newPath = path.Substring(0, path.LastIndexOf("\\") + 1) + "icons";

// check id icon directory exists
if (!Directory.Exists(newPath))
{
// create icons directory
Directory.CreateDirectory(newPath);
}

// copy file to data directory
newPath = newPath + "\\" + fileName;
try
{
File.Copy(originalFilePath, newPath);
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show("Unable to use the file; Restricted access!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

// record file name to data grid
ExtensionMethods.GetCell(ranksGrid, e.Row, e.Column.DisplayIndex).Content = fileName;
ladder.ElementAt(e.Row.GetIndex()).ImageUri = fileName;
}
}
}
}


Expand All @@ -440,4 +494,62 @@ public Level(int number, int requirement, String rankName, String imageUri, Stri
this.ImageUrl = imageUrl;
}
}

public static class ExtensionMethods
{
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
public static DataGridRow GetRow(this DataGrid grid, int index)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// May be virtualized, bring into view and try again.
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}

DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
}
}
2 changes: 1 addition & 1 deletion BGCRanker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private void rankImage_Loaded(object sender, RoutedEventArgs e)
missingPng = new BitmapImage();
missingPng.BeginInit();
string directory = System.IO.Directory.GetCurrentDirectory();
missingPng.UriSource = new Uri(directory + "//images//missing.png");
missingPng.UriSource = new Uri(directory + "\\images\\missing.png");
missingPng.EndInit();

rankImage.Source = missingPng;
Expand Down

0 comments on commit 9a9c7dd

Please sign in to comment.