Пример #1
0
        /// <summary>
        /// Handles the <see cref="ButtonBase.Click"/> event for the "Show Units", "Show Terrains",
        /// and "Show Upgrades" <see cref="Button"/> controls on the <see cref="AssetsTab"/> page.
        /// </summary>
        /// <param name="sender">
        /// The <see cref="Object"/> where the event handler is attached.</param>
        /// <param name="args">
        /// A <see cref="RoutedEventArgs"/> object containing event data.</param>
        /// <remarks>
        /// <b>OnShowEntities</b> shows a <see cref="ShowEntities"/> dialog with the selected <see
        /// cref="Faction"/> and an initial <see cref="EntityCategory"/> that corresponds to the
        /// clicked button. Any parent dialogs are temporarily hidden so that the default <see
        /// cref="MapView"/> is not obscured.</remarks>

        private void OnShowEntities(object sender, RoutedEventArgs args)
        {
            args.Handled = true;
            if (this._faction == null)
            {
                return;
            }

            // determine initial category (default is Unit)
            EntityCategory category = EntityCategory.Unit;

            if (args.Source == ShowTerrainsButton)
            {
                category = EntityCategory.Terrain;
            }
            else if (args.Source == ShowUpgradesButton)
            {
                category = EntityCategory.Upgrade;
            }

            // hide all intermediate dialogs
            var    owners = new Stack <Window>();
            Window owner  = this;

            while (owner != null && owner != MainWindow.Instance)
            {
                owner.Hide();
                owners.Push(owner);
                owner = owner.Owner;
            }

            // show read-only dialog with desired initial category
            var dialog = new ShowEntities(this._mapView, this._faction, category, 0);

            dialog.Owner = this;
            dialog.ShowDialog();

            // show all intermediate dialogs
            while (owners.Count > 0)
            {
                owner = owners.Pop();
                owner.Show();
            }
        }
Пример #2
0
        /// <summary>
        /// Allows the <see cref="WorldState.ActiveFaction"/>, which must be controlled by a local
        /// human player, to rename, place, and destroy existing entities.</summary>
        /// <param name="mode">
        /// A <see cref="Dialog.ShowEntitiesMode"/> value indicating the initial display mode of the
        /// <see cref="Dialog.ShowEntities"/> dialog. The <b>Command</b> flag is added automatically
        /// if not specified.</param>
        /// <exception cref="PropertyValueException">
        /// The current session <see cref="Session.State"/> is not <see cref="SessionState.Human"/>.
        /// </exception>
        /// <remarks><para>
        /// <b>ManageEntities</b> shows the <see cref="Dialog.ShowEntities"/> dialog, allowing the
        /// local human player to inspect, rename, place, and destroy existing entities. This dialog
        /// may issue one or more <see cref="DestroyCommand"/> and/or <see cref="RenameCommand"/>
        /// for the <see cref="WorldState.ActiveFaction"/>.
        /// </para><para>
        /// <b>ManageEntities</b> will enter <see cref="SessionState.Selection"/> mode if the user
        /// requests a <see cref="PlaceCommand"/>, allowing the local human player to select a
        /// target site for the desired <see cref="Entity"/>.</para></remarks>

        public static void ManageEntities(Dialog.ShowEntitiesMode mode)
        {
            CheckSessionState();

            Session    session = Session.Instance;
            WorldState world   = session.WorldState;
            Faction    faction = world.ActiveFaction;

            mode |= Dialog.ShowEntitiesMode.Command;
            EntityCategory category = EntityCategory.Unit;

            if ((mode & Dialog.ShowEntitiesMode.Site) != 0)
            {
                // retrieve selected site
                Site selected = world.GetSite(Session.MapView.SelectedSite);

                // check if any site selected
                if (selected == null)
                {
                    MessageBox.Show(MainWindow.Instance,
                                    Global.Strings.DialogSiteUnselected,
                                    Global.Strings.TitleSelectedEntities + faction.Name,
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // check if any owned units are present
                if (selected.Units.Count == 0 || selected.Units[0].Owner != faction)
                {
                    // check if any owned terrains are present
                    if (selected.Owner != faction)
                    {
                        MessageBox.Show(MainWindow.Instance,
                                        Global.Strings.DialogSiteEmpty,
                                        Global.Strings.TitleSelectedEntities + faction.Name,
                                        MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }

                    // no units, show terrains initially
                    category = EntityCategory.Terrain;
                }
            }

            // show "Faction Entities" dialog for active faction
            var dialog = new Dialog.ShowEntities(Session.MapView, faction, category, mode);

            dialog.Owner = MainWindow.Instance;
            dialog.ShowDialog();

            // retrieve entity to be placed, if any
            Entity placingEntity = dialog.PlacingEntity;

            if (placingEntity == null)
            {
                return;
            }

            // get valid target sites for entity class
            var targets = faction.GetPlaceTargets(world, placingEntity.EntityClass);

            if (targets.Count == 0)
            {
                return;
            }

            // create marker array for map view
            bool[,] region = Finder.MapGrid.CreateArray <Boolean>();

            // mark all valid target sites
            foreach (PointI site in targets)
            {
                region[site.X, site.Y] = true;
            }

            // start target selection for Place command
            session.TargetSelection.BeginCommand(typeof(PlaceCommand),
                                                 region, new List <Entity>(1)
            {
                placingEntity
            });
        }