-
-
Save colesnicov/718d2835b18f1e008e3511789264a563 to your computer and use it in GitHub Desktop.
Rotating text and icon demo for dear imgui
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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.5f * ImGui::GetTime()); | |
ImRotateStart(); ImGui::SameLine(); | |
ImGui::Text(ICON_MD_BRIGHTNESS_5); | |
ImRotateEnd(0.005f*::GetTickCount()*!ImGui::IsItemHovered()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment