aboutsummaryrefslogtreecommitdiff
path: root/scene/gui/base_button.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--scene/gui/base_button.cpp259
1 files changed, 119 insertions, 140 deletions
diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp
index 2dfc89dfd..6861b9d18 100644
--- a/scene/gui/base_button.cpp
+++ b/scene/gui/base_button.cpp
@@ -27,25 +27,24 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "base_button.h"
+#include "button_group.h"
#include "os/keyboard.h"
#include "print_string.h"
-#include "button_group.h"
-#include "scene/scene_string_names.h"
#include "scene/main/viewport.h"
+#include "scene/scene_string_names.h"
void BaseButton::_input_event(InputEvent p_event) {
-
if (status.disabled) // no interaction with disabled button
return;
- switch(p_event.type) {
+ switch (p_event.type) {
case InputEvent::MOUSE_BUTTON: {
- const InputEventMouseButton &b=p_event.mouse_button;
+ const InputEventMouseButton &b = p_event.mouse_button;
- if ( status.disabled || b.button_index!=1 )
+ if (status.disabled || b.button_index != 1)
return;
if (status.pressing_button)
@@ -59,42 +58,40 @@ void BaseButton::_input_event(InputEvent p_event) {
if (!toggle_mode) { //mouse press attempt
- status.press_attempt=true;
- status.pressing_inside=true;
+ status.press_attempt = true;
+ status.pressing_inside = true;
pressed();
if (get_script_instance()) {
Variant::CallError ce;
- get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce);
+ get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
}
emit_signal("pressed");
} else {
- status.pressed=!status.pressed;
+ status.pressed = !status.pressed;
pressed();
if (get_script_instance()) {
Variant::CallError ce;
- get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce);
+ get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
}
emit_signal("pressed");
toggled(status.pressed);
- emit_signal("toggled",status.pressed);
-
+ emit_signal("toggled", status.pressed);
}
-
} else {
emit_signal("button_up");
if (status.press_attempt && status.pressing_inside) {
-// released();
+ // released();
emit_signal("released");
}
- status.press_attempt=false;
+ status.press_attempt = false;
}
update();
break;
@@ -102,56 +99,52 @@ void BaseButton::_input_event(InputEvent p_event) {
if (b.pressed) {
- status.press_attempt=true;
- status.pressing_inside=true;
+ status.press_attempt = true;
+ status.pressing_inside = true;
emit_signal("button_down");
} else {
emit_signal("button_up");
- if (status.press_attempt &&status.pressing_inside) {
+ if (status.press_attempt && status.pressing_inside) {
if (!toggle_mode) { //mouse press attempt
pressed();
if (get_script_instance()) {
Variant::CallError ce;
- get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce);
+ get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
}
emit_signal("pressed");
} else {
- status.pressed=!status.pressed;
+ status.pressed = !status.pressed;
pressed();
emit_signal("pressed");
toggled(status.pressed);
- emit_signal("toggled",status.pressed);
+ emit_signal("toggled", status.pressed);
if (get_script_instance()) {
- get_script_instance()->call(SceneStringNames::get_singleton()->_toggled,status.pressed);
+ get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
}
-
-
}
-
}
- status.press_attempt=false;
-
+ status.press_attempt = false;
}
update();
} break;
case InputEvent::MOUSE_MOTION: {
- if (status.press_attempt && status.pressing_button==0) {
- bool last_press_inside=status.pressing_inside;
- status.pressing_inside=has_point(Point2(p_event.mouse_motion.x,p_event.mouse_motion.y));
- if (last_press_inside!=status.pressing_inside)
+ if (status.press_attempt && status.pressing_button == 0) {
+ bool last_press_inside = status.pressing_inside;
+ status.pressing_inside = has_point(Point2(p_event.mouse_motion.x, p_event.mouse_motion.y));
+ if (last_press_inside != status.pressing_inside)
update();
}
} break;
@@ -159,7 +152,6 @@ void BaseButton::_input_event(InputEvent p_event) {
case InputEvent::JOYSTICK_BUTTON:
case InputEvent::KEY: {
-
if (p_event.is_echo()) {
break;
}
@@ -168,7 +160,7 @@ void BaseButton::_input_event(InputEvent p_event) {
break;
}
- if (status.press_attempt && status.pressing_button==0) {
+ if (status.press_attempt && status.pressing_button == 0) {
break;
}
@@ -177,8 +169,8 @@ void BaseButton::_input_event(InputEvent p_event) {
if (p_event.is_pressed()) {
status.pressing_button++;
- status.press_attempt=true;
- status.pressing_inside=true;
+ status.press_attempt = true;
+ status.pressing_inside = true;
emit_signal("button_down");
} else if (status.press_attempt) {
@@ -189,8 +181,8 @@ void BaseButton::_input_event(InputEvent p_event) {
if (status.pressing_button)
break;
- status.press_attempt=false;
- status.pressing_inside=false;
+ status.press_attempt = false;
+ status.pressing_inside = false;
emit_signal("button_up");
@@ -200,92 +192,89 @@ void BaseButton::_input_event(InputEvent p_event) {
emit_signal("pressed");
} else {
- status.pressed=!status.pressed;
+ status.pressed = !status.pressed;
pressed();
emit_signal("pressed");
toggled(status.pressed);
if (get_script_instance()) {
- get_script_instance()->call(SceneStringNames::get_singleton()->_toggled,status.pressed);
+ get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
}
- emit_signal("toggled",status.pressed);
+ emit_signal("toggled", status.pressed);
}
}
accept_event();
update();
-
}
}
-
}
}
void BaseButton::_notification(int p_what) {
+ if (p_what == NOTIFICATION_MOUSE_ENTER) {
- if (p_what==NOTIFICATION_MOUSE_ENTER) {
-
- status.hovering=true;
+ status.hovering = true;
update();
}
- if (p_what==NOTIFICATION_MOUSE_EXIT) {
- status.hovering=false;
+ if (p_what == NOTIFICATION_MOUSE_EXIT) {
+ status.hovering = false;
update();
}
- if (p_what==NOTIFICATION_DRAG_BEGIN) {
+ if (p_what == NOTIFICATION_DRAG_BEGIN) {
if (status.press_attempt) {
- status.press_attempt=false;
- status.pressing_button=0;
+ status.press_attempt = false;
+ status.pressing_button = 0;
update();
}
}
-
- if (p_what==NOTIFICATION_FOCUS_ENTER) {
-
- status.hovering=true;
+
+ if (p_what == NOTIFICATION_FOCUS_ENTER) {
+
+ status.hovering = true;
update();
}
- if (p_what==NOTIFICATION_FOCUS_EXIT) {
+ if (p_what == NOTIFICATION_FOCUS_EXIT) {
if (status.pressing_button && status.press_attempt) {
- status.press_attempt=false;
- status.pressing_button=0;
- status.hovering=false;
+ status.press_attempt = false;
+ status.pressing_button = 0;
+ status.hovering = false;
update();
} else if (status.hovering) {
- status.hovering=false;
+ status.hovering = false;
update();
}
}
- if (p_what==NOTIFICATION_ENTER_TREE) {
+ if (p_what == NOTIFICATION_ENTER_TREE) {
- CanvasItem *ci=this;
- while(ci) {
+ CanvasItem *ci = this;
+ while (ci) {
ButtonGroup *bg = ci->cast_to<ButtonGroup>();
if (bg) {
- group=bg;
+ group = bg;
group->_add_button(this);
}
- ci=ci->get_parent_item();
+ ci = ci->get_parent_item();
}
}
- if (p_what==NOTIFICATION_EXIT_TREE) {
+ if (p_what == NOTIFICATION_EXIT_TREE) {
if (group)
group->_remove_button(this);
}
- if (p_what==NOTIFICATION_VISIBILITY_CHANGED && !is_visible()) {
+ if (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible()) {
if (!toggle_mode) {
status.pressed = false;
@@ -306,11 +295,9 @@ void BaseButton::pressed() {
void BaseButton::toggled(bool p_pressed) {
if (get_script_instance())
- get_script_instance()->call("toggled",p_pressed);
-
+ get_script_instance()->call("toggled", p_pressed);
}
-
void BaseButton::set_disabled(bool p_disabled) {
status.disabled = p_disabled;
@@ -331,21 +318,21 @@ void BaseButton::set_pressed(bool p_pressed) {
if (!toggle_mode)
return;
- if (status.pressed==p_pressed)
+ if (status.pressed == p_pressed)
return;
_change_notify("pressed");
- status.pressed=p_pressed;
+ status.pressed = p_pressed;
update();
}
-bool BaseButton::is_pressing() const{
+bool BaseButton::is_pressing() const {
return status.press_attempt;
}
bool BaseButton::is_pressed() const {
- return toggle_mode?status.pressed:status.press_attempt;
+ return toggle_mode ? status.pressed : status.press_attempt;
}
bool BaseButton::is_hovered() const {
@@ -360,8 +347,7 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const {
};
//print_line("press attempt: "+itos(status.press_attempt)+" hover: "+itos(status.hovering)+" pressed: "+itos(status.pressed));
- if (status.press_attempt==false && status.hovering && !status.pressed) {
-
+ if (status.press_attempt == false && status.hovering && !status.pressed) {
return DRAW_HOVER;
} else {
@@ -370,12 +356,12 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const {
bool pressing;
if (status.press_attempt) {
- pressing=status.pressing_inside;
+ pressing = status.pressing_inside;
if (status.pressed)
- pressing=!pressing;
+ pressing = !pressing;
} else {
- pressing=status.pressed;
+ pressing = status.pressed;
}
if (pressing)
@@ -389,7 +375,7 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const {
void BaseButton::set_toggle_mode(bool p_on) {
- toggle_mode=p_on;
+ toggle_mode = p_on;
}
bool BaseButton::is_toggle_mode() const {
@@ -399,7 +385,7 @@ bool BaseButton::is_toggle_mode() const {
void BaseButton::set_click_on_press(bool p_click_on_press) {
- status.click_on_press=p_click_on_press;
+ status.click_on_press = p_click_on_press;
}
bool BaseButton::get_click_on_press() const {
@@ -411,7 +397,7 @@ void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
enabled_focus_mode = p_mode;
if (!status.disabled) {
- set_focus_mode( p_mode );
+ set_focus_mode(p_mode);
}
}
@@ -420,16 +406,16 @@ Control::FocusMode BaseButton::get_enabled_focus_mode() const {
return enabled_focus_mode;
}
-void BaseButton::set_shortcut(const Ref<ShortCut>& p_shortcut) {
+void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) {
if (shortcut.is_null() == p_shortcut.is_null())
return;
- shortcut=p_shortcut;
+ shortcut = p_shortcut;
set_process_unhandled_input(shortcut.is_valid());
}
-Ref<ShortCut> BaseButton:: get_shortcut() const {
+Ref<ShortCut> BaseButton::get_shortcut() const {
return shortcut;
}
@@ -442,21 +428,21 @@ void BaseButton::_unhandled_input(InputEvent p_event) {
if (is_toggle_mode()) {
set_pressed(!is_pressed());
- emit_signal("toggled",is_pressed());
+ emit_signal("toggled", is_pressed());
}
emit_signal("pressed");
}
}
-String BaseButton::get_tooltip(const Point2& p_pos) const {
+String BaseButton::get_tooltip(const Point2 &p_pos) const {
- String tooltip=Control::get_tooltip(p_pos);
+ String tooltip = Control::get_tooltip(p_pos);
if (shortcut.is_valid() && shortcut->is_valid()) {
- if (tooltip.find("$sc")!=-1) {
- tooltip=tooltip.replace_first("$sc","("+shortcut->get_as_text()+")");
+ if (tooltip.find("$sc") != -1) {
+ tooltip = tooltip.replace_first("$sc", "(" + shortcut->get_as_text() + ")");
} else {
- tooltip+=" ("+shortcut->get_as_text()+")";
+ tooltip += " (" + shortcut->get_as_text() + ")";
}
}
return tooltip;
@@ -464,65 +450,58 @@ String BaseButton::get_tooltip(const Point2& p_pos) const {
void BaseButton::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("_input_event"),&BaseButton::_input_event);
- ObjectTypeDB::bind_method(_MD("_unhandled_input"),&BaseButton::_unhandled_input);
- ObjectTypeDB::bind_method(_MD("set_pressed","pressed"),&BaseButton::set_pressed);
- ObjectTypeDB::bind_method(_MD("is_pressed"),&BaseButton::is_pressed);
- ObjectTypeDB::bind_method(_MD("is_hovered"),&BaseButton::is_hovered);
- ObjectTypeDB::bind_method(_MD("set_toggle_mode","enabled"),&BaseButton::set_toggle_mode);
- ObjectTypeDB::bind_method(_MD("is_toggle_mode"),&BaseButton::is_toggle_mode);
- ObjectTypeDB::bind_method(_MD("set_disabled","disabled"),&BaseButton::set_disabled);
- ObjectTypeDB::bind_method(_MD("is_disabled"),&BaseButton::is_disabled);
- ObjectTypeDB::bind_method(_MD("set_click_on_press","enable"),&BaseButton::set_click_on_press);
- ObjectTypeDB::bind_method(_MD("get_click_on_press"),&BaseButton::get_click_on_press);
- ObjectTypeDB::bind_method(_MD("get_draw_mode"),&BaseButton::get_draw_mode);
- ObjectTypeDB::bind_method(_MD("set_enabled_focus_mode","mode"),&BaseButton::set_enabled_focus_mode);
- ObjectTypeDB::bind_method(_MD("get_enabled_focus_mode"),&BaseButton::get_enabled_focus_mode);
- ObjectTypeDB::bind_method(_MD("set_shortcut","shortcut"),&BaseButton::set_shortcut);
- ObjectTypeDB::bind_method(_MD("get_shortcut"),&BaseButton::get_shortcut);
+ ObjectTypeDB::bind_method(_MD("_input_event"), &BaseButton::_input_event);
+ ObjectTypeDB::bind_method(_MD("_unhandled_input"), &BaseButton::_unhandled_input);
+ ObjectTypeDB::bind_method(_MD("set_pressed", "pressed"), &BaseButton::set_pressed);
+ ObjectTypeDB::bind_method(_MD("is_pressed"), &BaseButton::is_pressed);
+ ObjectTypeDB::bind_method(_MD("is_hovered"), &BaseButton::is_hovered);
+ ObjectTypeDB::bind_method(_MD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
+ ObjectTypeDB::bind_method(_MD("is_toggle_mode"), &BaseButton::is_toggle_mode);
+ ObjectTypeDB::bind_method(_MD("set_disabled", "disabled"), &BaseButton::set_disabled);
+ ObjectTypeDB::bind_method(_MD("is_disabled"), &BaseButton::is_disabled);
+ ObjectTypeDB::bind_method(_MD("set_click_on_press", "enable"), &BaseButton::set_click_on_press);
+ ObjectTypeDB::bind_method(_MD("get_click_on_press"), &BaseButton::get_click_on_press);
+ ObjectTypeDB::bind_method(_MD("get_draw_mode"), &BaseButton::get_draw_mode);
+ ObjectTypeDB::bind_method(_MD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
+ ObjectTypeDB::bind_method(_MD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
+ ObjectTypeDB::bind_method(_MD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
+ ObjectTypeDB::bind_method(_MD("get_shortcut"), &BaseButton::get_shortcut);
BIND_VMETHOD(MethodInfo("_pressed"));
- BIND_VMETHOD(MethodInfo("_toggled",PropertyInfo(Variant::BOOL,"pressed")));
+ BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "pressed")));
- ADD_SIGNAL( MethodInfo("pressed" ) );
- ADD_SIGNAL( MethodInfo("released" ) );
- ADD_SIGNAL( MethodInfo("button_up") );
- ADD_SIGNAL( MethodInfo("button_down") );
- ADD_SIGNAL( MethodInfo("toggled", PropertyInfo( Variant::BOOL,"pressed") ) );
- ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "disabled"), _SCS("set_disabled"), _SCS("is_disabled"));
- ADD_PROPERTY( PropertyInfo( Variant::BOOL, "toggle_mode"), _SCS("set_toggle_mode"), _SCS("is_toggle_mode"));
- ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "is_pressed"), _SCS("set_pressed"), _SCS("is_pressed"));
- ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "click_on_press"), _SCS("set_click_on_press"), _SCS("get_click_on_press"));
- ADD_PROPERTY( PropertyInfo( Variant::INT,"enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All" ), _SCS("set_enabled_focus_mode"), _SCS("get_enabled_focus_mode") );
- ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "shortcut",PROPERTY_HINT_RESOURCE_TYPE,"ShortCut"), _SCS("set_shortcut"), _SCS("get_shortcut"));
-
-
- BIND_CONSTANT( DRAW_NORMAL );
- BIND_CONSTANT( DRAW_PRESSED );
- BIND_CONSTANT( DRAW_HOVER );
- BIND_CONSTANT( DRAW_DISABLED );
+ ADD_SIGNAL(MethodInfo("pressed"));
+ ADD_SIGNAL(MethodInfo("released"));
+ ADD_SIGNAL(MethodInfo("button_up"));
+ ADD_SIGNAL(MethodInfo("button_down"));
+ ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "pressed")));
+ ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "disabled"), _SCS("set_disabled"), _SCS("is_disabled"));
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), _SCS("set_toggle_mode"), _SCS("is_toggle_mode"));
+ ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "is_pressed"), _SCS("set_pressed"), _SCS("is_pressed"));
+ ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "click_on_press"), _SCS("set_click_on_press"), _SCS("get_click_on_press"));
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), _SCS("set_enabled_focus_mode"), _SCS("get_enabled_focus_mode"));
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), _SCS("set_shortcut"), _SCS("get_shortcut"));
+ BIND_CONSTANT(DRAW_NORMAL);
+ BIND_CONSTANT(DRAW_PRESSED);
+ BIND_CONSTANT(DRAW_HOVER);
+ BIND_CONSTANT(DRAW_DISABLED);
}
BaseButton::BaseButton() {
- toggle_mode=false;
- status.pressed=false;
- status.press_attempt=false;
- status.hovering=false;
- status.pressing_inside=false;
+ toggle_mode = false;
+ status.pressed = false;
+ status.press_attempt = false;
+ status.hovering = false;
+ status.pressing_inside = false;
status.disabled = false;
- status.click_on_press=false;
- status.pressing_button=0;
- set_focus_mode( FOCUS_ALL );
+ status.click_on_press = false;
+ status.pressing_button = 0;
+ set_focus_mode(FOCUS_ALL);
enabled_focus_mode = FOCUS_ALL;
- group=NULL;
-
-
+ group = NULL;
}
-BaseButton::~BaseButton()
-{
+BaseButton::~BaseButton() {
}
-
-