using System; using System.Collections.Generic; using System.Text; using System.Drawing; using PaintDotNet; using PaintDotNet.Data; using PaintDotNet.SystemLayer; namespace Wealth_Wars { class MapLayerSelectedFields : MapLayerUserLoc { #region Public Methods /// /// Image with selected "field" /// /// Mouse location (on control) /// Horizontal scrollbar value /// Vertical scrollbar value /// List of selected fields /// public BitmapLayer MapDBFieldSelector(Point mouseLoc, int hSCBarValue, int vSCBarValue, ref List mapDBFieldSelectedList, BitmapLayer mapDBFieldSelected) { int dbNum = 0; hSCBarValue = (hSCBarValue + mouseLoc.X) / XLengthField; vSCBarValue = (vSCBarValue + mouseLoc.Y) / YLengthField; String userCLoc = hSCBarValue + "." + vSCBarValue; MapDBFieldLocation(ref dbNum, ref hSCBarValue, ref vSCBarValue); int xCoord = dbParamsLength[dbNum].Width - hSCBarValue; int yCoord = dbParamsLength[dbNum].Height - vSCBarValue; ColorBgra fieldBColor = ColorBgra.YellowGreen; XYCoordResetter(dbNum, ref xCoord, ref yCoord); for (int i = 0; i < mapDBFieldSelectedList.Count; i++) { if (userCLoc == mapDBFieldSelectedList[i]) { fieldBColor = ColorBgra.Zero; mapDBFieldSelectedList.RemoveAt(i); break; } } if (fieldBColor == ColorBgra.YellowGreen) { mapDBFieldSelectedList.Add(userCLoc); } //paint on map fieldBColor.A = 120; mapDBFieldSelected.Surface.Clear(fieldBColor, new Rectangle(xCoord, yCoord, XLengthField, YLengthField)); return mapDBFieldSelected; } /// /// Returns image presenting selected fields from certain map database region /// /// List of selected fields /// public BitmapLayer MapDBFieldOnMapLoad(List mapDBFieldSelectedList) { BitmapLayer mapDBFieldsSelected = MapDBLayerConverter(new Bitmap(ImageDimX, ImageDimY)); String[] tempValueStr = new String[2]; int xVal = 0; int yVal = 0; //Acquiring xVal and yVal poing on map dimension eg. 0-1560. MapDBUserLocXYExec(MapForm.mdbLoc[0], int.Parse(MapForm.mdbLoc[1]), int.Parse(MapForm.mdbLoc[2]), ref xVal, ref yVal); Point xCoordRange = new Point(); //x - the min, y - the max of range Point yCoordRange = new Point(); //x - the min, y - the max of range //checking the range (if the middle point is in the middle of image, if not //calculate the lenght to borders xCoordRange.X = xVal / XLengthField; xCoordRange.Y = (ImageDimX - xVal) / XLengthField; yCoordRange.X = yVal / YLengthField; yCoordRange.Y = (ImageDimY - yVal) / YLengthField; //parsing the center load map field location coordinates int xValEx = int.Parse(MapForm.mdbLoc[1]); int yValEx = int.Parse(MapForm.mdbLoc[2]); //converting to extended format location MapDBFieldLocationInv(MapForm.mdbLoc[0], ref xValEx, ref yValEx); //making the ranges (for further check) xCoordRange.X = xValEx - xCoordRange.X; xCoordRange.Y = xValEx + xCoordRange.Y; yCoordRange.X = yValEx - yCoordRange.X; yCoordRange.Y = yValEx + yCoordRange.Y; List mapDBSelectedFieldsOnRegion = new List(); for (int i = 0; i < mapDBFieldSelectedList.Count; i++) { tempValueStr = mapDBFieldSelectedList[i].Split(".".ToCharArray()); xVal = int.Parse(tempValueStr[0]); yVal = int.Parse(tempValueStr[1]); if ((xCoordRange.X <= xVal) && (xCoordRange.Y >= xVal)) { if ((yCoordRange.X <= yVal) && (yCoordRange.Y >= yVal)) { mapDBSelectedFieldsOnRegion.Add(mapDBFieldSelectedList[i].ToString()); //paint field with coordinates (extended) xVal, yVal) } } } if (mapDBSelectedFieldsOnRegion.Count > 0) { mapDBFieldsSelected = MapDBFieldSelectorExtended(mapDBSelectedFieldsOnRegion); } return mapDBFieldsSelected; } /// /// Checks weather the chosen path can be followed /// /// The list with selected fields public void MapPathValidator(List mapDBFieldSelectedList) { if (mapDBFieldSelectedList.Count > 1) { String[] someValueStr = mapDBFieldSelectedList[0].Split(".".ToCharArray()); int xCoordO = int.Parse(someValueStr[0]); int yCoordO = int.Parse(someValueStr[1]); int xCoordN; int yCoordN; int[] tmpInt = new int[2]; for (int i = 1; i < mapDBFieldSelectedList.Count; i++) { someValueStr = mapDBFieldSelectedList[i].Split(".".ToCharArray()); xCoordN = int.Parse(someValueStr[0]); yCoordN = int.Parse(someValueStr[1]); if (((Math.Abs(xCoordO - xCoordN) == 1) && (yCoordO == yCoordN)) || ((Math.Abs(yCoordO - yCoordN) == 1) && (xCoordO == xCoordN))) { xCoordO = xCoordN; yCoordO = yCoordN; } else { tmpInt[0] = i-1; tmpInt[1] = i; List possibleCheckL = CloseFieldSearcher(ref mapDBFieldSelectedList, xCoordO, yCoordO, tmpInt); if (possibleCheckL.Count > 0) { //show on program dialog with available choice(s) } } } } } #endregion #region Private Methods /// /// Resets the x, y coordinates of field (extended format) /// /// Name of database /// X coordinate of field /// Y coordinate of field private void XYCoordResetter(int dbNum, ref int xCoord, ref int yCoord) { switch (dbNum) { case 0: xCoord = dbParamsLength[0].Width - xCoord + dbParamsLength[1].Width; yCoord += -1; break; case 1: xCoord += -1; yCoord += -1; break; case 2: xCoord = dbParamsLength[2].Width - xCoord + dbParamsLength[3].Width; yCoord = dbParamsLength[2].Height - yCoord + dbParamsLength[0].Height; break; case 3: xCoord += -1; yCoord = dbParamsLength[3].Height - yCoord + dbParamsLength[1].Height; break; } xCoord = xCoord * XLengthField; yCoord = yCoord * YLengthField; } /// /// Image with selected fields generated on map reload. /// /// The list with selected fields to paint on map. /// private BitmapLayer MapDBFieldSelectorExtended(List mapDBFieldSelectedList) { BitmapLayer mapDBFieldSelected = MapDBLayerConverter(new Bitmap(ImageDimX, ImageDimY)); ColorBgra fieldBColor = ColorBgra.YellowGreen; int dbNum; int xCoord; int yCoord; String[] tempValue; for (int i = 0; i < mapDBFieldSelectedList.Count; i++) { tempValue = mapDBFieldSelectedList[i].Split(".".ToCharArray()); dbNum = 0; xCoord = int.Parse(tempValue[0]); yCoord = int.Parse(tempValue[1]); MapDBFieldLocation(ref dbNum, ref xCoord, ref yCoord); xCoord = dbParamsLength[dbNum].Width - xCoord; yCoord = dbParamsLength[dbNum].Height - yCoord; XYCoordResetter(dbNum, ref xCoord, ref yCoord); fieldBColor.A = 120; mapDBFieldSelected.Surface.Clear(fieldBColor, new Rectangle(xCoord, yCoord, XLengthField, YLengthField)); } return mapDBFieldSelected; } /// /// Search for the nearest selected/to select field /// /// The list with path fields /// X coordinate of based field /// Y coordinate of based field /// Numbers of position excluded, eg. field before, actual private List CloseFieldSearcher(ref List mapDBFieldSelectedList, int xCoord, int yCoord, int[] fieldPos) { List possibleCheckFields = new List(); String[] xyValue; int xCoordN; int yCoordN; Boolean searchState = true; for (int i = 0; i < mapDBFieldSelectedList.Count; i++) { if ((i != fieldPos[0]) && (i != fieldPos[1])) { xyValue = mapDBFieldSelectedList[i].Split(".".ToCharArray()); xCoordN = int.Parse(xyValue[0]); yCoordN = int.Parse(xyValue[1]); if (((Math.Abs(xCoord - xCoordN) == 1) && (yCoord == yCoordN)) || ((Math.Abs(yCoord - yCoordN) == 1) && (xCoord == xCoordN))) { mapDBFieldSelectedList.Insert(fieldPos[1], mapDBFieldSelectedList[i]); searchState = false; break; //possibleCheckFields.Add(xCoordN + "." + yCoordN); } } } if (searchState == true) { for (int j = 0; j < mapDBFieldSelectedList.Count; j++) { xyValue = mapDBFieldSelectedList[j].Split(".".ToCharArray()); xCoordN = int.Parse(xyValue[0]); yCoordN = int.Parse(xyValue[1]); if ((xCoord - xCoordN == 0) && (Math.Abs(yCoord - yCoordN) == 2)) { possibleCheckFields.Add(xCoordN + "." + (yCoordN - 1).ToString()); } else if ((Math.Abs(xCoord - xCoordN) == 2) && (yCoord - yCoordN == 0)) { possibleCheckFields.Add((xCoordN - 1).ToString() + "." + yCoordN); } else if ((Math.Abs(xCoord - xCoordN) == 1) && (Math.Abs(yCoord - yCoordN) == 1)) { possibleCheckFields.Add(xCoordN + "." + (yCoordN - 1).ToString()); possibleCheckFields.Add((xCoord - 1).ToString() + "." + yCoordN); } } } return possibleCheckFields; } #endregion } }