libs/imgui/src/eu/imgui/ui.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eu/imgui/ui.h" | ||
| 2 | |||
| 3 | // #include "klotter/cint.h" | ||
| 4 | |||
| 5 | #include "eu/core/ui.h" | ||
| 6 | #include "eu/render/texture.h" | ||
| 7 | #include "eu/render/dependency_glad.h" | ||
| 8 | |||
| 9 | #include <iostream> | ||
| 10 | #include <numbers> | ||
| 11 | |||
| 12 | #include "dear_imgui/imgui.h" | ||
| 13 | |||
| 14 | namespace eu::imgui | ||
| 15 | { | ||
| 16 | |||
| 17 | // opengl code copied from the imgui opengl3 backend with minor modifications | ||
| 18 | // todo(Gustav): should we use the imgui backend code for "backend" rendering or use our own shader? | ||
| 19 | |||
| 20 | ✗ | static bool check_imgui_shader(GLuint handle, const char* desc) | |
| 21 | { | ||
| 22 | ✗ | GLint status = 0; | |
| 23 | ✗ | GLint log_length = 0; | |
| 24 | |||
| 25 | ✗ | glGetShaderiv(handle, GL_COMPILE_STATUS, &status); | |
| 26 | ✗ | glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); | |
| 27 | ✗ | if (status == GL_FALSE) | |
| 28 | { | ||
| 29 | ✗ | std::cerr << "ERROR: Shader code: failed to compile " << desc << "!\n"; | |
| 30 | } | ||
| 31 | |||
| 32 | ✗ | if (log_length > 1) | |
| 33 | { | ||
| 34 | ✗ | std::vector<GLchar> buf; | |
| 35 | ✗ | buf.resize(sizet_from_int(log_length + 1)); | |
| 36 | ✗ | glGetShaderInfoLog(handle, log_length, nullptr, buf.data()); | |
| 37 | ✗ | std::cerr << buf.data(); | |
| 38 | ✗ | } | |
| 39 | ✗ | return status == GL_TRUE; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | static bool imgui_check_program(GLuint handle, const char* desc) | |
| 43 | { | ||
| 44 | ✗ | GLint status = 0; | |
| 45 | ✗ | GLint log_length = 0; | |
| 46 | ✗ | glGetProgramiv(handle, GL_LINK_STATUS, &status); | |
| 47 | ✗ | glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); | |
| 48 | ✗ | if (status == GL_FALSE) | |
| 49 | { | ||
| 50 | ✗ | std::cerr << "ERROR: Shader program: failed to link " << desc << "!\n"; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | if (log_length > 1) | |
| 54 | { | ||
| 55 | ✗ | std::vector<GLchar> buf; | |
| 56 | ✗ | buf.resize(sizet_from_int(log_length + 1)); | |
| 57 | ✗ | glGetProgramInfoLog(handle, log_length, nullptr, buf.data()); | |
| 58 | ✗ | std::cerr << buf.data(); | |
| 59 | ✗ | } | |
| 60 | ✗ | return status == GL_TRUE; | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | void imgui_destroy_shader(ImguiShaderProgram* bd) | |
| 64 | { | ||
| 65 | ✗ | if (bd->program_handle == 0) | |
| 66 | { | ||
| 67 | ✗ | return; | |
| 68 | } | ||
| 69 | |||
| 70 | ✗ | glDeleteProgram(bd->program_handle); | |
| 71 | ✗ | bd->program_handle = 0; | |
| 72 | } | ||
| 73 | |||
| 74 | ✗ | ImguiShaderProgram imgui_load_shader(const char* glsl_version_string, const char* vertex_shader, const char* fragment_shader) | |
| 75 | { | ||
| 76 | // Create shaders | ||
| 77 | ✗ | const GLchar* vertex_shader_with_version[2] = {glsl_version_string, vertex_shader}; | |
| 78 | ✗ | const auto vert_handle = glCreateShader(GL_VERTEX_SHADER); | |
| 79 | ✗ | glShaderSource(vert_handle, 2, vertex_shader_with_version, nullptr); | |
| 80 | ✗ | glCompileShader(vert_handle); | |
| 81 | ✗ | if (! check_imgui_shader(vert_handle, "vertex shader")) | |
| 82 | { | ||
| 83 | ✗ | return {}; | |
| 84 | } | ||
| 85 | |||
| 86 | ✗ | const GLchar* fragment_shader_with_version[2] = {glsl_version_string, fragment_shader}; | |
| 87 | ✗ | const auto frag_handle = glCreateShader(GL_FRAGMENT_SHADER); | |
| 88 | ✗ | glShaderSource(frag_handle, 2, fragment_shader_with_version, nullptr); | |
| 89 | ✗ | glCompileShader(frag_handle); | |
| 90 | |||
| 91 | ✗ | if (! check_imgui_shader(frag_handle, "fragment shader")) | |
| 92 | { | ||
| 93 | ✗ | return {}; | |
| 94 | } | ||
| 95 | |||
| 96 | // Link | ||
| 97 | ✗ | ImguiShaderProgram prog; | |
| 98 | ✗ | prog.program_handle = glCreateProgram(); | |
| 99 | ✗ | glAttachShader(prog.program_handle, vert_handle); | |
| 100 | ✗ | glAttachShader(prog.program_handle, frag_handle); | |
| 101 | ✗ | glLinkProgram(prog.program_handle); | |
| 102 | ✗ | if (! imgui_check_program(prog.program_handle, "shader program")) | |
| 103 | { | ||
| 104 | ✗ | imgui_destroy_shader(&prog); | |
| 105 | ✗ | return {}; | |
| 106 | } | ||
| 107 | |||
| 108 | ✗ | glDetachShader(prog.program_handle, vert_handle); | |
| 109 | ✗ | glDetachShader(prog.program_handle, frag_handle); | |
| 110 | ✗ | glDeleteShader(vert_handle); | |
| 111 | ✗ | glDeleteShader(frag_handle); | |
| 112 | |||
| 113 | ✗ | prog.texture_attrib = glGetUniformLocation(prog.program_handle, "Texture"); | |
| 114 | ✗ | prog.projection_attrib = glGetUniformLocation(prog.program_handle, "ProjMtx"); | |
| 115 | ✗ | return prog; | |
| 116 | } | ||
| 117 | |||
| 118 | constexpr auto dear_imgui_shader_version = "#version 330 core\n"; | ||
| 119 | |||
| 120 | constexpr auto linear_to_gamma_glsl_vert = R"glsl( | ||
| 121 | layout (location = 0) in vec2 Position; | ||
| 122 | layout (location = 1) in vec2 UV; | ||
| 123 | layout (location = 2) in vec4 Color; | ||
| 124 | uniform mat4 ProjMtx; | ||
| 125 | out vec2 Frag_UV; | ||
| 126 | out vec4 Frag_Color; | ||
| 127 | void main() | ||
| 128 | { | ||
| 129 | Frag_UV = UV; | ||
| 130 | Frag_Color = Color; | ||
| 131 | gl_Position = ProjMtx * vec4(Position.xy,0,1); | ||
| 132 | } | ||
| 133 | )glsl"; | ||
| 134 | |||
| 135 | constexpr auto linear_to_gamma_glsl_frag = R"glsl( | ||
| 136 | in vec2 Frag_UV; | ||
| 137 | in vec4 Frag_Color; | ||
| 138 | uniform sampler2D Texture; | ||
| 139 | layout (location = 0) out vec4 Out_Color; | ||
| 140 | void main() | ||
| 141 | { | ||
| 142 | vec4 sample = texture(Texture, Frag_UV.st); | ||
| 143 | vec3 color = sample.rgb / (sample.rgb + vec3(1.0f)); // reinhard tone mapping | ||
| 144 | float gamma = 2.2f; | ||
| 145 | vec3 gamma_corrected = pow(color.rgb, vec3(1.0f/gamma)); | ||
| 146 | Out_Color = Frag_Color * vec4(gamma_corrected, sample.a); | ||
| 147 | } | ||
| 148 | )glsl"; | ||
| 149 | |||
| 150 | |||
| 151 | constexpr auto depth_ortho_glsl_vert = R"glsl( | ||
| 152 | layout (location = 0) in vec2 Position; | ||
| 153 | layout (location = 1) in vec2 UV; | ||
| 154 | layout (location = 2) in vec4 Color; | ||
| 155 | uniform mat4 ProjMtx; | ||
| 156 | out vec2 Frag_UV; | ||
| 157 | out vec4 Frag_Color; | ||
| 158 | void main() | ||
| 159 | { | ||
| 160 | Frag_UV = UV; | ||
| 161 | Frag_Color = Color; | ||
| 162 | gl_Position = ProjMtx * vec4(Position.xy,0,1); | ||
| 163 | } | ||
| 164 | )glsl"; | ||
| 165 | |||
| 166 | constexpr auto depth_ortho_glsl_frag = R"glsl( | ||
| 167 | in vec2 Frag_UV; | ||
| 168 | in vec4 Frag_Color; | ||
| 169 | uniform sampler2D Texture; | ||
| 170 | layout (location = 0) out vec4 Out_Color; | ||
| 171 | |||
| 172 | void main() | ||
| 173 | { | ||
| 174 | vec4 sample = texture(Texture, Frag_UV.st); | ||
| 175 | Out_Color = Frag_Color * vec4(vec3(sample.r), 1); | ||
| 176 | } | ||
| 177 | )glsl"; | ||
| 178 | |||
| 179 | |||
| 180 | ✗ | ImguiShaderCache::ImguiShaderCache() | |
| 181 | ✗ | : linear_to_gamma_shader(imgui_load_shader(dear_imgui_shader_version, | |
| 182 | linear_to_gamma_glsl_vert, linear_to_gamma_glsl_frag)) | ||
| 183 | ✗ | , depth_ortho_shader(imgui_load_shader(dear_imgui_shader_version, | |
| 184 | depth_ortho_glsl_vert, depth_ortho_glsl_frag) | ||
| 185 | ) | ||
| 186 | { | ||
| 187 | ✗ | } | |
| 188 | |||
| 189 | ✗ | ImguiShaderCache::~ImguiShaderCache() | |
| 190 | { | ||
| 191 | ✗ | imgui_destroy_shader(&linear_to_gamma_shader); | |
| 192 | ✗ | imgui_destroy_shader(&depth_ortho_shader); | |
| 193 | ✗ | } | |
| 194 | |||
| 195 | |||
| 196 | ✗ | void imgui_set_shared_shader_params(ImguiShaderProgram* prog) | |
| 197 | { | ||
| 198 | ✗ | ImDrawData* draw_data = ImGui::GetDrawData(); | |
| 199 | |||
| 200 | ✗ | const auto le = draw_data->DisplayPos.x; | |
| 201 | ✗ | const auto ri = draw_data->DisplayPos.x + draw_data->DisplaySize.x; | |
| 202 | ✗ | const auto to = draw_data->DisplayPos.y; | |
| 203 | ✗ | const auto bo = draw_data->DisplayPos.y + draw_data->DisplaySize.y; | |
| 204 | |||
| 205 | ✗ | const float ortho_projection[4][4] = { | |
| 206 | ✗ | {2.0f / (ri - le), 0.0f, 0.0f, 0.0f}, | |
| 207 | ✗ | {0.0f, 2.0f / (to - bo), 0.0f, 0.0f}, | |
| 208 | {0.0f, 0.0f, -1.0f, 0.0f}, | ||
| 209 | ✗ | {(ri + le) / (le - ri), (to + bo) / (bo - to), 0.0f, 1.0f}, | |
| 210 | ✗ | }; | |
| 211 | ✗ | glUseProgram(prog->program_handle); | |
| 212 | ✗ | glUniform1i(prog->texture_attrib, 0); | |
| 213 | ✗ | glUniformMatrix4fv(prog->projection_attrib, 1, GL_FALSE, &ortho_projection[0][0]); | |
| 214 | ✗ | } | |
| 215 | |||
| 216 | ✗ | void im_draw_callback_linear_to_gamma(const ImDrawList*, const ImDrawCmd* cmd) | |
| 217 | { | ||
| 218 | ✗ | auto* prog = static_cast<ImguiShaderProgram*>(cmd->UserCallbackData); | |
| 219 | |||
| 220 | ✗ | imgui_set_shared_shader_params(prog); | |
| 221 | ✗ | } | |
| 222 | |||
| 223 | ✗ | void im_draw_callback_depth_ortho(const ImDrawList*, const ImDrawCmd* cmd) | |
| 224 | { | ||
| 225 | ✗ | auto* prog = static_cast<ImguiShaderProgram*>(cmd->UserCallbackData); | |
| 226 | |||
| 227 | ✗ | imgui_set_shared_shader_params(prog); | |
| 228 | ✗ | } | |
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | ✗ | void imgui_text(const std::string& str) | |
| 233 | { | ||
| 234 | ✗ | ImGui::Text("%s", str.c_str()); | |
| 235 | ✗ | } | |
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | ✗ | static void draw_imgui_image(const render::FrameBuffer& img, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& border_col, ImguiShaderCache* cache, ImageShader shader) | |
| 240 | { | ||
| 241 | ✗ | ImDrawList* draw_list = ImGui::GetWindowDrawList(); | |
| 242 | |||
| 243 | // set shader | ||
| 244 | ✗ | switch (shader) | |
| 245 | { | ||
| 246 | ✗ | case ImageShader::TonemapAndGamma: | |
| 247 | ✗ | draw_list->AddCallback(im_draw_callback_linear_to_gamma, &cache->linear_to_gamma_shader); | |
| 248 | ✗ | break; | |
| 249 | ✗ | case ImageShader::DepthOrtho: | |
| 250 | ✗ | draw_list->AddCallback(im_draw_callback_depth_ortho, &cache->depth_ortho_shader); | |
| 251 | ✗ | break; | |
| 252 | ✗ | case ImageShader::None: | |
| 253 | // no shader | ||
| 254 | ✗ | break; | |
| 255 | } | ||
| 256 | |||
| 257 | // draw image | ||
| 258 | ✗ | ImGui::ImageWithBg(img.id, image_size, uv0, uv1, border_col); | |
| 259 | |||
| 260 | // reset shader | ||
| 261 | ✗ | if (shader != ImageShader::None) | |
| 262 | { | ||
| 263 | ✗ | draw_list->AddCallback(ImDrawCallback_ResetRenderState, nullptr); | |
| 264 | } | ||
| 265 | ✗ | } | |
| 266 | |||
| 267 | |||
| 268 | ✗ | static v2 v2_from_vec(const ImVec2& v) | |
| 269 | { | ||
| 270 | ✗ | return {v.x, v.y}; | |
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | ✗ | static void image_tooltip(const render::FrameBuffer& texture_id, const ImVec2 texture_size, const float& region_size, const float& hover_size, | |
| 275 | const ImVec2 mouse_pos, const ImVec2 widget_size, const ImVec2 pos, const ImVec4 border_col, ImguiShaderCache* cache, ImageShader shader) | ||
| 276 | { | ||
| 277 | ✗ | const auto region = core::calculate_region(v2_from_vec(mouse_pos), v2_from_vec(pos), v2_from_vec(texture_size), v2_from_vec(widget_size), region_size); | |
| 278 | ✗ | const auto flipped_region_y = texture_size.y - region.y; | |
| 279 | ✗ | const auto uv0 = ImVec2{region.x / texture_size.x, (flipped_region_y) / texture_size.y}; | |
| 280 | ✗ | const auto uv1 = ImVec2{(region.x + region_size) / texture_size.x, (flipped_region_y - region_size) / texture_size.y}; | |
| 281 | |||
| 282 | // todo(Gustav): can we display pixel value instead of where we are looking? is the region important information? | ||
| 283 | ✗ | imgui_text(fmt::format("UL: {} {}", region.x, region.y)); | |
| 284 | ✗ | imgui_text(fmt::format("LR: {} {}", region.x + region_size, region.y + region_size)); | |
| 285 | ✗ | draw_imgui_image(texture_id, ImVec2(hover_size, hover_size), uv0, uv1, border_col, cache, shader); | |
| 286 | ✗ | } | |
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | ✗ | void imgui_image(const char* name, const render::FrameBuffer& img, ImguiShaderCache* cache, ImageShader shader) | |
| 291 | { | ||
| 292 | ✗ | const auto texture_size = ImVec2{float_from_int(img.size.width), float_from_int(img.size.height)}; | |
| 293 | |||
| 294 | // todo(Gustav): make the arguments widget_size and zoom level AND make them configurable (with scrolling) | ||
| 295 | static float widget_height = 100.0f; | ||
| 296 | static float region_size = 32.0f; | ||
| 297 | static float hover_size = 128.0f; | ||
| 298 | ✗ | const auto& io = ImGui::GetIO(); | |
| 299 | |||
| 300 | ✗ | imgui_text(fmt::format("{}: {}x{}", name, texture_size.x, texture_size.y)); | |
| 301 | |||
| 302 | ✗ | const auto widget_width = (texture_size.x / texture_size.y) * widget_height; | |
| 303 | ✗ | const auto widget_size = ImVec2{widget_width, widget_height}; | |
| 304 | |||
| 305 | ✗ | const auto pos = ImGui::GetCursorScreenPos(); | |
| 306 | ✗ | constexpr auto uv_min = ImVec2{0.0f, 1.0f}; // Top-left | |
| 307 | ✗ | constexpr auto uv_max = ImVec2{1.0f, 0.0f}; // Lower-right | |
| 308 | ✗ | const auto border_col = ImGui::GetStyleColorVec4(ImGuiCol_Border); | |
| 309 | |||
| 310 | static ImVec2 latest_tooltip; | ||
| 311 | static ImGuiID current_id = 0; | ||
| 312 | |||
| 313 | ✗ | const constexpr char* const popup_id = "image config popup"; | |
| 314 | |||
| 315 | ✗ | draw_imgui_image(img, widget_size, uv_min, uv_max, border_col, cache, shader); | |
| 316 | ✗ | const auto id = ImGui::GetID(name); | |
| 317 | |||
| 318 | ✗ | if (id == current_id && ImGui::BeginPopupContextItem(popup_id)) | |
| 319 | { | ||
| 320 | ✗ | ImGui::DragFloat("Base", &widget_height, 1.0f); | |
| 321 | ✗ | ImGui::DragFloat("Size", ®ion_size, 0.01f); | |
| 322 | ✗ | ImGui::DragFloat("Scale", &hover_size, 1.0f); | |
| 323 | ✗ | image_tooltip(img, texture_size, region_size, hover_size, latest_tooltip, widget_size, pos, border_col, cache, shader); | |
| 324 | ✗ | if (ImGui::Button("Close")) | |
| 325 | { | ||
| 326 | ✗ | ImGui::CloseCurrentPopup(); | |
| 327 | } | ||
| 328 | ✗ | ImGui::EndPopup(); | |
| 329 | } | ||
| 330 | ✗ | ImGui::OpenPopupOnItemClick(popup_id, ImGuiPopupFlags_MouseButtonRight); | |
| 331 | |||
| 332 | // todo(Gustav): look into SetItemTooltip and BeginItemTooltip from https://github.com/ocornut/imgui/releases/tag/v1.89.7 | ||
| 333 | ✗ | if (ImGui::IsItemHovered()) | |
| 334 | { | ||
| 335 | ✗ | current_id = id; | |
| 336 | ✗ | latest_tooltip = io.MousePos; | |
| 337 | ✗ | if (ImGui::BeginTooltip()) | |
| 338 | { | ||
| 339 | ✗ | image_tooltip(img, texture_size, region_size, hover_size, io.MousePos, widget_size, pos, border_col, cache, shader); | |
| 340 | ✗ | ImGui::EndTooltip(); | |
| 341 | } | ||
| 342 | } | ||
| 343 | ✗ | } | |
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | ✗ | bool simple_gamma_slider(const char* label, float* gamma, float curve, float min_gamma, float max_gamma) | |
| 348 | { | ||
| 349 | ✗ | if (curve < 0.0f) | |
| 350 | { | ||
| 351 | ✗ | return ImGui::SliderFloat(label, gamma, min_gamma, max_gamma); | |
| 352 | } | ||
| 353 | |||
| 354 | // todo(Gustav): is this the correct way? it doesn't feel exactly right but perhaps that's just dear imgui | ||
| 355 | ✗ | const auto gamma_range = max_gamma - min_gamma; | |
| 356 | ✗ | const auto t = (*gamma - min_gamma) / (gamma_range); | |
| 357 | |||
| 358 | ✗ | auto slider_value = std::pow(t, 1.0f / curve); | |
| 359 | ✗ | if (ImGui::SliderFloat(label, &slider_value, 0.0f, 1.0f) == false) | |
| 360 | { | ||
| 361 | ✗ | return false; | |
| 362 | } | ||
| 363 | |||
| 364 | ✗ | const auto perceptual = std::pow(slider_value, curve); | |
| 365 | ✗ | *gamma = min_gamma + perceptual * gamma_range; | |
| 366 | ✗ | return true; | |
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | ✗ | bool drag(const char* const label, v3* drag) | |
| 371 | { | ||
| 372 | ✗ | return ImGui::DragFloat3(label, drag->get_data_ptr()); | |
| 373 | } | ||
| 374 | |||
| 375 | ✗ | bool drag(const char* const label, Ypr* drag) | |
| 376 | { | ||
| 377 | float angles[3] = { | ||
| 378 | ✗ | drag->yaw.as_degrees(), | |
| 379 | ✗ | drag->pitch.as_degrees(), | |
| 380 | ✗ | drag->roll.as_degrees() | |
| 381 | ✗ | }; | |
| 382 | |||
| 383 | ✗ | const auto changed = ImGui::DragFloat3(label, angles, 1.0f, -360.0f, 360.0f); | |
| 384 | |||
| 385 | ✗ | if (changed) | |
| 386 | { | ||
| 387 | ✗ | drag->yaw = An::from_degrees(angles[0]); | |
| 388 | ✗ | drag->pitch = An::from_degrees(angles[1]); | |
| 389 | ✗ | drag->roll = An::from_degrees(angles[2]); | |
| 390 | } | ||
| 391 | |||
| 392 | ✗ | return changed; | |
| 393 | } | ||
| 394 | |||
| 395 | ✗ | float length2(const ImVec2& v) | |
| 396 | { | ||
| 397 | ✗ | return v.x* v.x + v.y * v.y; | |
| 398 | } | ||
| 399 | |||
| 400 | ✗ | float length(const ImVec2& v) | |
| 401 | { | ||
| 402 | ✗ | return std::sqrt(v.x * v.x + v.y * v.y); | |
| 403 | } | ||
| 404 | |||
| 405 | ✗ | ImVec2 normalize(const ImVec2& v, float len) | |
| 406 | { | ||
| 407 | ✗ | return v / len; | |
| 408 | } | ||
| 409 | ✗ | ImVec2 normalize(const ImVec2& v) | |
| 410 | { | ||
| 411 | ✗ | return normalize(v, length(v)); | |
| 412 | } | ||
| 413 | |||
| 414 | ✗ | float dot(const ImVec2& lhs, const ImVec2& rhs) | |
| 415 | { | ||
| 416 | ✗ | return lhs.x * rhs.x + lhs.y * rhs.y; | |
| 417 | } | ||
| 418 | ✗ | bool is_positive(float f) | |
| 419 | { | ||
| 420 | ✗ | return f >= 0.0f; | |
| 421 | } | ||
| 422 | |||
| 423 | struct GearState | ||
| 424 | { | ||
| 425 | ImVec2 pos; | ||
| 426 | int turns; | ||
| 427 | float orig; | ||
| 428 | }; | ||
| 429 | |||
| 430 | // https://anttweakbar.sourceforge.io/doc/tools_anttweakbar_rotoslider.html | ||
| 431 | ✗ | bool gear(const char* const label, float* drag) | |
| 432 | { | ||
| 433 | static std::optional<GearState> state = std::nullopt; | ||
| 434 | |||
| 435 | ✗ | const ImGuiButtonFlags flags = 0; | |
| 436 | ✗ | const float min_radius = 20.0f; | |
| 437 | ✗ | const float radius = min_radius; | |
| 438 | ✗ | const float one_turn = 10.0f; | |
| 439 | |||
| 440 | ✗ | const auto orig = state ? state->orig : *drag; | |
| 441 | |||
| 442 | ✗ | const auto& style = ImGui::GetStyle(); | |
| 443 | |||
| 444 | ✗ | const float w = ImGui::CalcItemWidth(); | |
| 445 | |||
| 446 | ✗ | const auto label_size = ImGui::CalcTextSize(label, nullptr, true); | |
| 447 | ✗ | const auto frame = ImVec2(w, label_size.y + style.FramePadding.y * 2.0f); | |
| 448 | ✗ | const auto extra = ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f); | |
| 449 | |||
| 450 | |||
| 451 | ✗ | const auto size = frame + extra; | |
| 452 | ✗ | const auto pos = ImGui::GetWindowPos() + ImGui::GetCursorPos(); | |
| 453 | ✗ | const auto mp = ImGui::GetMousePos(); | |
| 454 | ✗ | const auto center = mp - ImGui::GetMouseDragDelta(); | |
| 455 | ✗ | const auto clicked = ImGui::InvisibleButton(label, size, flags); | |
| 456 | ✗ | const auto active = ImGui::IsItemActive(); | |
| 457 | |||
| 458 | ✗ | auto* draw = ImGui::GetForegroundDrawList(); | |
| 459 | |||
| 460 | ✗ | const auto bg_col = ImColor(100, 100, 100); // ImColor(style.Colors[ImGuiCol_ChildBg]); | |
| 461 | ✗ | const auto tx_col = ImColor(0, 0, 0); // ImColor(style.Colors[ImGuiCol_Text]); | |
| 462 | ✗ | const auto circle_col = ImColor(0, 0, 255); | |
| 463 | |||
| 464 | ✗ | draw->AddRectFilled(pos, pos + label_size, bg_col); | |
| 465 | ✗ | draw->AddText(pos, tx_col, label); | |
| 466 | |||
| 467 | ✗ | if (active) | |
| 468 | { | ||
| 469 | ✗ | const auto input = mp - center; | |
| 470 | ✗ | if (length2(input) > (min_radius * min_radius)) { | |
| 471 | ✗ | const auto dir_cur = normalize(input); | |
| 472 | ✗ | const auto ang_right = std::acos(dot(dir_cur, {1, 0})) * (180.0f/std::numbers::pi_v<float>); | |
| 473 | ✗ | const auto ang = dir_cur.y < 0 ? ang_right : 360 - ang_right; | |
| 474 | |||
| 475 | ✗ | int turns = -1; | |
| 476 | ✗ | if (state.has_value()) | |
| 477 | { | ||
| 478 | ✗ | turns = state->turns; | |
| 479 | |||
| 480 | ✗ | const auto changed_y = is_positive(input.y) != is_positive(state->pos.y); | |
| 481 | ✗ | const auto on_right_side = input.x > 0 && state->pos.x > 0; | |
| 482 | ✗ | const auto changed_dir = changed_y && on_right_side; | |
| 483 | ✗ | if (changed_dir) | |
| 484 | { | ||
| 485 | ✗ | turns += is_positive(input.y) ? -1 : 1; | |
| 486 | } | ||
| 487 | } | ||
| 488 | ✗ | state = GearState | |
| 489 | { | ||
| 490 | .pos = input, | ||
| 491 | .turns = turns, | ||
| 492 | .orig = orig | ||
| 493 | ✗ | }; | |
| 494 | |||
| 495 | ✗ | const auto new_ang = ang + static_cast<float>(turns) * 360.0f; | |
| 496 | ✗ | const auto val = orig + (new_ang / 360.0f) * one_turn; | |
| 497 | |||
| 498 | ✗ | if (drag) | |
| 499 | { | ||
| 500 | ✗ | *drag = val; | |
| 501 | } | ||
| 502 | } | ||
| 503 | else | ||
| 504 | { | ||
| 505 | ✗ | state = std::nullopt; | |
| 506 | } | ||
| 507 | |||
| 508 | ✗ | auto* fg = ImGui::GetForegroundDrawList(); | |
| 509 | ✗ | fg->AddCircle(center, radius, circle_col, 8); | |
| 510 | ✗ | fg->AddLine(center, mp, circle_col); | |
| 511 | } | ||
| 512 | |||
| 513 | ✗ | return clicked; | |
| 514 | } | ||
| 515 | |||
| 516 | |||
| 517 | } | ||
| 518 | |||
| 519 |