为组件增加了focusStatus属性,增加了FocusManager类

This commit is contained in:
梦凌汐 2024-12-11 21:46:19 +08:00
parent c95bb4eb3d
commit 96e743fc26
4 changed files with 64 additions and 7 deletions

View File

@ -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) {

View File

@ -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();
}

47
utils/FocusManager.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef FOCUSMANAGER_H
#define FOCUSMANAGER_H
#include "../mystl/my_vector.h"
#include "../components/BaseComponent.h"
class FocusManager {
private:
MyVector<BaseComponent*> 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

View File

@ -1,4 +0,0 @@
#ifndef INPUTMANAGER_H
#define INPUTMANAGER_H
#endif