using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Threading;
using WPFMachine.Screen;
using Frotz.Screen;
using Frotz.Constants;
namespace WPFMachine
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
ZMachineScreen _screen;
Thread _zThread;
List LastPlayedGames = new List();
bool closeOnQuit = false;
String _storyFileName;
Frotz.Blorb.Blorb _blorbFile;
public MainWindow()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
InitializeComponent();
Properties.Settings.Default.Upgrade();
Border b = new Border();
b.BorderThickness = new Thickness(1);
b.BorderBrush = Brushes.Black;
// _screen = new Screen.TextControlScreen(this);
_screen = new Absolute.AbsoluteScreen(this);
pnlScreenPlaceholder.Children.Add(b);
b.Child = (UIElement)_screen;
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
if (Properties.Settings.Default.LastPlayedGames != null)
{
var games = Properties.Settings.Default.LastPlayedGames.Split('|');
LastPlayedGames = new List(games);
}
buildMainMenu();
this.SizeChanged += new SizeChangedEventHandler(MainWindow_SizeChanged);
this.TextInput += new TextCompositionEventHandler(MainWindow_TextInput);
this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
statusBottom.Visibility = System.Windows.Visibility.Hidden;
setFrotzOptions();
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("EX:" + e.ExceptionObject);
}
void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
// I can capture the arrow keys here
if (!mnuInGame.IsFocused)
{
char c = '\0';
switch (e.Key)
{
case Key.Tab:
c = '\t'; break;
case Key.Up:
c = (char)Frotz.Constants.CharCodes.ZC_ARROW_UP;
break;
case Key.Down:
c = (char)Frotz.Constants.CharCodes.ZC_ARROW_DOWN;
break;
case Key.Left:
c = (char)Frotz.Constants.CharCodes.ZC_ARROW_LEFT;
break;
case Key.Right:
c = (char)Frotz.Constants.CharCodes.ZC_ARROW_RIGHT;
break;
}
if (c != 0)
{
_screen.AddInput(c);
e.Handled = true;
}
}
}
void MainWindow_TextInput(object sender, TextCompositionEventArgs e)
{
if (_screen != null)
{
if (e.Text.Length > 0)
{
_screen.AddInput(e.Text[0]);
}
if (e.SystemText.Length > 0)
{
ushort newKey = convertAltText(e.SystemText);
if (newKey != '\0')
{
_screen.AddInput((char)newKey);
}
}
}
}
// I'd like to make this a return char
private ushort convertAltText(String text)
{
char k = text.ToLower()[0];
switch (k)
{
case 'h': return CharCodes.ZC_HKEY_HELP;
case 'd': return CharCodes.ZC_HKEY_DEBUG;
case 'p': return CharCodes.ZC_HKEY_PLAYBACK;
case 'r': return CharCodes.ZC_HKEY_RECORD;
case 's': return CharCodes.ZC_HKEY_SEED;
case 'u': return CharCodes.ZC_HKEY_UNDO;
case 'n': return CharCodes.ZC_HKEY_RESTART;
case 'x': return CharCodes.ZC_HKEY_QUIT;
}
return 0;
}
private void buildMainMenu()
{
miRecentGames.Items.Clear();
miGames.Items.Clear();
foreach (String s in LastPlayedGames)
{
MenuItem mi = new MenuItem();
mi.Header = s;
mi.Tag = s;
mi.Click += new RoutedEventHandler(miMru_Click);
miRecentGames.Items.Add(mi);
}
setupGameDirectories();
}
private void setupGameDirectories()
{
String gameDirectories = Properties.Settings.Default.GameDirectoryList;
miGames.Items.Clear();
if (!String.IsNullOrWhiteSpace(gameDirectories))
{
String[] list = gameDirectories.Split(';');
if (list.Length == 1)
{
addFilesInPath(list[0], miGames, true);
if (miGames.Items.Count == 1)
{
MenuItem mi = miGames.Items[0] as MenuItem;
List