(final ImBoolean showCanvaWindow)
| 28 | } |
| 29 | |
| 30 | public static void show(final ImBoolean showCanvaWindow) { |
| 31 | ImGui.setNextWindowSize(700, 400, ImGuiCond.Once); |
| 32 | ImGui.setNextWindowPos(ImGui.getMainViewport().getPosX() + 200, ImGui.getMainViewport().getPosY() + 200, ImGuiCond.Once); |
| 33 | |
| 34 | if (ImGui.begin("Canvas Demo Window", showCanvaWindow)) { |
| 35 | ImGui.text("This a demo for ImGui Canvas editor"); |
| 36 | ImGui.sameLine(); |
| 37 | ImGui.text("Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click for context menu."); |
| 38 | |
| 39 | // Typically, you would use a BeginChild()/EndChild() pair to benefit from a clipping region + own scrolling. |
| 40 | // Here we demonstrate that this can be replaced by simple offsetting + custom drawing + PushClipRect/PopClipRect() calls. |
| 41 | // To use a child window instead we could use, e.g: |
| 42 | // ImGui.pushStyleVar(ImGuiStyleVar.WindowPadding, 0, 0); // Disable padding |
| 43 | // ImGui.pushStyleColor(ImGuiCol.ChildBg, ImColor.intToColor(50, 50, 50, 255)); // Set a background color |
| 44 | // ImGui.beginChild("canvas", new ImVec2(0.0f, 0.0f), true, ImGuiWindowFlags.NoMove); |
| 45 | // ImGui.popStyleColor(); |
| 46 | // ImGui.popStyleVar(); |
| 47 | // [...] |
| 48 | // ImGui.endChild(); |
| 49 | |
| 50 | // Using InvisibleButton() as a convenience 1) it will advance the layout cursor and 2) allows us to use IsItemHovered()/IsItemActive() |
| 51 | ImVec2 canvasP0 = ImGui.getCursorScreenPos(); // ImDrawList API uses screen coordinates! |
| 52 | ImVec2 canvasSize = ImGui.getContentRegionAvail(); // Resize canvas to what's available |
| 53 | |
| 54 | if (canvasSize.x < 50.0f) { |
| 55 | canvasSize.x = 50.0f; |
| 56 | } |
| 57 | |
| 58 | if (canvasSize.y < 50.0f) { |
| 59 | canvasSize.y = 50.0f; |
| 60 | } |
| 61 | |
| 62 | ImVec2 canvasP1 = new ImVec2(canvasP0.x + canvasSize.x, canvasP0.y + canvasSize.y); |
| 63 | |
| 64 | // Draw border and background color |
| 65 | ImGuiIO io = ImGui.getIO(); |
| 66 | ImDrawList drawList = ImGui.getWindowDrawList(); |
| 67 | drawList.addRectFilled(canvasP0.x, canvasP0.y, canvasP1.x, canvasP1.y, |
| 68 | ImColor.rgba(50, 50, 50, 255)); |
| 69 | drawList.addRect(canvasP0.x, canvasP0.y, canvasP1.x, canvasP1.y, |
| 70 | ImColor.rgba(255, 255, 255, 255)); |
| 71 | |
| 72 | // This will catch our interactions |
| 73 | ImGui.invisibleButton("canvas", canvasSize.x, canvasSize.y, |
| 74 | ImGuiButtonFlags.MouseButtonLeft | ImGuiButtonFlags.MouseButtonRight); |
| 75 | |
| 76 | boolean isHovered = ImGui.isItemHovered(); // Hovered |
| 77 | boolean isActive = ImGui.isItemActive(); // Held |
| 78 | |
| 79 | ImVec2 origin = new ImVec2(canvasP0.x + scrolling.x, canvasP0.y + scrolling.y); // Lock scrolled origin |
| 80 | |
| 81 | ImVec2 mousePosInCanvas = new ImVec2(io.getMousePos().x - origin.x, io.getMousePos().y - origin.y); |
| 82 | |
| 83 | // Add first and second point |
| 84 | if (isHovered && !addingLine && ImGui.isMouseClicked(ImGuiMouseButton.Left)) { |
| 85 | pointList.add(mousePosInCanvas); |
| 86 | pointList.add(mousePosInCanvas); |
| 87 | addingLine = true; |
no test coverage detected