コード例 #1
0
        /// <summary>
        /// Creates all of the custom controls for the blocks
        /// </summary>
        private void MakeControls()
        {
            IMyTerminalControlSeparator sep1
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlSeparator, IMyUpgradeModule>("Sep1");

            _radarControls.Add(sep1);

            IMyTerminalControlSlider rangeSlider
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlSlider, IMyUpgradeModule>("RangeSlider");

            rangeSlider.Title   = MyStringId.GetOrCompute("Range");
            rangeSlider.Tooltip = MyStringId.GetOrCompute("Maximum range of this radar system");
            rangeSlider.SetLimits(100, 15000);
            rangeSlider.Getter = (block) => {
                RadarController controller = block.GameLogic.GetAs <RadarController>();
                return(controller.GetRange());
            };
            rangeSlider.Setter = (block, value) => {
                RadarController controller = block.GameLogic.GetAs <RadarController>();
                controller.SetRange((int)value);
                SendRadarSettings(new BlockAddress(block.CubeGrid.EntityId, block.EntityId));
            };
            rangeSlider.Writer = (block, str) => {
                RadarController controller = block.GameLogic.GetAs <RadarController>();
                str.Append(controller.GetRange() + "m");
            };
            _radarRangeSlider = rangeSlider;
            _radarControls.Add(rangeSlider);

            IMyTerminalControlSlider freqSlider
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlSlider, IMyUpgradeModule>("FreqSlider");

            freqSlider.Title   = MyStringId.GetOrCompute("Frequency");
            freqSlider.Tooltip = MyStringId.GetOrCompute("Operating frequency of this system");
            freqSlider.SetLimits(8.0f, 12.0f);
            freqSlider.Getter = (block) => {
                RadarController controller = block.GameLogic.GetAs <RadarController>();
                return(controller.GetFreq());
            };
            freqSlider.Setter = (block, value) => {
                RadarController controller = block.GameLogic.GetAs <RadarController>();
                controller.SetFreq(value);
                SendRadarSettings(new BlockAddress(block.CubeGrid.EntityId, block.EntityId));
            };
            freqSlider.Writer = (block, str) => {
                RadarController controller = block.GameLogic.GetAs <RadarController>();
                str.Append(controller.GetFreq() + "GHz");
            };
            _radarFreqSlider = freqSlider;
            _radarControls.Add(freqSlider);

            IMyTerminalControlSeparator sep2
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlSeparator, IMyUpgradeModule>("Sep2");

            _radarControls.Add(sep2);

            IMyTerminalControlListbox unassignedList
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlListbox, IMyUpgradeModule>("UnassignedList");

            unassignedList.Title = MyStringId.GetOrCompute("Available");
            //unassignedList.Tooltip = MyStringId.GetOrCompute("Radar blocks which are able to be assigned to this system.");
            unassignedList.Multiselect      = true;
            unassignedList.VisibleRowsCount = 6;
            unassignedList.ListContent      = (block, items, selected) => {
                RadarController controller
                    = block.GameLogic.GetAs <RadarController>();
                List <RadarController.Radar> available
                    = controller.GetAvailableRadars();

                foreach (RadarController.Radar r in available)
                {
                    MyTerminalControlListBoxItem item
                        = new MyTerminalControlListBoxItem(
                              MyStringId.GetOrCompute(r.block.FatBlock.DisplayNameText),
                              MyStringId.GetOrCompute(r.type.ToString()),
                              r
                              );
                    items.Add(item);
                }
            };
            unassignedList.ItemSelected = (block, items) => {
                _selectedUnassigned.Clear();

                foreach (MyTerminalControlListBoxItem item in items)
                {
                    _selectedUnassigned.Add(item.UserData as RadarController.Radar);
                }
            };
            _radarControls.Add(unassignedList);

            IMyTerminalControlButton addButton
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlButton, IMyUpgradeModule>("AddButton");

            addButton.Title   = MyStringId.GetOrCompute("Assign");
            addButton.Tooltip = MyStringId.GetOrCompute("Assign the selected radar to this system.");
            _radarControls.Add(addButton);

            IMyTerminalControlListbox assignedList
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlListbox, IMyUpgradeModule>("AssignedList");

            assignedList.Title            = MyStringId.GetOrCompute("Assigned");
            assignedList.Tooltip          = MyStringId.GetOrCompute("Radar blocks which are currently assigned to this system.");
            assignedList.Multiselect      = true;
            assignedList.VisibleRowsCount = 6;
            assignedList.ListContent      = (block, items, selected) => {
                RadarController controller
                    = block.GameLogic.GetAs <RadarController>();
                List <RadarController.Radar> assigned
                    = controller.GetAssignedRadars();

                foreach (RadarController.Radar r in assigned)
                {
                    MyTerminalControlListBoxItem item
                        = new MyTerminalControlListBoxItem(
                              MyStringId.GetOrCompute(r.block.FatBlock.DisplayNameText),
                              MyStringId.GetOrCompute(r.type.ToString()),
                              r
                              );
                    items.Add(item);
                }
            };
            assignedList.ItemSelected = (block, items) => {
                _selectedAssigned.Clear();

                foreach (MyTerminalControlListBoxItem item in items)
                {
                    _selectedAssigned.Add(item.UserData as RadarController.Radar);
                }
            };
            _radarControls.Add(assignedList);

            // Add button action must be after assigned list because it
            // needs the pointer
            addButton.Action = (block) => {
                RadarController controller
                    = block.GameLogic.GetAs <RadarController>();

                foreach (RadarController.Radar radar in _selectedUnassigned)
                {
                    controller.AssignRadar(radar);
                }

                unassignedList.UpdateVisual();
                assignedList.UpdateVisual();

                SetRadarSliderLimits((int)controller.GetRadarType());
                rangeSlider.UpdateVisual();
                freqSlider.UpdateVisual();
            };

            IMyTerminalControlButton removeButton
                = MyAPIGateway.TerminalControls.CreateControl <IMyTerminalControlButton, IMyUpgradeModule>("AddButton");

            removeButton.Title   = MyStringId.GetOrCompute("Remove");
            removeButton.Tooltip = MyStringId.GetOrCompute("Remove the selected radars from the system.");
            removeButton.Action  = (block) => {
                RadarController controller
                    = block.GameLogic.GetAs <RadarController>();

                foreach (RadarController.Radar radar in _selectedAssigned)
                {
                    controller.UnassignedRadar(radar);
                }

                unassignedList.UpdateVisual();
                assignedList.UpdateVisual();

                SetRadarSliderLimits((int)controller.GetRadarType());
                rangeSlider.UpdateVisual();
                freqSlider.UpdateVisual();
            };
            _radarControls.Add(removeButton);
        }