Fixed bugs with placement grid showing up on double-clicking object.

This commit is contained in:
Nyerguds 2023-02-26 22:05:24 +01:00
parent 1b1f1686d4
commit 2f92167e28
7 changed files with 156 additions and 80 deletions

View File

@ -906,7 +906,8 @@ namespace MobiusEditor
bool isTdMegaMap = false;
using (NewMapDialog nmd = new NewMapDialog(withImage))
{
if (nmd.ShowDialog() != DialogResult.OK)
nmd.StartPosition = FormStartPosition.CenterParent;
if (nmd.ShowDialog(this) != DialogResult.OK)
{
return;
}
@ -951,6 +952,7 @@ namespace MobiusEditor
if (!IdentifyMap(name, out FileType fileType, out GameType gameType, out bool isTdMegaMap))
{
string extension = Path.GetExtension(name).TrimStart('.');
// No point in supporting jpeg here; the mapping needs distinct colours without fades.
if ("PNG".Equals(extension, StringComparison.OrdinalIgnoreCase)
|| "BMP".Equals(extension, StringComparison.OrdinalIgnoreCase)
|| "GIF".Equals(extension, StringComparison.OrdinalIgnoreCase)

View File

@ -47,7 +47,7 @@ namespace MobiusEditor.Tools
protected override Boolean InPlacementMode
{
get { return placementMode || selectedBuildingLocation.HasValue; }
get { return placementMode || startedDragging; }
}
private readonly Building mockBuilding;
@ -56,6 +56,8 @@ namespace MobiusEditor.Tools
private Point? selectedBuildingLocation;
private Dictionary<Point, Smudge> selectedBuildingEatenSmudge;
private Point selectedBuildingPivot;
private bool startedDragging;
private ObjectPropertiesPopup selectedObjectProperties;
private BuildingType selectedBuildingType;
@ -118,6 +120,8 @@ namespace MobiusEditor.Tools
selectedBuilding = null;
selectedBuildingLocation = null;
selectedBuildingPivot = Point.Empty;
startedDragging = false;
mapPanel.Invalidate();
Building preEdit = building.Clone();
selectedObjectProperties?.Close();
selectedObjectProperties = new ObjectPropertiesPopup(objectProperties.Plugin, building);
@ -259,6 +263,7 @@ namespace MobiusEditor.Tools
selectedBuilding = null;
selectedBuildingLocation = null;
selectedBuildingPivot = Point.Empty;
startedDragging = false;
mapPanel.Invalidate();
UpdateStatus();
}
@ -336,6 +341,11 @@ namespace MobiusEditor.Tools
}
else if (selectedBuilding != null)
{
if (!startedDragging && selectedBuildingLocation.HasValue
&& new Point(selectedBuildingLocation.Value.X + selectedBuildingPivot.X, selectedBuildingLocation.Value.Y + selectedBuildingPivot.Y) != e.NewCell)
{
startedDragging = true;
}
Building toMove = selectedBuilding;
var oldLocation = map.Technos[toMove].Value;
var newLocation = new Point(Math.Max(0, e.NewCell.X - selectedBuildingPivot.X), Math.Max(0, e.NewCell.Y - selectedBuildingPivot.Y));
@ -406,10 +416,18 @@ namespace MobiusEditor.Tools
private void AddBuilding(Point location)
{
if (!map.Metrics.Contains(location))
{
return;
}
if (SelectedBuildingType == null)
{
return;
}
selectedBuilding = null;
selectedBuildingLocation = null;
selectedBuildingPivot = Point.Empty;
startedDragging = false;
var building = mockBuilding.Clone();
if (!map.Technos.CanAdd(location, building, building.Type.BaseOccupyMask))
{
@ -430,9 +448,6 @@ namespace MobiusEditor.Tools
}
}
}
selectedBuilding = null;
selectedBuildingLocation = null;
selectedBuildingPivot = Point.Empty;
if (map.Buildings.Add(location, building))
{
Building[] baseBuildings = null;
@ -643,6 +658,7 @@ namespace MobiusEditor.Tools
selectedBuilding = null;
selectedBuildingLocation = null;
selectedBuildingPivot = Point.Empty;
startedDragging = false;
if (map.Metrics.GetCell(location, out int cell))
{
Building selected = map.Buildings[cell] as Building;

View File

@ -47,7 +47,7 @@ namespace MobiusEditor.Tools
protected override Boolean InPlacementMode
{
get { return placementMode || selectedInfantryLocation.HasValue; }
get { return placementMode || startedDragging; }
}
private readonly Infantry mockInfantry;
@ -55,6 +55,7 @@ namespace MobiusEditor.Tools
private Infantry selectedInfantry;
private Point? selectedInfantryLocation;
private int selectedInfantryStop = -1;
private bool startedDragging;
private ObjectPropertiesPopup selectedObjectProperties;
private InfantryType selectedInfantryType;
@ -163,6 +164,9 @@ namespace MobiusEditor.Tools
{
selectedInfantry = null;
selectedInfantryLocation = null;
selectedInfantryStop = -1;
startedDragging = false;
mapPanel.Invalidate();
Infantry preEdit = infantry.Clone();
selectedObjectProperties?.Close();
selectedObjectProperties = new ObjectPropertiesPopup(objectProperties.Plugin, infantry);
@ -274,14 +278,19 @@ namespace MobiusEditor.Tools
}
else if (selectedInfantry != null)
{
Point curCell = navigationWidget.MouseCell;
Point oldLocation = map.Technos[selectedInfantry.InfantryGroup].Value;
if (!startedDragging && selectedInfantryLocation.HasValue && selectedInfantryLocation.Value != curCell)
{
startedDragging = true;
}
int oldStop = selectedInfantry.InfantryGroup.GetLocation(selectedInfantry);
InfantryGroup infantryGroup = null;
var techno = map.Technos[navigationWidget.MouseCell];
var techno = map.Technos[curCell];
if (techno == null)
{
infantryGroup = new InfantryGroup();
map.Technos.Add(navigationWidget.MouseCell, infantryGroup);
map.Technos.Add(curCell, infantryGroup);
}
else if (techno is InfantryGroup)
{
@ -309,6 +318,10 @@ namespace MobiusEditor.Tools
}
if (infantryGroup == selectedInfantry.InfantryGroup)
{
if (!startedDragging && selectedInfantryStop != i)
{
startedDragging = true;
}
break;
}
}
@ -351,6 +364,7 @@ namespace MobiusEditor.Tools
selectedInfantry = null;
selectedInfantryLocation = null;
selectedInfantryStop = -1;
startedDragging = false;
mapPanel.Invalidate();
UpdateStatus();
}
@ -450,14 +464,18 @@ namespace MobiusEditor.Tools
private void AddInfantry(Point location)
{
if (SelectedInfantryType == null)
{
return;
}
if (!map.Metrics.GetCell(location, out int cell))
{
return;
}
if (SelectedInfantryType == null)
{
return;
}
selectedInfantry = null;
selectedInfantryLocation = null;
selectedInfantryStop = -1;
startedDragging = false;
InfantryGroup infantryGroup = null;
var techno = map.Technos[cell];
if (techno == null)

View File

@ -47,7 +47,7 @@ namespace MobiusEditor.Tools
protected override Boolean InPlacementMode
{
get { return placementMode || selectedTerrainLocation.HasValue; }
get { return placementMode || startedDragging; }
}
private readonly Terrain mockTerrain;
@ -55,9 +55,11 @@ namespace MobiusEditor.Tools
private Terrain selectedTerrain;
private Point? selectedTerrainLocation;
private Point selectedTerrainPivot;
private bool startedDragging;
private TerrainPropertiesPopup selectedTerrainProperties;
private TerrainType selectedTerrainType;
private TerrainPropertiesPopup selectedTerrainProperties;
private TerrainType SelectedTerrainType
{
get => selectedTerrainType;
@ -118,6 +120,8 @@ namespace MobiusEditor.Tools
selectedTerrain = null;
selectedTerrainLocation = null;
selectedTerrainPivot = Point.Empty;
startedDragging = false;
mapPanel.Invalidate();
selectedTerrainProperties?.Close();
// only TD supports triggers ("Attacked" type) on terrain types.
if (plugin.GameType == GameType.TiberianDawn)
@ -235,6 +239,7 @@ namespace MobiusEditor.Tools
selectedTerrain = null;
selectedTerrainLocation = null;
selectedTerrainPivot = Point.Empty;
startedDragging = false;
mapPanel.Invalidate();
UpdateStatus();
}
@ -298,6 +303,11 @@ namespace MobiusEditor.Tools
}
else if (selectedTerrain != null)
{
if (!startedDragging && selectedTerrainLocation.HasValue
&& new Point(selectedTerrainLocation.Value.X + selectedTerrainPivot.X, selectedTerrainLocation.Value.Y + selectedTerrainPivot.Y) != e.NewCell)
{
startedDragging = true;
}
Terrain toMove = selectedTerrain;
var oldLocation = map.Technos[toMove].Value;
var newLocation = new Point(Math.Max(0, e.NewCell.X - selectedTerrainPivot.X), Math.Max(0, e.NewCell.Y - selectedTerrainPivot.Y));
@ -324,34 +334,39 @@ namespace MobiusEditor.Tools
{
return;
}
if (SelectedTerrainType != null)
if (SelectedTerrainType == null)
{
var terrain = mockTerrain.Clone();
if (map.Technos.Add(location, terrain))
return;
}
selectedTerrain = null;
selectedTerrainLocation = null;
selectedTerrainPivot = Point.Empty;
startedDragging = false;
var terrain = mockTerrain.Clone();
if (map.Technos.Add(location, terrain))
{
bool origDirtyState = plugin.Dirty;
plugin.Dirty = true;
mapPanel.Invalidate(map, terrain);
void undoAction(UndoRedoEventArgs e)
{
bool origDirtyState = plugin.Dirty;
plugin.Dirty = true;
mapPanel.Invalidate(map, terrain);
void undoAction(UndoRedoEventArgs e)
e.MapPanel.Invalidate(e.Map, location);
e.Map.Technos.Remove(terrain);
if (e.Plugin != null)
{
e.MapPanel.Invalidate(e.Map, location);
e.Map.Technos.Remove(terrain);
if (e.Plugin != null)
{
e.Plugin.Dirty = origDirtyState;
}
e.Plugin.Dirty = origDirtyState;
}
void redoAction(UndoRedoEventArgs e)
{
e.Map.Technos.Add(location, terrain);
e.MapPanel.Invalidate(e.Map, location);
if (e.Plugin != null)
{
e.Plugin.Dirty = true;
}
}
url.Track(undoAction, redoAction);
}
void redoAction(UndoRedoEventArgs e)
{
e.Map.Technos.Add(location, terrain);
e.MapPanel.Invalidate(e.Map, location);
if (e.Plugin != null)
{
e.Plugin.Dirty = true;
}
}
url.Track(undoAction, redoAction);
}
}
@ -433,6 +448,7 @@ namespace MobiusEditor.Tools
selectedTerrain = null;
selectedTerrainLocation = null;
selectedTerrainPivot = Point.Empty;
startedDragging = false;
if (map.Metrics.GetCell(location, out int cell) && map.Technos[cell] is Terrain selected && selected != null)
{
Point? selectedLocation = map.Technos[selected];

View File

@ -47,13 +47,15 @@ namespace MobiusEditor.Tools
protected override Boolean InPlacementMode
{
get { return placementMode || selectedUnitLocation.HasValue; }
get { return placementMode || startedDragging; }
}
private readonly Unit mockUnit;
private Unit selectedUnit;
private Point? selectedUnitLocation;
private bool startedDragging;
private ObjectPropertiesPopup selectedObjectProperties;
private UnitType selectedUnitType;
@ -156,6 +158,8 @@ namespace MobiusEditor.Tools
{
selectedUnit = null;
selectedUnitLocation = null;
startedDragging = false;
mapPanel.Invalidate();
Unit preEdit = unit.Clone();
selectedObjectProperties?.Close();
selectedObjectProperties = new ObjectPropertiesPopup(objectProperties.Plugin, unit);
@ -292,6 +296,7 @@ namespace MobiusEditor.Tools
AddMoveUndoTracking(selectedUnit, selectedUnitLocation.Value);
selectedUnit = null;
selectedUnitLocation = null;
startedDragging = false;
mapPanel.Invalidate();
UpdateStatus();
}
@ -344,16 +349,21 @@ namespace MobiusEditor.Tools
}
else if (selectedUnit != null)
{
var oldLocation = map.Technos[selectedUnit].Value;
mapPanel.Invalidate(map, selectedUnit);
map.Technos.Remove(selectedUnit);
if (map.Technos.Add(e.NewCell, selectedUnit))
if (!startedDragging && selectedUnitLocation.HasValue && selectedUnitLocation.Value != e.NewCell)
{
mapPanel.Invalidate(map, selectedUnit);
startedDragging = true;
}
Unit toMove = selectedUnit;
var oldLocation = map.Technos[toMove].Value;
mapPanel.Invalidate(map, toMove);
map.Technos.Remove(toMove);
if (map.Technos.Add(e.NewCell, toMove))
{
mapPanel.Invalidate(map, toMove);
}
else
{
map.Technos.Add(oldLocation, selectedUnit);
map.Technos.Add(oldLocation, toMove);
}
}
else if (e.MouseButtons == MouseButtons.Right)
@ -364,34 +374,42 @@ namespace MobiusEditor.Tools
private void AddUnit(Point location)
{
if (SelectedUnitType != null)
if (!map.Metrics.Contains(location))
{
var unit = mockUnit.Clone();
if (map.Technos.Add(location, unit))
return;
}
if (SelectedUnitType == null)
{
return;
}
selectedUnit = null;
selectedUnitLocation = null;
startedDragging = false;
var unit = mockUnit.Clone();
if (map.Technos.Add(location, unit))
{
bool origDirtyState = plugin.Dirty;
plugin.Dirty = true;
mapPanel.Invalidate(map, unit);
void undoAction(UndoRedoEventArgs e)
{
bool origDirtyState = plugin.Dirty;
plugin.Dirty = true;
mapPanel.Invalidate(map, unit);
void undoAction(UndoRedoEventArgs e)
e.MapPanel.Invalidate(e.Map, unit);
e.Map.Technos.Remove(unit);
if (e.Plugin != null)
{
e.MapPanel.Invalidate(e.Map, unit);
e.Map.Technos.Remove(unit);
if (e.Plugin != null)
{
e.Plugin.Dirty = origDirtyState;
}
e.Plugin.Dirty = origDirtyState;
}
void redoAction(UndoRedoEventArgs e)
{
e.Map.Technos.Add(location, unit);
e.MapPanel.Invalidate(e.Map, unit);
if (e.Plugin != null)
{
e.Plugin.Dirty = true;
}
}
url.Track(undoAction, redoAction);
}
void redoAction(UndoRedoEventArgs e)
{
e.Map.Technos.Add(location, unit);
e.MapPanel.Invalidate(e.Map, unit);
if (e.Plugin != null)
{
e.Plugin.Dirty = true;
}
}
url.Track(undoAction, redoAction);
}
}
@ -475,6 +493,7 @@ namespace MobiusEditor.Tools
{
selectedUnit = null;
selectedUnitLocation = null;
startedDragging = false;
if (map.Metrics.GetCell(location, out int cell))
{
Unit selected = map.Technos[cell] as Unit;

View File

@ -52,22 +52,22 @@ namespace MobiusEditor.Utility
image.UnlockBits(sourceData);
// Binarization: get brightness
Single[,] brightness = new Single[height, width];
Int32 offset = 0;
Int32 lineOffset = 0;
for (Int32 y = 0; y < height; ++y)
{
// use stride to get the start offset of each line
Int32 usedOffset = offset;
Int32 offset = lineOffset;
for (Int32 x = 0; x < width; ++x)
{
// get colour
Byte blu = data[usedOffset + 0];
Byte grn = data[usedOffset + 1];
Byte red = data[usedOffset + 2];
Byte blu = data[offset + 0];
Byte grn = data[offset + 1];
Byte red = data[offset + 2];
Color c = Color.FromArgb(red, grn, blu);
brightness[y, x] = c.GetBrightness();
usedOffset += 4;
offset += 4;
}
offset += stride;
lineOffset += stride;
}
Func<Single[,], Int32, Int32, Boolean> clearsThreshold;
if (detectDark)
@ -111,7 +111,7 @@ namespace MobiusEditor.Utility
/// <summary>
/// Detects a list of all blobs in the image, and merges any with bounds that intersect with each other according to the 'mergeThreshold' parameter.
/// Returns the result as Boolean[,] array.
/// Returns the results as Boolean[,] arrays.
/// </summary>
/// <typeparam name="T">Type of the list to detect equal neighbours in.</typeparam>
/// <param name="data">Image data array. It is processed as one pixel per coordinate.</param>
@ -164,7 +164,7 @@ namespace MobiusEditor.Utility
public static List<List<Point>> FindBlobs<T>(T data, Int32 width, Int32 height, Func<T, Int32, Int32, Boolean> clearsThreshold, Boolean allEightEdges, Boolean getEdgesOnly, out List<Boolean[,]> inBlobs, out Boolean[,] inAnyBlob)
{
List<List<Point>> blobs = new List<List<Point>>();
inAnyBlob = new Boolean[height,width];
inAnyBlob = new Boolean[height, width];
inBlobs = new List<Boolean[,]>();
for (Int32 y = 0; y < height; ++y)
{
@ -392,7 +392,7 @@ namespace MobiusEditor.Utility
}
/// <summary>
/// Gets the list of neighbouring points around one point in an image.
/// Gets the list of neighbouring points around one point in an image that are inside the full image bounds.
/// </summary>
/// <param name="x">X-coordinate of the point to get neighbours of.</param>
/// <param name="y">Y-coordinate of the point to get neighbours of.</param>

View File

@ -294,13 +294,17 @@ namespace MobiusEditor.Utility
public static Color[] FindMostCommonColors(int amount, byte[] imageData, int width, int height, int stride)
{
// Store colour frequencies in a dictionary.
if (amount < 0)
amount = Int32.MaxValue;
Dictionary<Color, Int32> colorFreq = new Dictionary<Color, Int32>();
Int32 lineStart = 0;
for (Int32 y = 0; y < height; ++y)
{
// Reset offset on every line, since stride is not guaranteed to always be width * pixel size.
Int32 inputOffs = y * stride;
//Final offset = y * line length in bytes + x * pixel length in bytes.
//To avoid recalculating that offset each time we just increase it with the pixel size at the end of each x iteration.
Int32 inputOffs = lineStart;
//Final offset = y * linelength + x * pixellength.
// To avoid recalculating that offset each time we just increase it with the pixel size at the end of each x iteration,
// and increase the line start with the stride at the end of each y iteration.
for (Int32 x = 0; x < width; ++x)
{
//Get colour components out. "ARGB" is actually the order in the final integer which is read as little-endian, so the real order is BGRA.
@ -318,6 +322,7 @@ namespace MobiusEditor.Utility
// Increase the offset by the pixel width. For 32bpp ARGB, each pixel is 4 bytes.
inputOffs += 4;
}
lineStart += stride;
}
// Get the maximum value in the dictionary values
return colorFreq.OrderByDescending(kvp => kvp.Value).Select(kvp => kvp.Key).Take(amount).ToArray();