private void OnCreateNewGame(CCBGameTemplate template, string name)
        {
            CCBGame         newGame  = m_games.AddGame(name, template);
            CCBTreeViewGame gameNode = new CCBTreeViewGame(newGame);

            tvGames.Items.Add(gameNode);
            AddBagsToGameNode(gameNode);
            AddOrMoveAdder();
        }
        private void AddBagsToGameNode(CCBTreeViewGame gameNode)
        {
            CCBGame game = gameNode.Game;

            AddBagToNode(gameNode, game.GroupItems);
            if (null != game.GroupBags)
            {
                foreach (CCBBag bag in game.GroupBags)
                {
                    AddBagToNode(gameNode, bag);
                }
            }
        }
        private void OnBagPickerClicked(object sender, RoutedEventArgs evt)
        {
            CCBTreeViewGame gameNode = FindGameFromNode(GetSelectedNode());
            CCBTreeViewBag  bagNode  = FindBagFromNode(GetSelectedNode());

            System.Diagnostics.Debug.Assert(null != bagNode);
            BagItemPicker bagPickerWnd = new BagItemPicker(new BagItemPicker.BagInfo(bagNode.ID, bagNode.Bag, gameNode.Game.GetAllBags(bagNode.Bag)));

            bagPickerWnd.Owner = this;
            bagPickerWnd.CopyBagItemsCallback   = new DOnCopyBagItems(OnCopyBagItems);
            bagPickerWnd.DeleteBagItemsCallback = new DOnDeleteBagItems(OnDeleteBagItems);
            bagPickerWnd.Show();
        }
        private CCBGame FindCurrentGame(bool defaultToTop = false)
        {
            CCBTreeViewGame gameNode = FindCurrentGameNode();

            if (defaultToTop && (null == gameNode))
            {
                gameNode = FindTopGameNode();
            }
            if (null != gameNode)
            {
                return(gameNode.Game);
            }
            return(null);
        }
        private void OpenTemplates()
        {
            CCBTreeViewItem selItem   = (CCBTreeViewItem)tvGames.SelectedItem;
            CCBGame         gameModel = null;

            if (null != selItem)
            {
                CCBTreeViewGame gameNode = FindGameFromNode(selItem);

                if (null != gameNode)
                {
                    gameModel = gameNode.Game;
                }
            }
            CCBChildWindow templatePickerWnd = new GameTemplatePicker(gameModel, m_onCreateNewGameD, m_onCreateNewTemplateD, m_games.TemplateList);

            //templatePickerWnd.Owner = this;
            templatePickerWnd.Show(this);
        }
        private CCBTreeViewGame FindGameFromNode(TreeViewItem node)
        {
            while (null != node)
            {
                if (node is CCBTreeViewGame)
                {
                    CCBTreeViewGame game = (CCBTreeViewGame)node;

                    if (null != game)
                    {
                        return(game);
                    }
                }
                if (node.Parent is TreeViewItem)
                {
                    node = (TreeViewItem)node.Parent;
                }
                else
                {
                    break;
                }
            }
            return(null);
        }
        public void PopulateCharacterList()
        {
            tvGames.Items.Clear();
            foreach (CCBGame game in m_games)
            {
                CCBTreeViewGame newGameItem = new CCBTreeViewGame(game);
                int             ix          = tvGames.Items.Add(newGameItem);

                newGameItem.StartBulkEdit();
                foreach (CCBCharacter character in game.Characters)
                {
                    CCBTreeViewCharacter newCharacterNode = newGameItem.Add(character);

                    newCharacterNode.StartBulkEdit();
                    AddPropertiesToCharacterNode(newCharacterNode);
                    AddBagsToCharacterNode(newCharacterNode);

                    newCharacterNode.EndBulkEdit();
                }
                AddBagsToGameNode(newGameItem);
                newGameItem.EndBulkEdit();
            }
            AddOrMoveAdder();
        }
        private void Save()
        {
            CEditMode editMode = CEditModeProperty.GetEditNode(tbItem);

            if (null == editMode)
            {
                return;
            }
            if (EEditMode.em_Frozen == editMode.EditMode)
            {
                return;
            }
            switch (editMode.EditMode)
            {
            case EEditMode.em_None:
                tbLastError.Text = "No mode.";
                break;

            case EEditMode.em_AddCharacter:
            {
                CCBTreeViewGame currentGameNode = FindGameFromNode(editMode.Node);

                if ((null == currentGameNode) || (CCBItemType.itpGame != currentGameNode.ItemType))
                {
                    tbLastError.Text = "No game selected.";
                }
                else
                {
                    CCBCharacter         newCharacter  = new CCBCharacter(tbItem.Text);
                    CCBTreeViewCharacter characterNode = currentGameNode.Add(newCharacter);

                    currentGameNode.Game.AddCharacter(newCharacter);
                    characterNode.StartBulkEdit();
                    AddPropertiesToCharacterNode(characterNode);
                    AddBagsToCharacterNode(characterNode);
                    characterNode.EndBulkEdit();
                    characterNode.IsSelected = true;
                }
                break;
            }

            case EEditMode.em_AddGame:
            {
                CCBGame         newGame  = m_games.AddGame(tbItem.Text);
                CCBTreeViewGame gameNode = new CCBTreeViewGame(newGame);

                tvGames.Items.Add(gameNode);
                AddBagToNode(gameNode, newGame.GroupItems);
                AddOrMoveAdder();
                gameNode.IsSelected = true;
                break;
            }

            case EEditMode.em_AddProperty:
            {
                CCBTreeViewCharacter characterNode = FindCharacterFromNode(editMode.Node);
                CCBTreeViewGame      gameNode      = FindGameFromNode(editMode.Node);

                if (null == characterNode)
                {
                    tbLastError.Text = "Internal error[p]: Cannot find character node.";
                    return;
                }
                CCBCharacterProperty newProperty = characterNode.Character.AddProperty(tbItem.Text, tbValue.Text);

                characterNode.Add(newProperty);
                if (null != gameNode)
                {
                    CCBGame game = gameNode.Game;

                    game.AddPropertyToTemplate(newProperty);
                }
                break;
            }

            case EEditMode.em_AddBag:
            {
                CCBTreeViewCharacter characterNode = FindCharacterFromNode(editMode.Node);

                if (null == characterNode)
                {
                    tbLastError.Text = "Internal error[b]: Cannot find node.";
                    return;
                }
                CCBBag newBag = characterNode.Character.AddBag(tbItem.Text);

                TreeViewItem newNode = characterNode.Add(newBag);
                newNode.IsSelected = true;
                break;
            }

            case EEditMode.em_AddBagItem:
            {
                CCBTreeViewBag bagNode     = FindBagFromNode(editMode.Node);
                bool           isCountable = true == cbCountable.IsChecked;
                CCBBagItem     newItem;

                if (null == bagNode)
                {
                    tbLastError.Text = "Internal error[b]: Cannot find bag node.";
                    return;
                }
                if (true == cbCountable.IsChecked)
                {
                    int count = 1;

                    if (!Int32.TryParse(tbValue.Text, out count))
                    {
                        tbLastError.Text = string.Format("Use number. Could not parse [{0}]", tbValue.Text);
                    }
                    newItem = bagNode.Bag.AddCountableItem(tbItem.Text, count);
                }
                else
                {
                    newItem = bagNode.Bag.AddItem(tbItem.Text);
                }
                TreeViewItem newNode = bagNode.Add(newItem);
                newNode.IsSelected = true;
                break;
            }

            case EEditMode.em_ModifyCharacter:
                if (null == editMode.Node)
                {
                    tbLastError.Text = "Internal error[mc]: No edit node.";
                    return;
                }
                editMode.Node.Header         = tbItem.Text;
                editMode.Node.Character.Name = tbItem.Text;
                break;

            case EEditMode.em_ModifyGame:
            {
                CCBTreeViewGame currentGameNode = FindGameFromNode(editMode.Node);

                currentGameNode.Game.Name = tbItem.Text;
                currentGameNode.Header    = tbItem.Text;
                break;
            }

            case EEditMode.em_ModifyProperty:
                if (null == editMode.Node)
                {
                    tbLastError.Text = "Internal error[mp]: No edit node.";
                    return;
                }
                editMode.Node.Header         = tbItem.Text;
                editMode.Node.Property.Name  = tbItem.Text;
                editMode.Node.Property.Value = tbValue.Text;
                break;

            case EEditMode.em_ModifyBag:
                if (null == editMode.Node)
                {
                    tbLastError.Text = "No bag node.";
                    return;
                }
                editMode.Node.Header   = tbItem.Text;
                editMode.Node.Bag.Name = tbItem.Text;
                break;

            case EEditMode.em_ModifyBagItem:
                if (null == editMode.Node)
                {
                    tbLastError.Text = "No bag item node.";
                    return;
                }
                editMode.Node.Header       = tbItem.Text;
                editMode.Node.BagItem.Item = tbItem.Text;
                if (editMode.Node.BagItem.IsCountable)
                {
                    int count;

                    if (Int32.TryParse(tbValue.Text, out count))
                    {
                        editMode.Node.BagItem.Count = count;
                    }
                    else
                    {
                        tbLastError.Text = string.Format("{0} not a number", tbValue.Text);
                    }
                }
                break;

            default:
                tbLastError.Text = "Unknown mode.";
                break;
            }
        }
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            m_deleteUsed = true;
            if (m_deleteEnabled)
            {
                CCBTreeViewItem selItem = (CCBTreeViewItem)tvGames.SelectedItem;

                if (null == selItem)
                {
                    tbLastError.Text = "Wrong item in treeview:";
                }
                else
                {
                    switch (selItem.ItemType)
                    {
                    case CCBItemType.itpCharacter:
                    {
                        CCBCharacter character = selItem.Character;

                        if (null == character)
                        {
                            tbLastError.Text = String.Format("Mismatch in CBTVI selected ({0})", selItem.ItemType);
                        }
                        else
                        {
                            CCBTreeViewGame gameNode = FindGameFromNode(selItem);

                            if (null == gameNode)
                            {
                                tbLastError.Text = "Internal error: cannot find game node.";
                            }
                            else
                            {
                                CCBGame game = gameNode.Game;

                                gameNode.Items.Remove(selItem);
                                game.DeleteCharacter(character);
                            }
                        }
                        break;
                    }

                    case CCBItemType.itpGame:
                    {
                        CCBGame game = selItem.Game;

                        if (null == game)
                        {
                            tbLastError.Text = String.Format("Mismatch in CBTVI selected ({0})", selItem.ItemType);
                        }
                        else
                        {
                            tvGames.Items.Remove(selItem);
                            m_games.DeleteGameSafe(game);
                        }
                        break;
                    }

                    case CCBItemType.itpProperty:
                    {
                        CCBCharacterProperty property = selItem.Property;

                        if (null == property)
                        {
                            tbLastError.Text = String.Format("Mismatch in CBTVI selected ({0})", selItem.ItemType);
                        }
                        else
                        {
                            CCBTreeViewCharacter characterNode = FindCharacterFromNode(selItem);
                            CCBTreeViewGame      gameNode      = FindGameFromNode(selItem);

                            if (null != characterNode)
                            {
                                characterNode.Items.Remove(selItem);
                                characterNode.Character.RemovePropertySafe(property);
                                if (null != gameNode)
                                {
                                    CCBGame game = gameNode.Game;

                                    game.CheckPropertyForDeletion(property.Name);
                                }
                            }
                        }
                        break;
                    }

                    case CCBItemType.itpBag:
                    {
                        CCBBag bag = selItem.Bag;

                        if (null == bag)
                        {
                            tbLastError.Text = String.Format("Mismatch in CBTVI selected ({0})", selItem.ItemType);
                        }
                        else
                        {
                            CCBTreeViewCharacter characterNode = FindCharacterFromNode(selItem);

                            if (null != characterNode)
                            {
                                characterNode.Character.RemoveBag(bag);
                            }
                            characterNode.Items.Remove(selItem);
                        }
                        break;
                    }

                    case CCBItemType.itpBagItem:
                    {
                        CCBBagItem bagItem = selItem.BagItem;

                        if (null == bagItem)
                        {
                            tbLastError.Text = String.Format("Mismatch in CBTVI selected ({0})", selItem.ItemType);
                        }
                        else
                        {
                            CCBTreeViewBag bagNode = FindBagFromNode(selItem);

                            if (null != bagNode)
                            {
                                bagNode.Bag.RemoveItem(bagItem);
                            }
                            bagNode.Items.Remove(selItem);
                        }
                        break;
                    }

                    default:
                        break;
                    }
                }
            }
            else
            {
                m_deleteEnabled   = true;
                btnDelete.Content = "Delete Selected";
            }
        }