view FrmNewUnitFromTree.cs @ 249:a11faa64c333

Re #354: Single-button Add Unit * Add right-click menu (doesn't work yet because of position maths)
author IBBoard <dev@ibboard.co.uk>
date Sat, 20 Aug 2011 17:05:45 +0100
parents 19a88383dfa7
children 694f23814ec2
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API;

namespace IBBoard.WarFoundry.GUI.WinForms
{
    public partial class FrmNewUnitFromTree : Form
    {
        public FrmNewUnitFromTree()
        {
            InitializeComponent();
            WarFoundryCore.ArmyChanged += new ArmyChangedDelegate(ArmyChanged);
        }

        private void ArmyChanged(Army oldArmy, Army newArmy)
        {
            unitTypeTree.Nodes.Clear();

            if (newArmy != null)
            {
                BuildTree(newArmy.Race);
            }
        }

        private void BuildTree(Race race)
        {
            foreach (Category cat in race.Categories)
            {
                UnitType[] unitTypes = race.GetUnitTypes(cat);
                TreeNode[] unitNodes = new TreeNode[unitTypes.Length];

                for (int i = 0; i < unitTypes.Length; i++)
                {
                    UnitType unitType = unitTypes[i];
                    TreeNode temp = new TreeNode(unitType.Name);
                    temp.Tag = unitType;
                    unitNodes[i] = temp;
                }

                TreeNode catTemp = new TreeNode(cat.Name, unitNodes);
                catTemp.Tag = cat;
                unitTypeTree.Nodes.Add(catTemp);
                catTemp.ExpandAll();
            }

            unitTypeTree.Show();
        }

        private void unitTypeTree_DoubleClick(object sender, EventArgs e)
        {
            AddUnitType();
        }

        private void AddUnitType()
        {
            TreeNode selected = unitTypeTree.SelectedNode;
            AddUnitType(selected);
        }

        private void AddUnitType(TreeNode selected)
        {
            object tagData = selected.Tag;

            if (tagData is UnitType)
            {
                UnitType unit = (UnitType)tagData;
                Category cat = (Category)selected.Parent.Tag;
                MdiParent.CreateUnitInCategory(unit, cat);
            }
        }

        public new FrmMain MdiParent
        {
            get { return (FrmMain)base.MdiParent; }
            set { base.MdiParent = value; }
        }

        private void AddUnitClicked(object sender, EventArgs e)
        {
            //FIXME: We need to do some kind of maths here
            // context menu position appears to be relative to the window, not the tree control!
            int left = contextMenu.Left - unitTypeTree.Left;
            int top = contextMenu.Top - unitTypeTree.Top;
            TreeNode node = unitTypeTree.GetNodeAt(left, top);

            if (node != null)
            {
                AddUnitType(node);
            }
        }
    }
}