FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
SkiaBlocksManagerRenderer.cpp
Go to the documentation of this file.
2
3namespace gui::elements {
4 // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
6 // make sure the interaction still references valid objects
8
10 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
11 auto startSide = connectPortsInteraction.getStartSide().value();
12
13 const auto* startSideBlockPtr = startSide.block;
14 const auto* startSidePortPtr = startSide.port;
15
16 if ((startSideBlockPtr != nullptr) && (startSidePortPtr != nullptr)) {
17 auto portCoords = startSideBlockPtr->getPortCoordinates(startSidePortPtr);
18
19 canvas->drawLine(portCoords.x, portCoords.y, mouseX, mouseY, connectorPaint);
20 }
21 }
22 }
23 // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
24
26 SkPaint paint;
27
28 paint.setColor(colors::PURPLE_BLUE);
29 paint.setStrokeWidth(4);
30 paint.setAntiAlias(true);
31
32 return paint;
33 }();
34
36 SkPaint paint;
37
38 paint.setColor(colors::RED);
39 paint.setStrokeWidth(4);
40 paint.setAntiAlias(true);
41
42 return paint;
43 }();
44
45 // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
47 SkCanvas* canvas,
50 // check if we have an active input choice interaction to render
51 if (inputChoiceInteraction.has_value()) {
52 std::vector<components::UITextsRow> rows = {
53 components::UITextsRow({components::UIText("Choose a block type:",
54 components::UIText::Variant::Headline)}),
57 "Press a number to choose a block type. Press ESC to cancel.",
58 components::UIText::Variant::MenuCaption),
59
60 }),
61 // some extra spacing below the above text
63 {components::UIText("", components::UIText::Variant::MenuCaption)})};
64
65 std::copy(inputChoicesUiTextsRows.begin(),
67 std::back_inserter(rows));
68
69 uiRendererDelegate->renderCenteredTextsRows(canvas, size, rows);
70 } else {
71 // calculate values flow, then render the blocks & friends
72
73 // first, set values to NaNs on all input ports
74 // this ensures that the values are not carried over from the previous calculation
75 // for ports that may have become disconnected
76 for (auto& block : blocks) {
77 for (const auto& port : block->getInputPorts()) {
78 block->setPortValue(&port, std::numeric_limits<FloatingPoint>::quiet_NaN());
79 }
80 }
81
82 // then, make values flow
83 try {
85
86 // reset the cycle path
87 maybeGraphCycle = std::nullopt;
89 logger->error("Graph cycle detected: {}", e.what());
90
92 }
93
94 auto maybeHoveredBlock = getBlockAtMousePos();
95
96 // iterate from oldest to newest (normal order) to render newer ones on top of older
97 // ones
98 for (const auto& block : blocks) {
99 auto isFocused = maybeHoveredBlock.has_value() && (maybeHoveredBlock == block);
100
101 SkiaBaseBlockRenderer::render(block.get(), canvas, mouseX, mouseY, isFocused);
102 }
103
104 // render the existing port connections
105 for (const auto& [source, destinations] : connectionsRegistry) {
106 const auto& sourceBlock = source.block;
107 const auto& sourcePort = source.port;
108 const auto& sourcePortCoords = sourceBlock->getPortCoordinates(sourcePort);
109
110 for (const auto& dest : destinations) {
111 const auto& destBlock = dest.block;
112 const auto& destPort = dest.port;
113
114 auto isPartOfCycle =
115 maybeGraphCycle.has_value() &&
116 maybeGraphCycle->contains({.block = destBlock, .port = destPort});
117
118 const auto& destPortCoords = destBlock->getPortCoordinates(destPort);
119
120 canvas->drawLine(sourcePortCoords.x,
121 sourcePortCoords.y,
122 destPortCoords.x,
123 destPortCoords.y,
124 isPartOfCycle ? cycleConnectorPaint : connectorPaint);
125 }
126 }
127
128 // render the current interaction connector line (if applicable)
130 }
131 }
132 // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
133} // namespace gui::elements
std::unordered_map< business_logic::elements::structures::BlocksConnectionSide, std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > > connectionsRegistry
std::optional< std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > > maybeGraphCycle
std::optional< std::shared_ptr< business_logic::elements::blocks::BaseBlock > > getBlockAtMousePos()
Gets the block at the mouse position.
business_logic::input::ConnectPortsInteraction connectPortsInteraction
std::optional< business_logic::input::InputChoiceInteraction< business_logic::elements::blocks::BlockType > > inputChoiceInteraction
std::vector< components::UITextsRow > inputChoicesUiTextsRows
std::vector< std::shared_ptr< business_logic::elements::blocks::BaseBlock > > blocks
std::shared_ptr< spdlog::logger > logger
Definition Loggable.h:75
void calculateValuesFlow()
Calculates the values flowing through the graph.
The UI renderable text primitive.
Definition UIText.h:14
The UI renderable row of UITexts primitive.
Definition UITextsRow.h:15
Exception thrown when a graph cycle is detected.
const std::unordered_set< business_logic::elements::structures::BlocksConnectionSide > & getCyclePath() const
Gets the cycle path.
const std::optional< business_logic::elements::structures::BlocksConnectionSide > & getStartSide() const
Gets an immutable reference to the start side.
bool isStarted() const
Returns whether the interaction has started (i.e., is pending and a dragging line should be rendered)
void sanitize()
Sanitizes the interaction by removing both sides if any of them has become invalid.
static void render(business_logic::elements::blocks::BaseBlock *block, SkCanvas *canvas, int mouseX, int mouseY, bool isHovered)
Renders a block on an SkCanvas.
void maybeRenderDraggedLine(SkCanvas *canvas)
Renders the dragged line on the canvas.
void render(SkCanvas *canvas, const business_logic::geometry::Size2D &size, gui::renderer::delegate::UIRendererDelegate< SkCanvas > *&uiRendererDelegate)
Renders the blocks on the canvas.
The delegate for the UI renderer.
virtual void renderCenteredTextsRows(Canvas *canvas, const business_logic::geometry::Size2D &size, const std::vector< components::UITextsRow > &rows)=0
Renders the texts on the canvas around the center horizontally, centered vertically.
The GUI counterpart carrying implementations integrating with business_logic::elements.
const SkColor RED
Definition colors.cpp:9
const SkColor PURPLE_BLUE
Definition colors.cpp:6
A basic struct representing a 2D size.
Definition Size2D.h:8