Skip to content

Commit

Permalink
Big refacto Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Nov 28, 2023
1 parent 16942a5 commit b1ebebd
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 184 deletions.
61 changes: 48 additions & 13 deletions imgui.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Window][DockSpaceViewport_11111111]
Pos=0,19
Size=1024,749
Size=1920,1010
Collapsed=0

[Window][Debug##Default]
Expand All @@ -10,44 +10,79 @@ Collapsed=0

[Window][Console]
Pos=0,19
Size=751,699
Size=1647,960
Collapsed=0
DockId=0x00000001,1

[Window][Message]
Pos=0,720
Size=1024,48
Pos=0,981
Size=1920,48
Collapsed=0
DockId=0x00000004,0

[Window][Places]
Pos=753,19
Size=271,699
Pos=1649,19
Size=271,960
Collapsed=0
DockId=0x00000002,1

[Window][Transitions]
Pos=753,19
Size=271,699
Pos=1649,19
Size=271,960
Collapsed=0
DockId=0x00000002,0

[Window][Arcs]
Pos=753,19
Size=271,699
Pos=1649,19
Size=271,960
Collapsed=0
DockId=0x00000002,2

[Window][Petri net]
Pos=0,19
Size=751,699
Size=1647,960
Collapsed=0
DockId=0x00000001,0

[Window][Dear ImGui Demo]
Pos=0,19
Size=1647,960
Collapsed=0
DockId=0x00000001,2

[Window][Example: Simple layout]
Pos=60,60
Size=500,440
Collapsed=0

[Window][Example: Simple layout/left pane_AED60EF8]
IsChild=1
Size=150,386

[Window][Dear ImGui Style Editor]
Pos=0,19
Size=751,699
Collapsed=0
DockId=0x00000001,3

[Window][Example: Property editor]
Pos=75,169
Size=430,450
Collapsed=0

[Window][Example: Custom rendering]
Pos=60,60
Size=531,414
Collapsed=0

[Table][0xD181190E,2]
Column 0 Weight=1.0000
Column 1 Weight=1.0000

[Docking][Data]
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=1024,749 Split=Y
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=1920,1010 Split=Y
DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=1920,960 Split=X
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=1647,1010 CentralNode=1 Selected=0x249E7C8C
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=271,1010 Selected=0x6372E3B7
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=271,1010 Selected=0xBB186BEF
DockNode ID=0x00000004 Parent=0x8B93E3BD SizeRef=1920,48 Selected=0x6B041B99

81 changes: 43 additions & 38 deletions src/Editor/DearImGui/Drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,63 @@

namespace tpne {

#define FILL_COLOR(a) IM_COL32(255, 165, 0, (a))
#define OUTLINE_COLOR IM_COL32(165, 42, 42, 255) // Arcs, Places, Transitions
#define CRITICAL_COLOR IM_COL32(255, 0, 0, 255)
#define FILL_COLOR(alpha) ImGui::GetColorU32(ImGuiCol_FrameBg, alpha)
#define OUTLINE_COLOR ImGui::GetColorU32(ImGuiCol_FrameBgActive)
#define CAPTION_COLOR ImGui::GetColorU32(ImGuiCol_Text)
#define DURATION_COLOR ImGui::GetColorU32(ImGuiCol_FrameBgActive)
#define TOKEN_COLOR ImGui::GetColorU32(ImGuiCol_Text)
#define CRITICAL_COLOR ImGui::GetColorU32(ImGuiCol_PlotLinesHovered)

//------------------------------------------------------------------------------
static void drawArrow(ImDrawList* draw_list, ImVec2 const& A, ImVec2 const& B,
const ImU32 color)
{
constexpr float pi = 3.14159265358979323846f;

// Orientation
const float teta = (B.y - A.y) / (B.x - A.x);
const float arrowAngle = std::atan(teta);
const float arrowAngle = std::atan((B.y - A.y) / (B.x - A.x));
// + (B.x < A.x) ? pi : ((B.y < A.y) ? (2.0f * pi) : 0.0f);
const float cos_a = std::cos(arrowAngle);
const float sin_a = std::sin(arrowAngle);

// Arc magnitude
const float arrowLength = norm(A, B);

// Tail of the arrow.
// Reduce the arrow magnitude to avoid entering in the place and having
// a mush of pixels when multiple arrows are pointing on the same
// position. To get full scaled arrow comment this block of code and
// uncomment A.x, B.x, A.y, B.y and tailSize.
const float r = arrowLength - PLACE_RADIUS;
const float dx = ((B.x - A.x) * r) / arrowLength;
const float dy = ((B.y - A.y) * r) / arrowLength;
const float a1 = B.x - dx;
const float b1 = B.y - dy;
const float a2 = A.x + dx;
const float b2 = A.y + dy;
const float length = norm(A, B);
float r = length - PLACE_RADIUS - ARROW_SPACING;
float dx = ((B.x - A.x) * r) / length;
float dy = ((B.y - A.y) * r) / length;
const ImVec2 from(B.x - dx, B.y - dy);
const ImVec2 to(A.x + dx, A.y + dy);

// Reduce the head size to avoid overlapping the line and head of the
// arrow. With the transparency this is noticable.
r = length - PLACE_RADIUS - ARROW_WIDTH - ARROW_SPACING;
dx = ((B.x - A.x) * r) / length;
dy = ((B.y - A.y) * r) / length;
const ImVec2 to2(A.x + dx, A.y + dy);
draw_list->AddLine(from, to2, color, 2.0f);

// Head of the arrow
const ImVec2 arrowHeadSize(14.0f, 14.0f);
const ImVec2 head(-ARROW_WIDTH, -ARROW_WIDTH / 2.0f);
std::vector<ImVec2> points = {
ImVec2(a2, b2 /*B.x, B.y*/) + rotate(ImVec2(0.0f, 0.0f), cos_a, sin_a),
ImVec2(a2, b2 /*B.x, B.y*/) + rotate(ImVec2(arrowHeadSize.x, arrowHeadSize.y / 2.0f), cos_a, sin_a),
ImVec2(a2, b2 /*B.x, B.y*/) + rotate(ImVec2(0.0f, arrowHeadSize.y), cos_a, sin_a)
to + rotate(head + ImVec2(0.0f, 0.0f), cos_a, sin_a),
to + rotate(head + ImVec2(ARROW_WIDTH, ARROW_WIDTH / 2.0f), cos_a, sin_a),
to + rotate(head + ImVec2(0.0f, ARROW_WIDTH), cos_a, sin_a)
};
draw_list->AddConvexPolyFilled(points.data(), points.size(), color);

// Tail of the arrow.
//const sf::Vector2f tailSize{ arrowLength - arrowHeadSize.x, 2.f };
const ImVec2 tailSize(r - arrowHeadSize.x - 15.0f, 2.0f);
draw_list->AddLine(A, B, color, 2.0f);
}

//------------------------------------------------------------------------------
void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2 const& origin, uint8_t const alpha)
void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2 const& origin, float const alpha)
{
ImU32 color;

if (alpha >= 0u)
if (alpha >= 0.0f)
{
color = FILL_COLOR(alpha);
color = OUTLINE_COLOR; // FIXME FILL_COLOR(alpha);
}
else
{
Expand All @@ -103,7 +108,7 @@ void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << arc.duration << ", "
<< arc.to.key << "(" << reinterpret_cast<Place&>(arc.to).tokens << ")";
draw_list->AddText(ImVec2(x, y), IM_COL32(0, 0, 0, 255), stream.str().c_str());
draw_list->AddText(ImVec2(x, y), DURATION_COLOR, stream.str().c_str());
}
else
{
Expand All @@ -118,20 +123,20 @@ void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2
float y = origin.y + arc.from.y + (arc.to.y - arc.from.y) / 2.0f - 15.0f;
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << arc.duration;
draw_list->AddText(ImVec2(x, y), IM_COL32(0, 0, 0, 255), stream.str().c_str());
draw_list->AddText(ImVec2(x, y), DURATION_COLOR, stream.str().c_str());
}
}
}

//------------------------------------------------------------------------------
void drawToken(ImDrawList* draw_list, float const x, float const y)
{
draw_list->AddCircleFilled(ImVec2(x, y), TOKEN_RADIUS, IM_COL32(0, 0, 0, 255));
draw_list->AddCircleFilled(ImVec2(x, y), TOKEN_RADIUS, TOKEN_COLOR);
}

// TODO a virer en utilisant un wrapper RenderablePlace avec fading (alpha)
//------------------------------------------------------------------------------
void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, uint8_t const alpha)
void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, float const alpha)
{
// In graph event we "compress" the graph by not displaying places.
if (type == TypeOfNet::TimedEventGraph)
Expand All @@ -142,12 +147,12 @@ void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type,

// Draw the place
draw_list->AddCircleFilled(p, PLACE_RADIUS, FILL_COLOR(alpha), 64);
draw_list->AddCircle(p, PLACE_RADIUS, OUTLINE_COLOR, 64);
draw_list->AddCircle(p, PLACE_RADIUS, OUTLINE_COLOR, 64, 2.5f);

// Draw the caption
ImVec2 dim = ImGui::CalcTextSize(place.key.c_str());
ImVec2 ptext = p - ImVec2(dim.x / 2.0f, PLACE_RADIUS + dim.y);
draw_list->AddText(ptext, IM_COL32(0, 0, 0, 255),
draw_list->AddText(ptext, CAPTION_COLOR,
show_caption ? place.caption.c_str() : place.key.c_str());

// Draw the number of tokens
Expand Down Expand Up @@ -188,12 +193,12 @@ void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type,
else
{
std::string tokens = std::to_string(place.tokens);
draw_list->AddText(ImVec2(p.x, p.y), IM_COL32(0, 0, 0, 255), tokens.c_str());
draw_list->AddText(ImVec2(p.x, p.y), CAPTION_COLOR, tokens.c_str());
}
}

//------------------------------------------------------------------------------
void drawTransition(ImDrawList* draw_list, Transition const& transition, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, uint8_t const alpha)
void drawTransition(ImDrawList* draw_list, Transition const& transition, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, float const alpha)
{
//const uint8_t alpha = 255; // TODO m_fading[place.key]
const ImVec2 p = origin + ImVec2(transition.x, transition.y);
Expand All @@ -214,12 +219,12 @@ void drawTransition(ImDrawList* draw_list, Transition const& transition, TypeOfN
const ImVec2 pmin(p.x - TRANS_WIDTH / 2.0f, p.y - TRANS_HEIGHT / 2.0f);
const ImVec2 pmax(p.x + TRANS_WIDTH / 2.0f, p.y + TRANS_HEIGHT / 2.0f);
draw_list->AddRectFilled(pmin, pmax, color);
draw_list->AddRect(pmin, pmax, OUTLINE_COLOR, 1.0f, ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBottomRight, 1.0f);
draw_list->AddRect(pmin, pmax, OUTLINE_COLOR, 0.0f, ImDrawFlags_None, 2.5f);

// Draw the caption
ImVec2 dim = ImGui::CalcTextSize(transition.key.c_str());
ImVec2 ptext = p - ImVec2(dim.x / 2.0f, TRANS_HEIGHT + dim.y);
draw_list->AddText(ptext, IM_COL32(0, 0, 0, 255),
ImVec2 ptext = p - ImVec2(dim.x / 2.0f, TRANS_HEIGHT / 2.0f + dim.y);
draw_list->AddText(ptext, CAPTION_COLOR,
show_caption ? transition.caption.c_str() : transition.key.c_str());
}

Expand Down
11 changes: 7 additions & 4 deletions src/Editor/DearImGui/Drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@

namespace tpne {

static const float ARROW_WIDTH = 14.0f;
static const float ARROW_SPACING = 10.0f;
static const float TRANS_WIDTH = 36.0f; // Rectangle width for rendering Transitions
static const float TRANS_HEIGHT = TRANS_WIDTH / 2.0f; // Rectangle height for rendering Transitions
static const float TRANS_HEIGHT = TRANS_WIDTH / 1.0f; // Rectangle height for rendering Transitions
static const float PLACE_RADIUS = TRANS_WIDTH / 2.0f; // Circle radius for rendering Places
static const float TOKEN_RADIUS = 2.0f; // Circle radius for rendering tokens

void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2 const& origin, uint8_t const alpha);

void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2 const& origin, float const alpha);
void drawToken(ImDrawList* draw_list, float const x, float const y);
void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, uint8_t const alpha);
void drawTransition(ImDrawList* draw_list, Transition const& transition, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, uint8_t const alpha);
void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, float const alpha);
void drawTransition(ImDrawList* draw_list, Transition const& transition, TypeOfNet const type, ImVec2 const& origin, bool const show_caption, float const alpha);

} // namespace tpne

Expand Down
Loading

0 comments on commit b1ebebd

Please sign in to comment.