@@ -48,11 +48,6 @@ public class LevelTerrainToolControl extends ChangeHeightTerrainToolControl {
4848 */
4949 private boolean precision ;
5050
51- /**
52- * Instantiates a new Level terrain tool control.
53- *
54- * @param component the component
55- */
5651 public LevelTerrainToolControl (@ NotNull final TerrainPaintingComponent component ) {
5752 super (component );
5853
@@ -61,6 +56,7 @@ public LevelTerrainToolControl(@NotNull final TerrainPaintingComponent component
6156 }
6257
6358 @ Override
59+ @ JmeThread
6460 protected void onAttached (@ NotNull final Node node ) {
6561 super .onAttached (node );
6662
@@ -74,6 +70,7 @@ protected void onAttached(@NotNull final Node node) {
7470 }
7571
7672 @ Override
73+ @ JmeThread
7774 protected void onDetached (@ NotNull final Node node ) {
7875 super .onDetached (node );
7976
@@ -82,26 +79,26 @@ protected void onDetached(@NotNull final Node node) {
8279 }
8380
8481 @ Override
82+ @ JmeThread
8583 protected void controlUpdate (final float tpf ) {
8684 super .controlUpdate (tpf );
8785
8886 final Geometry levelMarker = getLevelMarker ();
8987 levelMarker .setCullHint (isUseMarker () ? Spatial .CullHint .Never : Spatial .CullHint .Always );
9088 }
9189
92- @ FromAnyThread
93- @ NotNull
9490 @ Override
95- protected ColorRGBA getBrushColor () {
91+ @ FromAnyThread
92+ protected @ NotNull ColorRGBA getBrushColor () {
9693 return ColorRGBA .Red ;
9794 }
9895
99- @ JmeThread
10096 @ Override
101- public void startPainting (@ NotNull final PaintingInput paintingInput , @ NotNull final Vector3f contactPoint ) {
102- super .startPainting (paintingInput , contactPoint );
97+ @ JmeThread
98+ public void startPainting (@ NotNull final PaintingInput input , @ NotNull final Vector3f contactPoint ) {
99+ super .startPainting (input , contactPoint );
103100
104- switch (paintingInput ) {
101+ switch (input ) {
105102 case MOUSE_PRIMARY : {
106103 startChange ();
107104 modifyHeight (contactPoint );
@@ -115,11 +112,12 @@ public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull f
115112 }
116113
117114 @ Override
115+ @ JmeThread
118116 public void updatePainting (@ NotNull final Vector3f contactPoint ) {
119117
120- final PaintingInput paintingInput = notNull (getCurrentInput ());
118+ final PaintingInput input = notNull (getCurrentInput ());
121119
122- switch (paintingInput ) {
120+ switch (input ) {
123121 case MOUSE_PRIMARY : {
124122 modifyHeight (contactPoint );
125123 break ;
@@ -132,12 +130,13 @@ public void updatePainting(@NotNull final Vector3f contactPoint) {
132130 }
133131
134132 @ Override
133+ @ JmeThread
135134 public void finishPainting (@ NotNull final Vector3f contactPoint ) {
136135 super .finishPainting (contactPoint );
137136
138- final PaintingInput paintingInput = notNull (getCurrentInput ());
137+ final PaintingInput input = notNull (getCurrentInput ());
139138
140- switch (paintingInput ) {
139+ switch (input ) {
141140 case MOUSE_PRIMARY : {
142141 modifyHeight (contactPoint );
143142 commitChanges ();
@@ -155,150 +154,164 @@ public void finishPainting(@NotNull final Vector3f contactPoint) {
155154 *
156155 * @param contactPoint the contact point.
157156 */
157+ @ JmeThread
158158 private void modifyHeight (@ NotNull final Vector3f contactPoint ) {
159159
160- final LocalObjects local = getLocalObjects ();
161- final Node terrainNode = (Node ) notNull (getPaintedModel ());
162- final Geometry levelMarker = getLevelMarker ();
163-
164- final Vector3f markerTranslation = levelMarker .getLocalTranslation ();
165- final Vector3f worldTranslation = terrainNode .getWorldTranslation ();
166- final Vector3f localScale = terrainNode .getLocalScale ();
167- final Vector3f localPoint = contactPoint .subtract (worldTranslation , local .nextVector ());
168- final Vector2f terrainLoc = local .nextVector2f ();
169- final Vector2f effectPoint = local .nextVector2f ();
170160
171- final Terrain terrain = (Terrain ) terrainNode ;
161+ final LocalObjects local = getLocalObjects ();
162+ final Spatial paintedModel = notNull (getPaintedModel ());
172163 final Geometry brush = getBrush ();
164+ final Geometry levelMarker = getLevelMarker ();
173165
174166 final float brushSize = getBrushSize ();
175167 final float brushPower = getBrushPower ();
176168
177- final float markerHeight = markerTranslation .getY () - worldTranslation .getY ();
178- final float desiredHeight = isUseMarker () ? markerHeight : getLevel ();
169+ for (final Terrain terrain : getTerrains ()) {
179170
180- final int radiusStepsX = (int ) (brushSize / localScale .getX ());
181- final int radiusStepsZ = (int ) (brushSize / localScale .getZ ());
171+ final Node terrainNode = (Node ) terrain ;
182172
183- final float xStepAmount = localScale .getX ();
184- final float zStepAmount = localScale .getZ ();
173+ final Vector3f markerTranslation = levelMarker .getLocalTranslation ();
174+ final Vector3f worldTranslation = terrainNode .getWorldTranslation ();
175+ final Vector3f localScale = terrainNode .getLocalScale ();
176+ final Vector3f localPoint = contactPoint .subtract (worldTranslation , local .nextVector ());
177+ final Vector2f terrainLoc = local .nextVector2f ();
178+ final Vector2f effectPoint = local .nextVector2f ();
185179
186- final List < Vector2f > locs = new ArrayList <> ();
187- final List < Float > heights = new ArrayList <> ();
180+ final float markerHeight = markerTranslation . getY () - worldTranslation . getY ();
181+ final float desiredHeight = isUseMarker () ? markerHeight : getLevel ();
188182
189- for ( int z = - radiusStepsZ ; z < radiusStepsZ ; z ++) {
190- for ( int x = - radiusStepsX ; x < radiusStepsX ; x ++) {
183+ final int radiusStepsX = ( int ) ( brushSize / localScale . getX ());
184+ final int radiusStepsZ = ( int ) ( brushSize / localScale . getZ ());
191185
192- float locX = localPoint .getX () + ( x * xStepAmount );
193- float locZ = localPoint .getZ () + ( z * zStepAmount );
186+ final float xStepAmount = localScale .getX ();
187+ final float zStepAmount = localScale .getZ ();
194188
195- effectPoint .set (locX - localPoint .getX (), locZ - localPoint .getZ ());
189+ final List <Vector2f > locs = new ArrayList <>();
190+ final List <Float > heights = new ArrayList <>();
196191
197- if (!isContains (brush , effectPoint .getX (), effectPoint .getY ())) {
198- continue ;
199- }
192+ for (int z = -radiusStepsZ ; z < radiusStepsZ ; z ++) {
193+ for (int x = -radiusStepsX ; x < radiusStepsX ; x ++) {
200194
201- terrainLoc .set (locX , locZ );
195+ float locX = localPoint .getX () + (x * xStepAmount );
196+ float locZ = localPoint .getZ () + (z * zStepAmount );
202197
203- // adjust height based on radius of the tool
204- final float currentHeight = terrain .getHeightmapHeight (terrainLoc ) * localScale .getY ();
198+ effectPoint .set (locX - localPoint .getX (), locZ - localPoint .getZ ());
205199
206- if (isPrecision ()) {
207- locs .add (terrainLoc .clone ());
208- heights .add (desiredHeight / localScale .getY ());
209- } else {
200+ if (!isContains (brush , effectPoint .getX (), effectPoint .getY ())) {
201+ continue ;
202+ }
210203
211- float epsilon = 0.0001f * brushPower ; // rounding error for snapping
212- float adj = 0 ;
204+ terrainLoc .set (locX , locZ );
213205
214- if ( currentHeight < desiredHeight ) adj = 1 ;
215- else if ( currentHeight > desiredHeight ) adj = - 1 ;
206+ // adjust height based on radius of the tool
207+ final float currentHeight = terrain . getHeightmapHeight ( terrainLoc ) * localScale . getY () ;
216208
217- adj *= brushPower ;
218- adj *= calculateRadiusPercent (brushSize , effectPoint .getX (), effectPoint .getY ());
209+ if (isPrecision ()) {
210+ locs .add (terrainLoc .clone ());
211+ heights .add (desiredHeight / localScale .getY ());
212+ } else {
219213
220- // test if adjusting too far and then cap it
221- if (adj > 0 && floatGreaterThan ((currentHeight + adj ), desiredHeight , epsilon )) {
222- adj = desiredHeight - currentHeight ;
223- } else if (adj < 0 && floatLessThan ((currentHeight + adj ), desiredHeight , epsilon )) {
224- adj = desiredHeight - currentHeight ;
225- }
214+ float epsilon = 0.0001f * brushPower ; // rounding error for snapping
215+ float adj = 0 ;
226216
227- if (!floatEquals (adj , 0 , 0.001f )) {
228- locs .add (terrainLoc .clone ());
229- heights .add (currentHeight + adj );
217+ if (currentHeight < desiredHeight ) adj = 1 ;
218+ else if (currentHeight > desiredHeight ) adj = -1 ;
219+
220+ adj *= brushPower ;
221+ adj *= calculateRadiusPercent (brushSize , effectPoint .getX (), effectPoint .getY ());
222+
223+ // test if adjusting too far and then cap it
224+ if (adj > 0 && floatGreaterThan ((currentHeight + adj ), desiredHeight , epsilon )) {
225+ adj = desiredHeight - currentHeight ;
226+ } else if (adj < 0 && floatLessThan ((currentHeight + adj ), desiredHeight , epsilon )) {
227+ adj = desiredHeight - currentHeight ;
228+ }
229+
230+ if (!floatEquals (adj , 0 , 0.001f )) {
231+ locs .add (terrainLoc .clone ());
232+ heights .add (currentHeight + adj );
233+ }
230234 }
231235 }
232236 }
233- }
234237
235- locs .forEach (this :: change );
238+ locs .forEach (location -> change ( terrain , location ) );
236239
237- // do the actual height adjustment
238- terrain .setHeight (locs , heights );
240+ // do the actual height adjustment
241+ terrain .setHeight (locs , heights );
242+ }
239243
240- terrainNode .updateModelBound (); // or else we won't collide with it where we just edited
244+ // or else we won't collide with it where we just edited
245+ paintedModel .updateModelBound ();
241246 }
242247
243248 /**
244- * Sets level.
249+ * Set the target level.
245250 *
246251 * @param level the target level.
247252 */
253+ @ JmeThread
248254 public void setLevel (final float level ) {
249255 this .level = level ;
250256 }
251257
252258 /**
253- * Gets level.
259+ * Get the target level.
254260 *
255261 * @return the target level.
256262 */
263+ @ JmeThread
257264 public float getLevel () {
258265 return level ;
259266 }
260267
261268 /**
262- * Is precision boolean .
269+ * Return true if using precision changing .
263270 *
264271 * @return true if using precision changing.
265272 */
266- public boolean isPrecision () {
273+ @ JmeThread
274+ private boolean isPrecision () {
267275 return precision ;
268276 }
269277
270278 /**
271- * Sets precision.
279+ * Set the flag of using precision changing .
272280 *
273281 * @param precision the flag of using precision changing.
274282 */
283+ @ JmeThread
275284 public void setPrecision (final boolean precision ) {
276285 this .precision = precision ;
277286 }
278287
279288 /**
280- * Sets use marker.
289+ * Set the flag of using marker.
281290 *
282291 * @param useMarker the flag of using marker.
283292 */
293+ @ JmeThread
284294 public void setUseMarker (final boolean useMarker ) {
285295 this .useMarker = useMarker ;
286296 }
287297
288298 /**
289- * Is use marker boolean .
299+ * Return the flag of using marker .
290300 *
291301 * @return the flag of using marker.
292302 */
293- public boolean isUseMarker () {
303+ @ JmeThread
304+ private boolean isUseMarker () {
294305 return useMarker ;
295306 }
296307
297308 /**
309+ * Get the level marker.
310+ *
298311 * @return the level marker.
299312 */
300- @ NotNull
301- private Geometry getLevelMarker () {
313+ @ JmeThread
314+ private @ NotNull Geometry getLevelMarker () {
302315 return levelMarker ;
303316 }
304317}
0 commit comments