FifeGUI 0.3.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// Standard library includes
8#include <cassert>
9
10// Project headers
11#include "fifechan/graphics.hpp"
12#include "fifechan/widgets/menubar.hpp"
13#include "fifechan/widgets/menuitem.hpp"
14#include "fifechan/widgets/menupopup.hpp"
15
16namespace fcn
17{
19 {
20 assert("owner must not be null" && owner != nullptr);
21 setOpaque(false);
22 setFocusable(false);
23 setEnabled(true);
24 }
25
26 ModalBackdrop::~ModalBackdrop() = default;
27
28 void ModalBackdrop::draw([[maybe_unused]] Graphics* graphics)
29 {
30 // Intentionally transparent; no drawing required.
31 }
32
34 {
35 // Click outside the menu should close the owning menu tree.
36 // If the click targets the top MenuBar or a MenuItem,
37 // we should allow the event to propagate,
38 // so the MenuBar can toggle the popup.
39 int const x = event.getX();
40 int const y = event.getY();
41
42 bool clickHitsMenuBar = false;
43 Container const * topContainer = dynamic_cast<Container*>(getTop());
44 if (topContainer != nullptr) {
45 for (unsigned i = 0; i < topContainer->getChildrenCount(); ++i) {
46 Widget* child = topContainer->getChild(i);
47 if (child == nullptr || child == this) {
48 continue;
49 }
50 int cx = 0;
51 int cy = 0;
52 child->getAbsolutePosition(cx, cy);
53 if (x >= cx && x < cx + child->getWidth() && y >= cy && y < cy + child->getHeight()) {
54 if (dynamic_cast<MenuBar*>(child) != nullptr || dynamic_cast<MenuItem*>(child) != nullptr) {
55 clickHitsMenuBar = true;
56 break;
57 }
58 }
59 }
60 }
61
62 // If the click hits the top MenuBar/MenuItem, allow the event to
63 // propagate so the MenuBar can handle toggling the popup.
64 if (clickHitsMenuBar) {
65 return;
66 }
67
68 // Otherwise close the owning popup and consume the event
69 // so other widgets don't react to the click.
70 if (mOwner != nullptr) {
71 mOwner->hide();
72 }
73
74 event.consume();
75 }
76
77 void ModalBackdrop::mouseReleased([[maybe_unused]] MouseEvent& event)
78 {
79 }
80 void ModalBackdrop::mouseEntered([[maybe_unused]] MouseEvent& event)
81 {
82 }
83 void ModalBackdrop::mouseExited([[maybe_unused]] MouseEvent& event)
84 {
85 }
86} // 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:83
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:56
void setFocusable(bool focusable)
Sets the widget to be focusable, or not.
Definition widget.cpp:646
unsigned int getChildrenCount() const
Gets how many childs the widget have.
Definition widget.cpp:339
void setEnabled(bool enabled)
Sets the widget to enabled, or not.
Definition widget.cpp:1074
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
virtual Widget * getTop() const
Gets the top widget, or top parent, of this widget.
Definition widget.cpp:1316
virtual void getAbsolutePosition(int &x, int &y) const
Gets the absolute position on the screen for the widget.
Definition widget.cpp:978
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:265
Used replacement tokens by configure_file():