From bc5375149c0ecb416848a2d3657ea41ae97177b3 Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Wed, 10 Aug 2022 21:24:03 -0400 Subject: path rasterization started with opengl --- my_imgui_widgets.cpp | 147 +++++++++++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 63 deletions(-) (limited to 'my_imgui_widgets.cpp') diff --git a/my_imgui_widgets.cpp b/my_imgui_widgets.cpp index 6f785de..e334f63 100644 --- a/my_imgui_widgets.cpp +++ b/my_imgui_widgets.cpp @@ -71,14 +71,14 @@ ImGui_KeyframeDragging(project_data *File, project_state *State, ui *UI, propert } // Returns a normalized UV position of the composition -static ImVec2 +static v2 ImGui_ScreenPointToCompUV(ImVec2 ViewportMin, ImVec2 CompPos, ImVec2 CompZoom, ImVec2 MousePos) { ImVec2 LocalMousePos = MousePos - ViewportMin; ImVec2 LocalCompPos = CompPos - ViewportMin; ImVec2 MouseScreenUV = LocalMousePos - LocalCompPos; ImVec2 Result = MouseScreenUV / CompZoom; - return Result; + return V2(Result); } static void @@ -391,21 +391,24 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp if (p+1 == Mask->NumberOfPoints) Point1 = &Mask->Point[0]; - ImVec2 Point0_Pos = ImVec2(Point0->Pos.x, Point0->Pos.y); - ImVec2 Point0_Pos_Left = Point0_Pos + ImVec2(Point0->TangentLeft.x, Point0->TangentLeft.y); - ImVec2 Point0_Pos_Right = Point0_Pos + ImVec2(Point0->TangentRight.x, Point0->TangentRight.y); + // NOTE(fox): I want to keep operations in local space under the v2 data + // type and operations in screen space under ImVec2. + + v2 Point0_Pos = Point0->Pos; + v2 Point0_Pos_Left = Point0_Pos + Point0->TangentLeft; + v2 Point0_Pos_Right = Point0_Pos + Point0->TangentRight; - ImVec2 Point1_Pos = ImVec2(Point1->Pos.x, Point1->Pos.y); - ImVec2 Point1_Pos_Left = Point1_Pos + ImVec2(Point1->TangentLeft.x, Point1->TangentLeft.y); - ImVec2 Point1_Pos_Right = Point1_Pos + ImVec2(Point1->TangentRight.x, Point1->TangentRight.y); + v2 Point1_Pos = Point1->Pos; + v2 Point1_Pos_Left = Point1_Pos + Point1->TangentLeft; + v2 Point1_Pos_Right = Point1_Pos + Point1->TangentRight; - ImVec2 Point0_ScreenPos = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, V2(Point0_Pos)); - ImVec2 Point0_ScreenPos_Left = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, V2(Point0_Pos_Left)); - ImVec2 Point0_ScreenPos_Right = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, V2(Point0_Pos_Right)); + ImVec2 Point0_ScreenPos = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, Point0_Pos); + ImVec2 Point0_ScreenPos_Left = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, Point0_Pos_Left); + ImVec2 Point0_ScreenPos_Right = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, Point0_Pos_Right); - ImVec2 Point1_ScreenPos = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, V2(Point1_Pos)); - ImVec2 Point1_ScreenPos_Left = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, V2(Point1_Pos_Left)); - ImVec2 Point1_ScreenPos_Right = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, V2(Point1_Pos_Right)); + ImVec2 Point1_ScreenPos = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, Point1_Pos); + ImVec2 Point1_ScreenPos_Left = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, Point1_Pos_Left); + ImVec2 Point1_ScreenPos_Right = Layer_LocalToScreenSpace(Layer, UI, CompBuffer, Point1_Pos_Right); ImGui::PushID(p); @@ -414,15 +417,19 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp // The handle itself draw_list->AddNgon(Point0_ScreenPos, 10, col, 8, 5.0f); - draw_list->AddNgon(Point0_ScreenPos_Left, 10, col, 8, 5.0f); - draw_list->AddNgon(Point0_ScreenPos_Right, 10, col, 8, 5.0f); - draw_list->AddLine(Point0_ScreenPos, Point0_ScreenPos_Left, col, 2.0f); - draw_list->AddLine(Point0_ScreenPos, Point0_ScreenPos_Right, col, 2.0f); + // draw_list->AddNgon(Point0_ScreenPos_Left, 10, col, 8, 5.0f); + // draw_list->AddNgon(Point0_ScreenPos_Right, 10, col, 8, 5.0f); + // draw_list->AddLine(Point0_ScreenPos, Point0_ScreenPos_Left, col, 2.0f); + // draw_list->AddLine(Point0_ScreenPos, Point0_ScreenPos_Right, col, 2.0f); - ImU32 col2 = ImGui::GetColorU32(ImGuiCol_Button); + int max = 1; - for (int b = 0; b < 3; b++) + if (Point0->HandleBezier) { + max = 3; + } + + for (int b = 0; b < max; b++) { ImGui::PushID(b); if (b == 0) { @@ -462,11 +469,11 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp Point0->TangentRight.x -= YAxis.x; Point0->TangentRight.y += YAxis.y; } + State->UpdateFrame = true; } ImGui::PopID(); } - // The bezier path if (Mask->NumberOfPoints == 1) { @@ -474,49 +481,63 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp continue; } - // Ratio of the point along the curve. See internal for more info. - float ratio; + ImU32 col2 = ImGui::GetColorU32(ImGuiCol_Button); - if (ImGui::BezierInteractive(Point0_ScreenPos, Point0_ScreenPos_Right, - Point1_ScreenPos_Left, Point1_ScreenPos, ratio) && - State->Tool == tool_pen) - { - // Using a button like this may be kinda janky, but it gives us access - // to all of ButtonBehavior and the ID system without having to rewrite it. - ImGui::SetCursorScreenPos(io.MousePos - ImVec2(5,5)); - ImGui::Button("maskbezier", ImVec2(10, 10)); - - if(ImGui::IsItemHovered()) { - // ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll); - draw_list->AddNgon(io.MousePos, 2, col, 8, 5.0f); - ImVec2 RatioLeft = ImGui::RatioToPoint(Point0_ScreenPos, Point0_ScreenPos_Right, ratio); - ImVec2 RatioTop = ImGui::RatioToPoint(Point0_ScreenPos_Right, Point1_ScreenPos_Left, ratio); - ImVec2 RatioRight = ImGui::RatioToPoint(Point1_ScreenPos_Left, Point1_ScreenPos, ratio); - ImVec2 TangentLeft = ImGui::RatioToPoint(RatioLeft, RatioTop, ratio); - ImVec2 TangentRight = ImGui::RatioToPoint(RatioTop, RatioRight, ratio); - draw_list->AddLine(RatioLeft, RatioTop, col, 2.0f); - draw_list->AddLine(RatioRight, RatioTop, col, 2.0f); - draw_list->AddLine(TangentLeft, TangentRight, col, 2.0f); - } - if(ImGui::IsItemActivated() && io.KeyCtrl) { - ImVec2 Ratio0 = ImGui::RatioToPoint(Point0_Pos, Point0_Pos_Right, ratio); - ImVec2 RatioTop = ImGui::RatioToPoint(Point0_Pos_Right, Point1_Pos_Left, ratio); - ImVec2 Ratio1 = ImGui::RatioToPoint(Point1_Pos_Left, Point1_Pos, ratio); - ImVec2 TangentLeft = ImGui::RatioToPoint(Ratio0, RatioTop, ratio); - ImVec2 TangentRight = ImGui::RatioToPoint(RatioTop, Ratio1, ratio); - ImVec2 Point = ImGui::RatioToPoint(TangentLeft, TangentRight, ratio); - Mask_AddPointToCurve(Mask, Point, Point - TangentLeft, Point - TangentRight, - Point0_Pos - Ratio0, Point1_Pos - Ratio1, p); - } + if (Point0->HandleBezier || Point1->HandleBezier) { + draw_list->AddBezierCubic(Point0_ScreenPos, Point0_ScreenPos_Right, + Point1_ScreenPos_Left, Point1_ScreenPos, col2, 6.0f, 0); + } else { + draw_list->AddLine(Point0_ScreenPos, Point1_ScreenPos, col2, 6.0f); } - // DebugWatchVar("ratio", &ratio, d_float); - - // if (ImGui::TestLine(PointScreenPos[0], PointScreenPos[1])) { - // } + if (Point0->HandleBezier && Point1->HandleBezier) { + if (ImGui::BezierInteractive(Point0_ScreenPos, Point0_ScreenPos_Right, + Point1_ScreenPos_Left, Point1_ScreenPos) && + State->Tool == tool_pen) + { + // Using a button like this may be kinda janky, but it gives us access + // to all of ButtonBehavior and the ID system without having to write on top of ImGui's. + ImGui::SetCursorScreenPos(io.MousePos - ImVec2(5,5)); + ImGui::Button("maskbezier", ImVec2(10, 10)); + if (ImGui::IsItemHovered()) { +#if DEBUG + v2 LayerPoint = Layer_ScreenSpaceToLocal(Layer, UI, CompBuffer, ViewportMin, io.MousePos); + real32 ratio = Bezier_CubicRatioOfPoint(Point0_Pos, Point0_Pos_Right, Point1_Pos_Left, Point1_Pos, LayerPoint); + draw_list->AddNgon(io.MousePos, 2, col, 8, 5.0f); + ImVec2 RatioLeft = ImGui::RatioToPoint(Point0_ScreenPos, Point0_ScreenPos_Right, ratio); + ImVec2 RatioRight = ImGui::RatioToPoint(Point1_ScreenPos_Left, Point1_ScreenPos, ratio); + ImVec2 RatioTop = ImGui::RatioToPoint(Point0_ScreenPos_Right, Point1_ScreenPos_Left, ratio); + ImVec2 TangentLeft = ImGui::RatioToPoint(RatioLeft, RatioTop, ratio); + ImVec2 TangentRight = ImGui::RatioToPoint(RatioTop, RatioRight, ratio); + draw_list->AddLine(RatioLeft, RatioTop, col, 2.0f); + draw_list->AddLine(RatioRight, RatioTop, col, 2.0f); + draw_list->AddLine(TangentLeft, TangentRight, col, 2.0f); +#endif + } + if (ImGui::IsItemActivated() && io.KeyCtrl) { + v2 LayerPoint = Layer_ScreenSpaceToLocal(Layer, UI, CompBuffer, ViewportMin, io.MousePos); + real32 ratio = Bezier_CubicRatioOfPoint(Point0_Pos, Point0_Pos_Right, Point1_Pos_Left, Point1_Pos, LayerPoint); + Mask_AddPointToCurve(Mask, p, ratio); + } + } + } else { + if (ImGui::LineInteractive(Point0_ScreenPos, Point1_ScreenPos) && + State->Tool == tool_pen) + { + ImGui::SetCursorScreenPos(io.MousePos - ImVec2(5,5)); + ImGui::Button("maskline", ImVec2(10, 10)); + if (ImGui::IsItemHovered()) { + draw_list->AddNgon(io.MousePos, 2, col, 8, 5.0f); + } + if (ImGui::IsItemActivated() && io.KeyCtrl) { + v2 LayerPoint = Layer_ScreenSpaceToLocal(Layer, UI, CompBuffer, ViewportMin, io.MousePos); + Mask_AddPointToLine(Mask, p, LayerPoint); + } + } + } ImGui::PopID(); } ImGui::PopID(); @@ -543,6 +564,7 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp bool32 IsActive = ImGui::IsItemActive(); bool32 IsActivated = ImGui::IsItemActivated(); + /* if (State->MostRecentlySelectedLayer > -1) { project_layer *Layer = File.Layer[State->MostRecentlySelectedLayer]; @@ -555,15 +577,13 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp if (State->Pen.IsActive && !ImGui::IsKeyDown(ImGuiKey_Z)) { if (IsActivated) { - v2 CompUV = V2(ImGui_ScreenPointToCompUV(ViewportMin, UI->CompPos, UI->CompZoom, io.MousePos)); - v2 LayerUV = CompUVToLayerUV(Layer, &CompBuffer, CompUV); - v2 LayerPos = LayerUV * V2(Layer->Source->Info.Width, Layer->Source->Info.Height); + v2 LayerPos = Layer_ScreenSpaceToLocal(Layer, UI, CompBuffer, ViewportMin, io.MousePos); Mask_PushPoint(&Layer->Mask[Layer->NumberOfMasks-1], LayerPos); } if (IsActive) { mask *Mask = &Layer->Mask[Layer->NumberOfMasks-1]; mask_point *CurrentPoint = &Mask->Point[Mask->NumberOfPoints-1]; - v2 CompUV = V2(ImGui_ScreenPointToCompUV(ViewportMin, UI->CompPos, UI->CompZoom, io.MousePos)); + v2 CompUV = ImGui_ScreenPointToCompUV(ViewportMin, UI->CompPos, UI->CompZoom, io.MousePos); v2 LayerUV = CompUVToLayerUV(Layer, &CompBuffer, CompUV); v2 LayerPos = LayerUV * V2(Layer->Source->Info.Width, Layer->Source->Info.Height); v2 OffsetPos = CurrentPoint->Pos - LayerPos; @@ -575,11 +595,12 @@ ImGui_Viewport(project_data File, project_state *State, ui *UI, comp_buffer Comp } } } + */ if (IsHovered && IsActivated && ImGui::IsMouseDown(ImGuiMouseButton_Left)) { // Point to zoom in on if Z is held - UI->TempZoomRatio = V2(ImGui_ScreenPointToCompUV(ViewportMin, UI->CompPos, UI->CompZoom, io.MousePos)); + UI->TempZoomRatio = ImGui_ScreenPointToCompUV(ViewportMin, UI->CompPos, UI->CompZoom, io.MousePos); DebugWatchVar("MouseScreenUV", &UI->TempZoomRatio.x, d_float); DebugWatchVar("MouseScreenUV", &UI->TempZoomRatio.y, d_float); -- cgit v1.2.3