Files
Renderive/YSGraphic_Core/component/Button.h
T
2026-07-23 18:11:07 +08:00

83 lines
2.5 KiB
C++

#pragma once
#include "Background.h"
#include "Content.h"
#include <QAction>
// #define SETTER3(class_name, Type, prop, value) \
// Type prop;
//
// #define SETTER2(class_name, Type, prop) \
// class_name& set_##prop(const Type& prop) { this->##prop = prop; return *this;} \
// Type prop;
//
// #define SETTER(Type, prop, value) \
// auto& set_##prop(const Type& prop) { this->##prop = prop; return *this;} \
// Type prop = value;
struct Button : Base {
Q_OBJECT
public:
Background background;
Content content;
Button() {
paintAbles.append(&background);
paintAbles.append(&content);
}
[[nodiscard]] QSize sizeHint() const override {
auto ret = content.sizeHint();
if (background.border) {
int a = background.border->len * 2;
ret += QSize(a, a);
}
return ret;
}
};
inline Button* create_icon(const QString& path, const QColor& background, const Handle_Mouse& f) {
auto ret = new Button();
ret->content.set_icon(path, QColor(Qt::color0));
ret->background.set_color(background);
ret->background.mouse_handlers.append(f);
return ret;
};
struct Switch_Button : Base {
Q_OBJECT
public:
Background background;
Content content1;
Content content2;
Switch_Button() {
paintAbles.append(&background);
paintAbles.append(&content1);
paintAbles.append(&content2);
content1.show = true;
content2.show = false;
}
[[nodiscard]] QSize sizeHint() const override {
auto ret = content1.show ? content1.sizeHint() : content2.sizeHint();
if (background.border) {
int a = background.border->len * 2;
ret += QSize(a, a);
}
return ret;
}
};
inline Switch_Button* create_switch_icon(const QColor& background, const QString& path, const Handle_Mouse& f,
const QString& path2, const Handle_Mouse& f2) {
auto ret = new Switch_Button();
ret->content1.set_icon(path, QColor(Qt::color0));
ret->content2.set_icon(path2, QColor(Qt::color0));
ret->background.set_color(background);
ret->background.mouse_handlers.append([ret, f, f2](QEvent::Type t, QMouseEvent* e) {
if (t == QEvent::MouseButtonPress) {
if (ret->content1.show) {
f(t, e);
}
else {
f2(t, e);
}
std::swap(ret->content1.show, ret->content2.show);
ret->update();
}
});
return ret;
}