46 public static bool Keybind(
this ImGui gui,
string label, ref KeyCode keyCode)
50 var
id = gui.GetControlId(label);
53 gui.AddSpacingIfLayoutFrameNotEmpty();
54 gui.BeginHorizontal();
55 var rect = gui.AddLayoutRect(gui.GetLayoutWidth() * 0.8f, gui.GetRowHeight());
56 gui.Text(label, rect, overflow: ImTextOverflow.Ellipsis);
59 if (gui.GetActiveControl() ==
id)
63 if (SetToPressedKey(gui, ref keyCode))
66 gui.ResetActiveControl();
69 else if (gui.Button(keyCode.ToString()))
71 gui.SetActiveControl(
id);
80 bool SetToPressedKey(ImGui gui, ref KeyCode keyCode)
86 if (key == KeyCode.Mouse0 || key == KeyCode.Mouse1)
89 if (key == KeyCode.Escape)
91 keyCode = KeyCode.None;
102 string label = configEntry.Definition.Key;
103 string description = configEntry.Description.Description;
104 description = InsertLineBreaks(description, 40);
106 object value = configEntry.BoxedValue;
107 Type type = configEntry.SettingType;
109 var width = gui.Layout.GetAvailableWidth();
110 var height = gui.GetRowHeight();
111 var nextPosition = gui.Layout.GetNextPosition(height);
112 ImRect clickableArea =
new ImRect(nextPosition.x, nextPosition.y, width, height);
113 var groupId = gui.GetNextControlId();
114 gui.RegisterGroup(groupId, clickableArea);
116 if (type == typeof(
bool))
118 bool v = (bool)value;
120 if (gui.Checkbox(ref v, label))
121 configEntry.BoxedValue = v;
123 else if (type == typeof(
byte))
125 byte v = (byte)value;
129 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
130 configEntry.BoxedValue = (byte)v;
133 else if (type == typeof(sbyte))
135 int v = (sbyte)value;
139 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider, min: sbyte.MinValue, max: sbyte.MaxValue))
140 configEntry.BoxedValue = (sbyte)v;
143 else if (type == typeof(
short))
145 short v = (short)value;
149 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
150 configEntry.BoxedValue = (short)v;
153 else if (type == typeof(ushort))
155 int v = (ushort)value;
159 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider, min: ushort.MinValue, max: ushort.MaxValue))
160 configEntry.BoxedValue = (ushort)v;
163 else if (type == typeof(
int))
169 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
170 configEntry.BoxedValue = (int)v;
173 else if (type == typeof(uint))
175 long v = (uint)value;
179 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider, min: uint.MinValue, max: uint.MaxValue))
180 configEntry.BoxedValue = (uint)v;
183 else if (type == typeof(
long))
185 long v = (long)value;
189 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
190 configEntry.BoxedValue = v;
193 else if (type == typeof(ulong))
195 long v = (long)value;
199 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider, min: 0))
200 configEntry.BoxedValue = (ulong)v;
203 else if (type == typeof(
float))
205 float v = (float)value;
209 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
210 configEntry.BoxedValue = v;
213 else if (type == typeof(
double))
215 double v = (double)value;
219 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
220 configEntry.BoxedValue = v;
223 else if (type == typeof(decimal))
225 double v = Convert.ToDouble((decimal)value);
229 if (gui.NumericEdit(ref v, flags: ImNumericEditFlag.Slider))
230 configEntry.BoxedValue = (decimal)v;
233 else if (type == typeof(
string))
235 string v = (string)value ??
"";
239 if (gui.TextEdit(ref v))
240 configEntry.BoxedValue = v;
243 else if (type == typeof(Vector2))
245 Vector2 v = (Vector2)value;
249 if (gui.Vector(ref v))
250 configEntry.BoxedValue = v;
253 else if (type == typeof(Vector3))
255 Vector3 v = (Vector3)value;
259 if (gui.Vector(ref v))
260 configEntry.BoxedValue = v;
263 else if (type == typeof(Vector4))
265 Vector4 v = (Vector4)value;
269 if (gui.Vector(ref v))
270 configEntry.BoxedValue = v;
273 else if (type == typeof(Quaternion))
275 var quat = (Quaternion)value;
276 Vector4 v =
new Vector4(quat.x, quat.y, quat.z, quat.w);
280 if (gui.Vector(ref v))
281 configEntry.BoxedValue =
new Quaternion(v.x, v.y, v.z, v.w);
284 else if (type == typeof(UnityEngine.Color))
288 var tempValue = (UnityEngine.Color)configEntry.BoxedValue;
289 if (gui.ColorEdit(ref tempValue))
290 configEntry.BoxedValue = tempValue;
293 else if (type == typeof(KeyboardShortcut))
298 else if (type == typeof(KeyCode))
300 var tempValue = (KeyCode)configEntry.BoxedValue;
301 if (gui.Keybind(label, ref tempValue))
302 configEntry.BoxedValue = tempValue;
304 else if (type.IsEnum)
306 string[] names = Enum.GetNames(type);
308 int currentIndex = Array.IndexOf(names, value.ToString());
312 if (gui.Dropdown(ref currentIndex, names))
314 object enumValue = Enum.Parse(type, names[currentIndex]);
315 configEntry.BoxedValue = enumValue;
322 gui.Text($
"{label}: Unsupported type ({type.Name})");
325 if (!description.IsNullOrWhiteSpace() && gui.IsGroupHovered(groupId))
326 gui.Tooltip(description, gui.Input.MousePosition + gui.Style.Tooltip.OffsetPixels / gui.Canvas.ScreenScale);
328 string InsertLineBreaks(
string text,
int maxLineLength)
330 if (
string.IsNullOrWhiteSpace(text) || maxLineLength <= 0)
333 var words = text.Split(
' ');
334 var sb =
new StringBuilder();
336 int currentLineLength = 0;
338 foreach (var word
in words)
340 if (currentLineLength + word.Length + 1 > maxLineLength)
343 currentLineLength = 0;
345 else if (currentLineLength > 0)
352 currentLineLength += word.Length;
355 return sb.ToString();