ImGui 自定义控件框架,有简单的事件处理机制,当然比起不 im 的 gui,略显简陋:
1 // InvisibleButton() 测试代码 2 void display_panel() 3 { 4 static ImVec2 location; 5 static ImVec2 size; 6 7 static ImVec2 mousePos; 8 static ImVec2 localMousePos; 9 static ImVec2 delta; 10 11 12 ImGui::DragFloat2(u8"位置", (float*) &location); 13 ImGui::DragFloat2(u8"大小", (float*) &size); 14 ImGui::DragFloat2(u8"鼠标", (float*) &mousePos); 15 ImGui::DragFloat2(u8"本地鼠标", (float*) &localMousePos); 16 ImGui::DragFloat2("delta", (float*) &delta); 17 18 static int click = 0; 19 static int down = 0; 20 static int up = 0; 21 static bool hovering = false; 22 23 ImGui::Text("click: %d down: %d up: %d hover: %d", click, down, up, hovering); 24 25 ImGui::InvisibleButton("Panel##", ImVec2(100.0f, 100.0f)); 26 location = ImGui::GetItemRectMin(); 27 size = ImGui::GetItemRectSize(); 28 const ImVec2 minPos = ImGui::GetItemRectMin(); 29 const ImVec2 maxPos = ImGui::GetItemRectMax(); 30 mousePos = ImGui::GetMousePos(); 31 localMousePos = mousePos - minPos; 32 33 if (ImGui::IsItemActive()) { 34 if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) { 35 // delta = ImGui::GetIO().MouseDelta; // 即时拖动向量 36 delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left); // 累计拖动 37 // ImGui::ResetMouseDragDelta(ImGuiMouseButton_Left); 38 } 39 40 // 鼠标按下 41 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) { 42 ++click; 43 } 44 45 // 鼠标连发点击 46 if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) { 47 ++down; 48 } 49 } 50 else if (ImGui::IsItemDeactivated()) { 51 // 鼠标弹起 52 if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) { 53 ++up; 54 } 55 } 56 else { 57 // 鼠标是否在控件范围内 58 hovering = ImGui::IsMouseHoveringRect(minPos, maxPos); 59 if (ImGui::IsMouseHoveringRect(minPos, maxPos)) { 60 } 61 } 62 63 ImDrawList* draw_list = ImGui::GetWindowDrawList(); 64 ImGui::PushClipRect(minPos, maxPos, true); 65 draw_list->AddRectFilled(minPos, maxPos, IM_COL32(0, 127, 255, 255)); 66 // draw_list->AddImageRounded((ImTextureID) fboArcball.color_buffer().id(), p0, p1, ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), 0xFFFFFFFF, 1.0f); 67 ImGui::PopClipRect(); 68 }
sdragonx https://github.com/sdragonx