Skip to content

Commit ebf8a81

Browse files
authored
add the Spatial.addControlAt() method (#1899)
1 parent 0d6d6d9 commit ebf8a81

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

jme3-core/src/main/java/com/jme3/scene/Spatial.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2023 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -784,6 +784,38 @@ public void addControl(Control control) {
784784
}
785785
}
786786

787+
/**
788+
* Adds the specified control to the list, at the specified index. Any
789+
* controls with indices greater than or equal to the specified index will
790+
* have their indices increased by one.
791+
*
792+
* @param index the index at which to add the control (0→first, ≥0)
793+
* @param control the control to add (not null)
794+
* @throws IllegalStateException if the control is already added here
795+
*/
796+
@SuppressWarnings("unchecked")
797+
public void addControlAt(int index, Control control) {
798+
if (control == null) {
799+
throw new IllegalArgumentException("null control");
800+
}
801+
int numControls = getNumControls();
802+
if (index < 0 || index > numControls) {
803+
throw new IndexOutOfBoundsException(
804+
"index=" + index + " for numControls=" + numControls);
805+
}
806+
if (controls.contains(control)) {
807+
throw new IllegalStateException("Control is already added here.");
808+
}
809+
810+
addControl(control); // takes care of the bookkeeping
811+
812+
if (index < numControls) { // re-arrange the list directly
813+
boolean success = controls.remove(control);
814+
assert success;
815+
controls.add(index, control);
816+
}
817+
}
818+
787819
/**
788820
* Removes the first control that is an instance of the given class.
789821
*
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (c) 2023 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.scene;
33+
34+
import com.jme3.scene.control.UpdateControl;
35+
import org.junit.Assert;
36+
import org.junit.Test;
37+
38+
/**
39+
* Tests selected methods of the Spatial class.
40+
*
41+
* @author Stephen Gold
42+
*/
43+
public class SpatialTest {
44+
45+
/**
46+
* Tests addControlAt() with a duplicate Control.
47+
*/
48+
@Test(expected = IllegalStateException.class)
49+
public void addControlAtDuplicate() {
50+
Spatial testSpatial = new Node("testSpatial");
51+
UpdateControl control1 = new UpdateControl();
52+
testSpatial.addControlAt(0, control1);
53+
testSpatial.addControlAt(1, control1);
54+
}
55+
56+
/**
57+
* Tests addControlAt() with a negative index.
58+
*/
59+
@Test(expected = IndexOutOfBoundsException.class)
60+
public void addControlAtNegativeIndex() {
61+
Spatial testSpatial = new Node("testSpatial");
62+
UpdateControl control1 = new UpdateControl();
63+
testSpatial.addControlAt(-1, control1);
64+
}
65+
66+
/**
67+
* Tests addControlAt() with a null argument.
68+
*/
69+
@Test(expected = IllegalArgumentException.class)
70+
public void addControlAtNullControl() {
71+
Spatial testSpatial = new Node("testSpatial");
72+
testSpatial.addControlAt(0, null);
73+
}
74+
75+
/**
76+
* Tests addControlAt() with an out-of-range positive index.
77+
*/
78+
@Test(expected = IndexOutOfBoundsException.class)
79+
public void addControlAtOutOfRange() {
80+
Spatial testSpatial = new Node("testSpatial");
81+
UpdateControl control1 = new UpdateControl();
82+
testSpatial.addControlAt(1, control1);
83+
}
84+
85+
/**
86+
* Tests typical uses of addControlAt().
87+
*/
88+
@Test
89+
public void testAddControlAt() {
90+
Spatial testSpatial = new Node("testSpatial");
91+
92+
// Add to an empty list.
93+
UpdateControl control1 = new UpdateControl();
94+
testSpatial.addControlAt(0, control1);
95+
96+
Assert.assertEquals(1, testSpatial.getNumControls());
97+
Assert.assertEquals(control1, testSpatial.getControl(0));
98+
Assert.assertEquals(testSpatial, control1.getSpatial());
99+
100+
// Add at the end of a non-empty list.
101+
UpdateControl control2 = new UpdateControl();
102+
testSpatial.addControlAt(1, control2);
103+
104+
Assert.assertEquals(2, testSpatial.getNumControls());
105+
Assert.assertEquals(control1, testSpatial.getControl(0));
106+
Assert.assertEquals(control2, testSpatial.getControl(1));
107+
Assert.assertEquals(testSpatial, control1.getSpatial());
108+
Assert.assertEquals(testSpatial, control2.getSpatial());
109+
110+
// Add at the beginning of a non-empty list.
111+
UpdateControl control0 = new UpdateControl();
112+
testSpatial.addControlAt(0, control0);
113+
114+
Assert.assertEquals(3, testSpatial.getNumControls());
115+
Assert.assertEquals(control0, testSpatial.getControl(0));
116+
Assert.assertEquals(control1, testSpatial.getControl(1));
117+
Assert.assertEquals(control2, testSpatial.getControl(2));
118+
Assert.assertEquals(testSpatial, control0.getSpatial());
119+
Assert.assertEquals(testSpatial, control1.getSpatial());
120+
Assert.assertEquals(testSpatial, control2.getSpatial());
121+
}
122+
}

0 commit comments

Comments
 (0)