#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 MakeTransferScreen.cs * @date Created: 2007/09/24 * @author File creator: David Teviotdale * @author Credits: none */ #endregion #region Using Statements using System; using System.Collections.Generic; using System.Text; using CeGui; using ProjectXenocide.Utils; using ProjectXenocide.Model.Geoscape; using ProjectXenocide.Model.Geoscape.Outposts; using ProjectXenocide.Model.StaticData.Items; using Xenocide.Resources; #endregion namespace ProjectXenocide.UI.Screens { /// /// This is the screen that allows user to move items between outposts /// public class MakeTransferScreen : Screen { /// /// Constructor (obviously) /// /// Index to outpost items will be taken from /// Index to outpost items will be sent to public MakeTransferScreen(int sourceOutpostIndex, int destinationOutpostIndex) : base("MakeTransferScreen") { this.sourceOutpostIndex = sourceOutpostIndex; this.destinationOutpostIndex = destinationOutpostIndex; } #region Create the CeGui widgets /// /// add the buttons to the screen /// protected override void CreateCeguiWidgets() { // add text naming source outpost sourceText = GuiBuilder.CreateText(CeguiId + "_sourceText"); AddWidget(sourceText, 0.01f, 0.06f, 0.2275f, 0.04125f); sourceText.Text = Util.StringFormat(Strings.SCREEN_TRANSFER_SOURCE, SourceOutpost.Name); // add text giving the running total cost of the transfer totalCostText = GuiBuilder.CreateText(CeguiId + "_totalCostText"); AddWidget(totalCostText, 0.35f, 0.06f, 0.2275f, 0.04125f); UpdateTotalCost(); // combo box to allow user to pick destination outpost for items outpostsListComboBox = GuiBuilder.CreateComboBox("outpostsListComboBox"); AddWidget(outpostsListComboBox, 0.7475f, 0.06f, 0.2275f, 0.40f); Misc.PopulateHumanBasesList(outpostsListComboBox, destinationOutpostIndex); outpostsListComboBox.ListSelectionAccepted += new WindowEventHandler(OnOutpostSelectionChanged); // The gird of items available for purchase InitializeGrid(); PopulateGrid(); // other buttons moveMoreButton = AddButton("BUTTON_MOVE_MORE", 0.7475f, 0.80f, 0.2275f, 0.04125f, "PlanetView\\zoomin.ogg"); moveLessButton = AddButton("BUTTON_MOVE_LESS", 0.7475f, 0.85f, 0.2275f, 0.04125f, "PlanetView\\zoomout.ogg"); confirmButton = AddButton("BUTTON_CONFIRM", 0.7475f, 0.90f, 0.2275f, 0.04125f); cancelButton = AddButton("BUTTON_CANCEL", 0.7475f, 0.95f, 0.2275f, 0.04125f); moveMoreButton.Clicked += new CeGui.GuiEventHandler(OnMoveMoreButton); moveLessButton.Clicked += new CeGui.GuiEventHandler(OnMoveLessButton); confirmButton.Clicked += new CeGui.GuiEventHandler(OnConfirmButton); cancelButton.Clicked += new CeGui.GuiEventHandler(OnCancelButton); } private CeGui.Widgets.StaticText sourceText; private CeGui.Widgets.StaticText totalCostText; private CeGui.Widgets.ComboBox outpostsListComboBox; private CeGui.Widgets.MultiColumnList grid; private CeGui.Widgets.PushButton moveMoreButton; private CeGui.Widgets.PushButton moveLessButton; private CeGui.Widgets.PushButton confirmButton; private CeGui.Widgets.PushButton cancelButton; /// /// Create MultiColumnListBox which holds items that can be transfered /// private void InitializeGrid() { grid = GuiBuilder.CreateGrid("transferGrid"); AddWidget(grid, 0.01f, 0.13f, 0.70f, 0.86f); grid.AddColumn(Strings.SCREEN_TRANSFER_COLUMN_ITEM, grid.ColumnCount, 0.58f); grid.AddColumn(Strings.SCREEN_TRANSFER_COLUMN_QUANTITY_IN_BASE, grid.ColumnCount, 0.12f); grid.AddColumn(Strings.SCREEN_TRANSFER_COLUMN_QUANTITY_DESTINAION, grid.ColumnCount, 0.15f); grid.AddColumn(Strings.SCREEN_TRANSFER_COLUMN_QUANTITY, grid.ColumnCount, 0.12f); } /// /// Put the list of items that can be transfered into the grid /// private void PopulateGrid() { foreach (Item i in SourceOutpost.Inventory.ListContents()) { if (i.CanRemoveFromOutpost) { AddRowToGrid(new TransactionLineItem(i, SourceOutpost.Inventory, DestinationOutpost.Inventory)); } } } /// /// Add a row to the grid /// /// details to put on grid private void AddRowToGrid(TransactionLineItem lineItem) { // add item to grid CeGui.ListboxTextItem listboxItem = Util.CreateListboxItem(lineItem.Name); int rowNum = grid.AddRow(listboxItem, 0); listboxItem.ID = rowNum; Util.AddNumericElementToGrid(grid, 1, rowNum, lineItem.SourceCount); Util.AddNumericElementToGrid(grid, 2, rowNum, lineItem.DestinationCount); Util.AddNumericElementToGrid(grid, 3, rowNum, lineItem.NumMoving); // and record number of items of this type user has TransferList[rowNum] = lineItem; } #endregion Create the CeGui widgets #region event handlers /// Handle user clicking on the "Transfer More" button /// Not used /// Not used private void OnMoveMoreButton(object sender, GuiEventArgs e) { CeGui.Widgets.ListboxItem selectedItem = GetSelectedItem(); if (null != selectedItem) { // update count of items TransactionLineItem lineItem = TransferList[selectedItem.ID]; if (lineItem.NumMoving < lineItem.MaxMovable) { ++lineItem.NumMoving; // check that this item doesn't put us over the limit if (!CanManageTransfer()) { --lineItem.NumMoving; } // update display on screen UpdateDetails(selectedItem, lineItem); } } } /// Handle user clicking on the "Transfer Less" button /// Not used /// Not used private void OnMoveLessButton(object sender, GuiEventArgs e) { CeGui.Widgets.ListboxItem selectedItem = GetSelectedItem(); if (null != selectedItem) { TransactionLineItem lineItem = TransferList[selectedItem.ID]; if (0 < lineItem.NumMoving) { --lineItem.NumMoving; // update display on screen UpdateDetails(selectedItem, lineItem); } } } /// Handle user clicking on the "Confirm" button /// That is, ship the items the user has selected to the destination outpost /// Not used /// Not used private void OnConfirmButton(object sender, GuiEventArgs e) { // Get the money from selling the items Xenocide.GameState.GeoData.XCorp.Bank.Debit(CalculateTotalCost()); // and ship items to destination Shipment shipment = new Shipment(DestinationOutpost, Shipment.CalcEta()); foreach (TransactionLineItem lineItem in TransferList.Values) { lineItem.RemoveItems(SourceOutpost.Inventory, shipment); } shipment.Ship(); GoToBasesScreen(); } /// React to user pressing the Cancel button /// Not used /// Not used private void OnCancelButton(object sender, CeGui.GuiEventArgs e) { GoToBasesScreen(); } /// user has selected an entry in the destination outpost combo box /// Not used /// Not used private void OnOutpostSelectionChanged(object sender, WindowEventArgs e) { CeGui.Widgets.ListboxItem item = outpostsListComboBox.SelectedItem; if (item != null) { ChangeDestinationOutpost(outpostsListComboBox.GetItemIndex(item)); } } #endregion event handlers // Get currently selected item from Grid. Give error message if nothing selected private CeGui.Widgets.ListboxItem GetSelectedItem() { CeGui.Widgets.ListboxItem selectedItem = grid.GetFirstSelectedItem(); if (null == selectedItem) { Util.ShowMessageBox(Strings.MSGBOX_NO_SALE_SELECTED); } return selectedItem; } /// /// Populate the Total Value field on the dialog /// private void UpdateTotalCost() { totalCostText.Text = Util.StringFormat(Strings.SCREEN_TRANSFER_TOTAL_COST, CalculateTotalCost()); } /// /// Update the screen to reflect the latest changes /// /// row in gird that is selected /// LineItem with number of items of this type being sold private void UpdateDetails(CeGui.Widgets.ListboxItem selectedItem, TransactionLineItem lineItem) { UpdateTotalCost(); // update quantity column of row in grid int row = grid.GetRowIndexOfItem(selectedItem); CeGui.Widgets.GridReference position = new CeGui.Widgets.GridReference(row, 3); grid.GetItemAtGridReference(position).Text = Util.ToString(lineItem.NumMoving); } /// /// Figure out what the total cost to ship all the items will be /// /// total cost private int CalculateTotalCost() { int cost = 0; foreach (TransactionLineItem lineItem in TransferList.Values) { cost += lineItem.ShippingCost; } return cost; } /// /// Check if player can afford cost of transfer, and destination outpost has room for everything /// /// true if transfer can be done private bool CanManageTransfer() { if (!TransactionLineItem.CanFit(DestinationOutpost.Inventory, TransferList.Values)) { Util.ShowMessageBox(Strings.MSGBOX_DESTINATION_CANT_FIT_ITEMS); return false; } else { return Xenocide.GameState.GeoData.XCorp.Bank.CanAfford(CalculateTotalCost()); } } /// user wants to look at a different outpost /// Outpost to make destination for items private void ChangeDestinationOutpost(int newDestinationIndex) { // if new destination is same as current one nothing to do if (newDestinationIndex != destinationOutpostIndex) { // destination can't be same as source if (newDestinationIndex == sourceOutpostIndex) { Util.ShowMessageBox(Strings.MSGBOX_SOURCE_AND_DESTINATION_SAME); } else { ScreenManager.ScheduleScreen(new MakeTransferScreen(sourceOutpostIndex, newDestinationIndex)); } } } /// /// Close this screen and go back to the Bases Screen /// private void GoToBasesScreen() { ScreenManager.ScheduleScreen(new BasesScreen(sourceOutpostIndex)); } #region Fields /// /// The outpost items will be taken from /// private Outpost SourceOutpost { get { return Xenocide.GameState.GeoData.Outposts[sourceOutpostIndex]; } } /// /// The outpost items will be sent to /// private Outpost DestinationOutpost { get { return Xenocide.GameState.GeoData.Outposts[destinationOutpostIndex]; } } // index specifying the outpost that items will be taken from private int sourceOutpostIndex; // index specifying the outpost that items will be sent to private int destinationOutpostIndex; /// /// The list of items user is transferring /// Format is row in grid, details /// private Dictionary TransferList = new Dictionary(); #endregion Fields } }