#region Copyright /* -------------------------------------------------------------------------------- This source file is part of Xenocide by Project Xenocide Team For the latest info on Xenocide, see http://www.projectxenocide.com/ This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. -------------------------------------------------------------------------------- */ /* * @file InventoryLocation.cs * @date Created: 2007/11/19 * @author File creator: David Teviotdale * @author Credits: none */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using Microsoft.Xna.Framework; using ProjectXenocide.Utils; using ProjectXenocide.Model.Battlescape.Combatants; using ProjectXenocide.Model.StaticData.Items; namespace ProjectXenocide.UI.Scenes { /// /// A place that can show inventory on the EquipSolider screen. /// Either a position on the soldier, or a position on the ground/Outpost inventory /// public class InventoryLocation { /// /// Ctor /// /// Is this location on the soldier or in the inventory/ground area? /// X co-ordinate of the location /// Y co-ordinate of the location public InventoryLocation(bool isOnSoldier, int x, int y) { this.isOnSoldier = isOnSoldier; this.x = x; this.y = y; } /// /// Figure out where in inventory mouse is pointing to /// /// mouse position /// mouse position /// co-ordinates of window showing the EquipSoldier Scene /// Location in inventory corresponding to the mouse's position. Or null if no match public static InventoryLocation FromMousePosition(int x, int y, Rectangle scene) { // easy case is mouse is on the "ground" Rectangle groundArea = GroundArea(scene); if (groundArea.Contains(x, y)) { int column = (int)((x - groundArea.X) * GroundWidthInCells / (float)groundArea.Width); int row = (int)((y - groundArea.Y) * GroundHeightInCells / (float)groundArea.Height); return new InventoryLocation(false, column, row); } // check the cells making up the soldier's inventory for (int i = 0; i < InventoryLayout.CellsWidth; ++i) { for (int j = 0; j < InventoryLayout.CellsHeight; ++j) { if ((Zone.None != InventoryLayout.GetZone(i, j)) && InventoryLayout.GetScreenPosition(i, j, scene).Contains(x, y)) { return new InventoryLocation(true, i, j); } } } // if get here, then mouse isn't over any inventory return null; } /// /// Compute where to draw an item on the "Ground" area of screen /// /// total size of the screen /// size info of item to draw /// row of cell holding item's leftmost edge /// Rectangle to render object inside [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Will throw if carryInfo is null")] public static Rectangle GroundPosition(Rectangle sceneWindow, CarryInfo carryInfo, int offset) { Rectangle groundArea = GroundArea(sceneWindow); float cellWidth = groundArea.Width / GroundWidthInCells; float cellHeight = groundArea.Height / GroundHeightInCells; return new Rectangle( Util.Round(groundArea.Left + cellWidth * offset), groundArea.Top, Util.Round(carryInfo.X * cellWidth), Util.Round(carryInfo.Y * cellHeight) ); } /// /// Calculate the area of screen that is the "ground" /// /// Area of window on display /// Area of screen that is "ground" public static Rectangle GroundArea(Rectangle sceneWindow) { return new Rectangle( Util.Round( 10.0f / 800.0f * sceneWindow.Width), Util.Round(469.0f / 600.0f * sceneWindow.Height), Util.Round(779.0f / 800.0f * sceneWindow.Width), Util.Round(120.0f / 600.0f * sceneWindow.Height) ); } #region Fields /// The width of the ground area, in "cells" public const float GroundWidthInCells = 19.0f; /// The height of the ground area, in "cells" private const float GroundHeightInCells = 3.0f; /// /// Is this location on the soldier or in the inventory/ground area? /// public bool IsOnSoldier { get { return isOnSoldier; } } /// /// X co-ordinate of the location /// either x index to CombatantInventory.cells[y,x] /// or column on "ground" /// public int X { get { return x; } } /// /// Y co-ordinate of the location /// either y index to CombatantInventory.cells[y,x] /// or row on "ground" /// public int Y { get { return y; } } /// /// Is this location on the soldier or in the inventory/ground area? /// private bool isOnSoldier; /// /// X co-ordinate of the location /// either x index to CombatantInventory.cells[y,x] /// or column on "ground" /// private int x; /// /// Y co-ordinate of the location /// either y index to CombatantInventory.cells[y,x] /// or row on "ground" /// private int y; #endregion Fields } }