6
6
* Wrapper for {@link String} to use inside of th Dear ImGui input widgets.
7
7
*/
8
8
public final class ImString {
9
- private static final short DEFAULT_LENGTH = 100 ;
10
- private static final short CARET_LEN = 1 ;
11
- private static final short DEFAULT_RESIZE = 10 ;
9
+ /**
10
+ * Default size of the inner buffer, if {@link ImString} created with a constructor without args.
11
+ */
12
+ public static final short DEFAULT_LENGTH = 100 ;
13
+ /**
14
+ * Size of ImGui caret which is shown during the input text focus.
15
+ */
16
+ 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 ;
12
21
22
+ /**
23
+ * Configuration class to setup some specific behaviour for current string.
24
+ * This is useful when string used inside of ImGui#InputText*() methods.
25
+ */
13
26
public final ImGuiInputTextData inputData = new ImGuiInputTextData ();
14
27
/**
15
28
* String will be resized to the value equal to a new size plus this resize factor.
@@ -19,19 +32,35 @@ public final class ImString {
19
32
byte [] data ;
20
33
private String text = "" ;
21
34
35
+ /**
36
+ * Creates an {@link ImString} instance with {@link ImString#DEFAULT_LENGTH} size for the inner buffer.
37
+ */
22
38
public ImString () {
23
39
this (DEFAULT_LENGTH );
24
40
}
25
41
42
+ /**
43
+ * Creates an {@link ImString} instance with provided size for the inner buffer.
44
+ * @param length size of the inner buffer to use
45
+ */
26
46
public ImString (final int length ) {
27
47
data = new byte [length + CARET_LEN ];
28
48
}
29
49
50
+ /**
51
+ * Creates an {@link ImString} instance from provided string.
52
+ * Inner buffer size will be equal to the length of the string + {@link ImString#CARET_LEN}.
53
+ * @param text string to create a new {@link ImString}
54
+ */
30
55
public ImString (final String text ) {
31
- this (text .length ());
32
- set (text );
56
+ set (text , true , 0 );
33
57
}
34
58
59
+ /**
60
+ * Create an {@link ImString} instance from provided string with custom size for the inner buffer.
61
+ * @param text string to a create a new {@link ImString}
62
+ * @param length custom size for the inner buffer
63
+ */
35
64
public ImString (final String text , final int length ) {
36
65
this (length );
37
66
set (text );
@@ -46,15 +75,46 @@ public String get() {
46
75
}
47
76
48
77
public void set (final String value ) {
49
- set (value . getBytes () );
78
+ set (value , inputData . isResizable , resizeFactor );
50
79
}
51
80
52
81
public void set (final String value , final boolean resize ) {
53
- final byte [] str = value .getBytes ();
54
- if (resize && data .length - CARET_LEN < str .length ) {
55
- data = new byte [str .length + resizeFactor + CARET_LEN ];
82
+ set (value , resize , resizeFactor );
83
+ }
84
+
85
+ public void set (final String value , final boolean resize , final int resizeValue ) {
86
+ final byte [] valueBuff = value .getBytes ();
87
+ final int currentLen = data == null ? 0 : data .length ;
88
+ byte [] newBuff = null ;
89
+
90
+ if (resize && (currentLen - CARET_LEN ) < valueBuff .length ) {
91
+ newBuff = new byte [valueBuff .length + resizeValue + CARET_LEN ];
92
+ inputData .size = valueBuff .length ;
93
+ }
94
+
95
+ if (newBuff == null ) {
96
+ newBuff = new byte [currentLen ];
97
+ inputData .size = Math .max (0 , Math .min (valueBuff .length , currentLen - CARET_LEN ));
98
+ }
99
+
100
+ System .arraycopy (valueBuff , 0 , newBuff , 0 , Math .min (valueBuff .length , newBuff .length - CARET_LEN ));
101
+ data = newBuff ;
102
+ inputData .isDirty = true ;
103
+ }
104
+
105
+ public void resize (final int newSize ) {
106
+ resize (newSize , false );
107
+ }
108
+
109
+ public void resize (final int newSize , final boolean respectResizeFactor ) {
110
+ if (newSize < data .length ) {
111
+ throw new IllegalArgumentException ("New size should be greater than current size of the buffer" );
56
112
}
57
- set (str );
113
+
114
+ final int size = newSize + CARET_LEN + (respectResizeFactor ? resizeFactor : 0 );
115
+ final byte [] newBuffer = new byte [size ];
116
+ System .arraycopy (data , 0 , newBuffer , 0 , data .length );
117
+ data = newBuffer ;
58
118
}
59
119
60
120
/**
@@ -73,13 +133,6 @@ public int getBufferSize() {
73
133
return data .length ;
74
134
}
75
135
76
- private void set (final byte [] str ) {
77
- final int len = Math .min (str .length , data .length );
78
- System .arraycopy (str , 0 , data , 0 , len );
79
- inputData .isDirty = true ;
80
- inputData .size = len ;
81
- }
82
-
83
136
@ Override
84
137
public String toString () {
85
138
return get ();
0 commit comments