예제 #1
0
        public static void Convert(IEnumerable <string> paths)
        {
            CfgFile file = new CfgFile();

            foreach (string path in paths)
            {
                //if path is directoy apply to all cfg files with in.
                if (Directory.Exists(path))
                {
                    Convert(Directory.GetFiles(path, "*.cfg", SearchOption.AllDirectories));
                }

                //if there is no file, skip
                else if (!File.Exists(path))
                {
                    continue;
                }

                string json_path = Path.ChangeExtension(path, "json");
                //skip if json already exits
                if (File.Exists(json_path))
                {
                    continue;
                }

                file.FromLines(File.ReadAllText(path));
                file.CollectionEntries.Add(new CollectionSprite()
                {
                    Name = FixName(Path.GetFileNameWithoutExtension(path))
                });

                File.WriteAllText(json_path, file.ToJson());
            }
        }
예제 #2
0
        /// <summary>
        /// Method that sets up the binding for the CFGFile's byte property to the corresponding TextBoxes and CheckBoxes.
        /// This makes use of the fact that all TextBoxes are named txt_xxxx and all CheckBoxes are named chb_xxxx_bb.
        /// With xxxx being the address they represent and bb being the bit (in hex) that the checkbox controlls.
        /// </summary>
        /// <param name="file">The CFGFile object that is the DataSource</param>
        /// <param name="expr">The expression that selects the Property to bind to from the CFGFile</param>
        /// <param name="addr">The string representation of the address, used for searching through the controlls</param>
        /// <param name="start_bit">For the CheckBoxes, this indicates where to start</param>
        private void SetupBinding(CfgFile file, System.Linq.Expressions.Expression <Func <CfgFile, byte> > expr, string addr, int start_bit = 0)
        {
            TextBox txt = (TextBox)Controls.Find("txt_" + addr, true).FirstOrDefault();

            if (txt == null)
            {
                return;
            }
            txt.Bind(file, c => c.Text, expr, f => f.ToString("X2"), c => StringToByte(c));
            for (int i = start_bit; i < 8; i++)
            {
                CheckBox ch = (CheckBox)Controls.Find("chb_" + addr + "_" + (1 << i).ToString("X2"), true).FirstOrDefault();
                if (ch == null)
                {
                    continue;
                }
                ch.BitBind(file, c => c.Checked, expr, i).DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
            }
        }
예제 #3
0
        public CFG_Editor(string[] args)
        {
            if (args.Length > 0)
            {
                int flag = Array.IndexOf(args, "-c");
                if (flag >= 0)
                {
                    Converter.Convert(args.Skip(flag + 1));
                    Environment.Exit(0);
                    return;
                }
            }


            InitializeComponent();

            #region map16 resources

            List <byte> gfx = new List <byte>();
            gfx.AddRange(Enumerable.Repeat <byte>(0, 0x4000));
            gfx.AddRange(GetGfxArray(0x33, 0x3000));
            gfx.AddRange(Enumerable.Repeat <byte>(0, 0x800));

            resources.Graphics = new Map16.SnesGraphics(gfx.ToArray());
            resources.Palette  = Editors.PaletteEditorForm.ColorArrayFromBytes(CFG.Properties.Resources.sprites_palettes, rows: 22);

            byte[] map16data = new byte[0x2000];
            Array.Copy(CFG.Properties.Resources.m16Page1_3, map16data, 0x1800);

            #endregion


            ControlEnabler FileTypeEnabler = new ControlEnabler(() => FileType == FileType.CfgFile)
            {
                grpAsmActLike,
                grpExtraByteCount,
                grpExtraPropByte,
                tpgLm,
                tpgList,
                //saveAsToolStripMenuItem,
            };
            ControlEnabler CustomEnabler = new ControlEnabler(() => FileType == FileType.CfgFile && Data.Type != (int)CFG_SpriteType.Normal)
            {
                grpAsmActLike,
                grpExtraByteCount,
                grpExtraPropByte,
            };
            ControlEnabler ShooterEnabler = new ControlEnabler(() => FileType == FileType.CfgFile && Data.Type != (int)CFG_SpriteType.GeneratorShooter)
            {
                txt_1656,
                txt_1662,
                txt_166E,
                txt_167A,
                txt_1686,
                txt_190F,

                grp_1656,
                grp_1662,
                grp_166E,
                grp_167A,
                grp_1686,
                grp_190F,
            };
            control_enablers.Add(FileTypeEnabler);
            control_enablers.Add(CustomEnabler);
            control_enablers.Add(ShooterEnabler);

            Data = new CfgFile();
            Data.PropertyChanged += (_, e) =>
            {
                Unsaved = true;
                if (e.PropertyName == nameof(Data.Type))
                {
                    control_enablers.ForEach(c => c.Evaluate());
                }
            };

            #region Default Tab


            cmb_1656_0F.BitsBind(Data, c => c.SelectedIndex, f => f.Addr1656, 4, 0).DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
            cmb_1662_3F.BitsBind(Data, c => c.SelectedIndex, f => f.Addr1662, 6, 0).DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
            cmb_166E_0E.BitsBind(Data, c => c.SelectedIndex, f => f.Addr166E, 3, 1).DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;

            SetupBinding(Data, f => f.Addr1656, "1656", 0);
            SetupBinding(Data, f => f.Addr1662, "1662", 0);
            SetupBinding(Data, f => f.Addr166E, "166E", 0);
            SetupBinding(Data, f => f.Addr167A, "167A", 0);
            SetupBinding(Data, f => f.Addr1686, "1686", 0);
            SetupBinding(Data, f => f.Addr190F, "190F", 0);

            txt_0001.Bind(Data, c => c.Text, f => f.ExProp1, f => f.ToString("X2"), c => StringToByte(c));
            txt_0002.Bind(Data, c => c.Text, f => f.ExProp2, f => f.ToString("X2"), c => StringToByte(c));
            nudNormal.Bind(Data, c => c.Value, f => f.ByteCount, null, c => (byte)c);
            nudExtra.Bind(Data, c => c.Value, f => f.ExByteCount, null, c => (byte)c);
            txtASMFile.Bind(Data, c => c.Text, f => f.AsmFile);
            txtActLike.Bind(Data, c => c.Text, f => f.ActLike, f => f.ToString("X2"), c => StringToByte(c));


            //add types to the comboboxes.
            foreach (CFG_SpriteType type in Enum.GetValues(typeof(CFG_SpriteType)))
            {
                types_list.Add(new ComboBoxItem(type.GetName(), (int)type));
            }
            string[] sprite_list_lines = Properties.Resources.SpriteList.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < 201; i++)
            {
                sprites_list.Add(new ComboBoxItem(sprite_list_lines[i], i));
            }

            cmbType.DataSource = types_list;

            //fetch clipping images from resources and add them to the comboboxes.
            for (int i = 0; i < objClip.Length; i++)
            {
                using (Stream str = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("CFG.Resources.ObjClipping." + i.ToString("X2") + ".png"))
                    objClip[i] = ResizeImg(pcbObjClipping.Size, Image.FromStream(str));
                cmb_1656_0F.Items.Add(i.ToString("X2"));
            }
            for (int i = 0; i < sprClip.Length; i++)
            {
                using (Stream str = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("CFG.Resources.SprClipping." + i.ToString("X2") + ".png"))
                    sprClip[i] = ResizeImg(pcbSprClipping.Size, Image.FromStream(str));
                cmb_1662_3F.Items.Add(i.ToString("X2"));
            }

            //draw bitmap of palette file for sprites.
            for (int i = 0; i < sprPal.Length; i++)
            {
                Bitmap b = new Bitmap(8 * 16, 16);
                using (Graphics g = Graphics.FromImage(b))
                    for (int pal = 0; pal < 8; pal++)
                    {
                        Color     c   = resources.Palette[i][pal];
                        Rectangle rec = new Rectangle(16 * pal, 0, 16, 16);
                        g.FillRectangle(new SolidBrush(c), rec);
                    }
                sprPal[i] = b;
            }

            #endregion

            #region Lunar Magic Tab

            cmbTilesets.Items.AddRange(new[]
            {
                new SpriteTileset(0x13, 0x02, "Forest"),
                new SpriteTileset(0x12, 0x03, "Castle"),
                new SpriteTileset(0x13, 0x05, "Mushroom"),
                new SpriteTileset(0x13, 0x04, "Underground"),
                new SpriteTileset(0x13, 0x06, "Water"),
                new SpriteTileset(0x13, 0x09, "Pokey"),
                new SpriteTileset(0x06, 0x11, "Ghost House"),
                new SpriteTileset(0x13, 0x20, "Banzai Bill"),
                new SpriteTileset(0x1C, 0x1D, "Overworld")
                {
                    Sp1 = 0x10, Sp2 = 0x0F
                },
            });
            cmbTilesets.SelectedIndexChanged += (_, __) =>
            {
                var tileset = (SpriteTileset)cmbTilesets.SelectedItem;
                SetDataSelectorGfx(dsSP1, tileset.Sp1);
                SetDataSelectorGfx(dsSP2, tileset.Sp2);
                SetDataSelectorGfx(dsSP3, tileset.Sp3);
                SetDataSelectorGfx(dsSP4, tileset.Sp4);
            };
            dsSP1.Tag = 0x0000;
            dsSP2.Tag = 0x1000;
            dsSP3.Tag = 0x2000;
            dsSP4.Tag = 0x3000;
            cmbTilesets.SelectedIndex = 0;

            //Grid size combobox data + events
            foreach (GridSize gridSize in Enum.GetValues(typeof(GridSize)).Cast <GridSize>().Reverse())
            {
                cmbGrid.Items.Add(new ComboBoxItem(gridSize.GetName(), (int)gridSize));
            }
            cmbGrid.SelectedIndex         = 0;
            cmbGrid.SelectedIndexChanged += (_, __) => spriteEditor1.GridSize = ((ComboBoxItem)cmbGrid.SelectedItem).Value;

            //------------------------------------------------------------------------------------------------------

            //create binding source for later databinding.
            displaySpriteBindingSource.DataSource = Data;
            displaySpriteBindingSource.DataMember = nameof(CfgFile.DisplayEntries);

            //create databindings between display list and controls.
            BindToSourceDisplay(rtbDesc, displaySpriteBindingSource, ctrl => ctrl.Text, ds => ds.Description);
            BindToSourceDisplay(nudX, displaySpriteBindingSource, ctrl => ctrl.Value, ds => ds.X);
            BindToSourceDisplay(nudY, displaySpriteBindingSource, ctrl => ctrl.Value, ds => ds.Y);
            BindToSourceDisplay(chbExtraBit, displaySpriteBindingSource, ctrl => ctrl.Checked, ds => ds.ExtraBit);
            BindToSourceDisplay(chbUseText, displaySpriteBindingSource, ctrl => ctrl.Checked, ds => ds.UseText).DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
            displaySpriteBindingSource.CurrentChanged += (_, __) => spriteEditor1.Sprite = (DisplaySprite)displaySpriteBindingSource.Current;

            ConnectViewAndButtons(displaySpriteBindingSource, dgvDisplay, btnDisplayNew, btnDisplayClone, btnDisplayDelete);


            map16Editor1.Initialize(map16data, resources);
            map16Editor1.SelectionChanged += (_, e) => pnlEdit.Enabled = e.Tile >= 0x300;
            map16Editor1.In8x8ModeChanged += (_, __) => txtTopRight.Enabled = txtBottomLeft.Enabled = txtBottomRight.Enabled = !map16Editor1.In8x8Mode;

            //create databindings between map16 and controls.
            txtTopLeft.Bind(map16Editor1, c => c.Text, e => e.SelectedObject.TopLeft, i => i.ToString("X2"), StringToInt);
            txtTopRight.Bind(map16Editor1, c => c.Text, e => e.SelectedObject.TopRight, i => i.ToString("X2"), StringToInt);
            txtBottomLeft.Bind(map16Editor1, c => c.Text, e => e.SelectedObject.BottomLeft, i => i.ToString("X2"), StringToInt);
            txtBottomRight.Bind(map16Editor1, c => c.Text, e => e.SelectedObject.BottomRight, i => i.ToString("X2"), StringToInt);

            //linking palette combobox and property.
            //If you're wondering why I'm not using DataBinding for this... because for some reason it's insanly slow. 10 seconds slow...
            int trigger = 0;
            cmbPalette.SelectedIndexChanged += (_, __) =>
            {
                if (cmbPalette.SelectedIndex >= 8 || trigger == 2)
                {
                    return;
                }
                trigger = 1;
                map16Editor1.SelectedObject.Palette = cmbPalette.SelectedIndex;
                trigger = 0;
            };
            map16Editor1.SelectionChanged += (_, __) =>
            {
                if (trigger == 1)
                {
                    return;
                }
                trigger = 2;
                cmbPalette.SelectedIndex = map16Editor1.SelectedObject.Palette;
                trigger = 0;
            };

            btnX.Click += (_, __) => map16Editor1.SelectedObject.FlipX();
            btnY.Click += (_, __) => map16Editor1.SelectedObject.FlipY();


            //link sprite editor and map16 editor
            spriteEditor1.Map16Editor  = map16Editor1;
            map16Editor1.HoverChanged += (_, e) => { tslHover.Text = $"Tile: {e.Tile:X3}"; };

            tsb8x8Mode.CheckedChanged += (_, __) => map16Editor1.In8x8Mode = tsb8x8Mode.Checked;
            tsbGrid.CheckedChanged    += (_, __) => map16Editor1.ShowGrid = tsbGrid.Checked;
            tsbPage.CheckedChanged    += (_, __) => map16Editor1.PrintPage = tsbPage.Checked;

            tsbPalette.Click += (_, __) =>
            {
                var palette_editor = new Editors.PaletteEditorForm(resources);
                palette_editor.PaletteChanged += (___, e) => map16Editor1.Map.PaletteChanged(e.Row);
                palette_editor.Show();
            };

            #endregion

            #region Custom List Tab

            //create binding source for later databinding.
            collectionSpriteBindingSource.DataSource = Data;
            collectionSpriteBindingSource.DataMember = nameof(CfgFile.CollectionEntries);

            //bind controlls to current selection in list
            BindToSourceCollection(txtListName, collectionSpriteBindingSource, ctrl => ctrl.Text, cs => cs.Name);
            BindToSourceCollection(chbListExtraBit, collectionSpriteBindingSource, ctrl => ctrl.Checked, cs => cs.ExtraBit);
            Binding[] expropbind = new Binding[]
            {
                txtListExProp1.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte1)),
                txtListExProp2.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte2)),
                txtListExProp3.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte3)),
                txtListExProp4.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte4)),
                txtListExProp5.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte5)),
                txtListExProp6.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte6)),
                txtListExProp7.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte7)),
                txtListExProp8.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte8)),
                txtListExProp9.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte9)),
                txtListExProp10.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte10)),
                txtListExProp11.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte11)),
                txtListExProp12.DataBindings.Add(nameof(TextBox.Text), collectionSpriteBindingSource, nameof(CollectionSprite.ExtraPropertyByte12)),
            };
            expropbind.ForEach(b =>
            {
                b.Format += (_, e) => e.Value = ((byte)e.Value).ToString("X2");
                b.Parse  += (_, e) => e.Value = StringToByte((string)e.Value);
            });
            ConnectViewAndButtons(collectionSpriteBindingSource, dgvList, btnColNew, btnColClone, btnColRemove);

            #endregion


            if (args.Length == 0)
            {
                cmb_1656_0F.SelectedIndex = cmb_1662_3F.SelectedIndex = cmb_166E_0E.SelectedIndex = 0;
                Data.Clear();
            }
            else
            {
                try
                {
                    LoadFile(args[0]);
                }
                catch (Exception ex)
                {
                    Data.Clear();
                    Filename = "";
                    Unsaved  = false;
                    cmb_1656_0F.SelectedIndex = cmb_1662_3F.SelectedIndex = cmb_166E_0E.SelectedIndex = 0;
                    MessageBox.Show("An error occured while trying to read the file.\n\n" + ex.Message, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
#if DEBUG
            testToolStripMenuItem.Visible = true;
#endif
        }