All pastes #226689 Raw Edit

WW_SelectingFields

public text v1 · immutable
#226689 ·published 2006-10-28 19:41 UTC
rendered paste body
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

        /// <summary>
        /// Image with selected "field"
        /// </summary>
        /// <param name="mouseLoc">Mouse location (on control)</param>
        /// <param name="hSCBarValue">Horizontal scrollbar value</param>
        /// <param name="vSCBarValue">Vertical scrollbar value</param>
        /// <param name="mapDBFieldSelectedList">List of selected fields</param>
        /// <returns></returns>
        public BitmapLayer MapDBFieldSelector(Point mouseLoc, int hSCBarValue,
            int vSCBarValue, ref List<String> 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;
        }

        /// <summary>
        /// Returns image presenting selected fields from certain map database region
        /// </summary>
        /// <param name="mapDBFieldSelectedList">List of selected fields</param>
        /// <returns></returns>
        public BitmapLayer MapDBFieldOnMapLoad(List<String> 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<String> mapDBSelectedFieldsOnRegion = new List<String>();
            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;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Resets the x, y coordinates of field (extended format)
        /// </summary>
        /// <param name="dbNum">Name of database</param>
        /// <param name="xCoord">X coordinate of field</param>
        /// <param name="yCoord">Y coordinate of field</param>
        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;
        }

        /// <summary>
        /// Image with selected fields generated on map reload.
        /// </summary>
        /// <param name="mapDBFieldSelectedList">The list with selected fields to paint on map.</param>
        /// <returns></returns>
        private BitmapLayer MapDBFieldSelectorExtended(List<String> 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;
        }

        #endregion
    }
}