diff --git a/components/BaseComponent.h b/components/BaseComponent.h index bc663e9..954edcb 100644 --- a/components/BaseComponent.h +++ b/components/BaseComponent.h @@ -4,7 +4,7 @@ class BaseComponent { protected: int top, left, width, height; - bool focused; + bool focusStatus; public: BaseComponent(int top, int left, int width, int height) : top(top), left(left), width(width), height(height) {}; @@ -36,8 +36,12 @@ public: return height; } - void setFocused(bool focused) { - this->focused = focused; + void setFocus(bool focused) { + this->focusStatus = focused; + } + + bool isFocused() { + return focusStatus; } void setPosition(int top, int left) { diff --git a/mystl/my_vector.h b/mystl/my_vector.h index ba8abf5..ee81e59 100644 --- a/mystl/my_vector.h +++ b/mystl/my_vector.h @@ -88,6 +88,16 @@ public: } } + void erase(size_t index) override { + if (index >= m_size) { + throw std::out_of_range("Index out of range"); + } + for (size_t i = index; i < m_size - 1; ++i) { + m_data[i] = std::move(m_data[i + 1]); + } + --m_size; + } + T& top() override { return back(); } diff --git a/utils/FocusManager.h b/utils/FocusManager.h new file mode 100644 index 0000000..66e3716 --- /dev/null +++ b/utils/FocusManager.h @@ -0,0 +1,47 @@ +#ifndef FOCUSMANAGER_H +#define FOCUSMANAGER_H + + +#include "../mystl/my_vector.h" +#include "../components/BaseComponent.h" + +class FocusManager { +private: + MyVector components; + +public: + FocusManager(); + ~FocusManager() { + components.clear(); + } + + void addComponent(BaseComponent* component) { + components.push_back(component); + } + + void removeComponent(BaseComponent* component) { + for(int i = 0; i < components.size(); i++) { + if(components[i] == component) { + components.erase(i); + break; + } + } + } + + void setFocus(BaseComponent* component) { + if(component->isFocused()) return; + for(int i = 0; i < components.size(); i++) { + components[i]->setFocus(false); + } + component->setFocus(true); + } + + BaseComponent* getFocusedComponent() { + for(int i = 0; i < components.size(); i++) { + if(components[i]->isFocused()) return components[i]; + } + return NULL; + } +}; + +#endif \ No newline at end of file diff --git a/utils/InputManager.h b/utils/InputManager.h deleted file mode 100644 index c19ab40..0000000 --- a/utils/InputManager.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef INPUTMANAGER_H -#define INPUTMANAGER_H - -#endif \ No newline at end of file