// This file is part of the Noddybox.Emulation C# suite.
//
// Noddybox.Emulation is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Noddybox.Emulation is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Noddybox.Emulation. If not, see .
//
// Copyright (c) 2012 Ian Cowburn
//
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Noddybox.Emulation.Keyboard.Schema;
using System.Xml;
namespace EmuKeyboardDesigner
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private bool RunningOps {get; set;}
private void SetKey(bool select, bool allowNew)
{
string sym = keysymTextBox.Text;
if (!String.IsNullOrWhiteSpace(sym))
{
int index = keyList.FindStringExact(sym);
if (index > -1)
{
KeyboardKey key = (KeyboardKey)keyList.Items[index];
key.X = imageControl.KeyX;
key.Y = imageControl.KeyY;
key.Width = imageControl.KeyWidth;
key.Height = imageControl.KeyHeight;
key.Sticky = stickyCheckBox.Checked;
if (select)
{
keyList.SelectedItem = key;
}
}
else if (allowNew)
{
KeyboardKey key = new KeyboardKey()
{
KeySymbol = sym,
X = imageControl.KeyX,
Y = imageControl.KeyY,
Width = imageControl.KeyWidth,
Height = imageControl.KeyHeight,
Sticky = stickyCheckBox.Checked
};
keyList.Items.Add(key);
keyList.SelectedItem = key;
}
}
}
private void OnSetKey(object sender, EventArgs e)
{
SetKey(true, true);
}
private void OnMoveUp(object sender, EventArgs e)
{
if (keyList.SelectedIndex > 0)
{
object o = keyList.Items[keyList.SelectedIndex-1];
keyList.Items[keyList.SelectedIndex - 1] = keyList.Items[keyList.SelectedIndex];
keyList.Items[keyList.SelectedIndex] = o;
keyList.SelectedIndex--;
}
}
private void OnMoveDown(object sender, EventArgs e)
{
if (keyList.SelectedIndex > -1 && keyList.SelectedIndex < keyList.Items.Count - 1)
{
object o = keyList.Items[keyList.SelectedIndex];
keyList.Items[keyList.SelectedIndex] = keyList.Items[keyList.SelectedIndex + 1];
keyList.Items[keyList.SelectedIndex+1] = o;
keyList.SelectedIndex++;
}
}
private void OnLoad(object sender, EventArgs e)
{
OpenFileDialog fsel = new OpenFileDialog();
fsel.Filter = "Keyboard Files (*.keyboard)|*.keyboard";
if (fsel.ShowDialog() == DialogResult.OK)
{
try
{
BinaryReader stream = new BinaryReader(File.OpenRead(fsel.FileName), Encoding.UTF8);
KeyboardDefinition def = KeyboardDefinition.Load(stream);
keyList.Items.Clear();
foreach (KeyboardKey k in def.Definitions)
{
keyList.Items.Add(k);
}
if (keyList.Items.Count > 0)
{
keyList.SelectedIndex = 0;
}
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("Failed to load " + fsel.FileName + "\n\n" + ex.Message, ex.GetType().ToString(), MessageBoxButtons.OK);
}
}
}
private void OnSave(object sender, EventArgs e)
{
SaveFileDialog fsel = new SaveFileDialog();
fsel.Filter = "Keyboard Files (*.keyboard)|*.keyboard";
fsel.AddExtension = true;
if (fsel.ShowDialog() == DialogResult.OK)
{
try
{
KeyboardDefinition def = new KeyboardDefinition();
foreach (KeyboardKey k in keyList.Items)
{
def.Definitions.Add(k);
}
BinaryWriter stream = new BinaryWriter(File.Create(fsel.FileName), Encoding.UTF8);
def.Save(stream);
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("Failed to save " + fsel.FileName + "\n\n" + ex.Message, ex.GetType().ToString(), MessageBoxButtons.OK);
}
}
}
private void LoadKey(KeyboardKey key)
{
if (key != null)
{
keysymTextBox.Text = key.KeySymbol;
imageControl.KeyX = key.X;
imageControl.KeyY = key.Y;
imageControl.KeyWidth = key.Width;
imageControl.KeyHeight = key.Height;
stickyCheckBox.Checked = key.Sticky;
}
}
private void OnKeySelected(object sender, EventArgs e)
{
if (keyList.SelectedIndex > -1)
{
if (!RunningOps)
{
SetKey(false, false);
}
LoadKey((KeyboardKey)keyList.SelectedItem);
}
}
private void OnRemoveKey(object sender, EventArgs e)
{
if (keyList.SelectedIndex > -1)
{
int index = keyList.SelectedIndex;
keyList.Items.RemoveAt(index);
if (index > keyList.Items.Count - 1)
{
index--;
}
if (index > -1)
{
keyList.SelectedIndex = index;
}
}
}
private void OnOps(object sender, EventArgs e)
{
OpsForm f = new OpsForm();
if (f.ShowDialog() == DialogResult.OK)
{
RunningOps = true;
SetKey(false, false);
int index = keyList.SelectedIndex;
List l = new List();
foreach (KeyboardKey k in keyList.Items)
{
// I know -- these blocks are *real* lazy
//
try {if (f.RemoveFront > 0) k.KeySymbol = k.KeySymbol.Remove(0, f.RemoveFront);} catch {}
try {if (f.RemoveRear > 0) k.KeySymbol = k.KeySymbol.Remove(k.KeySymbol.Length - f.RemoveRear);} catch {}
k.KeySymbol = f.Prefix + k.KeySymbol;
k.KeySymbol = k.KeySymbol + f.Suffix;
k.X = Math.Max(0, k.X + f.AdjustX);
k.Y = Math.Max(0, k.Y + f.AdjustY);
k.Width = Math.Max(0, k.Width + f.AdjustWidth);
k.Height = Math.Max(0, k.Height + f.AdjustHeight);
l.Add(k);
}
keyList.BeginUpdate();
keyList.Items.Clear();
keyList.Items.AddRange(l.ToArray());
keyList.EndUpdate();
keyList.SelectedIndex = index;
RunningOps = false;
}
}
}
}