Skip to content

Commit cb80d08

Browse files
committed
Improve resize API
1 parent 9d0d53a commit cb80d08

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,9 @@ private static boolean preInputText(boolean multiline, String label, ImString te
24732473

24742474
if (inputData.isResized) {
24752475
inputData.isResized = false;
2476-
text.resize(nGetResizeValue(), true);
2476+
final int resizeValue = nGetResizeValue();
2477+
text.resize(resizeValue + inputData.resizeFactor);
2478+
inputData.size = resizeValue;
24772479
}
24782480

24792481
return hasInput;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Use this class to customize your ImGui input.
55
*/
66
public final class ImGuiInputTextData {
7+
private static final short DEFAULT_RESIZE_FACTOR = 10;
8+
79
/**
810
* If not empty, then other chars which are different from provided will be filtered during the {@link ImGui#inputText}
911
* and {@link ImGui#inputTextMultiline} methods.
@@ -13,10 +15,16 @@ public final class ImGuiInputTextData {
1315
/**
1416
* If true, then string will be resized during the the {@link ImGui#inputText} and {@link ImGui#inputTextMultiline} methods.
1517
* Alternatively you can provide {@link imgui.enums.ImGuiInputTextFlags#CallbackResize} flag to the input text widgets to enable string resizing.
16-
* Resize factor of the string could be modified by changing {@link ImString#resizeFactor} field.
18+
* Resize factor of the string could be modified by changing {@link #resizeFactor} field.
1719
*/
1820
public boolean isResizable;
1921

22+
/**
23+
* String will be resized to the value equal to a new size plus this resize factor.
24+
* Default value is 10.
25+
*/
26+
public int resizeFactor = DEFAULT_RESIZE_FACTOR;
27+
2028
int size;
2129
boolean isDirty;
2230
boolean isResized = false;

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@ public final class ImString {
1414
* Size of ImGui caret which is shown during the input text focus.
1515
*/
1616
public static final short CARET_LEN = 1;
17-
/**
18-
* Default resize value, used to set up {@link ImString#resizeFactor}.
19-
*/
20-
public static final short DEFAULT_RESIZE = 10;
2117

2218
/**
2319
* Configuration class to setup some specific behaviour for current string.
2420
* This is useful when string used inside of ImGui#InputText*() methods.
2521
*/
2622
public final ImGuiInputTextData inputData = new ImGuiInputTextData();
27-
/**
28-
* String will be resized to the value equal to a new size plus this resize factor.
29-
*/
30-
public int resizeFactor = DEFAULT_RESIZE;
3123

3224
byte[] data;
3325
private String text = "";
@@ -75,23 +67,25 @@ public String get() {
7567
}
7668

7769
public void set(final String value) {
78-
set(value, inputData.isResizable, resizeFactor);
70+
set(value, inputData.isResizable, inputData.resizeFactor);
7971
}
8072

8173
public void set(final String value, final boolean resize) {
82-
set(value, resize, resizeFactor);
74+
set(value, resize, inputData.resizeFactor);
8375
}
8476

8577
public void set(final String value, final boolean resize, final int resizeValue) {
8678
final byte[] valueBuff = value.getBytes();
8779
final int currentLen = data == null ? 0 : data.length;
8880
byte[] newBuff = null;
8981

82+
// If provided value require bigger buffer and we can resize it
9083
if (resize && (currentLen - CARET_LEN) < valueBuff.length) {
9184
newBuff = new byte[valueBuff.length + resizeValue + CARET_LEN];
9285
inputData.size = valueBuff.length;
9386
}
9487

88+
// If there were no resize and we still need a new buffer
9589
if (newBuff == null) {
9690
newBuff = new byte[currentLen];
9791
inputData.size = Math.max(0, Math.min(valueBuff.length, currentLen - CARET_LEN));
@@ -103,15 +97,11 @@ public void set(final String value, final boolean resize, final int resizeValue)
10397
}
10498

10599
public void resize(final int newSize) {
106-
resize(newSize, false);
107-
}
108-
109-
public void resize(final int newSize, final boolean respectResizeFactor) {
110100
if (newSize < data.length) {
111101
throw new IllegalArgumentException("New size should be greater than current size of the buffer");
112102
}
113103

114-
final int size = newSize + CARET_LEN + (respectResizeFactor ? resizeFactor : 0);
104+
final int size = newSize + CARET_LEN;
115105
final byte[] newBuffer = new byte[size];
116106
System.arraycopy(data, 0, newBuffer, 0, data.length);
117107
data = newBuffer;

0 commit comments

Comments
 (0)