FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
business_logic::elements::blocks::BaseBlock Class Referenceabstract

The base class for all blocks, containing common functionality and members. More...

#include <BaseBlock.h>

+ Inheritance diagram for business_logic::elements::blocks::BaseBlock:
+ Collaboration diagram for business_logic::elements::blocks::BaseBlock:

Public Member Functions

 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.
 
void updateWidthHeight (int newWidth, int newHeight)
 Updates the width and height of the block.
 
 ~BaseBlock () noexcept override
 
 BaseBlock (const BaseBlock &)=delete
 
BaseBlockoperator= (const BaseBlock &)=delete
 
 BaseBlock (BaseBlock &&)=delete
 
BaseBlockoperator= (BaseBlock &&)=delete
 
virtual bool isHovered (int x, int y) const
 Checks if the block is hovered over by the mouse.
 
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 present, it will not be rendered.
 
void onDragStart () override
 Called when dragging starts.
 
void onDragProgress (int x, int y) override
 Called during dragging.
 
void onDragEnd () override
 Called when dragging ends.
 
const business_logic::geometry::Point2DgetPortCoordinates (const structures::Port *port) const
 Gets the coordinates of a port.
 
std::optional< const structures::Port * > getPortAtCoordinates (const geometry::Point2D &point) const
 Gets the port at given coordinates.
 
virtual std::string getSelfId () const =0
 
virtual BlockType getBlockType () const =0
 Gets the type of the block.
 
const FloatingPointgetPortValue (const structures::Port *port) const
 Gets the value of the port.
 
void setPortValue (const business_logic::elements::structures::Port *port, const FloatingPoint &value)
 Sets the value of the port.
 
virtual void calculateOutputValues ()=0
 Calculates the output port values based on input port values.
 
virtual const std::vector< structures::Port > & getInputPorts () const =0
 Gets the input ports of the block.
 
virtual const std::vector< structures::Port > & getOutputPorts () const =0
 Gets the output ports of the block.
 
int getCx () const
 Gets the center y coordinate of the block.
 
int getCy () const
 Gets the center y coordinate of the block.
 
int getLeftX () const
 Gets the left x coordinate of the block.
 
int getRightX () const
 Gets the right x coordinate of the block.
 
int getTopY () const
 Gets the top y coordinate of the block.
 
int getBottomY () const
 Gets the bottom y coordinate of the block.
 
int getWidth () const
 Gets the width of the block.
 
int getHeight () const
 Gets the height of the block.
 
std::unordered_map< const structures::Port *, geometry::Point2D > & getPortCoordinates ()
 Gets the cache of (center) coordinates of the ports of the block; updated after rendering.
 
- Public Member Functions inherited from business_logic::elements::interactions::IDraggable
virtual ~IDraggable ()=default
 

Protected Member Functions

void cacheCornerCoordinates ()
 Caches the bottom right corner of the block, so that it is not recalculated every time isHovered() is called.
 
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.
 

Protected Attributes

int cx
 
int cy
 
int width
 
int height
 
int leftX
 
int rightX
 
int topY
 
int bottomY
 
float centerY
 
std::shared_ptr< spdlog::logger > logger
 
std::unordered_map< const structures::Port *, FloatingPointportValues
 The registry of port values.
 
business_logic::delegate::INewBlockChoiceDelegatenewBlockChoiceDelegate
 
business_logic::delegate::IBlockLifecycleManagerDelegateblockLifecycleManagerDelegate
 
business_logic::delegate::IWindowDelegatewindowDelegate
 
std::unordered_map< const structures::Port *, geometry::Point2DportCoordinates
 Caches the (center) coordinates of the ports of the block; must be updated after rendering.
 

Static Protected Attributes

static const FloatingPoint NaN = std::numeric_limits<FloatingPoint>::quiet_NaN()
 

Friends

struct std::hash< BaseBlock >
 

Detailed Description

The base class for all blocks, containing common functionality and members.

Any block subclassing this class can be dragged around to change its position

Definition at line 36 of file BaseBlock.h.

Constructor & Destructor Documentation

◆ BaseBlock() [1/3]

business_logic::elements::blocks::BaseBlock::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.

Parameters
cxThe center x coordinate where to place the center of the block
cyThe center y coordinate where to place the center of the block
blockWidthThe width of the block
blockHeightThe height of the block
newBlockChoiceDelegateThe delegate that is notified when a new block is chosen to be added to the canvas
blockLifecycleManagerDelegateThe delegate that manages the lifecycles of blocks
windowDelegateThe delegate of the window
loggerThe logger for the block

Definition at line 10 of file BaseBlock.cpp.

References cacheCornerCoordinates().

+ Here is the call graph for this function:

◆ ~BaseBlock()

business_logic::elements::blocks::BaseBlock::~BaseBlock ( )
overridenoexcept

Definition at line 35 of file BaseBlock.cpp.

References blockLifecycleManagerDelegate, and business_logic::delegate::IBlockLifecycleManagerDelegate::onBlockDeleted().

+ Here is the call graph for this function:

◆ BaseBlock() [2/3]

business_logic::elements::blocks::BaseBlock::BaseBlock ( const BaseBlock )
delete

◆ BaseBlock() [3/3]

business_logic::elements::blocks::BaseBlock::BaseBlock ( BaseBlock &&  )
delete

Member Function Documentation

◆ cacheCornerCoordinates()

void business_logic::elements::blocks::BaseBlock::cacheCornerCoordinates ( )
protected

Caches the bottom right corner of the block, so that it is not recalculated every time isHovered() is called.

Definition at line 58 of file BaseBlock.cpp.

References bottomY, centerY, cx, cy, business_logic::delegate::IWindowDelegate::getWindowSize(), height, leftX, rightX, topY, width, and windowDelegate.

Referenced by BaseBlock(), onDragProgress(), and updateWidthHeight().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ calculateOutputValues()

virtual void business_logic::elements::blocks::BaseBlock::calculateOutputValues ( )
pure virtual

◆ checkPort()

std::optional< const business_logic::elements::structures::Port * > business_logic::elements::blocks::BaseBlock::checkPort ( const structures::Port port,
const geometry::Point2D point 
) const
protected

Checks if a point is inside the hitbox of a port.

Parameters
portThe port that hitbox of is to be checked
pointThe point to check whether it is inside the hitbox of the port
Returns
An optional carrying the Port if it is hit or std::nullopt otherwise

Definition at line 113 of file BaseBlock.cpp.

References getPortCoordinates(), business_logic::geometry::isCircleHovered(), business_logic::constants::TOTAL_PORT_HITBOX_RADIUS, business_logic::geometry::Point2D::x, and business_logic::geometry::Point2D::y.

Referenced by getPortAtCoordinates().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBlockType()

◆ getBottomY()

int business_logic::elements::blocks::BaseBlock::getBottomY ( ) const
inline

Gets the bottom y coordinate of the block.

Returns
The bottom y coordinate of the block

Definition at line 212 of file BaseBlock.h.

References bottomY.

Referenced by gui::elements::SkiaBaseBlockRenderer::render().

+ Here is the caller graph for this function:

◆ getCx()

int business_logic::elements::blocks::BaseBlock::getCx ( ) const
inline

Gets the center y coordinate of the block.

Returns
The center y coordinate of the block

Definition at line 182 of file BaseBlock.h.

References cx.

◆ getCy()

int business_logic::elements::blocks::BaseBlock::getCy ( ) const
inline

Gets the center y coordinate of the block.

Returns
The center y coordinate of the block

Definition at line 188 of file BaseBlock.h.

References cy.

Referenced by gui::elements::SkiaBaseBlockRenderer::render().

+ Here is the caller graph for this function:

◆ getHeight()

int business_logic::elements::blocks::BaseBlock::getHeight ( ) const
inline

Gets the height of the block.

Returns
The height of the block

Definition at line 224 of file BaseBlock.h.

References height.

◆ getInputPorts()

◆ getLeftX()

int business_logic::elements::blocks::BaseBlock::getLeftX ( ) const
inline

Gets the left x coordinate of the block.

Returns
The left x coordinate of the block

Definition at line 194 of file BaseBlock.h.

References leftX.

Referenced by gui::elements::SkiaBaseBlockRenderer::render(), and gui::elements::SkiaBaseBlockRenderer::renderValueAboveBlock().

+ Here is the caller graph for this function:

◆ getOutputPorts()

◆ getPortAtCoordinates()

std::optional< const business_logic::elements::structures::Port * > business_logic::elements::blocks::BaseBlock::getPortAtCoordinates ( const geometry::Point2D point) const

Gets the port at given coordinates.

Parameters
pointThe coordinates to check
Returns
An optional carrying the Port if it is hit or std::nullopt otherwise

Definition at line 130 of file BaseBlock.cpp.

References checkPort(), getInputPorts(), and getOutputPorts().

Referenced by business_logic::BlocksManager::handleRightClickOnBlock().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPortCoordinates() [1/2]

std::unordered_map< const structures::Port *, geometry::Point2D > & business_logic::elements::blocks::BaseBlock::getPortCoordinates ( )
inline

Gets the cache of (center) coordinates of the ports of the block; updated after rendering.

Definition at line 232 of file BaseBlock.h.

References portCoordinates.

Referenced by checkPort().

+ Here is the caller graph for this function:

◆ getPortCoordinates() [2/2]

const business_logic::geometry::Point2D & business_logic::elements::blocks::BaseBlock::getPortCoordinates ( const structures::Port port) const

Gets the coordinates of a port.

Parameters
portThe port to get the coordinates of
Returns
The coordinates of the port

Definition at line 100 of file BaseBlock.cpp.

References portCoordinates, and business_logic::geometry::Point2D::x.

Referenced by gui::elements::SkiaBaseBlockRenderer::render().

+ Here is the caller graph for this function:

◆ getPortValue()

◆ getRightX()

int business_logic::elements::blocks::BaseBlock::getRightX ( ) const
inline

Gets the right x coordinate of the block.

Returns
The right x coordinate of the block

Definition at line 200 of file BaseBlock.h.

References rightX.

Referenced by gui::elements::SkiaBaseBlockRenderer::render().

+ Here is the caller graph for this function:

◆ getSelfId()

virtual std::string business_logic::elements::blocks::BaseBlock::getSelfId ( ) const
pure virtual

◆ getTopY()

int business_logic::elements::blocks::BaseBlock::getTopY ( ) const
inline

Gets the top y coordinate of the block.

Returns
The top y coordinate of the block

Definition at line 206 of file BaseBlock.h.

References topY.

Referenced by gui::elements::SkiaBaseBlockRenderer::render(), and gui::elements::SkiaBaseBlockRenderer::renderValueAboveBlock().

+ Here is the caller graph for this function:

◆ getValueToRenderAboveBlock()

std::optional< std::string > business_logic::elements::blocks::BaseBlock::getValueToRenderAboveBlock ( bool  isHovered)
virtual

Wrapper that gets the value of the output port above the block; if more or less than 1 output port is present, it will not be rendered.

Parameters
isHoveredTrue if the block is hovered over, false otherwise
Returns
The value to render above the block or std::nullopt if the value should not be rendered

Reimplemented in business_logic::elements::blocks::MonitorBlock.

Definition at line 81 of file BaseBlock.cpp.

References business_logic::constants::DEFAULT_VALUE_DISPLAY_PRECISION, getOutputPorts(), and getPortValue().

Referenced by gui::elements::SkiaBaseBlockRenderer::render().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getWidth()

int business_logic::elements::blocks::BaseBlock::getWidth ( ) const
inline

Gets the width of the block.

Returns
The width of the block

Definition at line 218 of file BaseBlock.h.

References width.

Referenced by gui::elements::SkiaBaseBlockRenderer::render(), and gui::elements::SkiaBaseBlockRenderer::renderValueAboveBlock().

+ Here is the caller graph for this function:

◆ isHovered()

virtual bool business_logic::elements::blocks::BaseBlock::isHovered ( int  x,
int  y 
) const
inlinevirtual

Checks if the block is hovered over by the mouse.

Parameters
xThe x coordinate of the mouse
yThe y coordinate of the mouse
Returns
True if the block is hovered over, false otherwise

Definition at line 85 of file BaseBlock.h.

References bottomY, leftX, and topY.

Referenced by business_logic::elements::blocks::MonitorBlock::getValueToRenderAboveBlock().

+ Here is the caller graph for this function:

◆ onDragEnd()

void business_logic::elements::blocks::BaseBlock::onDragEnd ( )
overridevirtual

Called when dragging ends.

Implements business_logic::elements::interactions::IDraggable.

Definition at line 56 of file BaseBlock.cpp.

References getSelfId(), and logger.

Referenced by business_logic::BlocksManager::handleMouseUp().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ onDragProgress()

void business_logic::elements::blocks::BaseBlock::onDragProgress ( int  x,
int  y 
)
overridevirtual

Called during dragging.

Parameters
xCurrent X coordinate
yCurrent Y coordinate

Implements business_logic::elements::interactions::IDraggable.

Definition at line 45 of file BaseBlock.cpp.

References cacheCornerCoordinates(), cx, cy, getSelfId(), and logger.

Referenced by business_logic::BlocksManager::handleMouseMove().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ onDragStart()

void business_logic::elements::blocks::BaseBlock::onDragStart ( )
overridevirtual

Called when dragging starts.

Implements business_logic::elements::interactions::IDraggable.

Definition at line 54 of file BaseBlock.cpp.

References getSelfId(), and logger.

Referenced by business_logic::BlocksManager::handleMouseDown().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=() [1/2]

BaseBlock & business_logic::elements::blocks::BaseBlock::operator= ( BaseBlock &&  )
delete

◆ operator=() [2/2]

BaseBlock & business_logic::elements::blocks::BaseBlock::operator= ( const BaseBlock )
delete

◆ setPortValue()

void business_logic::elements::blocks::BaseBlock::setPortValue ( const business_logic::elements::structures::Port port,
const FloatingPoint value 
)

Sets the value of the port.

Parameters
portThe port to set the value of
valueThe value to set the port to

Definition at line 160 of file BaseBlock.cpp.

References portValues.

Referenced by business_logic::calculations::BlocksCalculator::calculateBlockValues().

+ Here is the caller graph for this function:

◆ updateWidthHeight()

void business_logic::elements::blocks::BaseBlock::updateWidthHeight ( int  newWidth,
int  newHeight 
)

Updates the width and height of the block.

Parameters
newWidthThe new width of the block
newHeightThe new height of the block

Definition at line 38 of file BaseBlock.cpp.

References cacheCornerCoordinates(), height, and width.

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ std::hash< BaseBlock >

friend struct std::hash< BaseBlock >
friend

Definition at line 161 of file BaseBlock.h.

Member Data Documentation

◆ blockLifecycleManagerDelegate

business_logic::delegate::IBlockLifecycleManagerDelegate* business_logic::elements::blocks::BaseBlock::blockLifecycleManagerDelegate
protected

The delegate that manages the lifecycles of blocks

Definition at line 301 of file BaseBlock.h.

Referenced by business_logic::elements::blocks::MonitorBlock::getValueToRenderAboveBlock(), and ~BaseBlock().

◆ bottomY

int business_logic::elements::blocks::BaseBlock::bottomY
protected

The bottom edge y of the block; cached and used for the needs of isHovered()

Definition at line 275 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getBottomY(), and isHovered().

◆ centerY

float business_logic::elements::blocks::BaseBlock::centerY
protected

The center y coordinate of the block; cached and used elsewhere

Definition at line 280 of file BaseBlock.h.

Referenced by cacheCornerCoordinates().

◆ cx

int business_logic::elements::blocks::BaseBlock::cx
protected

The center x coordinate of the block

Definition at line 240 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getCx(), onDragProgress(), and std::hash< business_logic::elements::blocks::BaseBlock >::operator()().

◆ cy

int business_logic::elements::blocks::BaseBlock::cy
protected

The center y coordinate of the block

Definition at line 245 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getCy(), onDragProgress(), and std::hash< business_logic::elements::blocks::BaseBlock >::operator()().

◆ height

int business_logic::elements::blocks::BaseBlock::height
protected

The height of the block

Definition at line 255 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getHeight(), and updateWidthHeight().

◆ leftX

int business_logic::elements::blocks::BaseBlock::leftX
protected

The left edge x coordinate of the block; cached and used elsewhere

Definition at line 260 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getLeftX(), and isHovered().

◆ logger

std::shared_ptr<spdlog::logger> business_logic::elements::blocks::BaseBlock::logger
protected

The logger for the block

Definition at line 285 of file BaseBlock.h.

Referenced by business_logic::elements::blocks::ConstantBlock::onDoubleClick(), onDragEnd(), onDragProgress(), and onDragStart().

◆ NaN

const FloatingPoint business_logic::elements::blocks::BaseBlock::NaN = std::numeric_limits<FloatingPoint>::quiet_NaN()
staticprotected

The NaN value - for convenience of usage in blocks implementations

Definition at line 312 of file BaseBlock.h.

Referenced by getPortValue().

◆ newBlockChoiceDelegate

business_logic::delegate::INewBlockChoiceDelegate* business_logic::elements::blocks::BaseBlock::newBlockChoiceDelegate
protected

The delegate that is notified when a new block is chosen to be added to the canvas

Definition at line 295 of file BaseBlock.h.

◆ portCoordinates

std::unordered_map<const structures::Port*, geometry::Point2D> business_logic::elements::blocks::BaseBlock::portCoordinates
protected

Caches the (center) coordinates of the ports of the block; must be updated after rendering.

Definition at line 318 of file BaseBlock.h.

Referenced by getPortCoordinates(), and getPortCoordinates().

◆ portValues

◆ rightX

int business_logic::elements::blocks::BaseBlock::rightX
protected

The right edge x of the block; cached and used for the needs of isHovered()

Definition at line 265 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), and getRightX().

◆ topY

int business_logic::elements::blocks::BaseBlock::topY
protected

The top edge y coordinate of the block; cached and used elsewhere

Definition at line 270 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getTopY(), and isHovered().

◆ width

int business_logic::elements::blocks::BaseBlock::width
protected

The width of the block

Definition at line 250 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), getWidth(), and updateWidthHeight().

◆ windowDelegate

business_logic::delegate::IWindowDelegate* business_logic::elements::blocks::BaseBlock::windowDelegate
protected

The delegate of the window

Definition at line 307 of file BaseBlock.h.

Referenced by cacheCornerCoordinates(), and business_logic::elements::blocks::ConstantBlock::onDoubleClick().


The documentation for this class was generated from the following files: