FifeGUI 0.2.0
A C++ GUI library designed for games.
modalbackdrop.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2026 Fifengine contributors
3
4// Corresponding header include
5#include "fifechan/widgets/modalbackdrop.hpp"
6
7// Project headers
8#include "fifechan/graphics.hpp"
9#include "fifechan/widgets/menubar.hpp"
10#include "fifechan/widgets/menuitem.hpp"
11#include "fifechan/widgets/menupopup.hpp"
12
13namespace fcn
14{
16 {
17 setOpaque(false);
18 setFocusable(false);
19 setEnabled(true);
20 }
21
22 ModalBackdrop::~ModalBackdrop() = default;
23
24 void ModalBackdrop::draw([[maybe_unused]] Graphics*)
25 {
26 // Intentionally transparent; no drawing required.
27 }
28
30 {
31 // Click outside the menu should close the owning menu tree.
32 // If the click targets the top MenuBar or a MenuItem,
33 // we should allow the event to propagate,
34 // so the MenuBar can toggle the popup.
35 int const x = event.getX();
36 int const y = event.getY();
37
38 bool clickHitsMenuBar = false;
39 Container const * topContainer = dynamic_cast<Container*>(getTop());
40 if (topContainer != nullptr) {
41 for (unsigned i = 0; i < topContainer->getChildrenCount(); ++i) {
42 Widget* child = topContainer->getChild(i);
43 if (child == nullptr || child == this) {
44 continue;
45 }
46 int cx = 0;
47 int cy = 0;
48 child->getAbsolutePosition(cx, cy);
49 if (x >= cx && x < cx + child->getWidth() && y >= cy && y < cy + child->getHeight()) {
50 if (dynamic_cast<MenuBar*>(child) != nullptr || dynamic_cast<MenuItem*>(child) != nullptr) {
51 clickHitsMenuBar = true;
52 break;
53 }
54 }
55 }
56 }
57
58 // If the click hits the top MenuBar/MenuItem, allow the event to
59 // propagate so the MenuBar can handle toggling the popup.
60 if (clickHitsMenuBar) {
61 return;
62 }
63
64 // Otherwise close the owning popup and consume the event
65 // so other widgets don't react to the click.
66 if (mOwner != nullptr) {
67 mOwner->hide();
68 }
69
70 event.consume();
71 }
72
74 {
75 }
77 {
78 }
79 void ModalBackdrop::mouseExited([[maybe_unused]] MouseEvent&)
80 {
81 }
82} // namespace fcn
Container()
Constructor.
Widget * getChild(unsigned int index) const
Gets child by index.
virtual void setOpaque(bool opaque)
Sets the container to be opaque or not.
Definition container.cpp:82
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
A menu bar widget that displays menus at the top of a window.
Definition menubar.hpp:32
A menu item widget for use in menus.
Definition menuitem.hpp:124
A menu popup widget that displays a dropdown menu.
Definition menupopup.hpp:38
void mousePressed(MouseEvent &event) override
Called when a mouse button has been pressed down on the widget area.
ModalBackdrop(MenuPopup *owner)
Constructor.
void mouseExited(MouseEvent &event) override
Called when the mouse has exited the widget area.
void mouseEntered(MouseEvent &event) override
Called when the mouse has entered into the widget area.
void draw(Graphics *graphics) override
Draws the widget.
void mouseReleased(MouseEvent &event) override
Called when a mouse button has been released on the widget area.
Represents a mouse event.
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:55
void setFocusable(bool focusable)
Sets the widget to be focusable, or not.
Definition widget.cpp:645
unsigned int getChildrenCount() const
Gets how many childs the widget have.
Definition widget.cpp:338
void setEnabled(bool enabled)
Sets the widget to enabled, or not.
Definition widget.cpp:1073
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:251
virtual Widget * getTop() const
Gets the top widget, or top parent, of this widget.
Definition widget.cpp:1315
virtual void getAbsolutePosition(int &x, int &y) const
Gets the absolute position on the screen for the widget.
Definition widget.cpp:977
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:264
Used replacement tokens by configure_file():