コード例 #1
0
        private void toolImportCSV_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter   = "CSV Files|*.csv";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                /*var lines*/
                IEnumerable <string[]> lines = File.ReadAllLines(openFileDialog1.FileName).Select(a => a.Split(','));

                List <string> duplicates = new List <string>();

                foreach (string[] line in lines)
                {
                    if (line.Count() < 1)
                    {
                        continue;
                    }
                    ItemExtendable itemData = new ItemExtendable();

                    int ID = 0;
                    int.TryParse(line[0], out ID);
                    if (CurrentProject.itemCollection.ContainsKey(ID))
                    {
                        ID = CurrentProject.itemCollection.Count + 1;
                    }
                    itemData.ID = ID;

                    int i = 1;
                    foreach (KeyValuePair <string, DefinitionData> entry in CurrentProject.attributeDefinitions)
                    {
                        if (i < line.Count())
                        {
                            itemData.setValue(entry.Key, line[i]);
                            i++;
                            continue;
                        }

                        itemData.setValue(entry.Key, "");
                    }

                    CurrentProject.itemCollection.Add(itemData.ID, itemData);
                }

                renderItemList();
            }
        }
コード例 #2
0
        private void editItem(bool edit, int selectedID)
        {
            if (CurrentProject == null)
            {
                return;
            }

            int itemID = -1;

            if (edit)
            {
                int.TryParse(itemListViewExt.Items[selectedID].Text, out itemID);
            }

            //ListViewItem newItem = itemListView.Items.Add("Item " + itemListView.Items.Count.ToString());
            using (Edit form = new Edit())
            {
                int            counter     = 10;
                List <Control> controlList = new List <Control>();

                if (edit)
                {
                    form.itemID.Text = itemID.ToString();
                    form.EditID      = itemID;
                }

                #region render_controls
                // render controls
                foreach (KeyValuePair <string, DefinitionData> definition in CurrentProject.attributeDefinitions)
                {
                    Label l = new Label();
                    l.Text   = definition.Key + ":";
                    l.Top    = counter;
                    l.Left   = 20;
                    l.Height = 13;
                    form.Controls.Add(l);

                    counter += 15;

                    switch (definition.Value.DataType)
                    {
                    case DefinitionDataType.Bool:
                        CheckBox c = new CheckBox();
                        c.Top  = counter;
                        c.Left = 20;
                        c.Tag  = definition.Key;
                        if (edit)
                        {
                            c.Checked = CurrentProject.itemCollection[itemID].getValue(definition.Key) == "1";
                        }
                        form.Controls.Add(c);
                        controlList.Add(c);
                        break;

                    case DefinitionDataType.Int:
                    case DefinitionDataType.String:

                        if (definition.Value.GroupLink > -1)
                        {
                            ComboBox t = new ComboBox();
                            t.Top           = counter;
                            t.Left          = 20;
                            t.DropDownStyle = ComboBoxStyle.DropDownList;
                            t.Tag           = definition.Key;

                            foreach (string option in CurrentProject.groupDefinitions.ElementAt(definition.Value.GroupLink).Value)
                            {
                                t.Items.Add(option);
                            }
                            t.SelectedIndex = 0;
                            if (edit)
                            {
                                int selected = 0;
                                if (int.TryParse(CurrentProject.itemCollection[itemID].getValue(definition.Key), out selected))
                                {
                                    t.SelectedIndex = selected;
                                }
                            }

                            form.Controls.Add(t);
                            controlList.Add(t);
                        }
                        else
                        {
                            TextBox t = new TextBox();
                            t.Top  = counter;
                            t.Left = 20;
                            t.Tag  = definition.Key;
                            if (edit)
                            {
                                t.Text = CurrentProject.itemCollection[itemID].getValue(definition.Key);
                            }
                            form.Controls.Add(t);
                            controlList.Add(t);
                        }
                        break;

                    case DefinitionDataType.Sprite:
                        ComboBox cb = new ComboBox();
                        cb.Top           = counter;
                        cb.Left          = 20;
                        cb.DropDownStyle = ComboBoxStyle.DropDownList;
                        cb.Tag           = definition.Key;
                        foreach (string sprite in CurrentProject.GMXspritesFiltered)
                        {
                            cb.Items.Add(sprite);
                        }
                        if (edit)
                        {
                            cb.SelectedIndex = cb.FindStringExact(CurrentProject.itemCollection[itemID].getValue(definition.Key));
                        }
                        form.Controls.Add(cb);
                        controlList.Add(cb);
                        break;
                    }

                    counter += 25;
                }
                #endregion

                Label la = new Label();
                la.Text   = "";
                la.Top    = counter;
                la.Left   = 20;
                la.Height = 13;
                form.Controls.Add(la);

                form.Height = Math.Min(500, counter + 50);

                form.ShowDialog();

                if (form.DialogResult == DialogResult.OK)
                {
                    ItemExtendable itemData = new ItemExtendable();

                    // since it was already try-parsed in Edit form, just parse here
                    itemData.ID = int.Parse(form.itemID.Text);

                    if (edit)
                    {
                        if (itemData.ID == itemID)
                        {
                            itemData = CurrentProject.itemCollection[itemID];
                        }
                    }

                    // fill data for (non-)existing item
                    foreach (Control control in controlList)
                    {
                        switch (CurrentProject.attributeDefinitions[control.Tag.ToString()].DataType)
                        {
                        case DefinitionDataType.Bool:
                            itemData.setValue(control.Tag.ToString(), ((CheckBox)control).Checked);
                            break;

                        case DefinitionDataType.Int:
                            int value = 0;
                            if (control is ComboBox)
                            {
                                value = ((ComboBox)control).SelectedIndex;
                            }
                            else
                            {
                                int.TryParse(control.Text, out value);
                            }
                            itemData.setValue(control.Tag.ToString(), value);
                            break;

                        case DefinitionDataType.Sprite:
                        case DefinitionDataType.String:
                            itemData.setValue(control.Tag.ToString(), control.Text);
                            break;
                        }
                    }

                    if (edit)
                    {
                        if (itemData.ID == itemID)
                        {
                            //CurrentProject.itemCollection[itemID] = itemData;
                        }
                        else
                        {
                            CurrentProject.itemCollection.Remove(itemID);
                            CurrentProject.itemCollection.Add(itemData.ID, itemData);
                        }
                    }
                    else
                    {
                        CurrentProject.itemCollection.Add(itemData.ID, itemData);
                    }

                    // render only if OK was pressed

                    int selection = itemListViewExt.SelectedIndex;

                    //itemListViewExt.SetObjects(CurrentProject.itemCollection.Values.ToList());
                    itemListViewExt.BuildList();

                    if (selection > -1)
                    {
                        itemListViewExt.SelectedIndex = selection;
                    }
                    //renderItemList();
                }
            }

            ensureButtonsVisible();
        }