add onmouseover, onmouseout ui events & cleanup
This commit is contained in:
parent
418a400ea3
commit
05d6fbd995
@ -42,7 +42,7 @@ Button::Button(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (action) {
|
if (action) {
|
||||||
listenClick(action);
|
listenAction(UIAction::CLICK, action);
|
||||||
}
|
}
|
||||||
setScrollable(false);
|
setScrollable(false);
|
||||||
|
|
||||||
|
|||||||
@ -19,14 +19,14 @@ SelectBox::SelectBox(
|
|||||||
: Button(gui, selected.text, padding, nullptr, glm::vec2(contentWidth, -1)),
|
: Button(gui, selected.text, padding, nullptr, glm::vec2(contentWidth, -1)),
|
||||||
options(std::move(options)) {
|
options(std::move(options)) {
|
||||||
|
|
||||||
listenClick([this](GUI& gui) {
|
listenAction(UIAction::CLICK, [this](GUI& gui) {
|
||||||
auto panel = std::make_shared<Panel>(gui, getSize());
|
auto panel = std::make_shared<Panel>(gui, getSize());
|
||||||
panel->setPos(calcPos() + glm::vec2(0, size.y));
|
panel->setPos(calcPos() + glm::vec2(0, size.y));
|
||||||
for (const auto& option : this->options) {
|
for (const auto& option : this->options) {
|
||||||
auto button = std::make_shared<Button>(
|
auto button = std::make_shared<Button>(
|
||||||
gui, option.text, glm::vec4(10.0f), nullptr, glm::vec2(-1.0f)
|
gui, option.text, glm::vec4(10.0f), nullptr, glm::vec2(-1.0f)
|
||||||
);
|
);
|
||||||
button->listenFocus([this, option](GUI& gui) {
|
button->listenAction(UIAction::FOCUS, [this, option](GUI& gui) {
|
||||||
setSelected(option);
|
setSelected(option);
|
||||||
changeCallbacks.notify(gui, option.value);
|
changeCallbacks.notify(gui, option.value);
|
||||||
});
|
});
|
||||||
@ -34,7 +34,7 @@ SelectBox::SelectBox(
|
|||||||
}
|
}
|
||||||
panel->setZIndex(GUI::CONTEXT_MENU_ZINDEX);
|
panel->setZIndex(GUI::CONTEXT_MENU_ZINDEX);
|
||||||
gui.setFocus(panel);
|
gui.setFocus(panel);
|
||||||
panel->listenDefocus([panel=panel.get()](GUI& gui) {
|
panel->listenAction(UIAction::DEFOCUS, [panel=panel.get()](GUI& gui) {
|
||||||
gui.remove(panel);
|
gui.remove(panel);
|
||||||
});
|
});
|
||||||
gui.add(panel);
|
gui.add(panel);
|
||||||
|
|||||||
@ -11,8 +11,7 @@ using gui::Align;
|
|||||||
UINode::UINode(GUI& gui, glm::vec2 size) : gui(gui), size(size) {
|
UINode::UINode(GUI& gui, glm::vec2 size) : gui(gui), size(size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
UINode::~UINode() {
|
UINode::~UINode() = default;
|
||||||
}
|
|
||||||
|
|
||||||
bool UINode::isVisible() const {
|
bool UINode::isVisible() const {
|
||||||
if (visible && parent) {
|
if (visible && parent) {
|
||||||
@ -49,7 +48,11 @@ void UINode::setAlign(Align align) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UINode::setHover(bool flag) {
|
void UINode::setHover(bool flag) {
|
||||||
|
if (hover == flag) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
hover = flag;
|
hover = flag;
|
||||||
|
actions.notify(flag ? UIAction::MOUSE_OVER : UIAction::MOUSE_OUT, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UINode::isHover() const {
|
bool UINode::isHover() const {
|
||||||
@ -64,24 +67,8 @@ UINode* UINode::getParent() const {
|
|||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UINode::listenClick(OnAction action) {
|
void UINode::listenAction(UIAction type, OnAction action) {
|
||||||
actions.listen(UIAction::CLICK, std::move(action));
|
actions.listen(type, std::move(action));
|
||||||
}
|
|
||||||
|
|
||||||
void UINode::listenRightClick(OnAction action) {
|
|
||||||
actions.listen(UIAction::RIGHT_CLICK, std::move(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UINode::listenDoubleClick(OnAction action) {
|
|
||||||
actions.listen(UIAction::DOUBLE_CLICK, std::move(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UINode::listenFocus(OnAction action) {
|
|
||||||
actions.listen(UIAction::FOCUS, std::move(action));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UINode::listenDefocus(OnAction action) {
|
|
||||||
actions.listen(UIAction::DEFOCUS, std::move(action));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UINode::click(int, int) {
|
void UINode::click(int, int) {
|
||||||
|
|||||||
@ -53,7 +53,13 @@ namespace gui {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum class UIAction {
|
enum class UIAction {
|
||||||
CLICK, DOUBLE_CLICK, FOCUS, DEFOCUS, RIGHT_CLICK
|
CLICK,
|
||||||
|
DOUBLE_CLICK,
|
||||||
|
FOCUS,
|
||||||
|
DEFOCUS,
|
||||||
|
RIGHT_CLICK,
|
||||||
|
MOUSE_OVER,
|
||||||
|
MOUSE_OUT
|
||||||
};
|
};
|
||||||
|
|
||||||
using ActionsSet = TaggedCallbacksSet<UIAction, GUI&>;
|
using ActionsSet = TaggedCallbacksSet<UIAction, GUI&>;
|
||||||
@ -191,11 +197,7 @@ namespace gui {
|
|||||||
/// @brief Get element z-index
|
/// @brief Get element z-index
|
||||||
int getZIndex() const;
|
int getZIndex() const;
|
||||||
|
|
||||||
virtual void listenClick(OnAction action);
|
void listenAction(UIAction type, OnAction action);
|
||||||
virtual void listenRightClick(OnAction action);
|
|
||||||
virtual void listenDoubleClick(OnAction action);
|
|
||||||
virtual void listenFocus(OnAction action);
|
|
||||||
virtual void listenDefocus(OnAction action);
|
|
||||||
|
|
||||||
virtual void onFocus();
|
virtual void onFocus();
|
||||||
virtual void doubleClick(int x, int y);
|
virtual void doubleClick(int x, int y);
|
||||||
|
|||||||
@ -73,16 +73,18 @@ static runnable create_runnable(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static OnAction create_action(
|
static void register_action(
|
||||||
|
UINode& node,
|
||||||
const UiXmlReader& reader,
|
const UiXmlReader& reader,
|
||||||
const xml::xmlelement& element,
|
const xml::xmlelement& element,
|
||||||
const std::string& name
|
const std::string& name,
|
||||||
|
UIAction action
|
||||||
) {
|
) {
|
||||||
auto callback = create_runnable(reader, element, name);
|
auto callback = create_runnable(reader, element, name);
|
||||||
if (callback == nullptr) {
|
if (callback == nullptr) {
|
||||||
return nullptr;
|
return;
|
||||||
}
|
}
|
||||||
return [callback](GUI&) { callback(); };
|
node.listenAction(action, [callback](GUI&) { callback(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Read basic UINode properties
|
/// @brief Read basic UINode properties
|
||||||
@ -177,25 +179,13 @@ static void read_uinode(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto onclick = create_action(reader, element, "onclick")) {
|
register_action(node, reader, element, "onclick", UIAction::CLICK);
|
||||||
node.listenClick(onclick);
|
register_action(node, reader, element, "onrightclick", UIAction::RIGHT_CLICK);
|
||||||
}
|
register_action(node, reader, element, "onfocus", UIAction::FOCUS);
|
||||||
|
register_action(node, reader, element, "ondefocus", UIAction::DEFOCUS);
|
||||||
if (auto onclick = create_action(reader, element, "onrightclick")) {
|
register_action(node, reader, element, "ondoubleclick", UIAction::DOUBLE_CLICK);
|
||||||
node.listenRightClick(onclick);
|
register_action(node, reader, element, "onmouseover", UIAction::MOUSE_OVER);
|
||||||
}
|
register_action(node, reader, element, "onmouseout", UIAction::MOUSE_OUT);
|
||||||
|
|
||||||
if (auto onfocus = create_action(reader, element, "onfocus")) {
|
|
||||||
node.listenFocus(onfocus);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto ondefocus = create_action(reader, element, "ondefocus")) {
|
|
||||||
node.listenDefocus(ondefocus);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto ondoubleclick = create_action(reader, element, "ondoubleclick")) {
|
|
||||||
node.listenDoubleClick(ondoubleclick);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_container_impl(
|
static void read_container_impl(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user