Improve resize API

This commit is contained in:
SpaiR committed 2020-01-04 21:23:32 +03:00
1 parent 9d0d53a56c
commit cb80d08ec9
3 files changed
+17 -17

No files matched your search

+3 -1
View File
@@ -2473,7 +2473,9 @@ public final class ImGui {
if (inputData.isResized) { if (inputData.isResized) {
inputData.isResized = false; inputData.isResized = false;
text.resize(nGetResizeValue(), true); final int resizeValue = nGetResizeValue();
text.resize(resizeValue + inputData.resizeFactor);
inputData.size = resizeValue;
} }
return hasInput; return hasInput;
@@ -4,6 +4,8 @@ package imgui;
* Use this class to customize your ImGui input. * Use this class to customize your ImGui input.
*/ */
public final class ImGuiInputTextData { public final class ImGuiInputTextData {
private static final short DEFAULT_RESIZE_FACTOR = 10;
/** /**
* If not empty, then other chars which are different from provided will be filtered during the {@link ImGui#inputText} * If not empty, then other chars which are different from provided will be filtered during the {@link ImGui#inputText}
* and {@link ImGui#inputTextMultiline} methods. * and {@link ImGui#inputTextMultiline} methods.
@@ -13,10 +15,16 @@ public final class ImGuiInputTextData {
/** /**
* If true, then string will be resized during the the {@link ImGui#inputText} and {@link ImGui#inputTextMultiline} methods. * If true, then string will be resized during the the {@link ImGui#inputText} and {@link ImGui#inputTextMultiline} methods.
* Alternatively you can provide {@link imgui.enums.ImGuiInputTextFlags#CallbackResize} flag to the input text widgets to enable string resizing. * Alternatively you can provide {@link imgui.enums.ImGuiInputTextFlags#CallbackResize} flag to the input text widgets to enable string resizing.
* Resize factor of the string could be modified by changing {@link ImString#resizeFactor} field. * Resize factor of the string could be modified by changing {@link #resizeFactor} field.
*/ */
public boolean isResizable; public boolean isResizable;
/**
* String will be resized to the value equal to a new size plus this resize factor.
* Default value is 10.
*/
public int resizeFactor = DEFAULT_RESIZE_FACTOR;
int size; int size;
boolean isDirty; boolean isDirty;
boolean isResized = false; boolean isResized = false;
@@ -14,20 +14,12 @@ public final class ImString {
* Size of ImGui caret which is shown during the input text focus. * Size of ImGui caret which is shown during the input text focus.
*/ */
public static final short CARET_LEN = 1; public static final short CARET_LEN = 1;
/**
* Default resize value, used to set up {@link ImString#resizeFactor}.
*/
public static final short DEFAULT_RESIZE = 10;
/** /**
* Configuration class to setup some specific behaviour for current string. * Configuration class to setup some specific behaviour for current string.
* This is useful when string used inside of ImGui#InputText*() methods. * This is useful when string used inside of ImGui#InputText*() methods.
*/ */
public final ImGuiInputTextData inputData = new ImGuiInputTextData(); public final ImGuiInputTextData inputData = new ImGuiInputTextData();
/**
* String will be resized to the value equal to a new size plus this resize factor.
*/
public int resizeFactor = DEFAULT_RESIZE;
byte[] data; byte[] data;
private String text = ""; private String text = "";
@@ -75,11 +67,11 @@ public final class ImString {
} }
public void set(final String value) { public void set(final String value) {
set(value, inputData.isResizable, resizeFactor); set(value, inputData.isResizable, inputData.resizeFactor);
} }
public void set(final String value, final boolean resize) { public void set(final String value, final boolean resize) {
set(value, resize, resizeFactor); set(value, resize, inputData.resizeFactor);
} }
public void set(final String value, final boolean resize, final int resizeValue) { public void set(final String value, final boolean resize, final int resizeValue) {
@@ -87,11 +79,13 @@ public final class ImString {
final int currentLen = data == null ? 0 : data.length; final int currentLen = data == null ? 0 : data.length;
byte[] newBuff = null; byte[] newBuff = null;
// If provided value require bigger buffer and we can resize it
if (resize && (currentLen - CARET_LEN) < valueBuff.length) { if (resize && (currentLen - CARET_LEN) < valueBuff.length) {
newBuff = new byte[valueBuff.length + resizeValue + CARET_LEN]; newBuff = new byte[valueBuff.length + resizeValue + CARET_LEN];
inputData.size = valueBuff.length; inputData.size = valueBuff.length;
} }
// If there were no resize and we still need a new buffer
if (newBuff == null) { if (newBuff == null) {
newBuff = new byte[currentLen]; newBuff = new byte[currentLen];
inputData.size = Math.max(0, Math.min(valueBuff.length, currentLen - CARET_LEN)); inputData.size = Math.max(0, Math.min(valueBuff.length, currentLen - CARET_LEN));
@@ -103,15 +97,11 @@ public final class ImString {
} }
public void resize(final int newSize) { public void resize(final int newSize) {
resize(newSize, false);
}
public void resize(final int newSize, final boolean respectResizeFactor) {
if (newSize < data.length) { if (newSize < data.length) {
throw new IllegalArgumentException("New size should be greater than current size of the buffer"); throw new IllegalArgumentException("New size should be greater than current size of the buffer");
} }
final int size = newSize + CARET_LEN + (respectResizeFactor ? resizeFactor : 0); final int size = newSize + CARET_LEN;
final byte[] newBuffer = new byte[size]; final byte[] newBuffer = new byte[size];
System.arraycopy(data, 0, newBuffer, 0, data.length); System.arraycopy(data, 0, newBuffer, 0, data.length);
data = newBuffer; data = newBuffer;