FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
BaseBlock.cpp
Go to the documentation of this file.
1#include "BaseBlock.h"
2
3#include <utility>
4
9
11 int cx,
12 int cy,
13 int blockWidth,
14 int blockHeight,
18 std::shared_ptr<spdlog::logger> logger)
19 : cx(cx),
20 cy(cy),
21 width(blockWidth),
22 height(blockHeight),
23 leftX(0),
24 rightX(0),
25 topY(0),
26 bottomY(0),
27 centerY(0),
28 logger(std::move(logger)),
29 newBlockChoiceDelegate(newBlockChoiceDelegate),
30 blockLifecycleManagerDelegate(blockLifecycleManagerDelegate),
31 windowDelegate(windowDelegate) {
33 }
34
36
37 // cppcheck-suppress unusedFunction
38 void BaseBlock::updateWidthHeight(int newWidth, int newHeight) {
39 width = newWidth;
40 height = newHeight;
41
43 }
44
45 void BaseBlock::onDragProgress(int x, int y) {
46 logger->info("Block {} drag progress to {}, {}", getSelfId(), x, y);
47
48 this->cx = x;
49 this->cy = y;
50
52 }
53
54 void BaseBlock::onDragStart() { logger->info("Block {} drag start", getSelfId()); }
55
56 void BaseBlock::onDragEnd() { logger->info("Block {} drag end", getSelfId()); }
57
59 auto windowSize = windowDelegate->getWindowSize();
60
61 leftX = std::min(std::max(0, cx - (width / 2)), windowSize.width - width);
62 topY = std::min(std::max(0, cy - (height / 2)), windowSize.height - height);
63
65
66 rightX = leftX + width;
67
68 if (rightX > windowSize.width) {
69 leftX = windowSize.width - width;
70 rightX = windowSize.width;
71 }
72
73 if (bottomY > windowSize.height) {
74 topY = windowSize.height - height;
75 bottomY = windowSize.height;
76 }
77
78 centerY = static_cast<float>(topY + bottomY) / 2.0F;
79 }
80
81 std::optional<std::string> BaseBlock::getValueToRenderAboveBlock(
82 [[maybe_unused]] bool isHovered) {
83 const auto& outputPorts = getOutputPorts();
84
85 // if more or less than 1 output port is present, do not render the value above the block
86 if (outputPorts.size() != 1) {
87 return std::nullopt;
88 }
89
90 auto blockValue = getPortValue(&(outputPorts[0]));
91
92 std::ostringstream oss;
93 oss << std::scientific
95 << blockValue;
96
97 return oss.str();
98 }
99
102 // trick to be able to return a static reference for this function; also optimizes the
103 // function call
104 static const geometry::Point2D zeroPoint = {.x = 0, .y = 0};
105
106 if (portCoordinates.find(port) != portCoordinates.end()) {
107 return portCoordinates.at(port);
108 }
109
110 return zeroPoint;
111 }
112
113 std::optional<const business_logic::elements::structures::Port*> BaseBlock::checkPort(
115 const geometry::Point2D& point) const {
116 const auto& predCoordinates = getPortCoordinates(port);
117
119 point.y,
120 predCoordinates.x,
121 predCoordinates.y,
123 return std::make_optional(port);
124 }
125
126 return std::nullopt;
127 }
128
129 std::optional<const business_logic::elements::structures::Port*>
131 for (const auto& port : getInputPorts()) {
132 // cppcheck-suppress useStlAlgorithm
133 if (auto maybePort = checkPort(&port, point)) {
134 return maybePort;
135 }
136 }
137
138 for (const auto& port : getOutputPorts()) {
139 // cppcheck-suppress useStlAlgorithm
140 if (auto maybePort = checkPort(&port, point)) {
141 return maybePort;
142 }
143 }
144
145 // port not found
146 return std::nullopt;
147 }
148
151 auto maybeValueIt = portValues.find(port);
152
153 if (maybeValueIt != portValues.end()) {
154 return maybeValueIt->second;
155 }
156
157 return NaN;
158 }
159
161 const FloatingPoint& value) {
162 portValues[port] = value;
163 }
164
165 const FloatingPoint BaseBlock::NaN = std::numeric_limits<FloatingPoint>::quiet_NaN();
166} // namespace business_logic::elements::blocks
virtual void onBlockDeleted(const business_logic::elements::blocks::BaseBlock *block)=0
Called when a block is deleted.
Delegate that is notified when a new block is chosen to be added to the canvas.
virtual geometry::Size2D getWindowSize()=0
Get the window size.
business_logic::delegate::IBlockLifecycleManagerDelegate * blockLifecycleManagerDelegate
Definition BaseBlock.h:301
std::optional< const structures::Port * > checkPort(const structures::Port *port, const geometry::Point2D &point) const
Checks if a point is inside the hitbox of a port.
std::shared_ptr< spdlog::logger > logger
Definition BaseBlock.h:285
std::unordered_map< const structures::Port *, geometry::Point2D > portCoordinates
Caches the (center) coordinates of the ports of the block; must be updated after rendering.
Definition BaseBlock.h:318
void onDragStart() override
Called when dragging starts.
Definition BaseBlock.cpp:54
void cacheCornerCoordinates()
Caches the bottom right corner of the block, so that it is not recalculated every time isHovered() is...
Definition BaseBlock.cpp:58
business_logic::delegate::IWindowDelegate * windowDelegate
Definition BaseBlock.h:307
BaseBlock(int cx, int cy, int blockWidth, int blockHeight, business_logic::delegate::INewBlockChoiceDelegate *newBlockChoiceDelegate, business_logic::delegate::IBlockLifecycleManagerDelegate *blockLifecycleManagerDelegate, business_logic::delegate::IWindowDelegate *windowDelegate, std::shared_ptr< spdlog::logger > logger)
Constructor.
Definition BaseBlock.cpp:10
const FloatingPoint & getPortValue(const structures::Port *port) const
Gets the value of the port.
std::unordered_map< const structures::Port *, geometry::Point2D > & getPortCoordinates()
Gets the cache of (center) coordinates of the ports of the block; updated after rendering.
Definition BaseBlock.h:232
virtual const std::vector< structures::Port > & getOutputPorts() const =0
Gets the output ports of the block.
void onDragEnd() override
Called when dragging ends.
Definition BaseBlock.cpp:56
std::optional< const structures::Port * > getPortAtCoordinates(const geometry::Point2D &point) const
Gets the port at given coordinates.
virtual const std::vector< structures::Port > & getInputPorts() const =0
Gets the input ports of the block.
std::unordered_map< const structures::Port *, FloatingPoint > portValues
The registry of port values.
Definition BaseBlock.h:290
void updateWidthHeight(int newWidth, int newHeight)
Updates the width and height of the block.
Definition BaseBlock.cpp:38
void onDragProgress(int x, int y) override
Called during dragging.
Definition BaseBlock.cpp:45
virtual std::string getSelfId() const =0
virtual std::optional< std::string > getValueToRenderAboveBlock(bool isHovered)
Wrapper that gets the value of the output port above the block; if more or less than 1 output port is...
Definition BaseBlock.cpp:81
void setPortValue(const business_logic::elements::structures::Port *port, const FloatingPoint &value)
Sets the value of the port.
constexpr int TOTAL_PORT_HITBOX_RADIUS
Definition constants.h:26
constexpr int DEFAULT_VALUE_DISPLAY_PRECISION
Definition constants.h:39
The base classes and structs for elements.
bool isCircleHovered(int mouseX, int mouseY, int cX, int cY, int radius)
Check if a point is within a circle.
Definition helpers.cpp:4
STL namespace.
Represents a port on a block.
Definition Port.h:11
A basic struct representing a 2D point.
Definition Point2D.h:8
boost::multiprecision::cpp_dec_float_50 FloatingPoint
Definition typenames.h:6