Fixed warnings for more stringent compilation settings. Added various small helpers.

This commit is contained in:
ocornut 2014-08-12 19:57:46 +01:00
parent c938affcac
commit 2573ffb6fc
3 changed files with 84 additions and 85 deletions

View File

@ -28,7 +28,7 @@
/* /*
namespace ImGui namespace ImGui
{ {
void Value(const char* prefix, cosnt MyVec2& v, const char* float_format = NULL); void Value(const char* prefix, const MyVec2& v, const char* float_format = NULL);
void Value(const char* prefix, cosnt MyVec4& v, const char* float_format = NULL); void Value(const char* prefix, const MyVec4& v, const char* float_format = NULL);
}; };
*/ */

150
imgui.cpp
View File

@ -274,7 +274,7 @@ ImGuiIO::ImGuiIO()
// - on Windows you can get those using ToAscii+keyboard state, or via the VM_CHAR message // - on Windows you can get those using ToAscii+keyboard state, or via the VM_CHAR message
void ImGuiIO::AddInputCharacter(char c) void ImGuiIO::AddInputCharacter(char c)
{ {
const int n = strlen(InputCharacters); const size_t n = strlen(InputCharacters);
if (n < sizeof(InputCharacters) / sizeof(InputCharacters[0])) if (n < sizeof(InputCharacters) / sizeof(InputCharacters[0]))
{ {
InputCharacters[n] = c; InputCharacters[n] = c;
@ -287,7 +287,7 @@ void ImGuiIO::AddInputCharacter(char c)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#undef ARRAYSIZE #undef ARRAYSIZE
#define ARRAYSIZE(_ARR) (sizeof(_ARR)/sizeof(*_ARR)) #define ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
#undef PI #undef PI
const float PI = 3.14159265358979323846f; const float PI = 3.14159265358979323846f;
@ -337,7 +337,7 @@ static const char* ImStristr(const char* haystack, const char* needle, const cha
if (!needle_end) if (!needle_end)
needle_end = needle + strlen(needle); needle_end = needle + strlen(needle);
const char un0 = toupper(*needle); const char un0 = (char)toupper(*needle);
while (*haystack) while (*haystack)
{ {
if (toupper(*haystack) == un0) if (toupper(*haystack) == un0)
@ -382,7 +382,7 @@ static size_t ImFormatString(char* buf, size_t buf_size, const char* fmt, ...)
int w = vsnprintf(buf, buf_size, fmt, args); int w = vsnprintf(buf, buf_size, fmt, args);
va_end(args); va_end(args);
buf[buf_size-1] = 0; buf[buf_size-1] = 0;
if (w == -1) w = buf_size; if (w == -1) w = (int)buf_size;
return w; return w;
} }
@ -390,7 +390,7 @@ static size_t ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_li
{ {
int w = vsnprintf(buf, buf_size, fmt, args); int w = vsnprintf(buf, buf_size, fmt, args);
buf[buf_size-1] = 0; buf[buf_size-1] = 0;
if (w == -1) w = buf_size; if (w == -1) w = (int)buf_size;
return w; return w;
} }
@ -541,7 +541,7 @@ struct ImGuiTextEditState
{ {
char Text[1024]; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so own buffer. char Text[1024]; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so own buffer.
char InitialText[1024]; // backup of end-user buffer at focusing time, to ESC key can do a revert. Also used for arithmetic operations (but could use a pre-parsed float there). char InitialText[1024]; // backup of end-user buffer at focusing time, to ESC key can do a revert. Also used for arithmetic operations (but could use a pre-parsed float there).
int MaxLength; // end-user buffer size <= 1024 (or increase above) size_t BufSize; // end-user buffer size, <= 1024 (or increase above)
float Width; // widget width float Width; // widget width
float ScrollX; float ScrollX;
STB_TexteditState StbState; STB_TexteditState StbState;
@ -555,7 +555,7 @@ struct ImGuiTextEditState
void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking
bool CursorIsVisible() const { return CursorAnim <= 0.0f || fmodf(CursorAnim, 1.20f) <= 0.80f; } // Blinking bool CursorIsVisible() const { return CursorAnim <= 0.0f || fmodf(CursorAnim, 1.20f) <= 0.80f; } // Blinking
bool HasSelection() const { return StbState.select_start != StbState.select_end; } bool HasSelection() const { return StbState.select_start != StbState.select_end; }
void SelectAll() { StbState.select_start = 0; StbState.select_end = strlen(Text); StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; } void SelectAll() { StbState.select_start = 0; StbState.select_end = (int)strlen(Text); StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
void OnKeyboardPressed(int key); void OnKeyboardPressed(int key);
void UpdateScrollOffset(); void UpdateScrollOffset();
@ -717,7 +717,7 @@ static ImVector<ImGuiStorage::Pair>::iterator LowerBound(ImVector<ImGuiStorage::
{ {
ImVector<ImGuiStorage::Pair>::iterator first = data.begin(); ImVector<ImGuiStorage::Pair>::iterator first = data.begin();
ImVector<ImGuiStorage::Pair>::iterator last = data.end(); ImVector<ImGuiStorage::Pair>::iterator last = data.end();
int count = last - first; int count = (int)(last - first);
while (count > 0) while (count > 0)
{ {
int count2 = count / 2; int count2 = count / 2;
@ -870,18 +870,20 @@ bool ImGuiTextFilter::PassFilter(const char* val) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ImGuiTextBuffer::Append(const char* fmt, ...) void ImGuiTextBuffer::append(const char* fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
int len = vsnprintf(NULL, 0, fmt, args); int len = vsnprintf(NULL, 0, fmt, args);
va_end(args); va_end(args);
if (len <= 0)
return;
const size_t write_off = Buf.size(); const size_t write_off = Buf.size();
if (write_off + len >= Buf.capacity()) if (write_off + len >= Buf.capacity())
Buf.reserve(Buf.capacity() * 2); Buf.reserve(Buf.capacity() * 2);
Buf.resize(write_off + len); Buf.resize(write_off + (size_t)len);
va_start(args, fmt); va_start(args, fmt);
ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args); ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args);
@ -1165,7 +1167,7 @@ void NewFrame()
else else
g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
g.IO.MousePosPrev = g.IO.MousePos; g.IO.MousePosPrev = g.IO.MousePos;
for (int i = 0; i < ARRAYSIZE(g.IO.MouseDown); i++) for (size_t i = 0; i < ARRAYSIZE(g.IO.MouseDown); i++)
{ {
g.IO.MouseDownTime[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownTime[i] < 0.0f ? 0.0f : g.IO.MouseDownTime[i] + g.IO.DeltaTime) : -1.0f; g.IO.MouseDownTime[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownTime[i] < 0.0f ? 0.0f : g.IO.MouseDownTime[i] + g.IO.DeltaTime) : -1.0f;
g.IO.MouseClicked[i] = (g.IO.MouseDownTime[i] == 0.0f); g.IO.MouseClicked[i] = (g.IO.MouseDownTime[i] == 0.0f);
@ -1185,7 +1187,7 @@ void NewFrame()
} }
} }
} }
for (int i = 0; i < ARRAYSIZE(g.IO.KeysDown); i++) for (size_t i = 0; i < ARRAYSIZE(g.IO.KeysDown); i++)
g.IO.KeysDownTime[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownTime[i] < 0.0f ? 0.0f : g.IO.KeysDownTime[i] + g.IO.DeltaTime) : -1.0f; g.IO.KeysDownTime[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownTime[i] < 0.0f ? 0.0f : g.IO.KeysDownTime[i] + g.IO.DeltaTime) : -1.0f;
// Clear reference to active widget if the widget isn't alive anymore // Clear reference to active widget if the widget isn't alive anymore
@ -1303,7 +1305,6 @@ static void AddWindowToSortedBuffer(ImGuiWindow* window, ImVector<ImGuiWindow*>&
static void PushClipRect(const ImVec4& clip_rect, bool clipped = true) static void PushClipRect(const ImVec4& clip_rect, bool clipped = true)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
ImVec4 cr = clip_rect; ImVec4 cr = clip_rect;
@ -1320,7 +1321,6 @@ static void PushClipRect(const ImVec4& clip_rect, bool clipped = true)
static void PopClipRect() static void PopClipRect()
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
window->ClipRectStack.pop_back(); window->ClipRectStack.pop_back();
window->DrawList->PopClipRect(); window->DrawList->PopClipRect();
@ -1451,9 +1451,9 @@ static void LogText(const ImVec2& ref_pos, const char* text, const char* text_en
else else
{ {
if (log_new_line || !is_first_line) if (log_new_line || !is_first_line)
g.LogClipboard.Append("\n%*s%.*s", tree_depth*4, "", char_count, text_remaining); g.LogClipboard.append("\n%*s%.*s", tree_depth*4, "", char_count, text_remaining);
else else
g.LogClipboard.Append(" %.*s", char_count, text_remaining); g.LogClipboard.append(" %.*s", char_count, text_remaining);
} }
} }
@ -1496,7 +1496,6 @@ static void RenderText(ImVec2 pos, const char* text, const char* text_end, const
static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding) static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding); window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding);
@ -1509,7 +1508,6 @@ static void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border,
static void RenderCollapseTriangle(ImVec2 p_min, bool open, float scale = 1.0f, bool shadow = false) static void RenderCollapseTriangle(ImVec2 p_min, bool open, float scale = 1.0f, bool shadow = false)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
const float h = window->FontSize() * 1.00f; const float h = window->FontSize() * 1.00f;
@ -1538,7 +1536,6 @@ static void RenderCollapseTriangle(ImVec2 p_min, bool open, float scale = 1.0f,
static ImVec2 CalcTextSize(const char* text, const char* text_end, const bool hide_text_after_hash) static ImVec2 CalcTextSize(const char* text, const char* text_end, const bool hide_text_after_hash)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
const char* text_display_end; const char* text_display_end;
@ -1556,7 +1553,7 @@ static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs)
ImGuiState& g = GImGui; ImGuiState& g = GImGui;
for (int i = (int)g.Windows.size()-1; i >= 0; i--) for (int i = (int)g.Windows.size()-1; i >= 0; i--)
{ {
ImGuiWindow* window = g.Windows[i]; ImGuiWindow* window = g.Windows[(size_t)i];
if (!window->Visible) if (!window->Visible)
continue; continue;
if (excluding_childs && (window->Flags & ImGuiWindowFlags_ChildWindow) != 0) if (excluding_childs && (window->Flags & ImGuiWindowFlags_ChildWindow) != 0)
@ -1588,6 +1585,11 @@ static bool IsMouseHoveringBox(const ImGuiAabb& box)
return box_for_touch.Contains(g.IO.MousePos); return box_for_touch.Contains(g.IO.MousePos);
} }
bool IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max)
{
return IsMouseHoveringBox(ImGuiAabb(box_min, box_max));
}
static bool IsKeyPressedMap(ImGuiKey key, bool repeat) static bool IsKeyPressedMap(ImGuiKey key, bool repeat)
{ {
ImGuiState& g = GImGui; ImGuiState& g = GImGui;
@ -1631,6 +1633,13 @@ bool IsMouseClicked(int button, bool repeat)
return false; return false;
} }
bool IsMouseDoubleClicked(int button)
{
ImGuiState& g = GImGui;
IM_ASSERT(button >= 0 && button < ARRAYSIZE(g.IO.MouseDown));
return g.IO.MouseDoubleClicked[button];
}
ImVec2 GetMousePos() ImVec2 GetMousePos()
{ {
return GImGui.IO.MousePos; return GImGui.IO.MousePos;
@ -1681,7 +1690,7 @@ void BeginChild(const char* str_id, ImVec2 size, bool border, ImGuiWindowFlags e
ImGuiState& g = GImGui; ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
ImU32 flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_ChildWindow; ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_ChildWindow;
const ImVec2 content_max = window->Pos + ImGui::GetWindowContentRegionMax(); const ImVec2 content_max = window->Pos + ImGui::GetWindowContentRegionMax();
const ImVec2 cursor_pos = window->Pos + ImGui::GetCursorPos(); const ImVec2 cursor_pos = window->Pos + ImGui::GetCursorPos();
@ -2084,7 +2093,7 @@ void End()
} }
if (g.LogClipboard.size() > 1) if (g.LogClipboard.size() > 1)
{ {
g.LogClipboard.Append("\n"); g.LogClipboard.append("\n");
if (g.IO.SetClipboardTextFn) if (g.IO.SetClipboardTextFn)
g.IO.SetClipboardTextFn(g.LogClipboard.begin(), g.LogClipboard.end()); g.IO.SetClipboardTextFn(g.LogClipboard.begin(), g.LogClipboard.end());
g.LogClipboard.clear(); g.LogClipboard.clear();
@ -2296,21 +2305,24 @@ void SetCursorPos(ImVec2 p)
void SetScrollPosHere() void SetScrollPosHere()
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
window->NextScrollY = (window->DC.CursorPos.y + window->ScrollY) - (window->Pos.y + window->SizeFull.y * 0.5f) - (window->TitleBarHeight() + window->WindowPadding().y); window->NextScrollY = (window->DC.CursorPos.y + window->ScrollY) - (window->Pos.y + window->SizeFull.y * 0.5f) - (window->TitleBarHeight() + window->WindowPadding().y);
} }
void SetTreeStateStorage(ImGuiStorage* tree) void SetTreeStateStorage(ImGuiStorage* tree)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
window->DC.StateStorage = tree ? tree : &window->StateStorage; window->DC.StateStorage = tree ? tree : &window->StateStorage;
} }
ImGuiStorage* GetTreeStateStorage()
{
ImGuiWindow* window = GetCurrentWindow();
return window->DC.StateStorage;
}
void TextV(const char* fmt, va_list args) void TextV(const char* fmt, va_list args)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
if (window->Collapsed) if (window->Collapsed)
return; return;
@ -2577,7 +2589,6 @@ bool SmallButton(const char* label)
static bool CloseWindowButton(bool* open) static bool CloseWindowButton(bool* open)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
const ImGuiID id = window->GetID("##CLOSE"); const ImGuiID id = window->GetID("##CLOSE");
@ -2613,7 +2624,8 @@ void LogToTTY(int max_depth)
return; return;
g.LogEnabled = true; g.LogEnabled = true;
g.LogFile = stdout; g.LogFile = stdout;
g.LogAutoExpandMaxDepth = max_depth; if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth;
} }
void LogToFile(int max_depth, const char* filename) void LogToFile(int max_depth, const char* filename)
@ -2621,10 +2633,12 @@ void LogToFile(int max_depth, const char* filename)
ImGuiState& g = GImGui; ImGuiState& g = GImGui;
if (g.LogEnabled) if (g.LogEnabled)
return; return;
IM_ASSERT(filename); if (!filename)
filename = g.IO.LogFilename;
g.LogEnabled = true; g.LogEnabled = true;
g.LogFile = fopen(filename, "at"); g.LogFile = fopen(filename, "at");
g.LogAutoExpandMaxDepth = max_depth; if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth;
} }
void LogToClipboard(int max_depth) void LogToClipboard(int max_depth)
@ -2634,7 +2648,8 @@ void LogToClipboard(int max_depth)
return; return;
g.LogEnabled = true; g.LogEnabled = true;
g.LogFile = NULL; g.LogFile = NULL;
g.LogAutoExpandMaxDepth = max_depth; if (max_depth >= 0)
g.LogAutoExpandMaxDepth = max_depth;
} }
void LogButtons() void LogButtons()
@ -2761,7 +2776,7 @@ void BulletText(const char* fmt, ...)
const float line_height = window->FontSize(); const float line_height = window->FontSize();
const ImVec2 text_size = CalcTextSize(text_begin, text_end); const ImVec2 text_size = CalcTextSize(text_begin, text_end);
const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height + (text_size.x ? (g.Style.FramePadding.x*2) : 0.0f),0) + text_size); // Empty text doesn't add padding const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height + (text_size.x > 0.0f ? (g.Style.FramePadding.x*2) : 0.0f),0) + text_size); // Empty text doesn't add padding
ItemSize(bb); ItemSize(bb);
if (ClipAdvance(bb)) if (ClipAdvance(bb))
@ -2775,10 +2790,6 @@ void BulletText(const char* fmt, ...)
bool TreeNode(const char* str_id, const char* fmt, ...) bool TreeNode(const char* str_id, const char* fmt, ...)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow();
ImGuiStorage* tree = window->DC.StateStorage;
static char buf[1024]; static char buf[1024];
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
@ -2800,10 +2811,6 @@ bool TreeNode(const char* str_id, const char* fmt, ...)
bool TreeNode(const void* ptr_id, const char* fmt, ...) bool TreeNode(const void* ptr_id, const char* fmt, ...)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow();
ImGuiStorage* tree = window->DC.StateStorage;
static char buf[1024]; static char buf[1024];
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
@ -3147,7 +3154,6 @@ bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const
return false; return false;
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const ImVec2 text_size = CalcTextSize(label);
bool value_changed = false; bool value_changed = false;
@ -3192,7 +3198,6 @@ static void Plot(ImGuiPlotType plot_type, const char* label, const float* values
return; return;
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const ImVec2 text_size = CalcTextSize(label); const ImVec2 text_size = CalcTextSize(label);
if (graph_size.x == 0) if (graph_size.x == 0)
@ -3430,17 +3435,17 @@ static bool is_separator(char c) { return c==',' || c==';' || c=='(' || c==')' |
#define STB_TEXTEDIT_IS_SPACE(c) (is_white(c) || is_separator(c)) #define STB_TEXTEDIT_IS_SPACE(c) (is_white(c) || is_separator(c))
void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int idx, int n) { char* dst = obj->Text+idx; const char* src = obj->Text+idx+n; while (char c = *src++) *dst++ = c; *dst = '\0'; } void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int idx, int n) { char* dst = obj->Text+idx; const char* src = obj->Text+idx+n; while (char c = *src++) *dst++ = c; *dst = '\0'; }
bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int idx, const char* new_text, int new_text_size) bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int idx, const char* new_text, int new_text_len)
{ {
char* buf_end = obj->Text + obj->MaxLength; char* buf_end = obj->Text + obj->BufSize;
int text_size = strlen(obj->Text); const int text_len = (int)strlen(obj->Text);
if (new_text_size > buf_end - (obj->Text + text_size + 1)) if (new_text_len > buf_end - (obj->Text + text_len + 1))
return false; return false;
memmove(obj->Text + idx + new_text_size, obj->Text + idx, text_size - idx); memmove(obj->Text + idx + new_text_len, obj->Text + idx, text_len - idx);
memcpy(obj->Text + idx, new_text, new_text_size); memcpy(obj->Text + idx, new_text, new_text_len);
obj->Text[text_size + new_text_size] = 0; obj->Text[text_len + new_text_len] = 0;
return true; return true;
} }
@ -3522,7 +3527,7 @@ void ImGuiTextEditState::RenderTextScrolledClipped(ImFont font, float font_size,
const float clip_end = (text_end[0] != '\0' && text_end > text_start) ? symbol_w : 0.0f; const float clip_end = (text_end[0] != '\0' && text_end > text_start) ? symbol_w : 0.0f;
// Draw text // Draw text
ImGui::RenderText(pos+ImVec2(clip_begin,0), text_start+(clip_begin?1:0), text_end-(clip_end?1:0), false);//, &text_params_with_clipping); ImGui::RenderText(pos+ImVec2(clip_begin,0), text_start+(clip_begin>0.0f?1:0), text_end-(clip_end>0.0f?1:0), false);//, &text_params_with_clipping);
// Draw the clip symbol // Draw the clip symbol
const char s[2] = {symbol_c,'\0'}; const char s[2] = {symbol_c,'\0'};
@ -3549,7 +3554,7 @@ bool InputFloat(const char* label, float *v, float step, float step_fast, int de
ImGui::PushID(label); ImGui::PushID(label);
const float button_sz = window->FontSize(); const float button_sz = window->FontSize();
if (step) if (step > 0.0f)
ImGui::PushItemWidth(ImMax(1.0f, window->DC.ItemWidth.back() - (button_sz+g.Style.FramePadding.x*2.0f+g.Style.ItemInnerSpacing.x)*2)); ImGui::PushItemWidth(ImMax(1.0f, window->DC.ItemWidth.back() - (button_sz+g.Style.FramePadding.x*2.0f+g.Style.ItemInnerSpacing.x)*2));
char buf[64]; char buf[64];
@ -3563,21 +3568,20 @@ bool InputFloat(const char* label, float *v, float step, float step_fast, int de
ApplyNumericalTextInput(buf, v); ApplyNumericalTextInput(buf, v);
value_changed = true; value_changed = true;
} }
if (step)
ImGui::PopItemWidth();
if (step) if (step > 0.0f)
{ {
ImGui::PopItemWidth();
ImGui::SameLine(0, 0); ImGui::SameLine(0, 0);
if (ImGui::Button("-", ImVec2(button_sz,button_sz), true)) if (ImGui::Button("-", ImVec2(button_sz,button_sz), true))
{ {
*v -= g.IO.KeyCtrl && step_fast ? step_fast : step; *v -= g.IO.KeyCtrl && step_fast > 0.0f ? step_fast : step;
value_changed = true; value_changed = true;
} }
ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x); ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x);
if (ImGui::Button("+", ImVec2(button_sz,button_sz), true)) if (ImGui::Button("+", ImVec2(button_sz,button_sz), true))
{ {
*v += g.IO.KeyCtrl && step_fast ? step_fast : step; *v += g.IO.KeyCtrl && step_fast > 0.0f ? step_fast : step;
value_changed = true; value_changed = true;
} }
} }
@ -3664,7 +3668,7 @@ bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlag
bool cancel_edit = false; bool cancel_edit = false;
if (g.ActiveId == id) if (g.ActiveId == id)
{ {
edit_state.MaxLength = buf_size < ARRAYSIZE(edit_state.Text) ? buf_size : ARRAYSIZE(edit_state.Text); edit_state.BufSize = buf_size < ARRAYSIZE(edit_state.Text) ? buf_size : ARRAYSIZE(edit_state.Text);
edit_state.Font = window->Font(); edit_state.Font = window->Font();
edit_state.FontSize = window->FontSize(); edit_state.FontSize = window->FontSize();
@ -3729,7 +3733,7 @@ bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlag
if (const char* clipboard = g.IO.GetClipboardTextFn()) if (const char* clipboard = g.IO.GetClipboardTextFn())
{ {
// Remove new-line from pasted buffer // Remove new-line from pasted buffer
int clipboard_len = strlen(clipboard); size_t clipboard_len = strlen(clipboard);
char* clipboard_filtered = (char*)malloc(clipboard_len+1); char* clipboard_filtered = (char*)malloc(clipboard_len+1);
int clipboard_filtered_len = 0; int clipboard_filtered_len = 0;
for (int i = 0; clipboard[i]; i++) for (int i = 0; clipboard[i]; i++)
@ -3832,7 +3836,6 @@ bool InputFloat3(const char* label, float v[3], int decimal_precision)
return false; return false;
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const ImVec2 text_size = CalcTextSize(label);
bool value_changed = false; bool value_changed = false;
@ -4083,8 +4086,6 @@ bool ColorEdit4(const char* label, float col[4], bool alpha)
const float w_full = window->DC.ItemWidth.back(); const float w_full = window->DC.ItemWidth.back();
const float square_sz = (window->FontSize() + style.FramePadding.x * 2.0f); const float square_sz = (window->FontSize() + style.FramePadding.x * 2.0f);
const ImVec2 text_size = CalcTextSize(label);
ImGuiColorEditMode edit_mode = window->DC.ColorEditMode; ImGuiColorEditMode edit_mode = window->DC.ColorEditMode;
if (edit_mode == ImGuiColorEditMode_UserSelect) if (edit_mode == ImGuiColorEditMode_UserSelect)
edit_mode = g.ColorEditModeStorage.GetInt(id, 0) % 3; edit_mode = g.ColorEditModeStorage.GetInt(id, 0) % 3;
@ -4206,15 +4207,12 @@ bool ColorEdit4(const char* label, float col[4], bool alpha)
void ColorEditMode(ImGuiColorEditMode mode) void ColorEditMode(ImGuiColorEditMode mode)
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
window->DC.ColorEditMode = mode; window->DC.ColorEditMode = mode;
} }
void Separator() void Separator()
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
if (window->Collapsed) if (window->Collapsed)
return; return;
@ -4240,7 +4238,6 @@ void Separator()
void Spacing() void Spacing()
{ {
ImGuiState& g = GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
if (window->Collapsed) if (window->Collapsed)
return; return;
@ -4581,7 +4578,7 @@ void ImDrawList::AddCommand(ImDrawCmdType cmd_type, int vtx_count)
// Merge commands if we can, turning them into less draw calls // Merge commands if we can, turning them into less draw calls
ImDrawCmd* prev = commands.empty() ? NULL : &commands.back(); ImDrawCmd* prev = commands.empty() ? NULL : &commands.back();
if (vtx_count > 0 && prev && prev->cmd_type == (ImU32)cmd_type && prev->vtx_count + vtx_count < VTX_COUNT_MAX) if (vtx_count > 0 && prev && prev->cmd_type == cmd_type && prev->vtx_count + vtx_count < VTX_COUNT_MAX)
prev->vtx_count += vtx_count; prev->vtx_count += vtx_count;
else else
commands.push_back(ImDrawCmd(cmd_type, vtx_count)); commands.push_back(ImDrawCmd(cmd_type, vtx_count));
@ -4799,14 +4796,14 @@ void ImDrawList::AddText(ImFont font, float font_size, const ImVec2& pos, ImU32
if (text_end == NULL) if (text_end == NULL)
text_end = text_begin + strlen(text_begin); text_end = text_begin + strlen(text_begin);
int char_count = text_end - text_begin; int char_count = (int)(text_end - text_begin);
int vtx_count_max = char_count * 6; int vtx_count_max = char_count * 6;
int vtx_begin = vtx_buffer.size(); size_t vtx_begin = vtx_buffer.size();
AddCommand(ImDrawCmdType_DrawTriangleList, vtx_count_max); AddCommand(ImDrawCmdType_DrawTriangleList, vtx_count_max);
font->RenderText(font_size, pos, col, clip_rect_stack_.back(), text_begin, text_end, vtx_write_); font->RenderText(font_size, pos, col, clip_rect_stack_.back(), text_begin, text_end, vtx_write_);
vtx_buffer.resize(vtx_write_ - &vtx_buffer.front()); vtx_buffer.resize(vtx_write_ - &vtx_buffer.front());
int vtx_count = vtx_buffer.size() - vtx_begin; int vtx_count = (int)(vtx_buffer.size() - vtx_begin);
commands.back().vtx_count -= (vtx_count_max - vtx_count); commands.back().vtx_count -= (vtx_count_max - vtx_count);
vtx_write_ -= (vtx_count_max - vtx_count); vtx_write_ -= (vtx_count_max - vtx_count);
@ -4858,7 +4855,7 @@ bool ImBitmapFont::LoadFromFile(const char* filename)
fclose(f); fclose(f);
return false; return false;
} }
if (fread(Data, 1, DataSize, f) != DataSize) if ((int)fread(Data, 1, DataSize, f) != DataSize)
{ {
fclose(f); fclose(f);
free(Data); free(Data);
@ -4919,7 +4916,7 @@ bool ImBitmapFont::LoadFromMemory(const void* data, int data_size)
void ImBitmapFont::BuildLookupTable() void ImBitmapFont::BuildLookupTable()
{ {
ImU32 max_c = 0; ImU32 max_c = 0;
for (int i = 0; i != GlyphsCount; i++) for (size_t i = 0; i != GlyphsCount; i++)
if (max_c < Glyphs[i].Id) if (max_c < Glyphs[i].Id)
max_c = Glyphs[i].Id; max_c = Glyphs[i].Id;
@ -4969,7 +4966,8 @@ ImVec2 ImBitmapFont::CalcTextSize(float size, float max_width, const char* text_
if (const FntGlyph* glyph = FindGlyph((unsigned short)c)) if (const FntGlyph* glyph = FindGlyph((unsigned short)c))
{ {
const float char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale; const float char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale;
const float char_extend = (glyph->XOffset + glyph->Width * scale); //const float char_extend = (glyph->XOffset + glyph->Width * scale);
if (line_width + char_width >= max_width) if (line_width + char_width >= max_width)
break; break;
line_width += char_width; line_width += char_width;
@ -5011,8 +5009,6 @@ void ImBitmapFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& c
pos.x = (float)(int)pos.x + 0.5f; pos.x = (float)(int)pos.x + 0.5f;
pos.y = (float)(int)pos.y + 0.5f; pos.y = (float)(int)pos.y + 0.5f;
ImVec2 text_size = ImVec2(0,0);
float line_width = 0.0f;
const ImVec4 clip_rect = clip_rect_ref; const ImVec4 clip_rect = clip_rect_ref;
const float uv_offset = GImGui.IO.PixelCenterOffset; const float uv_offset = GImGui.IO.PixelCenterOffset;
@ -5032,7 +5028,7 @@ void ImBitmapFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& c
if (const FntGlyph* glyph = FindGlyph((unsigned short)c)) if (const FntGlyph* glyph = FindGlyph((unsigned short)c))
{ {
const float char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale; const float char_width = (glyph->XAdvance + Info->SpacingHoriz) * scale;
const float char_extend = (glyph->XOffset + glyph->Width * scale); //const float char_extend = (glyph->XOffset + glyph->Width * scale);
if (c != ' ' && c != '\n') if (c != ' ' && c != '\n')
{ {
@ -5150,7 +5146,7 @@ void ShowStyleEditor(ImGuiStyle* ref)
filter.Draw("Filter colors", 200); filter.Draw("Filter colors", 200);
ImGui::ColorEditMode(edit_mode); ImGui::ColorEditMode(edit_mode);
for (size_t i = 0; i < ImGuiCol_COUNT; i++) for (int i = 0; i < ImGuiCol_COUNT; i++)
{ {
const char* name = GetStyleColorName(i); const char* name = GetStyleColorName(i);
if (!filter.PassFilter(name)) if (!filter.PassFilter(name))
@ -5476,7 +5472,7 @@ void ShowTestWindow(bool* open)
static float foo = 1.0f; static float foo = 1.0f;
ImGui::InputFloat("red", &foo, 0.05f, 0, 3); ImGui::NextColumn(); ImGui::InputFloat("red", &foo, 0.05f, 0, 3); ImGui::NextColumn();
static float bar = 1.0f; static float bar = 1.0f;
ImGui::InputFloat("blue", &foo, 0.05f, 0, 3); ImGui::NextColumn(); ImGui::InputFloat("blue", &bar, 0.05f, 0, 3); ImGui::NextColumn();
ImGui::Columns(1); ImGui::Columns(1);
ImGui::Separator(); ImGui::Separator();
@ -5536,7 +5532,7 @@ void ShowTestWindow(bool* open)
if (ImGui::Button("Add 1000 lines")) if (ImGui::Button("Add 1000 lines"))
{ {
for (size_t i = 0; i < 1000; i++) for (size_t i = 0; i < 1000; i++)
log.Append("%i The quick brown fox jumps over the lazy dog\n", lines+i); log.append("%i The quick brown fox jumps over the lazy dog\n", lines+i);
lines += 1000; lines += 1000;
} }
ImGui::BeginChild("Log"); ImGui::BeginChild("Log");

15
imgui.h
View File

@ -29,8 +29,8 @@ typedef ImU32 ImGuiID;
typedef int ImGuiCol; // enum ImGuiCol_ typedef int ImGuiCol; // enum ImGuiCol_
typedef int ImGuiKey; // enum ImGuiKey_ typedef int ImGuiKey; // enum ImGuiKey_
typedef int ImGuiColorEditMode; // enum ImGuiColorEditMode_ typedef int ImGuiColorEditMode; // enum ImGuiColorEditMode_
typedef ImU32 ImGuiWindowFlags; // enum ImGuiWindowFlags_ typedef int ImGuiWindowFlags; // enum ImGuiWindowFlags_
typedef ImU32 ImGuiInputTextFlags; // enum ImGuiInputTextFlags_ typedef int ImGuiInputTextFlags; // enum ImGuiInputTextFlags_
typedef ImBitmapFont* ImFont; typedef ImBitmapFont* ImFont;
struct ImVec2 struct ImVec2
@ -143,6 +143,7 @@ namespace ImGui
void SetFontScale(float scale); void SetFontScale(float scale);
void SetScrollPosHere(); void SetScrollPosHere();
void SetTreeStateStorage(ImGuiStorage* tree); void SetTreeStateStorage(ImGuiStorage* tree);
ImGuiStorage* GetTreeStateStorage();
void PushItemWidth(float item_width); void PushItemWidth(float item_width);
void PopItemWidth(); void PopItemWidth();
float GetItemWidth(); float GetItemWidth();
@ -221,9 +222,9 @@ namespace ImGui
// Logging // Logging
void LogButtons(); void LogButtons();
void LogToTTY(int max_depth); void LogToTTY(int max_depth = -1);
void LogToFile(int max_depth, const char* filename); void LogToFile(int max_depth = -1, const char* filename = NULL);
void LogToClipboard(int max_depth); void LogToClipboard(int max_depth = -1);
// Utilities // Utilities
void SetTooltip(const char* fmt, ...); // set tooltip under mouse-cursor, typically use with ImGui::IsHovered(). (currently no contention handling, last call win) void SetTooltip(const char* fmt, ...); // set tooltip under mouse-cursor, typically use with ImGui::IsHovered(). (currently no contention handling, last call win)
@ -232,6 +233,8 @@ namespace ImGui
bool IsClipped(ImVec2 item_size); // to perform coarse clipping on user's side (as an optimisation) bool IsClipped(ImVec2 item_size); // to perform coarse clipping on user's side (as an optimisation)
bool IsKeyPressed(int key_index, bool repeat = true); // key_index into the keys_down[512] array, imgui doesn't know the semantic of each entry bool IsKeyPressed(int key_index, bool repeat = true); // key_index into the keys_down[512] array, imgui doesn't know the semantic of each entry
bool IsMouseClicked(int button, bool repeat = false); bool IsMouseClicked(int button, bool repeat = false);
bool IsMouseDoubleClicked(int button);
bool IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max);
ImVec2 GetMousePos(); ImVec2 GetMousePos();
float GetTime(); float GetTime();
int GetFrameCount(); int GetFrameCount();
@ -469,7 +472,7 @@ struct ImGuiTextBuffer
size_t size() const { return Buf.size()-1; } size_t size() const { return Buf.size()-1; }
bool empty() { return Buf.empty(); } bool empty() { return Buf.empty(); }
void clear() { Buf.clear(); Buf.push_back(0); } void clear() { Buf.clear(); Buf.push_back(0); }
void Append(const char* fmt, ...); void append(const char* fmt, ...);
}; };
// Helper: Key->value storage // Helper: Key->value storage