Skip to content

Commit d27e3aa

Browse files
authored
feat(api): make ImVec2/ImVec4 math operations immutable (#317)
resolves #302 Updated math operations (`plus`, `minus`, `times`, `div`) for `ImVec2` and `ImVec4` to return new instances instead of modifying the existing ones, ensuring immutability.
1 parent 02691fd commit d27e3aa

File tree

6 files changed

+147
-65
lines changed

6 files changed

+147
-65
lines changed

imgui-binding/src/generated/java/imgui/ImVec2.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,37 @@ public ImVec2 set(final ImVec2 value) {
3131
}
3232

3333
public ImVec2 plus(final float x, final float y) {
34-
this.x += x;
35-
this.y += y;
36-
return this;
34+
return new ImVec2(this.x + x, this.y + y);
3735
}
3836

3937
public ImVec2 plus(final ImVec2 value) {
4038
return plus(value.x, value.y);
4139
}
4240

4341
public ImVec2 minus(final float x, final float y) {
44-
this.x -= x;
45-
this.y -= y;
46-
return this;
42+
return new ImVec2(this.x - x, this.y -y);
4743
}
4844

4945
public ImVec2 minus(final ImVec2 value) {
5046
return minus(value.x, value.y);
5147
}
5248

5349
public ImVec2 times(final float x, final float y) {
54-
this.x *= x;
55-
this.y *= y;
56-
return this;
50+
return new ImVec2(this.x * x, this.y * y);
5751
}
5852

5953
public ImVec2 times(final ImVec2 value) {
6054
return times(value.x, value.y);
6155
}
6256

57+
public ImVec2 div(final float x, final float y) {
58+
return new ImVec2(this.x / x, this.y / y);
59+
}
60+
61+
public ImVec2 div(final ImVec2 value) {
62+
return div(value.x, value.y);
63+
}
64+
6365
@Override
6466
public String toString() {
6567
return "ImVec2{"

imgui-binding/src/generated/java/imgui/ImVec4.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,37 @@ public ImVec4 set(final ImVec4 value) {
3535
}
3636

3737
public ImVec4 plus(final float x, final float y, final float z, final float w) {
38-
this.x += x;
39-
this.y += y;
40-
this.z += z;
41-
this.w += w;
42-
return this;
38+
return new ImVec4(this.x + x, this.y + y, this.z + z, this.w + w);
4339
}
4440

4541
public ImVec4 plus(final ImVec4 value) {
4642
return plus(value.x, value.y, value.z, value.w);
4743
}
4844

4945
public ImVec4 minus(final float x, final float y, final float z, final float w) {
50-
this.x -= x;
51-
this.y -= y;
52-
this.z -= z;
53-
this.w -= w;
54-
return this;
46+
return new ImVec4(this.x - x, this.y - y, this.z - z, this.w - w);
5547
}
5648

5749
public ImVec4 minus(final ImVec4 value) {
5850
return minus(value.x, value.y, value.z, value.w);
5951
}
6052

6153
public ImVec4 times(final float x, final float y, final float z, final float w) {
62-
this.x *= x;
63-
this.y *= y;
64-
this.z *= z;
65-
this.w *= w;
66-
return this;
54+
return new ImVec4(this.x * x, this.y * y, this.z * z, this.w * w);
6755
}
6856

6957
public ImVec4 times(final ImVec4 value) {
7058
return times(value.x, value.y, value.z, value.w);
7159
}
7260

61+
public ImVec4 div(final float x, final float y, final float z, final float w) {
62+
return new ImVec4(this.x / x, this.y / y, this.z / z, this.w / w);
63+
}
64+
65+
public ImVec4 div(final ImVec4 value) {
66+
return div(value.x, value.y, value.z, value.w);
67+
}
68+
7369
@Override
7470
public String toString() {
7571
return "ImVec4{"

imgui-binding/src/main/java/imgui/ImVec2.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,37 @@ public ImVec2 set(final ImVec2 value) {
3131
}
3232

3333
public ImVec2 plus(final float x, final float y) {
34-
this.x += x;
35-
this.y += y;
36-
return this;
34+
return new ImVec2(this.x + x, this.y + y);
3735
}
3836

3937
public ImVec2 plus(final ImVec2 value) {
4038
return plus(value.x, value.y);
4139
}
4240

4341
public ImVec2 minus(final float x, final float y) {
44-
this.x -= x;
45-
this.y -= y;
46-
return this;
42+
return new ImVec2(this.x - x, this.y -y);
4743
}
4844

4945
public ImVec2 minus(final ImVec2 value) {
5046
return minus(value.x, value.y);
5147
}
5248

5349
public ImVec2 times(final float x, final float y) {
54-
this.x *= x;
55-
this.y *= y;
56-
return this;
50+
return new ImVec2(this.x * x, this.y * y);
5751
}
5852

5953
public ImVec2 times(final ImVec2 value) {
6054
return times(value.x, value.y);
6155
}
6256

57+
public ImVec2 div(final float x, final float y) {
58+
return new ImVec2(this.x / x, this.y / y);
59+
}
60+
61+
public ImVec2 div(final ImVec2 value) {
62+
return div(value.x, value.y);
63+
}
64+
6365
@Override
6466
public String toString() {
6567
return "ImVec2{"

imgui-binding/src/main/java/imgui/ImVec4.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,37 @@ public ImVec4 set(final ImVec4 value) {
3535
}
3636

3737
public ImVec4 plus(final float x, final float y, final float z, final float w) {
38-
this.x += x;
39-
this.y += y;
40-
this.z += z;
41-
this.w += w;
42-
return this;
38+
return new ImVec4(this.x + x, this.y + y, this.z + z, this.w + w);
4339
}
4440

4541
public ImVec4 plus(final ImVec4 value) {
4642
return plus(value.x, value.y, value.z, value.w);
4743
}
4844

4945
public ImVec4 minus(final float x, final float y, final float z, final float w) {
50-
this.x -= x;
51-
this.y -= y;
52-
this.z -= z;
53-
this.w -= w;
54-
return this;
46+
return new ImVec4(this.x - x, this.y - y, this.z - z, this.w - w);
5547
}
5648

5749
public ImVec4 minus(final ImVec4 value) {
5850
return minus(value.x, value.y, value.z, value.w);
5951
}
6052

6153
public ImVec4 times(final float x, final float y, final float z, final float w) {
62-
this.x *= x;
63-
this.y *= y;
64-
this.z *= z;
65-
this.w *= w;
66-
return this;
54+
return new ImVec4(this.x * x, this.y * y, this.z * z, this.w * w);
6755
}
6856

6957
public ImVec4 times(final ImVec4 value) {
7058
return times(value.x, value.y, value.z, value.w);
7159
}
7260

61+
public ImVec4 div(final float x, final float y, final float z, final float w) {
62+
return new ImVec4(this.x / x, this.y / y, this.z / z, this.w / w);
63+
}
64+
65+
public ImVec4 div(final ImVec4 value) {
66+
return div(value.x, value.y, value.z, value.w);
67+
}
68+
7369
@Override
7470
public String toString() {
7571
return "ImVec4{"

imgui-binding/src/test/java/imgui/ImVec2Test.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.jupiter.api.Test;
55

66
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNotSame;
8+
import static org.junit.jupiter.api.Assertions.assertSame;
79

810
class ImVec2Test {
911
@Nested
@@ -34,67 +36,104 @@ void fromImVec2() {
3436
class set {
3537
@Test
3638
void withFloats() {
37-
final ImVec2 value = new ImVec2().set(1, 2);
39+
final ImVec2 original = new ImVec2();
40+
final ImVec2 value = original.set(1, 2);
3841
assertEquals(1, value.x);
3942
assertEquals(2, value.y);
43+
assertSame(original, value);
4044
}
4145

4246
@Test
4347
void withImVec2() {
44-
final ImVec2 value = new ImVec2().set(new ImVec2(1, 2));
48+
final ImVec2 original = new ImVec2();
49+
final ImVec2 value = original.set(new ImVec2(1, 2));
4550
assertEquals(1, value.x);
4651
assertEquals(2, value.y);
52+
assertSame(original, value);
4753
}
4854
}
4955

5056
@Nested
5157
class plus {
5258
@Test
5359
void withFloats() {
54-
final ImVec2 value = new ImVec2(5, 5).plus(1, 2);
60+
final ImVec2 original = new ImVec2(5, 5);
61+
final ImVec2 value = original.plus(1, 2);
5562
assertEquals(6, value.x);
5663
assertEquals(7, value.y);
64+
assertNotSame(original, value);
5765
}
5866

5967
@Test
6068
void withImVec2() {
61-
final ImVec2 value = new ImVec2(5, 5).plus(new ImVec2(1, 2));
69+
final ImVec2 original = new ImVec2(5, 5);
70+
final ImVec2 value = original.plus(new ImVec2(1, 2));
6271
assertEquals(6, value.x);
6372
assertEquals(7, value.y);
73+
assertNotSame(original, value);
6474
}
6575
}
6676

6777
@Nested
6878
class minus {
6979
@Test
7080
void withFloats() {
71-
final ImVec2 value = new ImVec2(5, 5).minus(1, 2);
81+
final ImVec2 original = new ImVec2(5, 5);
82+
final ImVec2 value = original.minus(1, 2);
7283
assertEquals(4, value.x);
7384
assertEquals(3, value.y);
85+
assertNotSame(original, value);
7486
}
7587

7688
@Test
7789
void withImVec2() {
78-
final ImVec2 value = new ImVec2(5, 5).minus(new ImVec2(1, 2));
90+
final ImVec2 original = new ImVec2(5, 5);
91+
final ImVec2 value = original.minus(new ImVec2(1, 2));
7992
assertEquals(4, value.x);
8093
assertEquals(3, value.y);
94+
assertNotSame(original, value);
8195
}
8296
}
8397

8498
@Nested
8599
class times {
86100
@Test
87101
void withFloats() {
88-
final ImVec2 value = new ImVec2(5, 5).times(1, 2);
102+
final ImVec2 original = new ImVec2(5, 5);
103+
final ImVec2 value = original.times(1, 2);
89104
assertEquals(5, value.x);
90105
assertEquals(10, value.y);
106+
assertNotSame(original, value);
91107
}
92108

93109
@Test
94110
void withImVec2() {
95-
final ImVec2 value = new ImVec2(5, 5).times(new ImVec2(1, 2));
111+
final ImVec2 original = new ImVec2(5, 5);
112+
final ImVec2 value = original.times(new ImVec2(1, 2));
96113
assertEquals(5, value.x);
97114
assertEquals(10, value.y);
115+
assertNotSame(original, value);
116+
}
117+
}
118+
119+
@Nested
120+
class div {
121+
@Test
122+
void withFloats() {
123+
final ImVec2 original = new ImVec2(10, 10);
124+
final ImVec2 value = original.div(5, 2);
125+
assertEquals(2, value.x);
126+
assertEquals(5, value.y);
127+
assertNotSame(original, value);
128+
}
129+
130+
@Test
131+
void withImVec2() {
132+
final ImVec2 original = new ImVec2(10, 10);
133+
final ImVec2 value = original.div(new ImVec2(5, 2));
134+
assertEquals(2, value.x);
135+
assertEquals(5, value.y);
136+
assertNotSame(original, value);
98137
}
99138
}
100139
}

0 commit comments

Comments
 (0)