Skip to content

Commit

Permalink
addons/imguivariouscontrols/imguivariouscontrols.h/.cpp: Added a pair…
Browse files Browse the repository at this point in the history
… of new widgets.

addons/imguistyleserializer/imguistyleserializer.h/.cpp: Added DafaultDark style.
  • Loading branch information
Flix01 committed Oct 30, 2017
1 parent 939ede8 commit 038d1e4
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 84 deletions.
34 changes: 23 additions & 11 deletions addons/imguibindings/imguibindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1639,16 +1639,26 @@ void ImImpl_RenderDrawLists(ImDrawData* draw_data)
#endif// IMGUI_USE_DIRECT3D9_BINDING

void ImImpl_NewFramePaused() {
// We use this method when ImGui is paused (= not displayed),
// so that we can still process input using ImGui calls

ImGuiContext& g = *GImGui;
g.Time += g.IO.DeltaTime;

// Update inputs state
if (g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0)
g.IO.MousePos = ImVec2(-9999.0f, -9999.0f);
// Update keyboard input state
memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));
for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f;

// Update mouse inputs state
// If mouse just appeared or disappeared (usually denoted by -FLT_MAX component, but in reality we test for -256000.0f) we cancel out movement in MouseDelta
if (ImGui::IsMousePosValid(&g.IO.MousePos) && ImGui::IsMousePosValid(&g.IO.MousePosPrev)) g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
else g.IO.MouseDelta = ImVec2(0.0f, 0.0f);
/* Old code:
if (g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0) g.IO.MousePos = ImVec2(-9999.0f, -9999.0f);
if ((g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0) || (g.IO.MousePosPrev.x < 0 && g.IO.MousePosPrev.y < 0)) // if mouse just appeared or disappeared (negative coordinate) we cancel out movement in MouseDelta
g.IO.MouseDelta = ImVec2(0.0f, 0.0f);
else
g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
else g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;*/
g.IO.MousePosPrev = g.IO.MousePos;
for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)
{
Expand All @@ -1670,16 +1680,17 @@ void ImImpl_NewFramePaused() {
g.IO.MouseClickedTime[i] = g.Time;
}
g.IO.MouseClickedPos[i] = g.IO.MousePos;
g.IO.MouseDragMaxDistanceAbs[i] = ImVec2(0.0f, 0.0f);
g.IO.MouseDragMaxDistanceSqr[i] = 0.0f;
}
else if (g.IO.MouseDown[i])
{
g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i]));
ImVec2 mouse_delta = g.IO.MousePos - g.IO.MouseClickedPos[i];
g.IO.MouseDragMaxDistanceAbs[i].x = ImMax(g.IO.MouseDragMaxDistanceAbs[i].x, mouse_delta.x < 0.0f ? -mouse_delta.x : mouse_delta.x);
g.IO.MouseDragMaxDistanceAbs[i].y = ImMax(g.IO.MouseDragMaxDistanceAbs[i].y, mouse_delta.y < 0.0f ? -mouse_delta.y : mouse_delta.y);
g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(mouse_delta));
}
}
memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));
for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f;

// Calculate frame-rate for the user, as a purely luxurious feature
g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx];
Expand All @@ -1689,11 +1700,12 @@ void ImImpl_NewFramePaused() {

g.IO.WantCaptureKeyboard = g.IO.WantCaptureMouse = g.IO.WantTextInput = false;

// New:
g.IO.WantTextInput = 0;
g.WantCaptureMouseNextFrame = g.WantCaptureKeyboardNextFrame = g.WantTextInputNextFrame = -1;


// New
g.MovingWindow = NULL;
g.MovingWindowMoveId = 0;
}


Expand Down
47 changes: 20 additions & 27 deletions addons/imguipanelmanager/imguipanelmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,13 @@ static bool DockWindowBegin(const char* name, bool* p_opened,bool* p_undocked, c
if (!(flags & ImGuiWindowFlags_NoSavedSettings))
MarkIniSettingsDirty(window);
}
IM_ASSERT(g.MovedWindow != NULL);
FocusWindow(g.MovedWindow);
IM_ASSERT(g.MovingWindow != NULL);
FocusWindow(g.MovingWindow);
}
else
{
ClearActiveID();
g.MovedWindow = NULL; // Not strictly necessary but doing it for sanity.
g.MovingWindow = NULL; // Not strictly necessary but doing it for sanity.
}
}

Expand Down Expand Up @@ -703,22 +703,6 @@ static bool DockWindowBegin(const char* name, bool* p_opened,bool* p_undocked, c
if (!(flags & ImGuiWindowFlags_NoCollapse))
RenderTriangle(window->Pos + style.FramePadding, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f);


/*
ImVec2 text_min = window->Pos + style.FramePadding;
//ImVec2 text_max = window->Pos + ImVec2(window->Size.x - style.FramePadding.x, style.FramePadding.y*2 + text_size.y);
//ImVec2 clip_max = ImVec2(window->Pos.x + window->Size.x - (p_opened ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x), text_max.y); // Match the size of CloseWindowButton()
ImVec2 text_max = window->Pos + ImVec2(window->Size.x - (p_opened ? (title_bar_rect.GetHeight()-3) : style.FramePadding.x) - (p_undocked ? (title_bar_rect.GetHeight()-3) : style.FramePadding.x), style.FramePadding.y*2 + text_size.y);
ImVec2 clip_max = ImVec2(window->Pos.x + window->Size.x - (p_opened ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x) - (p_undocked ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x), text_max.y); // Match the size of CloseWindowButton()
bool pad_left = (flags & ImGuiWindowFlags_NoCollapse) == 0;
bool pad_right = (p_opened != NULL);
if (style.WindowTitleAlign & ImGuiAlign_Center) pad_right = pad_left;
if (pad_left) text_min.x += g.FontSize + style.ItemInnerSpacing.x;
if (pad_right) text_max.x -= g.FontSize + style.ItemInnerSpacing.x;
RenderTextClipped(text_min, text_max, name, NULL, &text_size, style.WindowTitleAlign, NULL, &clip_max);
*/

ImVec2 text_min = window->Pos;
ImVec2 text_max = window->Pos + ImVec2(window->Size.x, style.FramePadding.y*2 + text_size.y);
ImRect clip_rect;
Expand Down Expand Up @@ -747,25 +731,34 @@ static bool DockWindowBegin(const char* name, bool* p_opened,bool* p_undocked, c
if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C))
ImGui::LogToClipboard();
*/

// Inner rectangle
// We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame
// Note that if our window is collapsed we will end up with a null clipping rectangle which is the correct behavior.
window->InnerRect.Min.x = title_bar_rect.Min.x;
window->InnerRect.Min.y = title_bar_rect.Max.y + window->MenuBarHeight();
window->InnerRect.Max.x = window->Pos.x + window->Size.x - window->ScrollbarSizes.x;
window->InnerRect.Max.y = window->Pos.y + window->Size.y - window->ScrollbarSizes.y;
//window->DrawList->AddRect(window->InnerRect.Min, window->InnerRect.Max, IM_COL32_WHITE);

}

// Inner clipping rectangle
// We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame
// Note that if our window is collapsed we will end up with a null clipping rectangle which is the correct behavior.
const ImRect title_bar_rect = window->TitleBarRect();
// Force round operator last to ensure that e.g. (int)(max.x-min.x) in user's render code produce correct result.
const float border_size = ((flags & ImGuiWindowFlags_ShowBorders) || gImGuiDockPanelManagerAlwaysDrawExternalBorders) ? 1.0f : 0.0f;
ImRect clip_rect;
clip_rect.Min.x = title_bar_rect.Min.x + 0.5f + ImMax(border_size, window->WindowPadding.x*0.5f);
clip_rect.Min.y = title_bar_rect.Max.y + window->MenuBarHeight() + 0.5f + border_size;
clip_rect.Max.x = window->Pos.x + window->Size.x - window->ScrollbarSizes.x - ImMax(border_size, window->WindowPadding.x*0.5f);
clip_rect.Max.y = window->Pos.y + window->Size.y - border_size - window->ScrollbarSizes.y;

clip_rect.Min.x = ImFloor(0.5f + window->InnerRect.Min.x + ImMax(border_size, ImFloor(window->WindowPadding.x*0.5f)));
clip_rect.Min.y = ImFloor(0.5f + window->InnerRect.Min.y + border_size);
clip_rect.Max.x = ImFloor(0.5f + window->InnerRect.Max.x - ImMax(border_size, ImFloor(window->WindowPadding.x*0.5f)));
clip_rect.Max.y = ImFloor(0.5f + window->InnerRect.Max.y - border_size);
PushClipRect(clip_rect.Min, clip_rect.Max, true);

// Clear 'accessed' flag last thing
if (first_begin_of_the_frame)
window->Accessed = false;

window->BeginCount++;
g.SetNextWindowSizeConstraint = false;

// Child window can be out of sight and have "negative" clip windows.
// Mark them as collapsed so commands are skipped earlier (we can't manually collapse because they have no title bar).
Expand Down
5 changes: 4 additions & 1 deletion addons/imguistyleserializer/imguistyleserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ bool ResetStyle(int styleEnum,ImGuiStyle& style) {
if (styleEnum<0 || styleEnum>=ImGuiStyle_Count) return false;
style = ImGuiStyle();
switch (styleEnum) {
case ImGuiStyle_DefaultDark:
ImGui::StyleColorsDark(&style);
break;
case ImGuiStyle_DefaultInverse:
InvertStyleColors(style);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.79f, 0.76f, 0.725f, 0.875f);
Expand Down Expand Up @@ -928,7 +931,7 @@ bool ResetStyle(int styleEnum,ImGuiStyle& style) {

return true;
}
static const char* DefaultStyleNames[ImGuiStyle_Count]={"Default","Gray","Light","OSX","OSXOpaque","DarkOpaque","Soft","EdinBlack","EdinWhite","Maya","DefaultInverse","OSXInverse","OSXOpaqueInverse","DarkOpaqueInverse"};
static const char* DefaultStyleNames[ImGuiStyle_Count]={"Default","DefaultDark","Gray","Light","OSX","OSXOpaque","DarkOpaque","Soft","EdinBlack","EdinWhite","Maya","DefaultInverse","OSXInverse","OSXOpaqueInverse","DarkOpaqueInverse"};
const char** GetDefaultStyleNames() {return &DefaultStyleNames[0];}

} // namespace ImGui
Expand Down
1 change: 1 addition & 0 deletions addons/imguistyleserializer/imguistyleserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

enum ImGuiStyleEnum {
ImGuiStyle_Default=0,
ImGuiStyle_DefaultDark,
ImGuiStyle_Gray, // This is the default theme of my main.cpp demo.
ImGuiStyle_Light,
ImGuiStyle_OSX, // Posted by @itamago here: https://github.com/ocornut/imgui/pull/511 (hope I can use it)
Expand Down
158 changes: 153 additions & 5 deletions addons/imguivariouscontrols/imguivariouscontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3953,10 +3953,9 @@ bool PasswordDrawer(char *password, int passwordSize,ImGuiPasswordDrawerFlags fl
unsigned int CheckboxFlags(const char* label,unsigned int* flags,int numFlags,int numRows,int numColumns,unsigned int flagAnnotations,int* itemHoveredOut,const unsigned int* pFlagsValues)
{
unsigned int itemPressed = 0;
if (itemHoveredOut) *itemHoveredOut=0;
if (itemHoveredOut) *itemHoveredOut=-1;
if (numRows*numColumns*numFlags==0) return itemPressed;


ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems) return itemPressed;

Expand All @@ -3968,7 +3967,6 @@ unsigned int CheckboxFlags(const char* label,unsigned int* flags,int numFlags,in
int numItemsPerGroup = numItemsPerRow/numColumns;if (numItemsPerRow%numColumns) ++numItemsPerGroup;
numItemsPerRow = numItemsPerGroup * numColumns;


ImGui::BeginGroup();
const ImVec2 startCurPos = window->DC.CursorPos;
ImVec2 curPos = startCurPos, maxPos = startCurPos;
Expand Down Expand Up @@ -4005,6 +4003,7 @@ unsigned int CheckboxFlags(const char* label,unsigned int* flags,int numFlags,in
hovered = ItemHoverable(bb, itemID);pressed = buttonPressed && hovered; // Way faster
if (pressed) {
v = !v;
if (!ImGui::GetIO().KeyShift) *flags=0;
if (v) *flags |= j;
else *flags &= ~j;
itemPressed = j;
Expand Down Expand Up @@ -4056,12 +4055,161 @@ unsigned int CheckboxFlags(const char* label,unsigned int* flags,int numFlags,in
ImGuiID itemID = window->GetID(label);
ItemSize(text_bb, style.FramePadding.y*numRows);
if (ItemAdd(text_bb, itemID)) RenderText(text_bb.Min, label);


}

return itemPressed;
}
// End CheckboxFlags =================================================================================================

// Start CheckBoxStyled ==============================================================================================
bool CheckboxStyled(const char* label, bool* v)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;

ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const ImVec2 label_size = CalcTextSize(label, NULL, true);
const ImVec2 check_size(label_size.y*2.5f,label_size.y);

const ImRect check_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(check_size.x + style.FramePadding.x*2, check_size.y + style.FramePadding.y*2)); // We want a square shape to we use Y twice
ItemSize(check_bb, style.FramePadding.y);

ImRect total_bb = check_bb;
if (label_size.x > 0) SameLine(0, style.ItemInnerSpacing.x);
const ImRect text_bb(window->DC.CursorPos + ImVec2(0,style.FramePadding.y), window->DC.CursorPos + ImVec2(0,style.FramePadding.y) + label_size);
if (label_size.x > 0)
{
ItemSize(ImVec2(text_bb.GetWidth(), check_bb.GetHeight()), style.FramePadding.y);
total_bb = ImRect(ImMin(check_bb.Min, text_bb.Min), ImMax(check_bb.Max, text_bb.Max));
}

if (!ItemAdd(total_bb, id))
return false;

bool hovered, held;
bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
static float timeBegin = -1.f;
static ImGuiID timeID = 0;
if (pressed) {
*v = !(*v);
timeID = id;
timeBegin = ImGui::GetTime();
}

// Widget Look Here ================================================================
float t = 0.f; // In (0,1) 0 = OFF 1 = ON
if (timeID==id) {
const float actionTime = 0.25f;
float elapsedTime = ImGui::GetTime()-timeBegin;
if (elapsedTime>actionTime) {timeBegin=-1;timeID=0;}
else t = 1.f-elapsedTime/actionTime;
}
if (*v) t = 1.f-t;
if (t<0) t=0;
else if (t>1) t=1;
const float check_bb_height = check_bb.GetHeight();
const float innerFrameHeight = check_bb_height*0.5f;
const float heightDelta = (check_bb_height-innerFrameHeight)*0.5f;
const float check_bb_width = check_bb.GetWidth();
float widthFraction = check_bb_width*t;
ImU32 baseColor = GetColorU32((held || hovered) ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
const float rounding = style.WindowRounding;//style.FrameRounding;
ImRect innerFrame0(ImVec2(check_bb.Min.x,check_bb.Min.y+heightDelta),ImVec2(check_bb.Min.x+widthFraction,check_bb.Max.y-heightDelta));
ImRect innerFrame1(ImVec2(check_bb.Min.x+widthFraction,check_bb.Min.y+heightDelta),ImVec2(check_bb.Max.x,check_bb.Max.y-heightDelta));
if (t>0) {
ImU32 fillColor0 = GetColorU32((held || hovered) ? ImGuiCol_CloseButtonHovered : ImGuiCol_CloseButton);
window->DrawList->AddRectFilled(innerFrame0.Min, innerFrame0.Max, fillColor0, rounding, t<1 ? 9 : 15);
}
if (t<1) {
ImU32 fillColor1 = baseColor;//GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
window->DrawList->AddRectFilled(innerFrame1.Min, innerFrame1.Max, fillColor1, rounding, t>0 ? 6 : 15);
}
if (window->Flags & ImGuiWindowFlags_ShowBorders) {
ImRect innerFrame(innerFrame0.Min,innerFrame1.Max);
window->DrawList->AddRect(innerFrame.Min+ImVec2(1,1), innerFrame.Max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding);
window->DrawList->AddRect(innerFrame.Min, innerFrame.Max, GetColorU32(ImGuiCol_Border), rounding);
}
// Ideally we need an opaque color here:
ImU32 circleColor = GetColorU32((held || hovered) ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
// GetColorU32(ImGuiCol_CheckMark);
int numSegments = (int)(check_bb_height*0.8f);if (numSegments<3) numSegments=3;else if (numSegments>24) numSegments = 24;
float radius = check_bb_height*0.5f;
if (widthFraction<radius) widthFraction=radius;
else if (widthFraction>check_bb_width-radius) widthFraction=check_bb_width-radius;
ImVec2 center(check_bb.Min.x+widthFraction,check_bb.Min.y+check_bb_height*0.5f);
window->DrawList->AddCircleFilled(center,radius,circleColor,numSegments);
// ==================================================================================

//if (g.LogEnabled) LogRenderedText(&text_bb.Min, *v ? "[x]" : "[ ]");
if (label_size.x > 0.0f) RenderText(text_bb.Min, label);

return pressed;
}
bool CheckboxStyledFlags(const char* label, unsigned int* flags, unsigned int flags_value) {
bool v = ((*flags & flags_value) == flags_value);
bool pressed = CheckboxStyled(label, &v);
if (pressed) {
if (v) *flags |= flags_value;
else *flags &= ~flags_value;
}
return pressed;
}
// End CheckBoxStyled ================================================================================================

// KnobFloat Starts Here =============================================================================================
bool KnobFloat(const char* label, float* p_value, float v_min, float v_max,float v_step) {
//@ocornut https://github.com/ocornut/imgui/issues/942
ImGuiIO& io = ImGui::GetIO();
ImGuiStyle& style = ImGui::GetStyle();

float radius_outer = 20.0f;
ImVec2 pos = ImGui::GetCursorScreenPos();
ImVec2 center = ImVec2(pos.x + radius_outer, pos.y + radius_outer);
float line_height = ImGui::GetTextLineHeight();
ImDrawList* draw_list = ImGui::GetWindowDrawList();

float ANGLE_MIN = 3.141592f * 0.75f;
float ANGLE_MAX = 3.141592f * 2.25f;

ImGui::InvisibleButton(label, ImVec2(radius_outer*2, radius_outer*2 + line_height + style.ItemInnerSpacing.y));
bool value_changed = false;
bool is_active = ImGui::IsItemActive();
bool is_hovered = ImGui::IsItemHovered();
if (is_active && io.MouseDelta.x != 0.0f) {
if (v_step<=0) v_step=50.f;
float step = (v_max - v_min) / v_step;
*p_value += io.MouseDelta.x * step;

if (*p_value < v_min) *p_value = v_min;
if (*p_value > v_max) *p_value = v_max;
value_changed = true;
}
else if (is_hovered && (io.MouseDoubleClicked[0] || io.MouseClicked[2])) {
*p_value = (v_max + v_min) * 0.5f; // reset value
value_changed = true;
}

float t = (*p_value - v_min) / (v_max - v_min);
float angle = ANGLE_MIN + (ANGLE_MAX - ANGLE_MIN) * t;
float angle_cos = cosf(angle), angle_sin = sinf(angle);
float radius_inner = radius_outer*0.40f;
draw_list->AddCircleFilled(center, radius_outer, ImGui::GetColorU32(ImGuiCol_FrameBg), 16);
draw_list->AddLine(ImVec2(center.x + angle_cos*radius_inner, center.y + angle_sin*radius_inner), ImVec2(center.x + angle_cos*(radius_outer-2), center.y + angle_sin*(radius_outer-2)), ImGui::GetColorU32(ImGuiCol_SliderGrabActive), 2.0f);
draw_list->AddCircleFilled(center, radius_inner, ImGui::GetColorU32(is_active ? ImGuiCol_FrameBgActive : is_hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16);
draw_list->AddText(ImVec2(pos.x, pos.y + radius_outer * 2 + style.ItemInnerSpacing.y), ImGui::GetColorU32(ImGuiCol_Text), label);

if (is_active || is_hovered) {
ImGui::SetNextWindowPos(ImVec2(pos.x - style.WindowPadding.x, pos.y - line_height - style.ItemInnerSpacing.y - style.WindowPadding.y));
ImGui::BeginTooltip();
ImGui::Text("%.3f", *p_value);
ImGui::EndTooltip();
}

return value_changed;
}
// ===================================================================================================================

} // namespace ImGui
Loading

0 comments on commit 038d1e4

Please sign in to comment.