private void NESW_CalculateFromSprite(PictureBox picture, NumericUpDown north, NumericUpDown east, NumericUpDown south, NumericUpDown west, bool loadExisting = false, string rotationName = "" /* MUST BE SUPPLIED IF loadExisting = true */) { SpriteBounds bounds = (loadExisting) ? SpriteAnalysis.LoadData(FilePaths.PathToUnityStreaming + "PROPCONFIGS/" + propName.Text.Trim().ToUpper().Replace(' ', '_') + "/" + Path.GetFileNameWithoutExtension(rotationName) + ".dwb") : SpriteAnalysis.CalculateSpriteBounds((Bitmap)picture.Image); north.Value = bounds.tile_excess_north; east.Value = bounds.tile_excess_east; south.Value = bounds.tile_excess_south; west.Value = bounds.tile_excess_west; }
/* Save bounds data */ static public void SaveData(string _path, SpriteBounds _data) { Directory.CreateDirectory(_path.Substring(0, _path.Length - Path.GetFileName(_path).Length)); BinaryWriter writer = new BinaryWriter(File.OpenWrite(_path)); writer.Write(_data.from_origin_north); writer.Write(_data.from_origin_east); writer.Write(_data.from_origin_south); writer.Write(_data.from_origin_west); writer.Write((float)_data.tile_excess_north); writer.Write((float)_data.tile_excess_east); writer.Write((float)_data.tile_excess_south); writer.Write((float)_data.tile_excess_west); writer.Close(); }
/* Load bounds data */ static public SpriteBounds LoadData(string _path) { BinaryReader reader = new BinaryReader(File.OpenRead(_path)); /* COMPATIBILITY FOR OLDER START */ if (reader.BaseStream.Length != 32) { reader.Close(); return(new SpriteBounds()); } /* COMPATIBILITY FOR OLDER END */ SpriteBounds bounds = new SpriteBounds(); bounds.from_origin_north = reader.ReadInt32(); bounds.from_origin_east = reader.ReadInt32(); bounds.from_origin_south = reader.ReadInt32(); bounds.from_origin_west = reader.ReadInt32(); bounds.tile_excess_north = (decimal)reader.ReadSingle(); bounds.tile_excess_east = (decimal)reader.ReadSingle(); bounds.tile_excess_south = (decimal)reader.ReadSingle(); bounds.tile_excess_west = (decimal)reader.ReadSingle(); reader.Close(); return(bounds); }
/* Calculate bounds */ static public SpriteBounds CalculateSpriteBounds(Bitmap _image) { int first_nonalpha_topdown = int.MaxValue; int first_nonalpha_leftright = int.MaxValue; int last_nonalpha_topdown = -int.MaxValue; int last_nonalpha_leftright = -int.MaxValue; for (int x = 0; x < _image.Width; x++) { for (int y = 0; y < _image.Height; y++) { if (_image.GetPixel(x, y).A != 0) { if (y < first_nonalpha_topdown) { first_nonalpha_topdown = y; } if (y > last_nonalpha_topdown) { last_nonalpha_topdown = y; } } } } for (int y = 0; y < _image.Height; y++) { for (int x = 0; x < _image.Width; x++) { if (_image.GetPixel(x, y).A != 0) { if (x < first_nonalpha_leftright) { first_nonalpha_leftright = x; } if (x > last_nonalpha_leftright) { last_nonalpha_leftright = x; } } } } int origin_x = _image.Width / 2; int origin_y = _image.Height / 2; SpriteBounds sprite_dims = new SpriteBounds(); sprite_dims.from_origin_north = origin_y - first_nonalpha_topdown; sprite_dims.from_origin_east = last_nonalpha_leftright - origin_x; sprite_dims.from_origin_south = last_nonalpha_topdown - origin_y; sprite_dims.from_origin_west = origin_x - first_nonalpha_leftright; //The value here should match the spritePixelsToUnits value in meta_template.txt decimal northVal = (decimal)(((float)sprite_dims.from_origin_north - 100.0f) / 200.0f); sprite_dims.tile_excess_north = ((northVal < 0) ? 0 : northVal); decimal eastVal = (decimal)(((float)sprite_dims.from_origin_east - 100.0f) / 200.0f); sprite_dims.tile_excess_east = ((eastVal < 0) ? 0 : eastVal); decimal southVal = (decimal)(((float)sprite_dims.from_origin_south - 100.0f) / 200.0f); sprite_dims.tile_excess_south = ((southVal < 0) ? 0 : southVal); decimal westVal = (decimal)(((float)sprite_dims.from_origin_west - 100.0f) / 200.0f); sprite_dims.tile_excess_west = ((westVal < 0) ? 0 : westVal); return(sprite_dims); }
/* Save the prop */ private void saveProp_Click(object sender, EventArgs e) { //Validation if (propName.Text == "") { MessageBox.Show("Prop must have a name!", "No prop name.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (propDesc.Text == "") { MessageBox.Show("Prop must have a description!", "No prop description.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (isWaypoint.Checked && (!(isStartPoint.Checked || isMidPoint.Checked || isEndPoint.Checked) || waypointFor.SelectedIndex == 0)) { MessageBox.Show("Waypoint data must all be selected if waypoint!", "Missing waypoint data.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (isEvent.Checked && eventScriptName.Text == "") { MessageBox.Show("Enter a script class name, or uncheck scripted object checkbox!", "Invalid scripted prop setup.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!ImageIsValid(tilePreviewFrontFacing, frontSpriteInUse.Checked) || !ImageIsValid(tilePreviewLeftFacing, leftSpriteInUse.Checked) || !ImageIsValid(tilePreviewRightFacing, rightSpriteInUse.Checked) || !ImageIsValid(tilePreviewBackFacing, backSpriteInUse.Checked)) { MessageBox.Show("Prop sprites MUST be square and a division of 200 pixels.\nPlease follow the guidlines for irregular size props!", "Invalid prop size.", MessageBoxButtons.OK, MessageBoxIcon.Error); DialogResult shouldDo = MessageBox.Show("Would you like to see an example?", "Example prop sizing offer", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (shouldDo == DialogResult.Yes && File.Exists("TileExample.png")) { Process.Start("TileExample.png"); } return; } if (tilePreviewIconUI.Image == null || (tilePreviewIconUI.Image.Width != tilePreviewIconUI.Image.Height)) { MessageBox.Show("UI sprite not assigned, or is not square!", "Invalid UI sprite.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!frontSpriteInUse.Checked && !leftSpriteInUse.Checked && !rightSpriteInUse.Checked && !backSpriteInUse.Checked) { MessageBox.Show("Prop requires at least one sprite!", "No in-use sprites.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((frontSpriteInUse.Checked && tilePreviewFrontFacing.Image == null) || (leftSpriteInUse.Checked && tilePreviewLeftFacing.Image == null) || (rightSpriteInUse.Checked && tilePreviewRightFacing.Image == null) || (backSpriteInUse.Checked && tilePreviewBackFacing.Image == null)) { MessageBox.Show("An in-use sprite has been unassigned!", "Unassigned sprite.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Create and check prop name string formattedPropName = propName.Text.Trim().ToUpper().Replace(' ', '_'); if (propIndex == -1) { for (int i = 0; i < PropFileInterface.GetData().Count; i++) { if (PropFileInterface.GetData()[i].propName == formattedPropName) { MessageBox.Show("Prop name must be unique!", "Prop already exists.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } Directory.CreateDirectory(FilePaths.PathToUnityPropResources + formattedPropName); //Save sprites spriteBounds.Clear(); if (frontSpriteInUse.Checked) { SaveSprite(tilePreviewFrontFacing, formattedPropName + "/FRONT_FACING", true, frontNorthCoverage.Value, frontEastCoverage.Value, frontSouthCoverage.Value, frontWestCoverage.Value); } if (leftSpriteInUse.Checked) { SaveSprite(tilePreviewLeftFacing, formattedPropName + "/LEFT_FACING", true, leftNorthCoverage.Value, leftEastCoverage.Value, leftSouthCoverage.Value, leftWestCoverage.Value); } if (rightSpriteInUse.Checked) { SaveSprite(tilePreviewRightFacing, formattedPropName + "/RIGHT_FACING", true, rightNorthCoverage.Value, rightEastCoverage.Value, rightSouthCoverage.Value, rightWestCoverage.Value); } if (backSpriteInUse.Checked) { SaveSprite(tilePreviewBackFacing, formattedPropName + "/BACK_FACING", true, backNorthCoverage.Value, backEastCoverage.Value, backSouthCoverage.Value, backWestCoverage.Value); } SaveSprite(tilePreviewIconUI, formattedPropName + "/EDITOR_UI"); //Work out complete bounds from all rotations SpriteBounds allBounds = new SpriteBounds(); for (int i = 0; i < spriteBounds.Count; i++) { if (allBounds.from_origin_north < spriteBounds[i].from_origin_north) { allBounds.from_origin_north = spriteBounds[i].from_origin_north; } if (allBounds.from_origin_east < spriteBounds[i].from_origin_east) { allBounds.from_origin_east = spriteBounds[i].from_origin_east; } if (allBounds.from_origin_south < spriteBounds[i].from_origin_south) { allBounds.from_origin_south = spriteBounds[i].from_origin_south; } if (allBounds.from_origin_west < spriteBounds[i].from_origin_west) { allBounds.from_origin_west = spriteBounds[i].from_origin_west; } } SpriteAnalysis.SaveData(FilePaths.PathToUnityStreaming + "PROPCONFIGS/" + formattedPropName + "/" + formattedPropName + ".dwb", allBounds); //Save data thisProp.propName = formattedPropName; thisProp.propDesc = propDesc.Text; thisProp.isWaypoint = isWaypoint.Checked; if (thisProp.isWaypoint) { thisProp.waypointType = (Int16)((isStartPoint.Checked) ? 0 : (isMidPoint.Checked) ? 1 : (isEndPoint.Checked) ? 2 : -1); thisProp.waypointFor = (Int16)waypointFor.SelectedIndex; } thisProp.isEventSpawn = isEvent.Checked; if (thisProp.isEventSpawn) { thisProp.eventType = eventScriptName.Text; } thisProp.isPOI = isPOI.Checked; if (thisProp.isPOI) { thisProp.poiType = (Int16)poiType.SelectedIndex; thisProp.poiGoonCount = (int)poiGoonCount.Value; } thisProp.isInside = useageInterior.Checked; thisProp.makesTileUnpathable = makeTileUnpathable.Checked; thisProp.hideInEditor = hideInEditor.Checked; thisProp.zBias = (int)zBias.Value; if (propIndex == -1) { PropFileInterface.GetData().Add(thisProp); } PropFileInterface.SaveData(); MessageBox.Show("Saved!", "Complete.", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }