Skip to content

Commit

Permalink
Merge pull request #41 from BlackLamb/Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
quandtm committed Feb 6, 2012
2 parents 2289284 + 1100fc6 commit 31daafc
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 33 deletions.
Expand Up @@ -61,6 +61,7 @@ private void BindMenuItems()
var loginBtn = window.GetChild<Button>("btnLogin");
var logoutBtn = window.GetChild<Button>("btnLogout");
var registerBtn = window.GetChild<Button>("btnRegister");
var settingsBtn = window.GetChild<Button>("btnSettings");
var status = window.GetChild<TextBlock>("status");
if (status != null)
status.Enabled = false;
Expand All @@ -85,6 +86,15 @@ private void BindMenuItems()
};
}

if (settingsBtn != null)
{
settingsBtn.Triggered +=
(b) =>
{
Manager.TransitionTo<SettingsScreen>();
};
}

var exitBtn = window.GetChild<Button>("btnExit");
if (exitBtn != null)
exitBtn.Triggered += (b) => Manager.ExitGame();
Expand Down Expand Up @@ -137,36 +147,16 @@ private void SavePlayer()
if (Player == null)
return;

using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
using (var file = iso.OpenFile("player.dat", FileMode.Create, FileAccess.Write))
using (var bw = new BinaryWriter(file))
{
bw.Write(Player.Id.ToString());
bw.Write(Player.Name);
bw.Write(Player.ApiKey);
}
Settings.Instance.CurrentPlayer = Player;
}

private void LoadPlayer()
{
if (Player != null)
return;

using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists("player.dat"))
{
using (var file = iso.OpenFile("player.dat", FileMode.Open, FileAccess.Read))
using (var br = new BinaryReader(file))
{
Player = new Player();
Player.Id = Guid.Parse(br.ReadString());
Player.Name = br.ReadString();
Player.ApiKey = br.ReadString();
Login();
}
}
}
if (Settings.Instance.CurrentPlayer != null)
Player = Settings.Instance.CurrentPlayer;
}

private void Login()
Expand Down
Expand Up @@ -17,12 +17,13 @@ class SettingsScreen : BaseScreen
private SpriteBatch sb;
private Window gui;
private WP7Touch touch;
private Settings settings;

private Button btnSound;
private TextBlock lblPlayer;

private const string soundOn = "Sound: On";
private const string soundOff = "Sound: Off";
private const string playerText = "Player: {0}";

public override void LoadContent()
{
Expand All @@ -36,8 +37,8 @@ public override void LoadContent()
gui.LoadGraphics(Manager.GraphicsDevice, content);
touch = new WP7Touch(gui);
touch.EnableTap();
settings = new Settings();
BindInput();
lblPlayer = gui.GetChild<TextBlock>("lblPlayer");
LoadSettings();
IsReady = true;
Expand All @@ -52,7 +53,7 @@ private void BindInput()
btnSound.Triggered +=
(b) =>
{
settings.Sound = !settings.Sound;
Settings.Instance.IsMute = !Settings.Instance.IsMute;
UpdateSoundLabel();
};
}
Expand All @@ -64,7 +65,7 @@ private void LoadSettings()

private void UpdateSoundLabel()
{
if (settings.Sound)
if (!Settings.Instance.IsMute)
btnSound.Text = soundOn;
else
btnSound.Text = soundOff;
Expand Down Expand Up @@ -92,5 +93,15 @@ public override void Draw(double elapsedSeconds, GraphicsDevice device)

base.Draw(elapsedSeconds, device);
}

public override void OnNavigatedTo()
{
if (Settings.Instance.CurrentPlayer != null)
{
lblPlayer.Text = String.Format(playerText, Settings.Instance.CurrentPlayer.Name);
}

base.OnNavigatedTo();
}
}
}
@@ -1,19 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;
using SamuraiServer.Data;

namespace Samurai.Client.Wp7
{
internal class Settings
{
// Version of settings file so we can migrate data if needed
const int SETTINGSVERSION = 1;

// Settings loaded from isolated storage
public IsolatedStorageSettings SettingsStore { get; set; }

// Lets make it a singleton
private static volatile Settings _instance = null;
public static Settings Instance
{
get
{
if (_instance == null)
{
lock (typeof(Settings))
{
_instance = new Settings();
_instance.LoadSettings();
}
}
return _instance;
}
}

public Settings()
{
SettingsStore = IsolatedStorageSettings.ApplicationSettings;

if (LoadSetting<int>("Version", 0) != SETTINGSVERSION)
RunSettingMigrate(LoadSetting<int>("Version", 0));
}

/// <summary>
/// Updates settings files to the current version and migrate data if needed
/// </summary>
/// <param name="fileVersion">Version that current settings file is at</param>
private void RunSettingMigrate(int fileVersion)
{
switch (fileVersion)
{
case 0:
if (CurrentPlayer != null)
return;

using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
// Load old player.dat and save it to new settings
if (iso.FileExists("player.dat"))
{
using (var file = iso.OpenFile("player.dat", FileMode.Open, FileAccess.Read))
using (var br = new BinaryReader(file))
{
var player = new Player();
player.Id = Guid.Parse(br.ReadString());
player.Name = br.ReadString();
player.ApiKey = br.ReadString();
CurrentPlayer = player;
}
iso.DeleteFile("player.dat");
}
}

UpdateSetting<bool>("IsMute", LoadSetting<bool>("Sound", false));

SettingsStore.Remove("Sound");
SettingsStore.Save();

UpdateSetting<int>("Version", 1);
return;

case 1:
return;

default:
return;
}
}

/// <summary>
Expand Down Expand Up @@ -51,6 +121,44 @@ protected T LoadSetting<T>(string key, T defaultValue)
return setting;
}

public bool Sound { get { return LoadSetting("Sound", false); } set { UpdateSetting("Sound", value); } }
/// <summary>
/// Loads all the settings and stores it in the settings class
/// </summary>
private void LoadSettings()
{
_currentPlayer = LoadSetting<Player>("CurrentPlayer", null);
_isMute = LoadSetting<bool>("IsMute", _isMute);
}

// Settings
private bool _isMute = false;
private Player _currentPlayer;

public Player CurrentPlayer
{
get { return _currentPlayer; }
set
{
if (_currentPlayer != value)
{
_currentPlayer = value;
UpdateSetting<Player>("CurrentPlayer", _currentPlayer);
}
}
}

public bool IsMute
{
get { return _isMute; }
set
{
if (_isMute != value)
{
_isMute = value;
UpdateSetting<bool>("IsMute", _isMute);
}
}
}

}
}
Expand Up @@ -10,6 +10,10 @@
<TextBlock Format="{parenttext}" Font="MainMenuFont.spritefont" Color="#000000" />
</Button>

<Button Name="btnSettings" Text="Settings" Visibility="Enabled">
<TextBlock Format="{parenttext}" Font="MainMenuFont.spritefont" Color="#000000" />
</Button>

<!--<Button Text="Quickmatch" Visibility="Enabled">
<TextBlock Format="{parenttext}" Font="MainMenuFont.spritefont" Color="#000000" />
</Button>
Expand Down
@@ -1,8 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<Window Size="100%">
<StackPanel>
<Button Name="btnSound" Text="Sound">
<TextBlock Font="MenuFont.spritefont" Color="#FFFFFF" Format="{parenttext}" />
<Image src="Textures\samurai_background_notext.png" Size="100%" />
<StackPanel Position="15%" Size="60%, 60%">
<TextBlock Font="MainMenuFont.spritefont" Color="#CC0033" Format="{text}" Text="Settings" />

<Button Name="btnSound" Text="Sound" Visibility="Enabled">
<TextBlock Format="{parenttext}" Font="MainMenuFont.spritefont" Color="#000000" />
</Button>

<TextBlock Font="MainMenuFont.spritefont" Color="#000000" Format="{text}" Text=" " />

<TextBlock Name="lblPlayer" Font="MainMenuFont.spritefont" Color="#000000" Format="{text}" Text="Player: " />
</StackPanel>
</Window>

0 comments on commit 31daafc

Please sign in to comment.