Skip to content

Commit 736d3e2

Browse files
committed
DragScalar, InputScalar, SliderScalar: Added support for u8/s8/u16/s16 data types. We are reusing function instances for larger types to reduce code size. (#643, #320, #708, #1011)
1 parent 525a53a commit 736d3e2

File tree

4 files changed

+126
-33
lines changed

4 files changed

+126
-33
lines changed

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Breaking Changes:
4242
Other Changes:
4343

4444
- Nav: Fixed a tap on AltGR (e.g. German keyboard) from navigation to the menu layer.
45+
- DragScalar, InputScalar, SliderScalar: Added support for u8/s8/u16/s16 data types.
46+
We are reusing function instances for larger types to reduce code size. (#643, #320, #708, #1011)
4547
- InputInt, InputFloat, InputScalar: Fix to keep the label of the +/- buttons centered when
4648
style.FramePadding.x is abnormally larger than style.FramePadding.y. Since the buttons are
4749
meant to be square (to align with e.g. color button) we always use FramePadding.y. (#2367)

imgui.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data);
150150
typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data);
151151

152152
// Scalar data types
153+
typedef char ImS8; // 8-bit signed integer == char
154+
typedef unsigned char ImU8; // 8-bit unsigned integer
155+
typedef short ImS16; // 16-bit signed integer
156+
typedef unsigned short ImU16; // 16-bit unsigned integer
153157
typedef signed int ImS32; // 32-bit signed integer == int
154158
typedef unsigned int ImU32; // 32-bit unsigned integer (often used to store packed colors)
155159
#if defined(_MSC_VER) && !defined(__clang__)
@@ -884,10 +888,14 @@ enum ImGuiDragDropFlags_
884888
// A primary data type
885889
enum ImGuiDataType_
886890
{
891+
ImGuiDataType_S8, // char
892+
ImGuiDataType_U8, // unsigned char
893+
ImGuiDataType_S16, // short
894+
ImGuiDataType_U16, // unsigned short
887895
ImGuiDataType_S32, // int
888896
ImGuiDataType_U32, // unsigned int
889-
ImGuiDataType_S64, // long long, __int64
890-
ImGuiDataType_U64, // unsigned long long, unsigned __int64
897+
ImGuiDataType_S64, // long long / __int64
898+
ImGuiDataType_U64, // unsigned long long / unsigned __int64
891899
ImGuiDataType_Float, // float
892900
ImGuiDataType_Double, // double
893901
ImGuiDataType_COUNT

imgui_demo.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,10 @@ static void ShowDemoWindowWidgets()
11981198
ImS64 LLONG_MAX = 9223372036854775807LL;
11991199
ImU64 ULLONG_MAX = (2ULL * 9223372036854775807LL + 1);
12001200
#endif
1201+
const char s8_zero = 0, s8_one = 1, s8_fifty = 50, s8_min = -128, s8_max = 127;
1202+
const ImU8 u8_zero = 0, u8_one = 1, u8_fifty = 50, u8_min = 0, u8_max = 255;
1203+
const short s16_zero = 0, s16_one = 1, s16_fifty = 50, s16_min = -32768, s16_max = 32767;
1204+
const ImU16 u16_zero = 0, u16_one = 1, u16_fifty = 50, u16_min = 0, u16_max = 65535;
12011205
const ImS32 s32_zero = 0, s32_one = 1, s32_fifty = 50, s32_min = INT_MIN/2, s32_max = INT_MAX/2, s32_hi_a = INT_MAX/2 - 100, s32_hi_b = INT_MAX/2;
12021206
const ImU32 u32_zero = 0, u32_one = 1, u32_fifty = 50, u32_min = 0, u32_max = UINT_MAX/2, u32_hi_a = UINT_MAX/2 - 100, u32_hi_b = UINT_MAX/2;
12031207
const ImS64 s64_zero = 0, s64_one = 1, s64_fifty = 50, s64_min = LLONG_MIN/2, s64_max = LLONG_MAX/2, s64_hi_a = LLONG_MAX/2 - 100, s64_hi_b = LLONG_MAX/2;
@@ -1206,6 +1210,10 @@ static void ShowDemoWindowWidgets()
12061210
const double f64_zero = 0., f64_one = 1., f64_lo_a = -1000000000000000.0, f64_hi_a = +1000000000000000.0;
12071211

12081212
// State
1213+
static char s8_v = 127;
1214+
static ImU8 u8_v = 255;
1215+
static short s16_v = 32767;
1216+
static ImU16 u16_v = 65535;
12091217
static ImS32 s32_v = -1;
12101218
static ImU32 u32_v = (ImU32)-1;
12111219
static ImS64 s64_v = -1;
@@ -1217,6 +1225,10 @@ static void ShowDemoWindowWidgets()
12171225
static bool drag_clamp = false;
12181226
ImGui::Text("Drags:");
12191227
ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); ImGui::SameLine(); ShowHelpMarker("As with every widgets in dear imgui, we never modify values unless there is a user interaction.\nYou can override the clamping limits by using CTRL+Click to input a value.");
1228+
ImGui::DragScalar("drag s8", ImGuiDataType_S8, &s8_v, drag_speed, drag_clamp ? &s8_zero : NULL, drag_clamp ? &s8_fifty : NULL);
1229+
ImGui::DragScalar("drag u8", ImGuiDataType_U8, &u8_v, drag_speed, drag_clamp ? &u8_zero : NULL, drag_clamp ? &u8_fifty : NULL, "%u ms");
1230+
ImGui::DragScalar("drag s16", ImGuiDataType_S16, &s16_v, drag_speed, drag_clamp ? &s16_zero : NULL, drag_clamp ? &s16_fifty : NULL);
1231+
ImGui::DragScalar("drag u16", ImGuiDataType_U16, &u16_v, drag_speed, drag_clamp ? &u16_zero : NULL, drag_clamp ? &u16_fifty : NULL, "%u ms");
12201232
ImGui::DragScalar("drag s32", ImGuiDataType_S32, &s32_v, drag_speed, drag_clamp ? &s32_zero : NULL, drag_clamp ? &s32_fifty : NULL);
12211233
ImGui::DragScalar("drag u32", ImGuiDataType_U32, &u32_v, drag_speed, drag_clamp ? &u32_zero : NULL, drag_clamp ? &u32_fifty : NULL, "%u ms");
12221234
ImGui::DragScalar("drag s64", ImGuiDataType_S64, &s64_v, drag_speed, drag_clamp ? &s64_zero : NULL, drag_clamp ? &s64_fifty : NULL);
@@ -1227,6 +1239,10 @@ static void ShowDemoWindowWidgets()
12271239
ImGui::DragScalar("drag double ^2", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", 2.0f);
12281240

12291241
ImGui::Text("Sliders");
1242+
ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d");
1243+
ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u");
1244+
ImGui::SliderScalar("slider s16 full", ImGuiDataType_S16, &s16_v, &s16_min, &s16_max, "%d");
1245+
ImGui::SliderScalar("slider u16 full", ImGuiDataType_U16, &u16_v, &u16_min, &u16_max, "%u");
12301246
ImGui::SliderScalar("slider s32 low", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty,"%d");
12311247
ImGui::SliderScalar("slider s32 high", ImGuiDataType_S32, &s32_v, &s32_hi_a, &s32_hi_b, "%d");
12321248
ImGui::SliderScalar("slider s32 full", ImGuiDataType_S32, &s32_v, &s32_min, &s32_max, "%d");
@@ -1249,6 +1265,10 @@ static void ShowDemoWindowWidgets()
12491265
static bool inputs_step = true;
12501266
ImGui::Text("Inputs");
12511267
ImGui::Checkbox("Show step buttons", &inputs_step);
1268+
ImGui::InputScalar("input s8", ImGuiDataType_S8, &s8_v, inputs_step ? &s8_one : NULL, NULL, "%d");
1269+
ImGui::InputScalar("input u8", ImGuiDataType_U8, &u8_v, inputs_step ? &u8_one : NULL, NULL, "%u");
1270+
ImGui::InputScalar("input s16", ImGuiDataType_S16, &s16_v, inputs_step ? &s16_one : NULL, NULL, "%d");
1271+
ImGui::InputScalar("input u16", ImGuiDataType_U16, &u16_v, inputs_step ? &u16_one : NULL, NULL, "%u");
12521272
ImGui::InputScalar("input s32", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%d");
12531273
ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%08X", ImGuiInputTextFlags_CharsHexadecimal);
12541274
ImGui::InputScalar("input u32", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%u");

0 commit comments

Comments
 (0)