/// <summary> /// Handles player input. When the escape key is pressed, the copy and paste is cancelled /// and all cleared. When you press the left mouse button, the item is pasted and the callback is called, if the clipboard is active. /// </summary> public override void HandleInput() { base.HandleInput(); if (MyInput.Static.IsNewKeyPressed(MyKeys.Escape)) { if (m_isActive) { MyPluginDrawSession.Static.RemoveRenderObject(m_copiedItem.GetHashCode()); m_copiedItem = null; m_callback = null; m_distanceToCam = 0; m_isActive = false; } } if (MyInput.Static.IsNewLeftMousePressed()) { if (m_isActive) { MyPluginDrawSession.Static.RemoveRenderObject(m_copiedItem.GetHashCode()); m_callback?.Invoke(m_copiedItem, m_currentPos); m_copiedItem = null; m_callback = null; m_distanceToCam = 0; m_isActive = false; } } }
/// <summary> /// Activates this clipboard with the given MySystemItem and sets the callback for the paste event /// </summary> /// <param name="item">Item to copy and paste</param> /// <param name="callback">Callback to call, when the item gets pasted</param> /// <param name="distanceToCam">The distance the object has to the camera</param> public void Activate(MySystemObject item, Action <MySystemObject, Vector3D> callback, float distanceToCam) { m_copiedItem = item; m_callback = callback; m_isActive = true; m_distanceToCam = distanceToCam; }
/// <summary> /// Deactivates the currently used clipboard /// </summary> public void Deactivate() { m_copiedItem = null; m_callback = null; m_isActive = false; m_distanceToCam = 0; }
private static void SendAddSystemObjectServer(MySystemObject obj, Guid parentId, ulong callbackId, ulong clientId) { MyPluginLog.Log("Server: Add object " + obj.DisplayName + " to system"); if (obj != null) { if (!Static.StarSystem.ObjectExists(obj.Id)) { var parent = Static.StarSystem.GetObjectById(parentId); if (parent != null) { parent.ChildObjects.Add(obj); obj.ParentId = parentId; PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, true, callbackId, clientId); } else { Static.StarSystem.CenterObject.ChildObjects.Add(obj); obj.ParentId = Static.StarSystem.CenterObject.Id; PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, true, callbackId, clientId); } Static.AddAllPersistentGps(); return; } } PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, false, callbackId, clientId); }
private static void SendRemoveSystemObjectServer(Guid objectId, ulong callbackId, ulong clientId) { MyPluginLog.Log("Server: Removing object " + objectId.ToString() + " from system"); MySystemObject o = Static.StarSystem.GetObjectById(objectId); if (o != null && o.DisplayName != Static.StarSystem.CenterObject.DisplayName) { if (o.Type == MySystemObjectType.ASTEROIDS) { var asteroids = o as MySystemAsteroids; if (MyAsteroidObjectsManager.Static.AsteroidObjectProviders.ContainsKey(asteroids.AsteroidTypeName)) { bool removed = MyAsteroidObjectsManager.Static.AsteroidObjectProviders[asteroids.AsteroidTypeName].RemoveInstance(asteroids); if (removed) { MyGPSManager.Static.RemovePersistentGps(objectId); PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, Static.StarSystem.RemoveObject(objectId), callbackId, clientId); return; } } } else { MyGPSManager.Static.RemovePersistentGps(objectId); PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, Static.StarSystem.RemoveObject(objectId), callbackId, clientId); return; } } PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, false, callbackId, clientId); }
/// <summary> /// Action when system object is clicked in the system object list box. /// Sets the currently selected system object /// </summary> /// <param name="box"></param> private void OnSystemObjectSelect(MyGuiControlListbox box) { if (m_currentAsteroidAdminMenu != null) { m_currentAsteroidAdminMenu.Close(); } m_selectedObject = box.SelectedItems[box.SelectedItems.Count - 1].UserData as MySystemObject; BuildEditingSubMenu(); }
private static void SendGetObjectClient(bool success, MySystemObject obj, ulong callbackId) { MyPluginLog.Log("Client: Received get object message from server with success " + success); if (Static.m_getActionCallbacks.ContainsKey(callbackId)) { Static.m_getActionCallbacks[callbackId](success, obj); Static.m_getActionCallbacks.Remove(callbackId); } }
/// <summary> /// Generates a MyAsteroidSphereData object from the gui elements of the spawn menu /// </summary> /// <returns>The generated data</returns> private MyAsteroidSphereData GetDataFromGui() { if (m_parentObjectListBox.SelectedItems.Count <= 0) { return(null); } var selected = m_parentObjectListBox.SelectedItems[m_parentObjectListBox.SelectedItems.Count - 1]; MySystemObject parent = selected.UserData as MySystemObject; MyAsteroidSphereData data = new MyAsteroidSphereData(); data.Center = parent.CenterPosition; data.InnerRadius = m_radiusSlider.Value * 1000; data.OuterRadius = m_radiusSlider.Value * 1000 + m_widthSlider.Value * 1000; return(data); }
/// <summary> /// Spawns a planet in the system /// </summary> /// <param name="planet">Planet to spawn</param> /// <param name="position">Position to spawn at</param> private void SpawnPlanet(MySystemObject planet, Vector3D position) { if (planet.Type == MySystemObjectType.PLANET) { MySystemPlanet p = planet as MySystemPlanet; p.CenterPosition = position; MyStarSystemGenerator.Static.AddObjectToSystem(p, callback : delegate(bool success) { if (!success) { MyPluginGuiHelper.DisplayError("Planet could not be spawned, because an object with the same id already exists. This error should not occour, so please try again.", "Error"); } }); } }
/// <summary> /// Generates an asteroid ring from the current slider values in the spawn menu /// </summary> /// <returns>The generated system ring data</returns> private MyAsteroidRingData GenerateAsteroidRing() { if (m_parentObjectListBox.SelectedItems.Count <= 0) { return(null); } var selected = m_parentObjectListBox.SelectedItems[m_parentObjectListBox.SelectedItems.Count - 1]; MySystemObject parent = selected.UserData as MySystemObject; MyAsteroidRingData ring = new MyAsteroidRingData(); ring.CenterPosition = parent.CenterPosition + m_offset; ring.Width = m_widthSlider.Value * 1000; ring.Height = m_heightSlider.Value * 1000; ring.Radius = m_radiusSlider.Value * 1000; ring.AngleDegrees = new Vector3D(m_angleXSlider.Value, m_angleYSlider.Value, m_angleZSlider.Value); return(ring); }
private static void SendGetObjectServer(ulong clientId, Guid id, ulong callbackId) { MyPluginLog.Log("Server: Client requests object. Client ID: " + clientId); bool success = true; if (Static != null) { MySystemObject res = Static.StarSystem.GetObjectById(id); if (res == null) { res = new MySystemObject(); success = false; } PluginEventHandler.Static.RaiseStaticEvent(SendGetObjectClient, success, res, callbackId, clientId); } else { PluginEventHandler.Static.RaiseStaticEvent(SendGetObjectClient, false, new MySystemObject(), callbackId, clientId); } }
/// <summary> /// Adds a new object to the system. If parentName is provided, it will be added as a child to that /// object, else it will be added as a child to the sun. /// </summary> /// <param name="systemObject">Object to add</param> /// <param name="parentId">Id of the parent object</param> /// <param name="callback">Callback to run when object was added</param> public void AddObjectToSystem(MySystemObject systemObject, Guid?parentId = null, Action <bool> callback = null) { m_simpleActionsCallbacks.Add(++m_currentSimpleIndex, callback); PluginEventHandler.Static.RaiseStaticEvent(SendAddSystemObjectServer, systemObject, parentId == null ? Guid.Empty : parentId.Value, m_currentSimpleIndex, Sync.MyId); }
/// <summary> /// Generates a completely new system based on the /// world settings. /// </summary> /// <returns></returns> private MyObjectBuilder_SystemData GenerateNewStarSystem() { MyPluginLog.Log("Generating a new Solar system ..."); int seed = MySession.Static.Settings.ProceduralSeed + Guid.NewGuid().GetHashCode(); MyObjectBuilder_SystemData system = new MyObjectBuilder_SystemData(); var settings = MySettingsSession.Static.Settings.GeneratorSettings; var orbitDistances = settings.MinMaxOrbitDistance; var planetsAmount = settings.MinMaxPlanets; var asteroidObjectAmount = settings.MinMaxAsteroidObjects; var worldSize = settings.WorldSize; var asteroidProviders = MyAsteroidObjectsManager.Static.AsteroidObjectProviders; using (MyRandom.Instance.PushSeed(seed)) { long planetCount = MyRandom.Instance.Next(planetsAmount.Min, planetsAmount.Max + 1); long asteroidObjectCount = MyRandom.Instance.Next(asteroidObjectAmount.Min, asteroidObjectAmount.Max + 1); long systemSize = planetCount + asteroidObjectCount; int currentPlanetIndex = 0; int currentAsteroidIndex = 0; long currentOrbitDistance = 0; double planetProb = planetCount / (double)(planetCount + asteroidObjectCount); if (m_suns.Count > 0) { MyPlanetGeneratorDefinition sunDef = m_suns[MyRandom.Instance.Next(0, m_suns.Count)]; MySystemPlanet sun = new MySystemPlanet(); sun.CenterPosition = Vector3D.Zero; sun.SubtypeId = sunDef.Id.SubtypeId.String; sun.DisplayName = sunDef.Id.SubtypeId.String; sun.Diameter = CalculatePlanetDiameter(sunDef) * 2; sun.ChildObjects = new HashSet <MySystemObject>(); sun.Generated = false; sun.Type = MySystemObjectType.PLANET; system.CenterObject = sun; currentOrbitDistance += (long)sun.Diameter * 2 + (long)sunDef.AtmosphereHeight; } else { system.CenterObject = new MySystemObject(); system.CenterObject.Type = MySystemObjectType.EMPTY; system.CenterObject.DisplayName = "System center"; } while (planetCount > 0 || asteroidObjectCount > 0) { currentOrbitDistance += MyRandom.Instance.Next(orbitDistances.Min, orbitDistances.Max); //Maybe rework to override orbit distance, so all objects fit if (worldSize >= 0 && currentOrbitDistance >= worldSize) { return(system); } MySystemObject obj = null; if (asteroidObjectCount <= 0 || (MyRandom.Instance.NextDouble() <= planetProb && planetCount > 0)) // Generate planet { obj = GeneratePlanet(currentPlanetIndex++, Math.Sin((system.Count() - 1) * Math.PI / systemSize), currentOrbitDistance); planetCount--; } else if (asteroidObjectCount > 0) // Generate asteroid object { int providerIndex = MyRandom.Instance.Next(0, asteroidProviders.Keys.Count); MyAbstractAsteroidObjectProvider provider = null; foreach (var prov in asteroidProviders) { if (!prov.Value.IsSystemGeneratable()) { continue; } if (providerIndex-- == 0) { provider = prov.Value; } } if (provider == null) { continue; } obj = provider.GenerateInstance(currentAsteroidIndex++, null, currentOrbitDistance); if (obj == null) { continue; } (obj as MySystemAsteroids).AsteroidTypeName = provider.GetTypeName(); asteroidObjectCount--; } if (obj == null) { continue; } obj.ParentId = system.CenterObject.Id; system.CenterObject.ChildObjects.Add(obj); } } MyPluginLog.Log("Solar system generated ..."); return(system); }
/// <summary> /// Builds the edit menu /// </summary> private void BuildEditMenu() { MyPluginLog.Debug("Adding edit menu"); if (m_fetchedStarSytem == null || ForceFetchStarSystem) { MyPluginLog.Debug("Fetching system data"); MyGuiControlRotatingWheel m_loadingWheel = new MyGuiControlRotatingWheel(position: Vector2.Zero); m_loadingWheel.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER; Controls.Add(m_loadingWheel); MyStarSystemGenerator.Static.GetStarSystem(delegate(MyObjectBuilder_SystemData starSystem) { m_fetchedStarSytem = starSystem; m_selectedObject = null; ShouldRecreate = true; ForceFetchStarSystem = false; }); return; } var topCombo = GetCombo(); Vector2 start = topCombo.Position + new Vector2(0, MARGIN_VERT * 2 + GetCombo().Size.Y); Vector2 end = start + new Vector2(topCombo.Size.X, 0.8f - MARGIN_VERT); MyGuiControlLabel systemObjsLabel = new MyGuiControlLabel(null, null, "System Objects"); systemObjsLabel.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP; systemObjsLabel.Position = start; Controls.Add(systemObjsLabel); m_systemObjectsBox = new MyGuiControlListbox(); m_systemObjectsBox.VisibleRowsCount = 8; m_systemObjectsBox.Size = new Vector2(m_usableWidth, m_systemObjectsBox.Size.Y); m_systemObjectsBox.Position = start; m_systemObjectsBox.PositionY += systemObjsLabel.Size.Y + MARGIN_VERT; m_systemObjectsBox.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP; foreach (var obj in m_fetchedStarSytem.GetAllObjects()) { if (obj.Type == MySystemObjectType.EMPTY) { continue; } m_systemObjectsBox.Add(new MyGuiControlListbox.Item(new System.Text.StringBuilder(obj.DisplayName), userData: obj)); } if (m_selectedObject != null) { m_systemObjectsBox.SelectByUserData(m_selectedObject); } m_systemObjectsBox.ItemsSelected += OnSystemObjectSelect; Controls.Add(m_systemObjectsBox); MyGuiControlSeparatorList sep = new MyGuiControlSeparatorList(); sep.AddHorizontal(new Vector2(m_systemObjectsBox.Position.X, m_systemObjectsBox.Position.Y + m_systemObjectsBox.Size.Y + MARGIN_VERT), m_usableWidth); BuildEditingSubMenu(); sep.AddHorizontal(new Vector2(m_scrollPane.Position.X - m_scrollPane.Size.X / 2, m_scrollPane.Position.Y + m_scrollPane.Size.Y), m_usableWidth); Controls.Add(sep); MyPluginLog.Debug("Added edit menu"); }