FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
BaseBlock.h
Go to the documentation of this file.
1#ifndef BUSINESS_LOGIC_ELEMENTS_BLOCKS_BASE_BLOCK_H
2#define BUSINESS_LOGIC_ELEMENTS_BLOCKS_BASE_BLOCK_H
3
4#include <magic_enum/magic_enum.hpp>
5#include <memory>
6#include <unordered_map>
7#include <vector>
8
9#include "constants.h"
15#include "geometry/Point2D.h"
16#include "geometry/helpers.h"
17#include "logging/Loggable.h"
18#include "structures/Port.h"
19#include "typenames.h"
20
22 class IBlockLifecycleManagerDelegate; // forward declaration to satisfy clang-tidy
23}
24
28namespace business_logic::elements {}
29
37 public:
51 int cx,
52 int cy,
53 int blockWidth,
54 int blockHeight,
56 // NOLINTBEGIN
58 // NOLINTEND
60 std::shared_ptr<spdlog::logger> logger);
61
67 void updateWidthHeight(int newWidth, int newHeight);
68
69 ~BaseBlock() noexcept override;
70
71 // disable copy semantics
72 BaseBlock(const BaseBlock&) = delete;
73 BaseBlock& operator=(const BaseBlock&) = delete;
74
75 // disable move semantics
76 BaseBlock(BaseBlock&&) = delete;
78
85 virtual bool isHovered(int x, int y) const {
86 return x >= leftX && x <= rightX && y >= topY && y <= bottomY;
87 }
88
96 virtual std::optional<std::string> getValueToRenderAboveBlock(
97 [[maybe_unused]] bool isHovered);
98
102 void onDragStart() override;
103
107 void onDragProgress(int x, int y) override;
108
112 void onDragEnd() override;
113
120 const structures::Port* port) const;
121
127 std::optional<const structures::Port*> getPortAtCoordinates(
128 const geometry::Point2D& point) const;
129
134 virtual std::string getSelfId() const = 0;
135
140 virtual BlockType getBlockType() const = 0;
141
147 const FloatingPoint& getPortValue(const structures::Port* port) const;
148
155 const FloatingPoint& value);
156
161 virtual void calculateOutputValues() = 0;
162
163 // make the hash function a friend to allow it to access non-public members
164 friend struct std::hash<BaseBlock>;
165
170 virtual const std::vector<structures::Port>& getInputPorts() const = 0;
171
176 virtual const std::vector<structures::Port>& getOutputPorts() const = 0;
177
182 int getCx() const { return cx; }
183
188 int getCy() const { return cy; }
189
194 int getLeftX() const { return leftX; }
195
200 int getRightX() const { return rightX; }
201
206 int getTopY() const { return topY; }
207
212 int getBottomY() const { return bottomY; }
213
218 int getWidth() const { return width; }
219
224 int getHeight() const { // cppcheck-suppress unusedFunction
225 return height;
226 }
227
232 std::unordered_map<const structures::Port*, geometry::Point2D>& getPortCoordinates() {
233 return portCoordinates;
234 }
235
236 protected:
240 int cx;
241
245 int cy;
246
250 int width;
251
256
260 int leftX;
261
266
270 int topY;
271
276
280 float centerY;
281
285 std::shared_ptr<spdlog::logger> logger;
286
290 std::unordered_map<const structures::Port*, FloatingPoint> portValues;
291
296
297 // NOLINTBEGIN
302 // NOLINTEND
303
308
312 static const FloatingPoint NaN;
313
318 std::unordered_map<const structures::Port*, geometry::Point2D> portCoordinates;
319
325
332 std::optional<const structures::Port*> checkPort(const structures::Port* port,
333 const geometry::Point2D& point) const;
334 };
335} // namespace business_logic::elements::blocks
336
337namespace std {
341 template <>
342 struct hash<business_logic::elements::blocks::BaseBlock> {
344 return std::hash<int>()(p.cx) ^ (std::hash<int>()(p.cy) << 1U);
345 }
346 };
347} // namespace std
348
349#endif // BUSINESS_LOGIC_ELEMENTS_BLOCKS_BASE_BLOCK_H
Delegate that is notified when a new block is chosen to be added to the canvas.
The base class for all blocks, containing common functionality and members.
Definition BaseBlock.h:36
virtual void calculateOutputValues()=0
Calculates the output port values based on input port values.
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
virtual bool isHovered(int x, int y) const
Checks if the block is hovered over by the mouse.
Definition BaseBlock.h:85
business_logic::delegate::INewBlockChoiceDelegate * newBlockChoiceDelegate
Definition BaseBlock.h:295
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
int getBottomY() const
Gets the bottom y coordinate of the block.
Definition BaseBlock.h:212
int getRightX() const
Gets the right x coordinate of the block.
Definition BaseBlock.h:200
BaseBlock & operator=(BaseBlock &&)=delete
const FloatingPoint & getPortValue(const structures::Port *port) const
Gets the value of the port.
int getCx() const
Gets the center y coordinate of the block.
Definition BaseBlock.h:182
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
int getCy() const
Gets the center y coordinate of the block.
Definition BaseBlock.h:188
int getHeight() const
Gets the height of the block.
Definition BaseBlock.h:224
virtual const std::vector< structures::Port > & getOutputPorts() const =0
Gets the output ports of the block.
int getLeftX() const
Gets the left x coordinate of the block.
Definition BaseBlock.h:194
void onDragEnd() override
Called when dragging ends.
Definition BaseBlock.cpp:56
BaseBlock & operator=(const BaseBlock &)=delete
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
int getWidth() const
Gets the width of the block.
Definition BaseBlock.h:218
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 BlockType getBlockType() const =0
Gets the type of the block.
int getTopY() const
Gets the top y coordinate of the block.
Definition BaseBlock.h:206
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.
Interface for elements that can be dragged.
Definition IDraggable.h:13
Delegates exposed by the business_logic module, used for integrating with a GUI / renderer implementa...
The base classes and structs for elements.
BlockType
The available block types.
Definition BlockType.h:10
The elements (i.e., renderable entities constituting for the calculator) module.
The business logic module.
STL namespace.
Represents a port on a block.
Definition Port.h:11
A basic struct representing a 2D point.
Definition Point2D.h:8
std::size_t operator()(const business_logic::elements::blocks::BaseBlock &p) const
Definition BaseBlock.h:343
boost::multiprecision::cpp_dec_float_50 FloatingPoint
Definition typenames.h:6