/// <summary> /// Places a <see cref="MapGrh"/> on the map. /// </summary> /// <param name="map">The map to place the <see cref="MapGrh"/> on.</param> /// <param name="camera">The <see cref="ICamera2D"/>.</param> /// <param name="screenPos">The screen position to place the <see cref="MapGrh"/>.</param> /// <param name="useTileMode">If TileMode should be used.</param> /// <param name="gd">The <see cref="GrhData"/> to place. Set to null to attempt to use the <see cref="GrhData"/> that is /// currently selected in the <see cref="GlobalState"/>.</param> /// <returns>The <see cref="MapGrh"/> instance that was added, or null if the the <see cref="MapGrh"/> could not be /// added for any reason.</returns> public static MapGrh PlaceMapGrhAtScreenPos(Map map, ICamera2D camera, Vector2 screenPos, bool useTileMode, GrhData gd = null) { // Get the world position to place it var worldPos = camera.ToWorld(screenPos); return(PlaceMapGrhAtWorldPos(map, worldPos, useTileMode, gd)); }
/// <summary> /// Handles when the mouse moves. /// </summary> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> /// <param name="camera">The <see cref="ICamera2D"/> describing the current view.</param> /// <returns>True if the <see cref="TransBoxManager"/> handled the event; otherwise false.</returns> public bool HandleMouseMove(MouseEventArgs e, ICamera2D camera) { if (_transBoxes.Count == 0) { return(false); } var worldPos = camera.ToWorld(e.Position()); _lastWorldPos = worldPos; // Update what transbox is under the cursor if (SelectedTransBox != null) { UnderCursor = SelectedTransBox; } else { UnderCursor = FindBoxAt(worldPos); } // Update position if (SelectedTransBox != null) { SelectedTransBox.CursorMoved(_lastWorldPos); } return(SelectedTransBox != null); }
/// <summary> /// Handles when the mouse button is raised on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseUp(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { var cursorPos = e.Position(); var worldPos = camera.ToWorld(cursorPos); // Create entity if (e.Button == MouseButtons.Right) { Type createType = null; // Create using same type as the last entity, if possible if (Input.IsCtrlDown) { createType = _lastCreatedType; } // Display selection dialog if (createType == null) { using (var frm = new EntityTypeUITypeEditorForm(_lastCreatedType)) { if (frm.ShowDialog(sender as IWin32Window) == DialogResult.OK) { createType = frm.SelectedItem; } } } // Create the type if (createType != null) { _lastCreatedType = null; try { // Create the Entity var entity = (Entity)Activator.CreateInstance(createType); map.AddEntity(entity); entity.Size = new Vector2(64); entity.Position = worldPos - (entity.Size / 2f); GridAligner.Instance.Fit(entity); _lastCreatedType = createType; } catch (Exception ex) { const string errmsg = "Failed to create entity of type `{0}` on map `{1}`. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, createType, map, ex); } Debug.Fail(string.Format(errmsg, createType, map, ex)); } } } base.MapContainer_MouseUp(sender, map, camera, e); }
/// <summary> /// Handles when the mouse moves over a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseMove(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { _mouseOverMap = map; var worldPos = camera.ToWorld(e.Position()); if (IsSelecting) { // Expand selection area _selectionEnd = worldPos; } else { // Create the tooltip var font = ToolTipFont; if (ShowObjectToolTip && font != null) { var hoverEntity = GetObjUnderCursor(map, worldPos); if (hoverEntity == null) { // Nothing under the cursor that we are allowed to select _toolTip = string.Empty; _toolTipObject = null; } else if (_toolTipObject != hoverEntity) { // Something found under the cursor _toolTipObject = hoverEntity; _toolTipPos = e.Position(); _toolTip = GetObjectToolTip(hoverEntity) ?? hoverEntity.ToString(); // Make sure the text stays in the view area const int toolTipPadding = 4; var toolTipSize = font.MeasureString(_toolTip); if (_toolTipPos.X < toolTipPadding) { _toolTipPos.X = toolTipPadding; } else if (_toolTipPos.X + toolTipSize.X + toolTipPadding > camera.Size.X) { _toolTipPos.X = camera.Size.X - toolTipSize.X - toolTipPadding; } if (_toolTipPos.Y < toolTipPadding) { _toolTipPos.Y = toolTipPadding; } else if (_toolTipPos.Y + toolTipSize.Y + toolTipPadding > camera.Size.Y) { _toolTipPos.Y = camera.Size.Y - toolTipSize.Y - toolTipPadding; } } } } base.MapContainer_MouseMove(sender, map, camera, e); }
/// <summary> /// Handles both mouse clicks and moves (does the place/deletes of grhs). /// </summary> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> void HandleMouseClickAndMove(EditorMap map, ICamera2D camera, MouseEventArgs e) { // Update some vars var cursorPos = e.Position(); _mouseOverMap = map; _mousePos = cursorPos; // Don't do any place/deletes while selecting if (IsSelecting) { return; } Vector2 worldPos = camera.ToWorld(cursorPos); // Handle mouse if (e.Button == MouseButtons.Left) { if (!Input.IsShiftDown) { // Place grh PlaceMapGrhAtScreenPos(map, camera, cursorPos, !Input.IsCtrlDown); } else { // Select grh under cursor var grhToSelect = GetGrhToSelect(map, worldPos); if (grhToSelect != null) { GlobalState.Instance.Map.SetGrhToPlace(grhToSelect.Grh.GrhData.GrhIndex); GlobalState.Instance.Map.Layer = grhToSelect.MapRenderLayer; GlobalState.Instance.Map.LayerDepth = grhToSelect.LayerDepth; } } } else if (e.Button == MouseButtons.Right) { if (!Input.IsShiftDown) { // Delete from current layer var grhToDelete = GetGrhToSelect(map, worldPos, mustBeOnLayer: GlobalState.Instance.Map.Layer); if (grhToDelete != null) { map.RemoveMapGrh(grhToDelete); } } else { // Delete from all layers var grhToSelect = GetGrhToSelect(map, worldPos); if (grhToSelect != null) { map.RemoveMapGrh(grhToSelect); } } } }
/// <summary> /// Handles both mouse clicks and moves. /// </summary> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> void HandleMouseClickAndMove(EditorMap map, ICamera2D camera, MouseEventArgs e) { // Update some vars var cursorPos = e.Position(); _mouseOverMap = map; _mousePos = cursorPos; var globalState = GlobalState.Instance; var currentGrhData = globalState.Map.GrhToPlace.GrhData; Vector2 worldPos = camera.ToWorld(cursorPos); // Handle mouse if (e.Button == MouseButtons.Left) { if (!Input.IsShiftDown) { // Fill if (currentGrhData != null) { var mapGrhsToReplace = GetFillMapGrhs(map, worldPos); foreach (var mapGrh in mapGrhsToReplace) { mapGrh.Grh.SetGrh(currentGrhData); } } } else { // Select grh under cursor var grhToSelect = MapGrhPencilTool.GetGrhToSelect(map, worldPos); if (grhToSelect != null) { globalState.Map.SetGrhToPlace(grhToSelect.Grh.GrhData.GrhIndex); globalState.Map.Layer = grhToSelect.MapRenderLayer; globalState.Map.LayerDepth = grhToSelect.LayerDepth; } } } else if (e.Button == MouseButtons.Right) { // Fill-delete if (currentGrhData != null) { var mapGrhsToReplace = GetFillMapGrhs(map, worldPos); foreach (var mapGrh in mapGrhsToReplace) { map.RemoveMapGrh(mapGrh); } } } }
/// <summary> /// Handles when the mouse button is raised on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseUp(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { if (IsSelecting && IsSelectMouseButton(e.Button)) { // End the mass selection _selectionEnd = camera.ToWorld(e.Position()); var area = Rectangle.FromPoints(_selectionStart, _selectionEnd); MapContainer_AreaSelected(map, camera, area, e); _isSelecting = false; _selectionStart = Vector2.Zero; _selectionEnd = Vector2.Zero; } base.MapContainer_MouseUp(sender, map, camera, e); }
/// <summary> /// Handles when the mouse button is raised on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseDown(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { var cursorPos = e.Position(); var worldPos = camera.ToWorld(cursorPos); if (e.Button == MouseButtons.Right) { var entity = new WallEntity(worldPos, new Vector2(4)); GridAligner.Instance.Fit(entity); map.AddEntity(entity); SOM.SetSelected(entity); } base.MapContainer_MouseDown(sender, map, camera, e); }
/// <summary> /// Handles when the mouse button is raised on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseUp(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { if (IsSelecting && e.Button == SelectMouseButton) { // End the mass selection _selectionEnd = camera.ToWorld(e.Position()); var area = Rectangle.FromPoints(_selectionStart, _selectionEnd); var selected = CursorSelectObjects(map, area); GlobalState.Instance.Map.SelectedObjsManager.SetManySelected(selected); _isSelecting = false; _selectionStart = Vector2.Zero; _selectionEnd = Vector2.Zero; } base.MapContainer_MouseUp(sender, map, camera, e); }
/// <summary> /// Handles when a mouse button is pressed down. /// </summary> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> /// <param name="camera">The <see cref="ICamera2D"/> describing the current view.</param> /// <returns>True if the <see cref="TransBoxManager"/> handled the event; otherwise false.</returns> public bool HandleMouseDown(MouseEventArgs e, ICamera2D camera) { if (e.Button == DragButton) { var worldPos = camera.ToWorld(e.Position()); UnderCursor = FindBoxAt(worldPos); SelectedTransBox = UnderCursor; _lastWorldPos = worldPos; if (SelectedTransBox != null) { _selectedTransBoxButton = e.Button; } else { _selectedTransBoxButton = MouseButtons.None; } } return(SelectedTransBox != null); }
/// <summary> /// Handles when a mouse button is pressed on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseDown(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { // Terminate any current area selection when any mouse button is pressed _selectionStart = Vector2.Zero; _selectionEnd = Vector2.Zero; _isSelecting = false; var worldPos = camera.ToWorld(e.Position()); var underCursor = GetObjUnderCursor(map, worldPos); GlobalState.Instance.Map.SelectedObjsManager.SetSelected(underCursor); if (e.Button == SelectMouseButton && ((Control.ModifierKeys & SelectKey) != 0)) { // Start area selection _selectionStart = worldPos; _selectionEnd = _selectionStart; _isSelecting = true; } base.MapContainer_MouseDown(sender, map, camera, e); }
/// <summary> /// Handles when a mouse button is pressed on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseDown(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { base.MapContainer_MouseDown(sender, map, camera, e); if (IsSelecting) { return; } // Left-click if (e.Button == MouseButtons.Left) { // Place light if (Input.IsKeyDown(_placeLightKey)) { var msc = MapScreenControl.TryFindInstance(map); if (msc != null) { var dm = msc.DrawingManager; if (dm != null) { var pos = camera.ToWorld(e.Position()); pos = GridAligner.Instance.Align(pos); var light = new Light { Center = pos, IsEnabled = true, Tag = map }; map.AddLight(light); dm.LightManager.Add(light); } } } } }
/// <summary> /// Draws the <see cref="ITransBox"/>. /// </summary> /// <param name="spriteBatch">The <see cref="ISpriteBatch"/> to draw to.</param> /// <param name="camera">The <see cref="ICamera2D"/>.</param> public void Draw(ISpriteBatch spriteBatch, ICamera2D camera) { var sprite = GetSprite(_type); if (sprite == null) { return; } var p = camera.ToScreen(Position).Round(); var s = Size.Round(); var r = new Rectangle((int)p.X, (int)p.Y, (int)s.X, (int)s.Y); p = camera.ToScreen(GetPosition() + Vector2.One).Round(); bool okFlag = false; if (ContainsPoint(camera.ToWorld(p))) // Something went right { okFlag = true; } var newR = new Rectangle((int)p.X, (int)p.Y, (int)s.X, (int)s.Y); sprite.Draw(spriteBatch, r, Color.White); // Draw a marker at ACTUAL transbox positions for debug purposes. if (okFlag) { sprite.Draw(spriteBatch, newR, Color.Green); } else { sprite.Draw(spriteBatch, newR, Color.Red); } }
/// <summary> /// Handles when the mouse moves over a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseMove(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { base.MapContainer_MouseMove(sender, map, camera, e); var cursorPos = e.Position(); _mouseOverMap = map; _mousePos = cursorPos; // Support dragging operations when using TileMode if (TileMode) { if (Input.IsKeyDown(_placeMapGrhKey)) { if (e.Button == MouseButtons.Left) { // Drag placement PlaceMapGrh(map, camera, e.Position(), true); } else if (e.Button == MouseButtons.Right) { // Drag delete var worldPos = camera.ToWorld(e.Position()); worldPos = GridAligner.Instance.Align(worldPos, true); var worldPosArea = worldPos.ToRectangle(Vector2.One, false); var toDelete = map.Spatial.GetMany <MapGrh>(worldPosArea, x => IsObjectVisible(map, x)); foreach (var x in toDelete) { map.RemoveMapGrh(x); } } } } }
/// <summary> /// Handles when a mouse button is pressed on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseDown(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { base.MapContainer_MouseDown(sender, map, camera, e); if (IsSelecting) return; // Left-click if (e.Button == MouseButtons.Left) { // Place light if (Input.IsKeyDown(_placeLightKey)) { var msc = MapScreenControl.TryFindInstance(map); if (msc != null) { var dm = msc.DrawingManager; if (dm != null) { var pos = camera.ToWorld(e.Position()); pos = GridAligner.Instance.Align(pos); var light = new Light { Center = pos, IsEnabled = true, Tag = map }; map.AddLight(light); dm.LightManager.Add(light); } } } } }
/// <summary> /// Places a <see cref="MapGrh"/> on the map. /// </summary> /// <param name="map">The map to place the <see cref="MapGrh"/> on.</param> /// <param name="camera">The <see cref="ICamera2D"/>.</param> /// <param name="screenPos">The screen position to place the <see cref="MapGrh"/>.</param> /// <param name="useTileMode">If TileMode should be used.</param> /// <param name="gd">The <see cref="GrhData"/> to place. Set to null to attempt to use the <see cref="GrhData"/> that is /// currently selected in the <see cref="GlobalState"/>.</param> /// <returns>The <see cref="MapGrh"/> instance that was added, or null if the the <see cref="MapGrh"/> could not be /// added for any reason.</returns> public static MapGrh PlaceMapGrh(Map map, ICamera2D camera, Vector2 screenPos, bool useTileMode, GrhData gd = null) { // Get the GrhData to place from the global state if (gd == null) gd = GlobalState.Instance.Map.GrhToPlace.GrhData; // Ensure we have a GrhData to place if (gd == null) return null; // Get the world position to place it var drawPos = camera.ToWorld(screenPos); drawPos = GridAligner.Instance.Align(drawPos, useTileMode).Round(); // Cache some other values var selGrhGrhIndex = gd.GrhIndex; var isForeground = EditorSettings.Default.MapGrh_DefaultIsForeground; var depth = EditorSettings.Default.MapGrh_DefaultDepth; var drawPosArea = drawPos.ToRectangle(new Vector2(2), true); if (!useTileMode) { // Make sure the same MapGrh doesn't already exist at that position if (map.Spatial.Contains<MapGrh>(drawPosArea, x => x.Grh.GrhData.GrhIndex == selGrhGrhIndex && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1)) return null; } else { // Make sure the same MapGrh doesn't already exist at that position on the same layer if (map.Spatial.Contains<MapGrh>(drawPosArea, x => x.Grh.GrhData.GrhIndex == selGrhGrhIndex && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1)) return null; // In TileMode, do not allow ANY MapGrh at the same position and layer depth. And if it does exist, instead of aborting, // delete the existing one. var existingMapGrhs = map.Spatial.GetMany<MapGrh>(drawPosArea, x => x.LayerDepth == depth && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1); foreach (var toDelete in existingMapGrhs) { Debug.Assert(toDelete != null); if (log.IsDebugEnabled) log.DebugFormat("TileMode caused MapGrh `{0}` to be overwritten.", toDelete); map.RemoveMapGrh(toDelete); } Debug.Assert( !map.Spatial.Contains<MapGrh>(drawPosArea, x => x.LayerDepth == depth && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1)); } // Create the new MapGrh and add it to the map var g = new Grh(gd, AnimType.Loop, map.GetTime()); var mg = new MapGrh(g, drawPos, isForeground) { LayerDepth = depth }; map.AddMapGrh(mg); return mg; }
/// <summary> /// Handles when the mouse moves over a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseMove(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { base.MapContainer_MouseMove(sender, map, camera, e); var cursorPos = e.Position(); _mouseOverMap = map; _mousePos = cursorPos; // Support dragging operations when using TileMode if (TileMode) { if (Input.IsKeyDown(_placeMapGrhKey)) { if (e.Button == MouseButtons.Left) { // Drag placement PlaceMapGrh(map, camera, e.Position(), true); } else if (e.Button == MouseButtons.Right) { // Drag delete var worldPos = camera.ToWorld(e.Position()); worldPos = GridAligner.Instance.Align(worldPos, true); var worldPosArea = worldPos.ToRectangle(Vector2.One, false); var toDelete = map.Spatial.GetMany<MapGrh>(worldPosArea, x => IsObjectVisible(map, x)); foreach (var x in toDelete) { map.RemoveMapGrh(x); } } } } }
/// <summary> /// Places a <see cref="MapGrh"/> on the map. /// </summary> /// <param name="map">The map to place the <see cref="MapGrh"/> on.</param> /// <param name="camera">The <see cref="ICamera2D"/>.</param> /// <param name="screenPos">The screen position to place the <see cref="MapGrh"/>.</param> /// <param name="useTileMode">If TileMode should be used.</param> /// <param name="gd">The <see cref="GrhData"/> to place. Set to null to attempt to use the <see cref="GrhData"/> that is /// currently selected in the <see cref="GlobalState"/>.</param> /// <returns>The <see cref="MapGrh"/> instance that was added, or null if the the <see cref="MapGrh"/> could not be /// added for any reason.</returns> public static MapGrh PlaceMapGrh(Map map, ICamera2D camera, Vector2 screenPos, bool useTileMode, GrhData gd = null) { // Get the GrhData to place from the global state if (gd == null) { gd = GlobalState.Instance.Map.GrhToPlace.GrhData; } // Ensure we have a GrhData to place if (gd == null) { return(null); } // Get the world position to place it var drawPos = camera.ToWorld(screenPos); drawPos = GridAligner.Instance.Align(drawPos, useTileMode).Round(); // Cache some other values var selGrhGrhIndex = gd.GrhIndex; var isForeground = EditorSettings.Default.MapGrh_DefaultIsForeground; var depth = EditorSettings.Default.MapGrh_DefaultDepth; var drawPosArea = drawPos.ToRectangle(new Vector2(2), true); if (!useTileMode) { // Make sure the same MapGrh doesn't already exist at that position if (map.Spatial.Contains <MapGrh>(drawPosArea, x => x.Grh.GrhData.GrhIndex == selGrhGrhIndex && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1)) { return(null); } } else { // Make sure the same MapGrh doesn't already exist at that position on the same layer if (map.Spatial.Contains <MapGrh>(drawPosArea, x => x.Grh.GrhData.GrhIndex == selGrhGrhIndex && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1)) { return(null); } // In TileMode, do not allow ANY MapGrh at the same position and layer depth. And if it does exist, instead of aborting, // delete the existing one. var existingMapGrhs = map.Spatial.GetMany <MapGrh>(drawPosArea, x => x.LayerDepth == depth && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1); foreach (var toDelete in existingMapGrhs) { Debug.Assert(toDelete != null); if (log.IsDebugEnabled) { log.DebugFormat("TileMode caused MapGrh `{0}` to be overwritten.", toDelete); } map.RemoveMapGrh(toDelete); } Debug.Assert( !map.Spatial.Contains <MapGrh>(drawPosArea, x => x.LayerDepth == depth && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1)); } // Create the new MapGrh and add it to the map var g = new Grh(gd, AnimType.Loop, map.GetTime()); var mg = new MapGrh(g, drawPos, isForeground) { LayerDepth = depth }; map.AddMapGrh(mg); return(mg); }
/// <summary> /// Handles when the mouse moves. /// </summary> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> /// <param name="camera">The <see cref="ICamera2D"/> describing the current view.</param> /// <returns>True if the <see cref="TransBoxManager"/> handled the event; otherwise false.</returns> public bool HandleMouseMove(MouseEventArgs e, ICamera2D camera) { if (_transBoxes.Count == 0) return false; var worldPos = camera.ToWorld(e.Position()); _lastWorldPos = worldPos; // Update what transbox is under the cursor if (SelectedTransBox != null) UnderCursor = SelectedTransBox; else UnderCursor = FindBoxAt(worldPos); // Update position if (SelectedTransBox != null) SelectedTransBox.CursorMoved(_lastWorldPos); return SelectedTransBox != null; }
/// <summary> /// Draws the <see cref="ITransBox"/>. /// </summary> /// <param name="spriteBatch">The <see cref="ISpriteBatch"/> to draw to.</param> /// <param name="camera">The <see cref="ICamera2D"/>.</param> public void Draw(ISpriteBatch spriteBatch, ICamera2D camera) { var sprite = GetSprite(_type); if (sprite == null) return; var p = camera.ToScreen(Position).Round(); var s = Size.Round(); var r = new Rectangle((int)p.X, (int)p.Y, (int)s.X, (int)s.Y); p = camera.ToScreen(GetPosition() + Vector2.One).Round(); bool okFlag = false; if (ContainsPoint(camera.ToWorld(p))) // Something went right okFlag = true; var newR = new Rectangle((int)p.X, (int)p.Y, (int)s.X, (int)s.Y); sprite.Draw(spriteBatch, r, Color.White); // Draw a marker at ACTUAL transbox positions for debug purposes. if (okFlag) sprite.Draw(spriteBatch, newR, Color.Green); else sprite.Draw(spriteBatch, newR, Color.Red); }
/// <summary> /// Handles when the mouse button is raised on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseDown(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { var cursorPos = e.Position(); var worldPos = camera.ToWorld(cursorPos); if (e.Button == MouseButtons.Right) { var ga = GridAligner.Instance; var entity = new WallEntity(worldPos, ga.GridSize); ga.Fit(entity); map.AddEntity(entity); SOM.SetSelected(entity); } base.MapContainer_MouseDown(sender, map, camera, e); }
/// <summary> /// Places a <see cref="MapGrh"/> on the map. /// </summary> /// <param name="map">The map to place the <see cref="MapGrh"/> on.</param> /// <param name="camera">The <see cref="ICamera2D"/>.</param> /// <param name="screenPos">The screen position to place the <see cref="MapGrh"/>.</param> /// <param name="useTileMode">If TileMode should be used.</param> /// <param name="gd">The <see cref="GrhData"/> to place. Set to null to attempt to use the <see cref="GrhData"/> that is /// currently selected in the <see cref="GlobalState"/>.</param> /// <returns>The <see cref="MapGrh"/> instance that was added, or null if the the <see cref="MapGrh"/> could not be /// added for any reason.</returns> public static MapGrh PlaceMapGrhAtScreenPos(Map map, ICamera2D camera, Vector2 screenPos, bool useTileMode, GrhData gd = null) { // Get the world position to place it var worldPos = camera.ToWorld(screenPos); return PlaceMapGrhAtWorldPos(map, worldPos, useTileMode, gd); }
/// <summary> /// Handles both mouse clicks and moves (does the place/deletes of grhs). /// </summary> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> void HandleMouseClickAndMove(EditorMap map, ICamera2D camera, MouseEventArgs e) { // Update some vars var cursorPos = e.Position(); _mouseOverMap = map; _mousePos = cursorPos; // Don't do any place/deletes while selecting if (IsSelecting) return; Vector2 worldPos = camera.ToWorld(cursorPos); // Handle mouse if (e.Button == MouseButtons.Left) { if (!Input.IsShiftDown) { // Place grh PlaceMapGrhAtScreenPos(map, camera, cursorPos, !Input.IsCtrlDown); } else { // Select grh under cursor var grhToSelect = GetGrhToSelect(map, worldPos); if (grhToSelect != null) { GlobalState.Instance.Map.SetGrhToPlace(grhToSelect.Grh.GrhData.GrhIndex); GlobalState.Instance.Map.Layer = grhToSelect.MapRenderLayer; GlobalState.Instance.Map.LayerDepth = grhToSelect.LayerDepth; } } } else if (e.Button == MouseButtons.Right) { if (!Input.IsShiftDown) { // Delete from current layer var grhToDelete = GetGrhToSelect(map, worldPos, mustBeOnLayer: GlobalState.Instance.Map.Layer); if (grhToDelete != null) map.RemoveMapGrh(grhToDelete); } else { // Delete from all layers var grhToSelect = GetGrhToSelect(map, worldPos); if (grhToSelect != null) map.RemoveMapGrh(grhToSelect); } } }
/// <summary> /// Handles when the mouse moves over a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseMove(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { _mouseOverMap = map; var worldPos = camera.ToWorld(e.Position()); if (IsSelecting) { // Expand selection area _selectionEnd = worldPos; } else { // Create the tooltip var font = ToolTipFont; if (ShowObjectToolTip && font != null) { var hoverEntity = GetObjUnderCursor(map, worldPos); if (hoverEntity == null) { // Nothing under the cursor that we are allowed to select _toolTip = string.Empty; _toolTipObject = null; } else if (_toolTipObject != hoverEntity) { // Something found under the cursor _toolTipObject = hoverEntity; _toolTipPos = e.Position(); _toolTip = GetObjectToolTip(hoverEntity) ?? hoverEntity.ToString(); // Make sure the text stays in the view area const int toolTipPadding = 4; var toolTipSize = font.MeasureString(_toolTip); if (_toolTipPos.X < toolTipPadding) _toolTipPos.X = toolTipPadding; else if (_toolTipPos.X + toolTipSize.X + toolTipPadding > camera.Size.X) _toolTipPos.X = camera.Size.X - toolTipSize.X - toolTipPadding; if (_toolTipPos.Y < toolTipPadding) _toolTipPos.Y = toolTipPadding; else if (_toolTipPos.Y + toolTipSize.Y + toolTipPadding > camera.Size.Y) _toolTipPos.Y = camera.Size.Y - toolTipSize.Y - toolTipPadding; } } } base.MapContainer_MouseMove(sender, map, camera, e); }
/// <summary> /// Handles when a mouse button is pressed down. /// </summary> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> /// <param name="camera">The <see cref="ICamera2D"/> describing the current view.</param> /// <returns>True if the <see cref="TransBoxManager"/> handled the event; otherwise false.</returns> public bool HandleMouseDown(MouseEventArgs e, ICamera2D camera) { if (e.Button == DragButton) { var worldPos = camera.ToWorld(e.Position()); UnderCursor = FindBoxAt(worldPos); SelectedTransBox = UnderCursor; _lastWorldPos = worldPos; if (SelectedTransBox != null) _selectedTransBoxButton = e.Button; else _selectedTransBoxButton = MouseButtons.None; } return SelectedTransBox != null; }
/// <summary> /// Handles when the mouse button is raised on a map. /// </summary> /// <param name="sender">The <see cref="IToolTargetMapContainer"/> the event came from. Cannot be null.</param> /// <param name="map">The <see cref="EditorMap"/>. Cannot be null.</param> /// <param name="camera">The <see cref="ICamera2D"/>. Cannot be null.</param> /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data. Cannot be null.</param> protected override void MapContainer_MouseUp(IToolTargetMapContainer sender, EditorMap map, ICamera2D camera, MouseEventArgs e) { var cursorPos = e.Position(); var worldPos = camera.ToWorld(cursorPos); // Create entity if (e.Button == MouseButtons.Right) { Type createType = null; // Create using same type as the last entity, if possible if (Input.IsCtrlDown) createType = _lastCreatedType; // Display selection dialog if (createType == null) { using (var frm = new EntityTypeUITypeEditorForm(_lastCreatedType)) { if (frm.ShowDialog(sender as IWin32Window) == DialogResult.OK) createType = frm.SelectedItem; } } // Create the type if (createType != null) { _lastCreatedType = null; try { // Create the Entity var entity = (Entity)Activator.CreateInstance(createType); map.AddEntity(entity); entity.Size = new Vector2(64); entity.Position = worldPos - (entity.Size / 2f); GridAligner.Instance.Fit(entity); _lastCreatedType = createType; } catch (Exception ex) { const string errmsg = "Failed to create entity of type `{0}` on map `{1}`. Exception: {2}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, createType, map, ex); Debug.Fail(string.Format(errmsg, createType, map, ex)); } } } base.MapContainer_MouseUp(sender, map, camera, e); }