Skip to content

Instantly share code, notes, and snippets.

@carasuca
Last active August 14, 2023 04:51
Show Gist options
  • Save carasuca/e72aacadcf6cf8139de46f97158f790f to your computer and use it in GitHub Desktop.
Save carasuca/e72aacadcf6cf8139de46f97158f790f 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;
void ImRotateStart()
{
rotation_start_index = ImGui::GetWindowDrawList()->VtxBuffer.Size;
}
ImVec2 ImRotationCenter()
{
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 ImVec2((l.x+u.x)/2, (l.y+u.y)/2); // or use _ClipRectStack?
}
ImVec2 operator-(const ImVec2& l, const ImVec2& r) { return{ l.x - r.x, l.y - r.y }; }
void ImRotateEnd(float rad, ImVec2 center = ImRotationCenter())
{
float s=sin(rad), c=cos(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;
}
//https://github.com/juliettef/IconFontCppHeaders/blob/master/IconsMaterialDesign_c.h
#include "IconsMaterialDesign_c.h"
void ImRotateDemo()
{
ImRotateStart();
ImGui::Text(__FUNCTION__);
ImRotateEnd(0.0005f*::GetTickCount());
ImRotateStart(); ImGui::SameLine();
ImGui::Text(ICON_MD_BRIGHTNESS_5);
ImRotateEnd(0.005f*::GetTickCount()*!ImGui::IsItemHovered());
}
@omorgan7
Copy link

omorgan7 commented Jan 5, 2018

Hi,

Do you have a license for this file?

Thanks

@ice1000
Copy link

ice1000 commented Aug 3, 2018

Nice

@deemano
Copy link

deemano commented Apr 18, 2022

Hi, Could you provide an example for your code, please. I cannot replicate this for some reason. Thanks @carasuca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment