1- using System ;
1+ using Microsoft . Xna . Framework ;
2+ using Rampastring . XNAUI ;
3+ using System ;
24using TSMapEditor . GameMath ;
35using TSMapEditor . Models ;
46using TSMapEditor . Mutations . Classes ;
@@ -41,8 +43,33 @@ public TerrainType TerrainType
4143 }
4244 }
4345
46+ private Point2D ? lineSourceCell ;
47+ private PlaceTerrainObjectLineMutation linePreviewMutation ;
48+ private bool blocked ;
49+
50+ public override void OnActionExit ( )
51+ {
52+ ClearLinePreview ( ) ;
53+ base . OnActionExit ( ) ;
54+ }
55+
56+ private ( Direction direction , int length ) GetLineInformation ( Point2D cellCoords )
57+ {
58+ Direction direction = Helpers . DirectionFromPoints ( lineSourceCell . Value , cellCoords ) ;
59+ Point2D vector = cellCoords - lineSourceCell . Value ;
60+ int length = Math . Max ( Math . Abs ( vector . X ) , Math . Abs ( vector . Y ) ) ;
61+
62+ return ( direction , length ) ;
63+ }
64+
4465 public override void PreMapDraw ( Point2D cellCoords )
4566 {
67+ if ( lineSourceCell . HasValue )
68+ {
69+ ApplyLinePreview ( cellCoords ) ;
70+ return ;
71+ }
72+
4673 var cell = CursorActionTarget . Map . GetTile ( cellCoords ) ;
4774 if ( cell . TerrainObject == null )
4875 {
@@ -55,6 +82,12 @@ public override void PreMapDraw(Point2D cellCoords)
5582
5683 public override void PostMapDraw ( Point2D cellCoords )
5784 {
85+ if ( lineSourceCell . HasValue )
86+ {
87+ ClearLinePreview ( ) ;
88+ return ;
89+ }
90+
5891 var cell = CursorActionTarget . Map . GetTile ( cellCoords ) ;
5992 if ( cell . TerrainObject == terrainObject )
6093 {
@@ -64,11 +97,65 @@ public override void PostMapDraw(Point2D cellCoords)
6497 CursorActionTarget . AddRefreshPoint ( cellCoords ) ;
6598 }
6699
100+ private void ApplyLinePreview ( Point2D cellCoords )
101+ {
102+ if ( ! lineSourceCell . HasValue || lineSourceCell . Value == cellCoords )
103+ return ;
104+
105+ ( Direction direction , int length ) = GetLineInformation ( cellCoords ) ;
106+
107+ if ( length < 1 )
108+ return ;
109+
110+ linePreviewMutation = new PlaceTerrainObjectLineMutation ( CursorActionTarget . MutationTarget , TerrainType , lineSourceCell . Value , direction , length ) ;
111+ linePreviewMutation . Perform ( ) ;
112+ }
113+
114+ private void ClearLinePreview ( )
115+ {
116+ if ( linePreviewMutation != null )
117+ {
118+ linePreviewMutation . Undo ( ) ;
119+ linePreviewMutation = null ;
120+ }
121+
122+ CursorActionTarget . InvalidateMap ( ) ;
123+ }
124+
125+ public override void DrawPreview ( Point2D cellCoords , Point2D cameraTopLeftPoint )
126+ {
127+ if ( ! lineSourceCell . HasValue )
128+ return ;
129+
130+ if ( cellCoords == lineSourceCell . Value )
131+ return ;
132+
133+ ( Direction direction , int length ) = GetLineInformation ( cellCoords ) ;
134+
135+ Point2D cameraPoint1 = ( CellMath . CellCenterPointFromCellCoords_3D ( lineSourceCell . Value , Map ) - cameraTopLeftPoint ) . ScaleBy ( CursorActionTarget . Camera . ZoomLevel ) ;
136+ Point2D cameraPoint2 = ( CellMath . CellCenterPointFromCellCoords_3D ( lineSourceCell . Value + Helpers . VisualDirectionToPoint ( direction ) . ScaleBy ( length ) , Map ) - cameraTopLeftPoint ) . ScaleBy ( CursorActionTarget . Camera . ZoomLevel ) ;
137+
138+ Renderer . DrawLine ( cameraPoint1 . ToXNAVector ( ) , cameraPoint2 . ToXNAVector ( ) , Color . Orange , 2 ) ;
139+ }
140+
67141 public override void LeftDown ( Point2D cellCoords )
68142 {
69143 if ( _terrainType == null )
70144 throw new InvalidOperationException ( nameof ( TerrainType ) + " cannot be null" ) ;
71145
146+ if ( blocked )
147+ return ;
148+
149+ if ( KeyboardCommands . Instance . PlaceTerrainLine . AreKeysOrModifiersDown ( Keyboard ) )
150+ {
151+ if ( lineSourceCell == null && CursorActionTarget . Map . GetTile ( cellCoords ) != null )
152+ {
153+ lineSourceCell = cellCoords ;
154+ }
155+
156+ return ;
157+ }
158+
72159 var cell = CursorActionTarget . Map . GetTile ( cellCoords ) ;
73160 if ( cell . TerrainObject != null )
74161 return ;
@@ -77,6 +164,43 @@ public override void LeftDown(Point2D cellCoords)
77164 CursorActionTarget . MutationManager . PerformMutation ( mutation ) ;
78165 }
79166
80- public override void LeftClick ( Point2D cellCoords ) => LeftDown ( cellCoords ) ;
167+ private void ApplyLine ( Point2D cellCoords )
168+ {
169+ ( Direction direction , int length ) = GetLineInformation ( cellCoords ) ;
170+ var mutation = new PlaceTerrainObjectLineMutation ( CursorActionTarget . MutationTarget , TerrainType , lineSourceCell . Value , direction , length ) ;
171+ PerformMutation ( mutation ) ;
172+ lineSourceCell = null ;
173+ }
174+
175+ public override void LeftClick ( Point2D cellCoords )
176+ {
177+ if ( KeyboardCommands . Instance . PlaceTerrainLine . AreKeysOrModifiersDown ( Keyboard ) )
178+ {
179+ if ( lineSourceCell != null && cellCoords != lineSourceCell . Value )
180+ {
181+ ApplyLine ( cellCoords ) ;
182+ }
183+
184+ return ;
185+ }
186+
187+ LeftDown ( cellCoords ) ;
188+ blocked = false ;
189+ }
190+
191+ public override void Update ( Point2D ? cellCoords )
192+ {
193+ if ( lineSourceCell != null && cellCoords != null && lineSourceCell != cellCoords )
194+ {
195+ if ( ! KeyboardCommands . Instance . PlaceTerrainLine . AreKeysOrModifiersDown ( Keyboard ) )
196+ {
197+ ApplyLine ( cellCoords . Value ) ;
198+ blocked = true ;
199+ }
200+ }
201+
202+ if ( ! CursorActionTarget . WindowManager . Cursor . LeftDown && ! CursorActionTarget . WindowManager . Cursor . LeftClicked )
203+ blocked = false ;
204+ }
81205 }
82206}
0 commit comments