Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. in imgui.cpp
  2.  
  3. IMGUI_API bool ImGui::MyColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags) {
  4. ImGuiWindow* window = GetCurrentWindow();
  5. if (window->SkipItems)
  6. return false;
  7.  
  8. ImGuiContext& g = *GImGui;
  9. const ImGuiStyle& style = g.Style;
  10. const ImGuiID id = window->GetID(label);
  11. const float w_full = CalcItemWidth();
  12. const float square_sz_with_spacing = (flags & ImGuiColorEditFlags_NoColorSquare) ? 0.0f : (g.FontSize + style.FramePadding.y * 2.0f + style.ItemInnerSpacing.x);
  13.  
  14. // If no mode is specified, defaults to RGB
  15. if (!(flags & ImGuiColorEditFlags_ModeMask_))
  16. flags |= ImGuiColorEditFlags_RGB;
  17.  
  18. // If we're not showing any slider there's no point in querying color mode, nor showing the options menu, nor doing any HSV conversions
  19. if (flags & ImGuiColorEditFlags_NoSliders)
  20. flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_NoOptions;
  21.  
  22. // Read back edit mode from persistent storage
  23. if (!(flags & ImGuiColorEditFlags_NoOptions))
  24. flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | (g.ColorEditModeStorage.GetInt(id, (flags & ImGuiColorEditFlags_ModeMask_)) & ImGuiColorEditFlags_ModeMask_);
  25.  
  26. // Check that exactly one of RGB/HSV/HEX is set
  27. //IM_ASSERT(ImIsPowerOfTwo((int)(flags & ImGuiColorEditFlags_ModeMask_))); //
  28.  
  29. float f[4] = { col[0], col[1], col[2], col[3] };
  30. if (flags & ImGuiColorEditFlags_HSV)
  31. ImGui::ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
  32.  
  33. int i[4] = { IM_F32_TO_INT8(f[0]), IM_F32_TO_INT8(f[1]), IM_F32_TO_INT8(f[2]), IM_F32_TO_INT8(f[3]) };
  34.  
  35. bool alpha = (flags & ImGuiColorEditFlags_Alpha) != 0;
  36. bool value_changed = false;
  37. int components = alpha ? 4 : 3;
  38.  
  39. ImGui::BeginGroup();
  40. ImGui::PushID(label);
  41.  
  42. if ((flags & (ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV)) != 0 && (flags & ImGuiColorEditFlags_NoSliders) == 0) {
  43. // RGB/HSV 0..255 Sliders
  44. const float w_items_all = w_full - square_sz_with_spacing;
  45. const float w_item_one = ImMax(1.0f, (float)(int)((w_items_all - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components));
  46. const float w_item_last = ImMax(1.0f, (float)(int)(w_items_all - (w_item_one + style.ItemInnerSpacing.x) * (components - 1)));
  47.  
  48. const bool hide_prefix = (w_item_one <= CalcTextSize("M:999").x);
  49. const char* ids[4] = { "##X", "##Y", "##Z", "##W" };
  50. const char* fmt_table[3][4] =
  51. {
  52. { "%3.0f", "%3.0f", "%3.0f", "%3.0f" }, // Short display
  53. { "R:%3.0f", "G:%3.0f", "B:%3.0f", "A:%3.0f" }, // Long display for RGBA
  54. { "H:%3.0f", "S:%3.0f", "V:%3.0f", "A:%3.0f" } // Long display for HSVV
  55. };
  56. const char** fmt = hide_prefix ? fmt_table[0] : (flags & ImGuiColorEditFlags_HSV) ? fmt_table[2] : fmt_table[1];
  57.  
  58. ImGui::PushItemWidth(w_item_one);
  59. for (int n = 0; n < components; n++) {
  60. if (n > 0)
  61. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  62. if (n + 1 == components)
  63. ImGui::PushItemWidth(w_item_last);
  64. value_changed |= ImGui::DragInt(ids[n], &i[n], 1.0f, 0, 255, fmt[n]);
  65. }
  66. ImGui::PopItemWidth();
  67. ImGui::PopItemWidth();
  68. }
  69. else if ((flags & ImGuiColorEditFlags_HEX) != 0 && (flags & ImGuiColorEditFlags_NoSliders) == 0) {
  70. // RGB Hexadecimal Input
  71. const float w_slider_all = w_full - square_sz_with_spacing;
  72. char buf[64];
  73. if (alpha)
  74. ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", i[0], i[1], i[2], i[3]);
  75. else
  76. ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", i[0], i[1], i[2]);
  77. ImGui::PushItemWidth(w_slider_all);
  78. if (ImGui::InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase)) {
  79. value_changed |= true;
  80. char* p = buf;
  81. while (*p == '#' || ImCharIsSpace(*p))
  82. p++;
  83. i[0] = i[1] = i[2] = i[3] = 0;
  84. if (alpha)
  85. sscanf_s(p, "%02X%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]); // Treat at unsigned (%X is unsigned)
  86. else
  87. sscanf_s(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]);
  88. }
  89. ImGui::PopItemWidth();
  90. }
  91.  
  92. const char* label_display_end = FindTextDisplayEnd(label);
  93.  
  94. bool picker_active = false;
  95. if (!(flags & ImGuiColorEditFlags_NoColorSquare)) {
  96. if (!(flags & ImGuiColorEditFlags_NoSliders))
  97. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  98.  
  99. const ImVec4 col_display(col[0], col[1], col[2], 1.0f);
  100. if (ImGui::ColorButton(col_display)) {
  101. if (!(flags & ImGuiColorEditFlags_NoPicker)) {
  102. ImGui::OpenPopup("picker");
  103. ImGui::SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1, style.ItemSpacing.y));
  104. }
  105. }
  106. else if (!(flags & ImGuiColorEditFlags_NoOptions) && ImGui::IsItemHovered() && ImGui::IsMouseClicked(1)) {
  107. ImGui::OpenPopup("context");
  108. }
  109.  
  110. if (ImGui::BeginPopup("picker")) {
  111. picker_active = true;
  112. if (label != label_display_end)
  113. ImGui::TextUnformatted(label, label_display_end);
  114. ImGui::PushItemWidth(256.0f + (alpha ? 2 : 1) * (style.ItemInnerSpacing.x));
  115. value_changed |= ImGui::MyColorPicker4("##picker", col, (flags & ImGuiColorEditFlags_Alpha) | (ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV | ImGuiColorEditFlags_HEX));
  116. ImGui::PopItemWidth();
  117. ImGui::EndPopup();
  118. }
  119. if (!(flags & ImGuiColorEditFlags_NoOptions) && ImGui::BeginPopup("context")) {
  120. // FIXME-LOCALIZATION
  121. if (ImGui::MenuItem("Edit as RGB", NULL, (flags & ImGuiColorEditFlags_RGB) ? 1 : 0)) g.ColorEditModeStorage.SetInt(id, (int)(ImGuiColorEditFlags_RGB));
  122. if (ImGui::MenuItem("Edit as HSV", NULL, (flags & ImGuiColorEditFlags_HSV) ? 1 : 0)) g.ColorEditModeStorage.SetInt(id, (int)(ImGuiColorEditFlags_HSV));
  123. if (ImGui::MenuItem("Edit as Hexadecimal", NULL, (flags & ImGuiColorEditFlags_HEX) ? 1 : 0)) g.ColorEditModeStorage.SetInt(id, (int)(ImGuiColorEditFlags_HEX));
  124. ImGui::EndPopup();
  125. }
  126.  
  127. // Recreate our own tooltip over's ColorButton() one because we want to display correct alpha here
  128. //if (ImGui::IsItemHovered())
  129. // ImGui::SetTooltip("Color:\n(%.2f,%.2f,%.2f,%.2f)\n#%02X%02X%02X%02X", col[0], col[1], col[2], col[3], IM_F32_TO_INT8(col[0]), IM_F32_TO_INT8(col[1]), IM_F32_TO_INT8(col[2]), IM_F32_TO_INT8(col[3]));
  130. //ImGui::SetTooltip("");
  131. }
  132.  
  133. if (label != label_display_end) {
  134. ImGui::SameLine(0, style.ItemInnerSpacing.x);
  135. ImGui::TextUnformatted(label, label_display_end);
  136. }
  137.  
  138. // Convert back
  139. if (!picker_active) {
  140. for (int n = 0; n < 4; n++)
  141. f[n] = i[n] / 255.0f;
  142. if (flags & ImGuiColorEditFlags_HSV)
  143. ImGui::ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
  144. if (value_changed) {
  145. col[0] = f[0];
  146. col[1] = f[1];
  147. col[2] = f[2];
  148. if (alpha)
  149. col[3] = f[3];
  150. }
  151. }
  152.  
  153. ImGui::PopID();
  154. ImGui::EndGroup();
  155.  
  156. return value_changed;
  157. }
  158.  
  159.  
  160.  
  161. IMGUI_API bool ImGui::MyColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags) {
  162. float col4[4] = { col[0], col[1], col[2], 1.0f };
  163. if (!MyColorEdit4(label, col4, flags & ~ImGuiColorEditFlags_Alpha))
  164. return false;
  165. col[0] = col4[0]; col[1] = col4[1]; col[2] = col4[2];
  166. return true;
  167. }
  168.  
  169.  
  170. in imgui.h
  171.  
  172.  
  173.  
  174. IMGUI_API bool MyColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); // click on colored squared to open a color picker, right-click for options
  175. IMGUI_API bool MyColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0x01); // 0x01 = ImGuiColorEditFlags_Alpha = very dodgily backward compatible with 'bool show_alpha=true'
  176.  
  177.  
  178.  
  179. for example on client.cpp
  180. ImGui::MyColorEdit3(u8"ЕСП КТ", Settings::Esp::Visuals_Color_CT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement