diff --git a/DungeonShooting_Godot/prefab/ui/TileSetEditorSegment.tscn b/DungeonShooting_Godot/prefab/ui/TileSetEditorSegment.tscn
index 9fcc463..b438f78 100644
--- a/DungeonShooting_Godot/prefab/ui/TileSetEditorSegment.tscn
+++ b/DungeonShooting_Godot/prefab/ui/TileSetEditorSegment.tscn
@@ -1,9 +1,10 @@
-[gd_scene load_steps=6 format=3 uid="uid://daias2tkvj20c"]
+[gd_scene load_steps=7 format=3 uid="uid://daias2tkvj20c"]
[ext_resource type="Script" path="res://src/game/ui/tileSetEditorSegment/TileSetEditorSegmentPanel.cs" id="1_to1lc"]
+[ext_resource type="Script" path="res://src/game/ui/tileSetEditorSegment/TileEditArea.cs" id="2_h43yx"]
[ext_resource type="Shader" path="res://resource/material/Grid.gdshader" id="2_t1p2n"]
[ext_resource type="Texture2D" uid="uid://uhhfgdhpk7i4" path="res://icon.png" id="2_wr143"]
-[ext_resource type="Script" path="res://src/game/ui/tileSetEditorSegment/TileSetEditorSegmentLeftBg.cs" id="3_dnmr6"]
+[ext_resource type="Script" path="res://src/game/ui/tileSetEditorSegment/MaskBrush.cs" id="4_ytys0"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_u3xqn"]
shader = ExtResource("2_t1p2n")
@@ -52,7 +53,7 @@
z_index = 1
layout_mode = 2
color = Color(0.203922, 0.203922, 0.203922, 1)
-script = ExtResource("3_dnmr6")
+script = ExtResource("2_h43yx")
[node name="TileTexture" type="TextureRect" parent="HSplitContainer/Left/MarginContainer/LeftBg"]
layout_mode = 1
@@ -70,7 +71,7 @@
scale = Vector2(2, 2)
texture = ExtResource("2_wr143")
-[node name="Brush" type="Control" parent="HSplitContainer/Left/MarginContainer/LeftBg/TileTexture"]
+[node name="MaskRoot" type="Control" parent="HSplitContainer/Left/MarginContainer/LeftBg/TileTexture"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
@@ -79,6 +80,23 @@
grow_vertical = 2
mouse_filter = 2
+[node name="MaskRect" type="ColorRect" parent="HSplitContainer/Left/MarginContainer/LeftBg/TileTexture/MaskRoot"]
+layout_mode = 1
+offset_right = 16.0
+offset_bottom = 16.0
+mouse_filter = 2
+color = Color(0, 0, 0, 0.588235)
+
+[node name="MaskBrush" type="Control" parent="HSplitContainer/Left/MarginContainer/LeftBg/TileTexture"]
+layout_mode = 1
+anchors_preset = 15
+anchor_right = 1.0
+anchor_bottom = 1.0
+grow_horizontal = 2
+grow_vertical = 2
+mouse_filter = 2
+script = ExtResource("4_ytys0")
+
[node name="Grid" type="ColorRect" parent="HSplitContainer/Left/MarginContainer/LeftBg"]
material = SubResource("ShaderMaterial_u3xqn")
layout_mode = 1
diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/MaskBrush.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/MaskBrush.cs
new file mode 100644
index 0000000..ac5750a
--- /dev/null
+++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/MaskBrush.cs
@@ -0,0 +1,44 @@
+using Godot;
+
+namespace UI.TileSetEditorSegment;
+
+public partial class MaskBrush : Control
+{
+ ///
+ /// 绑定的地图纹理节点
+ ///
+ public TextureRect TileTexture { get; set; }
+
+ ///
+ /// 绑定的TileSet编辑区域节点
+ ///
+ public TileEditArea TileEditArea { get; set; }
+
+ public override void _Process(double delta)
+ {
+ QueueRedraw();
+ }
+
+ public override void _Draw()
+ {
+ //绘制texture区域
+ if (TileTexture.Texture != null)
+ {
+ DrawRect(
+ new Rect2(Vector2.Zero, TileTexture.Size),
+ new Color(1, 1, 0, 0.5f), false,
+ 2f / TileTexture.Scale.X
+ );
+ }
+
+ //绘制鼠标悬停区域
+ if (TileEditArea.IsMouseInTexture())
+ {
+ var pos = TileEditArea.GetMouseCellPosition() * GameConfig.TileCellSize;
+ DrawRect(
+ new Rect2(pos,GameConfig.TileCellSizeVector2I),
+ Colors.Green, false, 3f / TileTexture.Scale.X
+ );
+ }
+ }
+}
\ No newline at end of file
diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/MaskRectCell.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/MaskRectCell.cs
new file mode 100644
index 0000000..f842a80
--- /dev/null
+++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/MaskRectCell.cs
@@ -0,0 +1,11 @@
+using Godot;
+
+namespace UI.TileSetEditorSegment;
+
+public class MaskRectCell : UiCell
+{
+ public override void OnSetData(bool data)
+ {
+ CellNode.Instance.Color = data ? new Color(0, 0, 0, 0) : new Color(0, 0, 0, 0.5882353F);
+ }
+}
\ No newline at end of file
diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileEditArea.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileEditArea.cs
new file mode 100644
index 0000000..1a2c12c
--- /dev/null
+++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileEditArea.cs
@@ -0,0 +1,202 @@
+using Godot;
+
+namespace UI.TileSetEditorSegment;
+
+public partial class TileEditArea : ColorRect, IUiNodeScript
+{
+ private TileSetEditorSegment.LeftBg _leftBg;
+ private DragBinder _dragBinder;
+ private UiGrid _maskGrid;
+
+ ///
+ /// 网格横轴数量
+ ///
+ public int CellHorizontal { get; private set; }
+ ///
+ /// 网格纵轴数量
+ ///
+ public int CellVertical { get; private set; }
+
+ public void SetUiNode(IUiNode uiNode)
+ {
+ _leftBg = (TileSetEditorSegment.LeftBg)uiNode;
+ var maskBrush = _leftBg.L_TileTexture.L_MaskBrush.Instance;
+ maskBrush.TileTexture = _leftBg.L_TileTexture.Instance;
+ maskBrush.TileEditArea = this;
+
+ _dragBinder = DragUiManager.BindDrag(this, "mouse_middle", OnDrag);
+ Resized += OnLeftBgResize;
+
+ _maskGrid = new UiGrid(_leftBg.L_TileTexture.L_MaskRoot.L_MaskRect, typeof(MaskRectCell));
+ _maskGrid.SetCellOffset(Vector2I.Zero);
+ }
+
+ public void OnDestroy()
+ {
+ _dragBinder.UnBind();
+ }
+
+ private void OnDrag(DragState state, Vector2 pos)
+ {
+ if (state == DragState.DragMove)
+ {
+ _leftBg.L_TileTexture.Instance.Position += pos;
+ OnLeftBgResize();
+ }
+ }
+
+ public override void _Input(InputEvent @event)
+ {
+ if (@event is InputEventMouseButton mouseButton)
+ {
+ if (_leftBg.UiPanel.IsOpen)
+ {
+ if (mouseButton.ButtonIndex == MouseButton.WheelDown)
+ {
+ if (GetGlobalRect().HasPoint(mouseButton.GlobalPosition))
+ {
+ //缩小
+ Shrink();
+ }
+ }
+ else if (mouseButton.ButtonIndex == MouseButton.WheelUp)
+ {
+ if (GetGlobalRect().HasPoint(mouseButton.GlobalPosition))
+ {
+ //放大
+ Magnify();
+ }
+ }
+ }
+ }
+ }
+
+ public override void _Process(double delta)
+ {
+ if (Input.IsMouseButtonPressed(MouseButton.Left))
+ {
+ if (IsMouseInTexture())
+ {
+ var cellPos = GetMouseCellPosition();
+ var index = CellPositionToIndex(cellPos);
+ Debug.Log($"cellPos: {cellPos}, index: {index}");
+ _maskGrid.UpdateByIndex(index, true);
+ }
+ }
+ }
+
+ //缩小
+ private void Shrink()
+ {
+ var textureRect = _leftBg.L_TileTexture.Instance;
+ var offset = textureRect.GetLocalMousePosition();
+ var prevScale = textureRect.Scale;
+ var newScale = prevScale / 1.1f;
+ if (newScale.LengthSquared() >= 0.5f)
+ {
+ textureRect.Scale = newScale;
+ var position = textureRect.Position + offset * 0.1f * newScale;
+ textureRect.Position = position;
+ SetGridTransform(position, newScale.X);
+ }
+ }
+ //放大
+ private void Magnify()
+ {
+ var textureRect = _leftBg.L_TileTexture.Instance;
+ var offset = textureRect.GetLocalMousePosition();
+ var prevScale = textureRect.Scale;
+ var newScale = prevScale * 1.1f;
+ if (newScale.LengthSquared() <= 2000)
+ {
+ textureRect.Scale = newScale;
+ var position = textureRect.Position - offset * 0.1f * prevScale;
+ textureRect.Position = position;
+ SetGridTransform(position, newScale.X);
+ }
+ }
+
+ ///
+ /// 当前Ui被显示出来时调用
+ ///
+ public void OnShow()
+ {
+ //背景颜色
+ Color = _leftBg.UiPanel.EditorPanel.BgColor;
+ OnLeftBgResize();
+ }
+
+ //背景宽度变化
+ private void OnLeftBgResize()
+ {
+ var sprite = _leftBg.L_TileTexture.Instance;
+ if (sprite.Texture != _leftBg.UiPanel.EditorPanel.Texture)
+ {
+ sprite.Texture = _leftBg.UiPanel.EditorPanel.Texture;
+ OnChangeTileSetTexture(_leftBg.UiPanel.EditorPanel.Texture);
+ }
+
+ var colorRect = _leftBg.L_Grid.Instance;
+ colorRect.Material.SetShaderMaterialParameter(ShaderParamNames.Size, Size);
+ SetGridTransform(sprite.Position, sprite.Scale.X);
+ }
+
+ //改变TileSet纹理
+ private void OnChangeTileSetTexture(Texture2D texture)
+ {
+ var width = texture.GetWidth() / GameConfig.TileCellSize;
+ var height = texture.GetHeight() / GameConfig.TileCellSize;
+ _maskGrid.RemoveAll();
+ _maskGrid.SetColumns(width);
+ for (int i = 0; i < width; i++)
+ {
+ for (int j = 0; j < height; j++)
+ {
+ _maskGrid.Add(false);
+ }
+ }
+ }
+
+ //设置网格位置和缩放
+ private void SetGridTransform(Vector2 pos, float scale)
+ {
+ var colorRect = _leftBg.L_Grid.Instance;
+ colorRect.Material.SetShaderMaterialParameter(ShaderParamNames.GridSize, GameConfig.TileCellSize * scale);
+ colorRect.Material.SetShaderMaterialParameter(ShaderParamNames.Offset, -pos);
+ }
+
+ ///
+ /// 返回鼠标是否在texture区域内
+ ///
+ public bool IsMouseInTexture()
+ {
+ var textureRect = _leftBg.L_TileTexture.Instance;
+ var pos = textureRect.GetLocalMousePosition();
+ if (pos.X < 0 || pos.Y < 0)
+ {
+ return false;
+ }
+
+ var size = textureRect.Size;
+ return pos.X <= size.X && pos.Y <= size.Y;
+ }
+
+ ///
+ /// 返回鼠标所在的单元格位置, 相对于纹理左上角
+ ///
+ public Vector2I GetMouseCellPosition()
+ {
+ var textureRect = _leftBg.L_TileTexture.Instance;
+ var pos = textureRect.GetLocalMousePosition() / GameConfig.TileCellSize;
+ return pos.AsVector2I();
+ }
+
+ ///
+ /// 将二维位置转换为索引的函数
+ ///
+ public int CellPositionToIndex(Vector2I pos)
+ {
+ var gridWidth = _maskGrid.GetColumns();
+ return pos.Y * gridWidth + pos.X;
+ }
+}
\ No newline at end of file
diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegment.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegment.cs
index 245e694..7b4aa50 100644
--- a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegment.cs
+++ b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegment.cs
@@ -30,12 +30,43 @@
}
///
- /// 类型: , 路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.Brush
+ /// 类型: , 路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskRoot.MaskRect
///
- public class Brush : UiNode
+ public class MaskRect : UiNode
{
- public Brush(TileSetEditorSegmentPanel uiPanel, Godot.Control node) : base(uiPanel, node) { }
- public override Brush Clone() => new (UiPanel, (Godot.Control)Instance.Duplicate());
+ public MaskRect(TileSetEditorSegmentPanel uiPanel, Godot.ColorRect node) : base(uiPanel, node) { }
+ public override MaskRect Clone() => new (UiPanel, (Godot.ColorRect)Instance.Duplicate());
+ }
+
+ ///
+ /// 类型: , 路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskRoot
+ ///
+ public class MaskRoot : UiNode
+ {
+ ///
+ /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskRect
+ ///
+ public MaskRect L_MaskRect
+ {
+ get
+ {
+ if (_L_MaskRect == null) _L_MaskRect = new MaskRect(UiPanel, Instance.GetNode("MaskRect"));
+ return _L_MaskRect;
+ }
+ }
+ private MaskRect _L_MaskRect;
+
+ public MaskRoot(TileSetEditorSegmentPanel uiPanel, Godot.Control node) : base(uiPanel, node) { }
+ public override MaskRoot Clone() => new (UiPanel, (Godot.Control)Instance.Duplicate());
+ }
+
+ ///
+ /// 类型: , 路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskBrush
+ ///
+ public class MaskBrush : UiNode
+ {
+ public MaskBrush(TileSetEditorSegmentPanel uiPanel, UI.TileSetEditorSegment.MaskBrush node) : base(uiPanel, node) { }
+ public override MaskBrush Clone() => new (UiPanel, (UI.TileSetEditorSegment.MaskBrush)Instance.Duplicate());
}
///
@@ -44,17 +75,30 @@
public class TileTexture : UiNode
{
///
- /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.Brush
+ /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.MaskRoot
///
- public Brush L_Brush
+ public MaskRoot L_MaskRoot
{
get
{
- if (_L_Brush == null) _L_Brush = new Brush(UiPanel, Instance.GetNode("Brush"));
- return _L_Brush;
+ if (_L_MaskRoot == null) _L_MaskRoot = new MaskRoot(UiPanel, Instance.GetNode("MaskRoot"));
+ return _L_MaskRoot;
}
}
- private Brush _L_Brush;
+ private MaskRoot _L_MaskRoot;
+
+ ///
+ /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.MaskBrush
+ ///
+ public MaskBrush L_MaskBrush
+ {
+ get
+ {
+ if (_L_MaskBrush == null) _L_MaskBrush = new MaskBrush(UiPanel, Instance.GetNode("MaskBrush"));
+ return _L_MaskBrush;
+ }
+ }
+ private MaskBrush _L_MaskBrush;
public TileTexture(TileSetEditorSegmentPanel uiPanel, Godot.TextureRect node) : base(uiPanel, node) { }
public override TileTexture Clone() => new (UiPanel, (Godot.TextureRect)Instance.Duplicate());
@@ -70,9 +114,9 @@
}
///
- /// 类型: , 路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg
+ /// 类型: , 路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg
///
- public class LeftBg : UiNode
+ public class LeftBg : UiNode
{
///
/// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.TileTexture
@@ -100,8 +144,8 @@
}
private Grid _L_Grid;
- public LeftBg(TileSetEditorSegmentPanel uiPanel, UI.TileSetEditorSegment.TileSetEditorSegmentLeftBg node) : base(uiPanel, node) { }
- public override LeftBg Clone() => new (UiPanel, (UI.TileSetEditorSegment.TileSetEditorSegmentLeftBg)Instance.Duplicate());
+ public LeftBg(TileSetEditorSegmentPanel uiPanel, UI.TileSetEditorSegment.TileEditArea node) : base(uiPanel, node) { }
+ public override LeftBg Clone() => new (UiPanel, (UI.TileSetEditorSegment.TileEditArea)Instance.Duplicate());
}
///
@@ -110,13 +154,13 @@
public class MarginContainer : UiNode
{
///
- /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.LeftBg
+ /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.LeftBg
///
public LeftBg L_LeftBg
{
get
{
- if (_L_LeftBg == null) _L_LeftBg = new LeftBg(UiPanel, Instance.GetNode("LeftBg"));
+ if (_L_LeftBg == null) _L_LeftBg = new LeftBg(UiPanel, Instance.GetNode("LeftBg"));
return _L_LeftBg;
}
}
@@ -216,9 +260,19 @@
///
- /// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.Brush
+ /// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskRoot.MaskRect
///
- public Brush S_Brush => L_HSplitContainer.L_Left.L_MarginContainer.L_LeftBg.L_TileTexture.L_Brush;
+ public MaskRect S_MaskRect => L_HSplitContainer.L_Left.L_MarginContainer.L_LeftBg.L_TileTexture.L_MaskRoot.L_MaskRect;
+
+ ///
+ /// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskRoot
+ ///
+ public MaskRoot S_MaskRoot => L_HSplitContainer.L_Left.L_MarginContainer.L_LeftBg.L_TileTexture.L_MaskRoot;
+
+ ///
+ /// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture.MaskBrush
+ ///
+ public MaskBrush S_MaskBrush => L_HSplitContainer.L_Left.L_MarginContainer.L_LeftBg.L_TileTexture.L_MaskBrush;
///
/// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg.TileTexture
@@ -231,7 +285,7 @@
public Grid S_Grid => L_HSplitContainer.L_Left.L_MarginContainer.L_LeftBg.L_Grid;
///
- /// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg
+ /// 场景中唯一名称的节点, 节点类型: , 节点路径: TileSetEditorSegment.HSplitContainer.Left.MarginContainer.LeftBg
///
public LeftBg S_LeftBg => L_HSplitContainer.L_Left.L_MarginContainer.L_LeftBg;
diff --git a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegmentLeftBg.cs b/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegmentLeftBg.cs
deleted file mode 100644
index 1c9ba1c..0000000
--- a/DungeonShooting_Godot/src/game/ui/tileSetEditorSegment/TileSetEditorSegmentLeftBg.cs
+++ /dev/null
@@ -1,160 +0,0 @@
-using Godot;
-
-namespace UI.TileSetEditorSegment;
-
-public partial class TileSetEditorSegmentLeftBg : ColorRect, IUiNodeScript
-{
- private TileSetEditorSegment.LeftBg _leftBg;
- private DragBinder _dragBinder;
-
- public void SetUiNode(IUiNode uiNode)
- {
- _leftBg = (TileSetEditorSegment.LeftBg)uiNode;
- _leftBg.L_TileTexture.L_Brush.Instance.Draw += OnBrushDraw;
- _dragBinder = DragUiManager.BindDrag(this, "mouse_middle", OnDrag);
- Resized += OnLeftBgResize;
- }
-
- public void OnDestroy()
- {
- _dragBinder.UnBind();
- }
-
- public override void _Process(double delta)
- {
- _leftBg.L_TileTexture.L_Brush.Instance.QueueRedraw();
- }
-
- private void OnDrag(DragState state, Vector2 pos)
- {
- if (state == DragState.DragMove)
- {
- _leftBg.L_TileTexture.Instance.Position += pos;
- OnLeftBgResize();
- }
- }
-
- public override void _Input(InputEvent @event)
- {
- if (@event is InputEventMouseButton mouseButton)
- {
- if (_leftBg.UiPanel.IsOpen)
- {
- if (mouseButton.ButtonIndex == MouseButton.WheelDown)
- {
- if (GetGlobalRect().HasPoint(mouseButton.GlobalPosition))
- {
- //缩小
- Shrink();
- }
- }
- else if (mouseButton.ButtonIndex == MouseButton.WheelUp)
- {
- if (GetGlobalRect().HasPoint(mouseButton.GlobalPosition))
- {
- //放大
- Magnify();
- }
- }
- }
- }
- }
-
- //缩小
- private void Shrink()
- {
- var textureRect = _leftBg.L_TileTexture.Instance;
- var offset = textureRect.GetLocalMousePosition();
- var prevScale = textureRect.Scale;
- var newScale = prevScale / 1.1f;
- if (newScale.LengthSquared() >= 0.5f)
- {
- textureRect.Scale = newScale;
- var position = textureRect.Position + offset * 0.1f * newScale;
- textureRect.Position = position;
- SetGridTransform(position, newScale.X);
- }
- }
- //放大
- private void Magnify()
- {
- var textureRect = _leftBg.L_TileTexture.Instance;
- var offset = textureRect.GetLocalMousePosition();
- var prevScale = textureRect.Scale;
- var newScale = prevScale * 1.1f;
- if (newScale.LengthSquared() <= 2000)
- {
- textureRect.Scale = newScale;
- var position = textureRect.Position - offset * 0.1f * prevScale;
- textureRect.Position = position;
- SetGridTransform(position, newScale.X);
- }
- }
-
- ///
- /// 当前Ui被显示出来时调用
- ///
- public void OnShow()
- {
- //背景颜色
- Color = _leftBg.UiPanel.EditorPanel.BgColor;
- OnLeftBgResize();
- }
-
- //背景宽度变化
- private void OnLeftBgResize()
- {
- var sprite = _leftBg.L_TileTexture.Instance;
- sprite.Texture = _leftBg.UiPanel.EditorPanel.Texture;
-
- var colorRect = _leftBg.L_Grid.Instance;
- colorRect.Material.SetShaderMaterialParameter(ShaderParamNames.Size, Size);
- SetGridTransform(sprite.Position, sprite.Scale.X);
- }
-
- //设置网格位置和缩放
- private void SetGridTransform(Vector2 pos, float scale)
- {
- var colorRect = _leftBg.L_Grid.Instance;
- colorRect.Material.SetShaderMaterialParameter(ShaderParamNames.GridSize, GameConfig.TileCellSize * scale);
- colorRect.Material.SetShaderMaterialParameter(ShaderParamNames.Offset, -pos);
- }
-
- private void OnBrushDraw()
- {
- //绘制texture区域
- var textureRect = _leftBg.L_TileTexture.Instance;
- var brush = _leftBg.L_TileTexture.L_Brush;
- if (textureRect.Texture != null)
- {
- brush.Instance.DrawRect(new Rect2(Vector2.Zero, textureRect.Size),
- new Color(1, 1, 0, 0.5f), false,
- 2f / textureRect.Scale.X);
- }
-
- //绘制鼠标悬停区域
- if (IsMouseInTexture())
- {
- var pos = textureRect.GetLocalMousePosition() / GameConfig.TileCellSize;
- pos = new Vector2((int)pos.X * GameConfig.TileCellSize, (int)pos.Y * GameConfig.TileCellSize);
- brush.Instance.DrawRect(
- new Rect2(pos,GameConfig.TileCellSizeVector2I),
- Colors.Green, false, 3f / textureRect.Scale.X
- );
- }
- }
-
- //返回鼠标是否在texture区域内
- private bool IsMouseInTexture()
- {
- var textureRect = _leftBg.L_TileTexture.Instance;
- var pos = textureRect.GetLocalMousePosition();
- if (pos.X < 0 || pos.Y < 0)
- {
- return false;
- }
-
- var size = textureRect.Size;
- return pos.X <= size.X && pos.Y <= size.Y;
- }
-}
\ No newline at end of file