TazGraph Project v0.1.0
Loading...
Searching...
No Matches
PortComponent.h
1#pragma once
2
3#include "../../../Components.h"
4
5// todo this can be generally a flexbox
6class PortComponent : public Component
7{
8public:
9 Color color = { 255, 255, 255, 255 };
10
11 bool isVertical = false;
12
13 std::vector<EmptyEntity*> portSlots;
14 float slotSpacing = 0.0f;
15 TransformComponent* transform = nullptr;
16
18 {
19
20 }
21
22 PortComponent(bool m_isVertical)
23 {
24 isVertical = m_isVertical;
25 }
26
28
29 }
30
31 void init() override {
32 transform = &entity->GetComponent<TransformComponent>();
33 }
34
35 void update(float deltaTime) override {
36 transform->size.x = entity->getParentEntity()->GetComponent<TransformComponent>().size.x;
37 transform->size.y = entity->getParentEntity()->GetComponent<TransformComponent>().size.y;
38
39 if (!isVertical) {
40 if (portSlots.size() > 1)
41 slotSpacing = transform->size.x / (portSlots.size());
42 else
43 slotSpacing = transform->size.x;
44 }
45 else { // Vertical
46 if (portSlots.size() > 1)
47 slotSpacing = transform->size.y / (portSlots.size());
48 else
49 slotSpacing = transform->size.y;
50 }
51
52 }
53
54 void draw(size_t v_index, PlaneColorRenderer& batch, TazGraphEngine::Window& window) {
55 glm::vec2 size(1, 1);
56 batch.draw(v_index, size, transform->bodyCenter, transform->rotation, color);
57 }
58
59 std::string GetComponentName() override {
60 return "PortComponent";
61 }
62
63 void showGUI() override {
64 ImGui::Separator();
65
66 ImVec4 a_color = ImVec4(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
67 if (ImGui::ColorPicker4("Color", (float*)&a_color)) {
68 Color newColor = {
69 (GLubyte)(a_color.x * 255),
70 (GLubyte)(a_color.y * 255),
71 (GLubyte)(a_color.z * 255),
72 (GLubyte)(a_color.w * 255)
73 };
74
75 color = newColor;
76 }
77
78 }
79
80 glm::vec3 getSlotPosition(size_t slotIndex) const {
81 if (slotIndex >= portSlots.size()) {
82 TazGraphEngine::ConsoleLogger::error("Port Slot index wrong");
83 return transform->bodyCenter;
84 }
85
86 glm::vec3 basePos = transform->bodyCenter;
87 glm::vec3 offset(0.0f);
88
89 if (!isVertical) {
90 offset.x = (static_cast<float>(slotIndex) - (portSlots.size() - 1) / 2.0f) * slotSpacing;
91 }
92 else { // Vertical
93 offset.y = (static_cast<float>(slotIndex) - (portSlots.size() - 1) / 2.0f) * slotSpacing;
94 }
95
96 return basePos + offset;
97 }
98
99 // Helper function to find the index of a specific slot
100 int getSlotIndex(EmptyEntity* slot) const {
101 for (size_t i = 0; i < portSlots.size(); ++i) {
102 if (portSlots[i] == slot) {
103 return static_cast<int>(i);
104 }
105 }
106 return -1; // Not found
107 }
108
109};
Definition GECS.h:125
Definition GECSEntity.h:7
Definition PlaneColorRenderer.h:19
void draw(size_t v_index, const glm::vec2 &rectSize, const glm::vec3 &bodyCenter, const glm::vec3 &mRotation, const Color &color)
draws are needed to convert the pos and size to vertices
Definition PlaneColorRenderer.cpp:72
Definition PortComponent.h:7
Definition Window.h:18
Definition TransformComponent.h:6
Definition Vertex.h:47