Esempio n. 1
0
        private void add_item(object sender, RoutedEventArgs e)
        {
            string default_cat = null;

            if (this.selection is not null)
            {
                if (this.selection.is_category)
                {
                    default_cat = this.selection.name;
                }
                else
                {
                    ElementReference <ItemSpec> sel_ref = this.state.items[this.selection.name];
                    default_cat = sel_ref.element.category.name;
                }
            }
            QueryPrompt[] prompts = new QueryPrompt[] {
                new QueryPrompt("Name:", QueryType.STRING),
                new QueryPrompt("Category:", QueryType.LIST, default_cat, values: this.categories.ToArray(), canEdit: true),
                new QueryPrompt("Cost:", QueryType.FLOAT),
                new QueryPrompt("Weight:", QueryType.FLOAT),
            };
            object[] results = SimpleDialog.askCompound("Add Item", prompts, this);
            if (results is null)
            {
                return;
            }

            string name = results[0] as string, cat_name = results[1] as string;
            double?cost = results[2] as double?, weight = results[3] as double?;

            if ((name is null) || (this.state.items.ContainsKey(name)) || (cat_name is null) || (cost is null) || (weight is null))
            {
                return;
            }

            ItemLibraryItemRow cat_row;
            int cat_idx = this.categories.IndexOf(cat_name);

            if (cat_idx >= 0)
            {
                cat_row = this.item_rows[cat_idx];
            }
            else
            {
                double?sale_value = SimpleDialog.askFloat(cat_name, "Value Factor:", 1.0, step: 0.125, owner: this);
                if (sale_value is null)
                {
                    return;
                }
                cat_row = this.do_add_category(cat_name, (decimal)sale_value);
            }

            ItemSpec item = new ItemSpec(name, this.state.categories[cat_name].element, (decimal)cost, (decimal)weight);

            this.state.items[name] = new ElementReference <ItemSpec>(item);
            this.state.add_category_reference(item);

            ItemLibraryItemRow row = new ItemLibraryItemRow(name);

            if (!this.dirty)
            {
                if (this.selection is not null)
                {
                    this.selection._is_selected = false;
                }
                cat_row._is_expanded = true;
                row._is_selected     = true;
            }
            cat_row._items.Add(row);
            cat_row._items.Sort((x, y) => x.name.CompareTo(y.name));
            this.item_list.Items.Refresh();
        }