Skip to content

Instantly share code, notes, and snippets.

@ice1000
Forked from carasuca/ImRotateDemo.cpp
Last active July 29, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice1000/865c7666d13b945628254aa00bd9d62d to your computer and use it in GitHub Desktop.
Save ice1000/865c7666d13b945628254aa00bd9d62d to your computer and use it in GitHub Desktop.
Rotating text and icon demo for dear imgui
#include "imgui_internal.h"
int rotation_start_index;
auto ImRotateStart() -> void {
rotation_start_index = ImGui::GetWindowDrawList()->VtxBuffer.Size;
}
auto ImRotationCenter() -> ImVec2 {
ImVec2 l{FLT_MAX, FLT_MAX}, u{-FLT_MAX, -FLT_MAX}; // bounds
const auto& buf = ImGui::GetWindowDrawList()->VtxBuffer;
for (int i = rotation_start_index; i < buf.Size; i++)
l = ImMin(l, buf[i].pos), u = ImMax(u, buf[i].pos);
return {(l.x + u.x) / 2, (l.y + u.y) / 2}; // or use _ClipRectStack?
}
void ImRotateEnd(float rad, ImVec2 center = ImRotationCenter()) {
float s = ImSin(rad), c = ImCos(rad);
center = ImRotate(center, s, c) - center;
auto& buf = ImGui::GetWindowDrawList()->VtxBuffer;
for (int i = rotation_start_index; i < buf.Size; i++)
buf[i].pos = ImRotate(buf[i].pos, s, c) - center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment