Skip to content

Commit

Permalink
Merge pull request #44 from aljen/feat-new-elements
Browse files Browse the repository at this point in the history
Features: New elements
  • Loading branch information
aljen authored Feb 16, 2018
2 parents 300160a + 139bda9 commit 6fdf4cb
Show file tree
Hide file tree
Showing 72 changed files with 4,335 additions and 62 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ option(SPAGHETTI_ENABLE_CPACK "Enable CPack" OFF)
option(SPAGHETTI_ENABLE_ALL_WARNINGS "Enable all warnings" OFF)
option(SPAGHETTI_TREAT_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
option(SPAGHETTI_USE_OPENGL "Use OpenGL in QGraphicsView" OFF)
option(SPAGHETTI_USE_CHARTS "Use Qt::Charts in Spaghetti Editor" OFF)
option(BUILD_SHARED_LIBS "Build shared libs" ON)
if (GCC OR CLANG)
option(SPAGHETTI_BUILD_NATIVE "Build native" OFF)
Expand Down
3 changes: 3 additions & 0 deletions cmake/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ if (MSVC)
list(APPEND SPAGHETTI_WARNINGS /WX)
endif ()
elseif (GCC OR CLANG)
if (MINGW)
list(APPEND SPAGHETTI_DEFINITIONS UNICODE _UNICODE NOMINMAX)
endif ()
if (SPAGHETTI_BUILD_NATIVE)
list(APPEND SPAGHETTI_FLAGS -march=native -mtune=native)
endif ()
Expand Down
70 changes: 61 additions & 9 deletions libspaghetti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ find_package(Qt5 REQUIRED COMPONENTS Widgets)
if (SPAGHETTI_USE_OPENGL)
find_package(Qt5 REQUIRED COMPONENTS OpenGL)
endif ()
if (SPAGHETTI_USE_CHARTS)
find_package(Qt5 REQUIRED COMPONENTS Charts)
endif ()

if (HAVE_CXX_FILESYSTEM)
set(SPAGHETTI_FILESYSTEM_IMPLEMENTATION SPAGHETTI_FS_STD_FILESYSTEM)
Expand Down Expand Up @@ -56,8 +59,6 @@ set(LIBSPAGHETTI_SOURCES
source/elements/logic/all.h
source/elements/logic/blinker.cc
source/elements/logic/blinker.h
source/elements/logic/clock.cc
source/elements/logic/clock.h
source/elements/logic/demultiplexer_int.cc
source/elements/logic/demultiplexer_int.h
source/elements/logic/if_equal.cc
Expand All @@ -70,10 +71,18 @@ set(LIBSPAGHETTI_SOURCES
source/elements/logic/if_lower.h
source/elements/logic/if_lower_equal.cc
source/elements/logic/if_lower_equal.h
source/elements/logic/memory_reset_set.cc
source/elements/logic/memory_reset_set.h
source/elements/logic/memory_set_reset.cc
source/elements/logic/memory_set_reset.h
source/elements/logic/multiplexer_int.cc
source/elements/logic/multiplexer_int.h
source/elements/logic/switch.cc
source/elements/logic/switch.h
source/elements/logic/trigger_falling.cc
source/elements/logic/trigger_falling.h
source/elements/logic/trigger_rising.cc
source/elements/logic/trigger_rising.h

source/elements/math/abs.cc
source/elements/math/abs.h
Expand All @@ -90,17 +99,39 @@ set(LIBSPAGHETTI_SOURCES
source/elements/math/divide.h
source/elements/math/divide_if.cc
source/elements/math/divide_if.h
source/elements/math/lerp.cc
source/elements/math/lerp.h
source/elements/math/multiply.cc
source/elements/math/multiply.h
source/elements/math/multiply_if.cc
source/elements/math/multiply_if.h
source/elements/math/sign.cc
source/elements/math/sign.h
source/elements/math/sin.cc
source/elements/math/sin.h
source/elements/math/subtract.cc
source/elements/math/subtract.h
source/elements/math/subtract_if.cc
source/elements/math/subtract_if.h

source/elements/pneumatic/all.h
source/elements/pneumatic/tank.cc
source/elements/pneumatic/tank.h
source/elements/pneumatic/valve.cc
source/elements/pneumatic/valve.h

source/elements/timers/all.h
source/elements/timers/clock.cc
source/elements/timers/clock.h
source/elements/timers/delta_time.cc
source/elements/timers/delta_time.h
source/elements/timers/t_off.cc
source/elements/timers/t_off.h
source/elements/timers/t_on.cc
source/elements/timers/t_on.h
source/elements/timers/t_pulse.cc
source/elements/timers/t_pulse.h

source/elements/ui/all.h
source/elements/ui/bcd_to_seven_segment_display.cc
source/elements/ui/bcd_to_seven_segment_display.h
Expand All @@ -116,6 +147,8 @@ set(LIBSPAGHETTI_SOURCES
source/elements/ui/seven_segment_display.h

source/elements/values/all.h
source/elements/values/characteristic_curve.cc
source/elements/values/characteristic_curve.h
source/elements/values/clamp_float.cc
source/elements/values/clamp_float.h
source/elements/values/clamp_int.cc
Expand Down Expand Up @@ -155,6 +188,10 @@ set(LIBSPAGHETTI_SOURCES
source/nodes/logic/clock.cc
source/nodes/logic/clock.h

source/nodes/pneumatic/all.h
source/nodes/pneumatic/tank.cc
source/nodes/pneumatic/tank.h

source/nodes/ui/all.h
source/nodes/ui/float_info.cc
source/nodes/ui/float_info.h
Expand Down Expand Up @@ -201,21 +238,41 @@ set(LIBSPAGHETTI_SOURCES
source/filesystem.h.in
)

set(LIBSPAGHETTI_CHARTS_SOURCES
source/nodes/values/characteristic_curve/editor_widget.cc
source/nodes/values/characteristic_curve/editor_widget.h
source/nodes/values/characteristic_curve/editor_window.cc
source/nodes/values/characteristic_curve/editor_window.h
source/nodes/values/characteristic_curve/editor_window.ui
source/nodes/values/characteristic_curve/generators.cc
source/nodes/values/characteristic_curve/generators.h
source/nodes/values/characteristic_curve/line.cc
source/nodes/values/characteristic_curve/line.h
source/nodes/values/characteristic_curve/point.cc
source/nodes/values/characteristic_curve/point.h
source/nodes/values/characteristic_curve.cc
source/nodes/values/characteristic_curve.h
)

set(LIBSPAGHETTI_ALL_SOURCES
${LIBSPAGHETTI_GENERATED_SOURCES}
${LIBSPAGHETTI_SOURCES}
)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${LIBSPAGHETTI_SOURCES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${LIBSPAGHETTI_CHARTS_SOURCES})
source_group(TREE ${CMAKE_CURRENT_BINARY_DIR} FILES ${LIBSPAGHETTI_GENERATED_SOURCES})

add_library(Spaghetti SHARED ${LIBSPAGHETTI_ALL_SOURCES})
target_sources(Spaghetti PRIVATE $<$<BOOL:${SPAGHETTI_USE_CHARTS}>:${LIBSPAGHETTI_CHARTS_SOURCES}>)
target_compile_features(Spaghetti PUBLIC cxx_std_17)
target_compile_definitions(Spaghetti
PUBLIC SPAGHETTI_SHARED
PRIVATE SPAGHETTI_EXPORTS ${SPAGHETTI_DEFINITIONS}
PRIVATE $<$<CONFIG:Debug>:${SPAGHETTI_DEFINITIONS_DEBUG}>
PRIVATE $<$<CONFIG:Release>:${SPAGHETTI_DEFINITIONS_RELEASE}>
PRIVATE $<$<BOOL:${SPAGHETTI_USE_OPENGL}>:SPAGHETTI_USE_OPENGL>
PRIVATE $<$<BOOL:${SPAGHETTI_USE_CHARTS}>:SPAGHETTI_USE_CHARTS>
)
target_compile_options(Spaghetti
PRIVATE ${SPAGHETTI_FLAGS}
Expand All @@ -236,18 +293,13 @@ target_include_directories(Spaghetti
target_include_directories(Spaghetti SYSTEM PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/vendor>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/vendor/spdlog/include>
## $<INSTALL_INTERFACE:vendor>
## $<INSTALL_INTERFACE:vendor/spdlog/include>
)
target_link_libraries(Spaghetti
PUBLIC ${CMAKE_THREAD_LIBS_INIT} Qt5::Widgets
PRIVATE ${CMAKE_DL_LIBS} ${CXX_FILESYSTEM_LIBS}
PRIVATE $<$<BOOL:${SPAGHETTI_USE_OPENGL}>:Qt5::OpenGL>
PRIVATE $<$<BOOL:${SPAGHETTI_USE_CHARTS}>:Qt5::Charts>
)
if (SPAGHETTI_USE_OPENGL)
target_compile_definitions(Spaghetti PRIVATE SPAGHETTI_USE_OPENGL)
target_link_libraries(Spaghetti PUBLIC Qt5::OpenGL)
endif ()

if (CLANG)
target_link_libraries(Spaghetti PUBLIC -stdlib=libc++)
endif ()
Expand Down
30 changes: 26 additions & 4 deletions libspaghetti/include/spaghetti/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ class SPAGHETTI_API Element {
using Json = nlohmann::json;
using Value = std::variant<bool, int32_t, float>;
enum class ValueType { eBool, eInt, eFloat };
template<typename T>
struct Vec2 {
double x{}, y{};
T x{};
T y{};
};
using vec2 = Vec2<int32_t>;
using vec2f = Vec2<float>;
using vec2d = Vec2<double>;

struct IOSocket {
enum Flags {
Expand Down Expand Up @@ -104,12 +109,15 @@ class SPAGHETTI_API Element {
m_position.x = a_x;
m_position.y = a_y;
}
void setPosition(Vec2 const a_position) { m_position = a_position; }
Vec2 const &position() const { return m_position; }
void setPosition(vec2d const a_position) { m_position = a_position; }
vec2d const &position() const { return m_position; }

void iconify(bool const a_iconify) { m_isIconified = a_iconify; }
bool isIconified() const { return m_isIconified; }

void setIconifyingHidesCentralWidget(bool const a_hide) { m_iconifyingHidesCentralWidget = a_hide; }
bool iconifyingHidesCentralWidget() const { return m_iconifyingHidesCentralWidget; }

IOSockets &inputs() { return m_inputs; }
IOSockets const &inputs() const { return m_inputs; }
IOSockets &outputs() { return m_outputs; }
Expand Down Expand Up @@ -188,8 +196,9 @@ class SPAGHETTI_API Element {
private:
size_t m_id{};
std::string m_name{};
Vec2 m_position{};
vec2d m_position{};
bool m_isIconified{};
bool m_iconifyingHidesCentralWidget{};
uint8_t m_minInputs{};
uint8_t m_maxInputs{ std::numeric_limits<uint8_t>::max() };
uint8_t m_minOutputs{};
Expand All @@ -198,6 +207,19 @@ class SPAGHETTI_API Element {
uint8_t m_defaultNewOutputFlags{};
};

template<typename T>
inline void to_json(Element::Json &a_json, Element::Vec2<T> const &a_value)
{
a_json = Element::Json{ a_value.x, a_value.y };
}

template<typename T>
inline void from_json(Element::Json const &a_json, Element::Vec2<T> &a_value)
{
a_value.x = a_json[0].get<T>();
a_value.y = a_json[1].get<T>();
}

} // namespace spaghetti

#endif // SPAGHETTI_ELEMENT_H
5 changes: 4 additions & 1 deletion libspaghetti/include/spaghetti/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ SPAGHETTI_API Loggers get();
inline void init_from_plugin()
{
auto loggers = get();
for (auto &&logger : loggers) spdlog::register_logger(logger);
try {
for (auto &&logger : loggers) spdlog::register_logger(logger);
} catch (spdlog::spdlog_ex &) {
}
}

} // namespace spaghetti::log
Expand Down
4 changes: 4 additions & 0 deletions libspaghetti/include/spaghetti/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ class SPAGHETTI_API Node : public QGraphicsItem {
virtual void showProperties();
virtual void refreshCentralWidget() {}
virtual void elementSet() {}
virtual bool open() { return false; }

void showCommonProperties();
void showIOProperties(IOSocketsType const a_type);

void calculateBoundingRect();

void changeInputName(int const a_id, QString const a_name);
void changeOutputName(int const a_id, QString const a_name);

protected:
void setCentralWidget(QGraphicsItem *a_centralWidget);
void propertiesInsertTitle(QString a_title);
Expand Down
12 changes: 6 additions & 6 deletions libspaghetti/include/spaghetti/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ class SPAGHETTI_API Package final : public Element {
void resumeDispatchThread();

void setInputsPosition(double const a_x, double const a_y);
void setInputsPosition(Vec2 const a_position) { m_inputsPosition = a_position; }
Vec2 const &inputsPosition() const { return m_inputsPosition; }
void setInputsPosition(vec2d const a_position) { m_inputsPosition = a_position; }
vec2d const &inputsPosition() const { return m_inputsPosition; }

void setOutputsPosition(double const a_x, double const a_y);
void setOutputsPosition(Vec2 const a_position) { m_outputsPosition = a_position; }
Vec2 const &outputsPosition() const { return m_outputsPosition; }
void setOutputsPosition(vec2d const a_position) { m_outputsPosition = a_position; }
vec2d const &outputsPosition() const { return m_outputsPosition; }

Elements const &elements() const { return m_elements; }
Connections const &connections() const { return m_connections; }
Expand All @@ -119,8 +119,8 @@ class SPAGHETTI_API Package final : public Element {
std::string m_packageDescription{ "A package" };
std::string m_packagePath{ "packages/unknown_package" };
std::string m_packageIcon{ "icons/unknown.png" };
Vec2 m_inputsPosition{};
Vec2 m_outputsPosition{};
vec2d m_inputsPosition{};
vec2d m_outputsPosition{};
Elements m_elements{};
Connections m_connections{};

Expand Down
6 changes: 6 additions & 0 deletions libspaghetti/include/spaghetti/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ inline bool nearly_equal(float const &a_a, float const &a_b)
std::nextafter(a_a, std::numeric_limits<float>::max()) >= a_b;
}

template<typename T>
inline T lerp(T a_v0, T a_v1, T a_t)
{
return (1 - a_t) * a_v0 + a_t * a_v1;
}

} // namespace spaghetti

#endif // SPAGHETTI_UTILS_H
3 changes: 3 additions & 0 deletions libspaghetti/source/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void Element::serialize(Element::Json &a_json)
jsonNode["position"]["x"] = m_position.x;
jsonNode["position"]["y"] = m_position.y;
jsonNode["iconify"] = m_isIconified;
jsonNode["iconifying_hides_central_widget"] = m_iconifyingHidesCentralWidget;
}

void Element::deserialize(Json const &a_json)
Expand All @@ -101,6 +102,7 @@ void Element::deserialize(Json const &a_json)

auto const &NODE = a_json["node"];
auto const ICONIFY = NODE["iconify"].get<bool>();
auto const ICONIFYING_HIDES_CENTRAL_WIDGET = NODE["iconifying_hides_central_widget"].get<bool>();
auto const &POSITION = NODE["position"];
auto const POSITION_X = POSITION["x"].get<double>();
auto const POSITION_Y = POSITION["y"].get<double>();
Expand All @@ -116,6 +118,7 @@ void Element::deserialize(Json const &a_json)
setDefaultNewInputFlags(DEFAULT_NEW_INPUT_FLAGS);
setDefaultNewOutputFlags(DEFAULT_NEW_OUTPUT_FLAGS);
iconify(ICONIFY);
setIconifyingHidesCentralWidget(ICONIFYING_HIDES_CENTRAL_WIDGET);

auto add_socket = [&](Json const &a_socket, bool const a_input, uint8_t &a_socketCount) {
auto const SOCKET_ID = a_socket["socket"].get<uint8_t>();
Expand Down
2 changes: 2 additions & 0 deletions libspaghetti/source/elements/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "elements/gates/all.h"
#include "elements/logic/all.h"
#include "elements/math/all.h"
#include "elements/pneumatic/all.h"
#include "elements/timers/all.h"
#include "elements/ui/all.h"
#include "elements/values/all.h"
#include "spaghetti/package.h"
Expand Down
5 changes: 4 additions & 1 deletion libspaghetti/source/elements/logic/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
#define ELEMENTS_LOGIC_ALL_H

#include "elements/logic/blinker.h"
#include "elements/logic/clock.h"
#include "elements/logic/demultiplexer_int.h"
#include "elements/logic/if_equal.h"
#include "elements/logic/if_greater.h"
#include "elements/logic/if_greater_equal.h"
#include "elements/logic/if_lower.h"
#include "elements/logic/if_lower_equal.h"
#include "elements/logic/memory_reset_set.h"
#include "elements/logic/memory_set_reset.h"
#include "elements/logic/multiplexer_int.h"
#include "elements/logic/switch.h"
#include "elements/logic/trigger_falling.h"
#include "elements/logic/trigger_rising.h"

#endif // ELEMENTS_LOGIC_ALL_H
2 changes: 2 additions & 0 deletions libspaghetti/source/elements/logic/demultiplexer_int.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void DemultiplexerInt::calculate()
int32_t const SIZE{ static_cast<int32_t>(m_outputs.size()) - 1 };
int32_t const INDEX{ std::clamp<int32_t>(SELECT, 0, SIZE) };

for (auto &&output : m_outputs) output.value = 0;

m_outputs[static_cast<size_t>(INDEX)].value = VALUE;
}

Expand Down
Loading

0 comments on commit 6fdf4cb

Please sign in to comment.