Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. void zgui::colorpicker(const char* id, color& item) noexcept
  2. {
  3. std::vector<std::string> id_split = split_str(id, '#');
  4.  
  5. const int control_height = 8;
  6. const int control_width = 8;
  7.  
  8. const int slider_height = 10;
  9. const int slider_width = 152.5f;
  10.  
  11. const vec2 cursor_pos = pop_cursor_pos();
  12. const vec2 draw_pos{ context.window.position.x + cursor_pos.x + 175, context.window.position.y + cursor_pos.y - 16 };
  13.  
  14. color rainbow;
  15. color grey;
  16.  
  17. int text_wide, text_tall;
  18. functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
  19.  
  20. int CtrXoffset = 0;
  21. /*float yoffset = draw_pos.y + 10;
  22. float slideryoffset = draw_pos.y;
  23. float xoffset = draw_pos.x + 300;
  24. float sliderxoffset = draw_pos.x + 400; */
  25. const float x_offset = 75;
  26. const float y_offset = 0;
  27. float yoffset = draw_pos.y + y_offset;
  28. float slideryoffset = draw_pos.y + 173;
  29. float xoffset = draw_pos.x + x_offset;
  30. float sliderxoffset = draw_pos.x + (x_offset / 2) - 3.5f;
  31.  
  32. const int min = 0;
  33. const int max = 255;
  34.  
  35. const bool active = context.window.blocking == hash(id);
  36.  
  37. std::string itemstring = std::to_string(item.r);
  38. itemstring += std::to_string(item.g);
  39. itemstring += std::to_string(item.b);
  40. itemstring += std::to_string(item.a);
  41.  
  42. if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width + 6 + text_wide, control_height); !active && hovered && key_pressed(VK_RBUTTON)) {
  43. rightclicked = true;
  44. }
  45. else {
  46. rightclicked = false;
  47. }
  48.  
  49. if (rightclicked)
  50. {
  51. sendToClipboard(hwndclip, itemstring);
  52. }
  53.  
  54. if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width * 2, control_height); !active && hovered && key_pressed(VK_LBUTTON))
  55. {
  56. context.window.blocking = hash(id);
  57. }
  58. else if (active)
  59. {
  60. if (mouse_in_region(draw_pos.x, draw_pos.y, 2 * control_width, control_height) && key_pressed(VK_LBUTTON))
  61. context.window.blocking = 0;
  62.  
  63. if (mouse_in_region(draw_pos.x + 20, draw_pos.y + 5, 200, 180) && key_pressed(VK_LBUTTON))
  64. context.window.blocking = hash(id);
  65.  
  66. if (!mouse_in_region(draw_pos.x + 15, draw_pos.y + 5, 200, 190) && key_pressed(VK_LBUTTON))
  67. context.window.blocking = 0;
  68.  
  69. for (int i = 0; i < 990; i++)
  70. {
  71. if (mouse_in_region(xoffset + 100, yoffset, 5, 5))
  72. {
  73. if (key_down(VK_LBUTTON)) {
  74. item.r = rainbow.r;
  75. item.g = rainbow.g;
  76. item.b = rainbow.b;
  77. }
  78. //if (key_released(VK_LBUTTON))
  79. // context.window.blocking = 0;
  80. }
  81.  
  82. if (xoffset >= draw_pos.x + x_offset)
  83. {
  84. xoffset -= 150;
  85. yoffset += 5;
  86. }
  87.  
  88. float hue = (i * .001f);
  89.  
  90. rainbow.FromHSV(hue, 1.f, 1.f);
  91.  
  92. context.window.render.emplace_back(zgui_control_render_t{ { xoffset + 110, yoffset, }, zgui_render_type::zgui_filled_rect,color { rainbow.r, rainbow.g, rainbow.b, global_colors.control_outline.a },"", { 5,5 } });
  93.  
  94. if (GetAsyncKeyState(VK_LBUTTON) && mouse_in_region(xoffset + 125, yoffset, 5, 5))
  95. {
  96. item.r = rainbow.r;
  97. item.g = rainbow.g;
  98. item.b = rainbow.b;
  99. }
  100.  
  101. xoffset += 5;
  102.  
  103. }
  104.  
  105. for (int i = 0; i < 40; i++)
  106. {
  107. if (mouse_in_region(draw_pos.x + 20, (yoffset + 5) - 170, 10, 4))
  108. {
  109. if (key_down(VK_LBUTTON)) {
  110. item.r = grey.r;
  111. item.g = grey.g;
  112. item.b = grey.b;
  113. }
  114. //if (key_released(VK_LBUTTON))
  115. // context.window.blocking = 0;
  116. }
  117.  
  118. if (xoffset >= draw_pos.x + x_offset)
  119. {
  120. xoffset -= 4;
  121. yoffset += 4;
  122. }
  123.  
  124. float hue = (i * .025f);
  125.  
  126. grey.FromHSV(0.f, 0.f, hue);
  127.  
  128. context.window.render.emplace_back(zgui_control_render_t{ { draw_pos.x + 20, (yoffset + 5) - 170, }, zgui_render_type::zgui_filled_rect,color { grey.r, grey.g, grey.b, global_colors.control_outline.a },"", { 10,4 } });
  129.  
  130. if (GetAsyncKeyState(VK_LBUTTON) && mouse_in_region(draw_pos.x + 20, (yoffset + 5) - 170, 10, 4))
  131. {
  132. item.r = grey.r;
  133. item.g = grey.g;
  134. item.b = grey.b;
  135. }
  136.  
  137. xoffset += 4;
  138.  
  139. }
  140.  
  141. if (context.window.blocking == 0 && mouse_in_region(draw_pos.x - (control_height - 2), draw_pos.y, 8, 10) && key_pressed(VK_LBUTTON))
  142. item.a = std::clamp(item.a - 1, min, max);
  143. else if (context.window.blocking == 0 && mouse_in_region(draw_pos.x + control_width, draw_pos.y, 8, 10) && key_pressed(VK_LBUTTON))
  144. item.a = std::clamp(item.a + 1, min, max);
  145.  
  146. if (key_down(VK_LBUTTON) && mouse_in_region(sliderxoffset, slideryoffset, slider_width + 5, slider_height) && context.window.blocking == hash(id)) {
  147. context.window.blocking = hash(id);
  148. float value_unmapped = std::clamp(mouse_pos.x - sliderxoffset, 0.0f, static_cast<float>(slider_width));
  149. int value_mapped = static_cast<int>(value_unmapped / slider_width * (max - min) + min);
  150. item.a = value_mapped;
  151. }
  152.  
  153. const int dynamic_width = (static_cast<float>(item.a) - min) / (max - min) * slider_width - 2;
  154.  
  155. int text_x = dynamic_width - text_wide;
  156.  
  157. if (text_x < 0)
  158. text_x = 0;
  159.  
  160. rainbow.r = item.r;
  161. rainbow.g = item.g;
  162. rainbow.b = item.b;
  163. rainbow.a = item.a;
  164.  
  165. context.window.render.emplace_back(zgui_control_render_t{ { sliderxoffset + 1 , slideryoffset + 1 }, zgui_render_type::zgui_filled_rect, color{item.r,item.g,item.b,item.a}, "", { static_cast<float>(dynamic_width), control_height - 2 } });
  166. context.window.render.emplace_back(zgui_control_render_t{ { sliderxoffset + 1 , slideryoffset + 1 }, zgui_render_type::zgui_filled_rect, global_colors.control_idle,"", { slider_width - 2, control_height - 2 } });
  167. context.window.render.emplace_back(zgui_control_render_t{ { sliderxoffset , slideryoffset }, zgui_render_type::zgui_filled_rect, global_colors.control_outline,"", { slider_width, control_height } });
  168.  
  169. }
  170.  
  171. if (global_colors.control_outline.a >= 250)
  172. context.window.render.emplace_back(zgui_control_render_t{ { draw_pos.x, draw_pos.y }, zgui_render_type::zgui_filled_rect, color{item.r,item.g,item.b,item.a},"", { 2 * control_width, control_height } });
  173. else
  174. context.window.render.emplace_back(zgui_control_render_t{ { draw_pos.x, draw_pos.y }, zgui_render_type::zgui_filled_rect, color{item.r,item.g,item.b,global_colors.control_outline.a},"", { 2 * control_width, control_height } });
  175.  
  176. context.window.render.emplace_back(zgui_control_render_t{ { draw_pos.x - 1, draw_pos.y - 1 }, zgui_render_type::zgui_rect, color{0,0,0,global_colors.control_outline.a},"", { 2 * control_width + 2, control_height + 2 } });
  177.  
  178. push_cursor_pos(vec2{ cursor_pos.x + 14 + global_config.item_spacing, cursor_pos.y });
  179. push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y });
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement