Skip to content

Commit 3ef0cdf

Browse files
committed
Adds Hit Margin for BaseButton by overriden has_point
1 parent d3a5a85 commit 3ef0cdf

5 files changed

Lines changed: 38 additions & 0 deletions

File tree

doc/classes/BaseButton.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,9 @@
129129
Require a press and a subsequent release before considering the button clicked.
130130
</constant>
131131
</constants>
132+
<theme_items>
133+
<theme_item name="click_margin" data_type="constant" type="int" default="0">
134+
Defines the margin around the button's area that still counts as a valid click. This is useful to make it easier to click small buttons.
135+
</theme_item>
136+
</theme_items>
132137
</class>

editor/themes/theme_classic.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,13 @@ void ThemeClassic::populate_standard_styles(const Ref<EditorTheme> &p_theme, Edi
419419

420420
p_theme->set_constant("align_to_largest_stylebox", "Button", 1); // Enabled.
421421

422+
#ifdef ANDROID_ENABLED
423+
const int click_margin = Math::round(4 * EDSCALE);
424+
#else
425+
const int click_margin = Math::round(2 * EDSCALE);
426+
#endif
427+
p_theme->set_constant("click_margin", "BaseButton", click_margin);
428+
422429
// MenuBar.
423430

424431
p_theme->set_stylebox(CoreStringName(normal), "MenuBar", p_config.button_style);

editor/themes/theme_modern.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,13 @@ void ThemeModern::populate_standard_styles(const Ref<EditorTheme> &p_theme, Edit
496496
p_theme->set_constant("outline_size", "Button", 0);
497497
p_theme->set_constant("align_to_largest_stylebox", "Button", 1); // Enabled.
498498

499+
#ifdef ANDROID_ENABLED
500+
const int click_margin = Math::round(4 * EDSCALE);
501+
#else
502+
const int click_margin = Math::round(2 * EDSCALE);
503+
#endif
504+
p_theme->set_constant("click_margin", "BaseButton", click_margin);
505+
499506
// MenuBar.
500507

501508
p_theme->set_stylebox(CoreStringName(normal), "MenuBar", p_config.button_style);

scene/gui/base_button.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "core/object/class_db.h"
3636
#include "scene/gui/label.h"
3737
#include "scene/main/timer.h"
38+
#include "scene/theme/theme_db.h"
3839
#include "servers/display/accessibility_server.h"
3940

4041
void BaseButton::_unpress_group() {
@@ -394,6 +395,16 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const {
394395
}
395396
}
396397

398+
bool BaseButton::has_point(const Point2 &p_point) const {
399+
ERR_READ_THREAD_GUARD_V(false);
400+
bool ret;
401+
if (GDVIRTUAL_CALL(_has_point, p_point, ret)) {
402+
return ret;
403+
}
404+
Rect2 rect = Rect2(Point2(), get_size()).grow(theme_cache.click_margin);
405+
return rect.has_area() && rect.has_point(p_point);
406+
}
407+
397408
void BaseButton::set_toggle_mode(bool p_on) {
398409
// Make sure to set 'pressed' to false if we are not in toggle mode
399410
if (!p_on) {
@@ -625,6 +636,8 @@ void BaseButton::_bind_methods() {
625636
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback");
626637
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
627638

639+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, BaseButton, click_margin);
640+
628641
BIND_ENUM_CONSTANT(DRAW_NORMAL);
629642
BIND_ENUM_CONSTANT(DRAW_PRESSED);
630643
BIND_ENUM_CONSTANT(DRAW_HOVER);

scene/gui/base_button.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class BaseButton : public Control {
4646
};
4747

4848
private:
49+
struct ThemeCache {
50+
int click_margin = 0;
51+
} theme_cache;
52+
4953
BitField<MouseButtonMask> button_mask = MouseButtonMask::LEFT;
5054
bool toggle_mode = false;
5155
bool shortcut_in_tooltip = true;
@@ -103,6 +107,8 @@ class BaseButton : public Control {
103107

104108
DrawMode get_draw_mode() const;
105109

110+
virtual bool has_point(const Point2 &p_point) const override;
111+
106112
/* Signals */
107113

108114
bool is_pressed() const; ///< return whether button is pressed (toggled in)

0 commit comments

Comments
 (0)