Skip to content

Commit a783b77

Browse files
committed
fix: selected when we show popup
1 parent 93010a6 commit a783b77

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

Assets/Code/LevelEditor/BlockPopupWindow.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static void ShowPopup(
3636
Action onClear,
3737
List<Vector2Int> currentSelection)
3838
{
39+
3940
if (Current != null)
4041
Current.Close();
4142

Assets/Code/LevelEditor/LevelHegsagonEditor.cs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public class LevelHegsagonEditor : BaseLevelDataEditor
3030

3131
#if UNITY_EDITOR
3232

33-
private static BlockLibrary cachedLibrary;
34-
private Vector2Int? selectionStart;
35-
private List<Vector2Int> currentSelection = new();
33+
private static BlockLibrary _cachedLibrary;
34+
private Vector2Int? _selectionStart;
35+
private List<Vector2Int> _currentSelection = new();
3636

3737
private LevelCell DrawLevelCell(Rect rect, LevelCell cell, int x, int y)
3838
{
@@ -73,11 +73,11 @@ private void DrawHexBackground(Rect rect)
7373

7474
private void LoadLibrary()
7575
{
76-
if (cachedLibrary != null) return;
76+
if (_cachedLibrary != null) return;
7777
string[] guids = AssetDatabase.FindAssets("t:BlockLibrary");
7878
if (guids.Length == 0) return;
7979
string path = AssetDatabase.GUIDToAssetPath(guids[0]);
80-
cachedLibrary = AssetDatabase.LoadAssetAtPath<BlockLibrary>(path);
80+
_cachedLibrary = AssetDatabase.LoadAssetAtPath<BlockLibrary>(path);
8181
}
8282

8383
private void DrawCellContent(Rect rect, LevelCell cell)
@@ -144,7 +144,7 @@ private void DrawRotationArrow(Rect rect, Quaternion rotation)
144144

145145
private void DrawSelectionOverlay(Rect rect, int x, int y)
146146
{
147-
if (currentSelection.Contains(new Vector2Int(x, y)))
147+
if (_currentSelection.Contains(new Vector2Int(x, y)))
148148
{
149149
EditorGUI.DrawRect(rect, new Color(0f, 1f, 0f, 0.25f));
150150
}
@@ -158,33 +158,33 @@ private void HandleCellInteraction(Rect rect, int x, int y)
158158
{
159159
if (Event.current.control)
160160
{
161-
if (!selectionStart.HasValue)
161+
if (!_selectionStart.HasValue)
162162
{
163-
selectionStart = new Vector2Int(x, y);
164-
currentSelection.Clear();
165-
currentSelection.Add(selectionStart.Value);
163+
_selectionStart = new Vector2Int(x, y);
164+
_currentSelection.Clear();
165+
_currentSelection.Add(_selectionStart.Value);
166166
}
167167
else
168168
{
169169
Vector2Int end = new Vector2Int(x, y);
170-
currentSelection.Clear();
171-
int minX = Mathf.Min(selectionStart.Value.x, end.x);
172-
int maxX = Mathf.Max(selectionStart.Value.x, end.x);
173-
int minY = Mathf.Min(selectionStart.Value.y, end.y);
174-
int maxY = Mathf.Max(selectionStart.Value.y, end.y);
170+
_currentSelection.Clear();
171+
int minX = Mathf.Min(_selectionStart.Value.x, end.x);
172+
int maxX = Mathf.Max(_selectionStart.Value.x, end.x);
173+
int minY = Mathf.Min(_selectionStart.Value.y, end.y);
174+
int maxY = Mathf.Max(_selectionStart.Value.y, end.y);
175175

176176
for (int i = minX; i <= maxX; i++)
177177
for (int j = minY; j <= maxY; j++)
178-
currentSelection.Add(new Vector2Int(i, j));
178+
_currentSelection.Add(new Vector2Int(i, j));
179179

180-
selectionStart = null;
180+
_selectionStart = null;
181181
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
182182
}
183183
}
184184
else
185185
{
186-
selectionStart = null;
187-
currentSelection.Clear();
186+
_selectionStart = null;
187+
_currentSelection.Clear();
188188
}
189189

190190
Event.current.Use();
@@ -200,28 +200,42 @@ private void HandleCellInteraction(Rect rect, int x, int y)
200200

201201
private void ShowContextMenu(LevelCell cell, int x, int y)
202202
{
203-
if (cachedLibrary == null) return;
203+
if (_cachedLibrary == null || cell == null)
204+
return;
204205

205206
Vector2 screenPos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
206-
Rect rect = new Rect(screenPos, Vector2.zero);
207-
207+
Rect rect = new Rect(screenPos, new Vector2(350, 500));
208+
209+
Vector2Int clickedPos = new Vector2Int(x, y);
210+
if (!_currentSelection.Contains(clickedPos))
211+
{
212+
Debug.Log("Already selected");
213+
Debug.Log(_currentSelection.Contains(clickedPos));
214+
_currentSelection.Add(clickedPos);
215+
}
216+
else
217+
{
218+
Debug.Log("Dont Already selected");
219+
}
220+
208221
BlockPopupWindow.LevelEditorHelpers.TryGetCell = pos => GetCell(pos);
209222

210223
BlockPopupWindow.ShowPopup(
211224
rect,
212-
cachedLibrary,
225+
_cachedLibrary,
213226
block => ApplyBlock(block, cell),
214227
angle => ApplyRotation(angle, cell),
215228
() => ClearBlock(cell),
216-
currentSelection
229+
_currentSelection
217230
);
218231
}
219232

233+
220234
private void ApplyBlock(BlockDataEditor block, LevelCell fallbackCell)
221235
{
222-
if (currentSelection.Count > 0)
236+
if (_currentSelection.Count > 0)
223237
{
224-
foreach (var pos in currentSelection)
238+
foreach (var pos in _currentSelection)
225239
GetCell(pos).Block = block;
226240
}
227241
else fallbackCell.Block = block;
@@ -231,9 +245,9 @@ private void ApplyBlock(BlockDataEditor block, LevelCell fallbackCell)
231245
private void ApplyRotation(float angle, LevelCell fallbackCell)
232246
{
233247
Quaternion rotation = Quaternion.Euler(0, 0, angle);
234-
if (currentSelection.Count > 0)
248+
if (_currentSelection.Count > 0)
235249
{
236-
foreach (var pos in currentSelection)
250+
foreach (var pos in _currentSelection)
237251
GetCell(pos).Rotation = rotation;
238252
}
239253
else fallbackCell.Rotation = rotation;
@@ -242,11 +256,11 @@ private void ApplyRotation(float angle, LevelCell fallbackCell)
242256

243257
private void ClearBlock(LevelCell fallbackCell)
244258
{
245-
if (currentSelection.Count > 0)
259+
if (_currentSelection.Count > 0)
246260
{
247-
foreach (var pos in currentSelection)
261+
foreach (var pos in _currentSelection)
248262
GetCell(pos).Block = null;
249-
currentSelection.Clear();
263+
_currentSelection.Clear();
250264
}
251265
else fallbackCell.Block = null;
252266
MarkDirty();

Assets/Code/LevelEditor/LevelMatrixEditor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ private void ShowContextMenu(LevelCell cell, int x, int y)
165165
Vector2 windowSize = new Vector2(350, 550);
166166
Rect rect = new Rect(screenPos, windowSize);
167167

168+
Vector2Int clickedPos = new Vector2Int(x, y);
169+
if (currentSelection == null || !currentSelection.Contains(clickedPos))
170+
{
171+
currentSelection = new List<Vector2Int> { clickedPos };
172+
}
173+
168174
BlockPopupWindow.LevelEditorHelpers.TryGetCell = pos => GetCell(pos);
169175

170176
BlockPopupWindow.ShowPopup(

0 commit comments

Comments
 (0)