123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- /*************************************************************************/
- /* base_button.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "base_button.h"
- #include "os/keyboard.h"
- #include "print_string.h"
- #include "button_group.h"
- void BaseButton::_input_event(InputEvent p_event) {
- if (status.disabled) // no interaction with disabled button
- return;
-
- switch(p_event.type) {
-
- case InputEvent::MOUSE_BUTTON: {
-
- const InputEventMouseButton &b=p_event.mouse_button;
-
- if ( status.disabled || b.button_index!=1 )
- return;
- if (status.pressing_button)
- break;
-
- if (status.click_on_press) {
- if (b.pressed) {
- if (!toggle_mode) { //mouse press attempt
- pressed();
- emit_signal("pressed");
- } else {
- status.pressed=!status.pressed;
- pressed();
- emit_signal("pressed");
- toggled(status.pressed);
- emit_signal("toggled",status.pressed);
- }
- }
- break;
- }
- if (b.pressed) {
-
- status.press_attempt=true;
- status.pressing_inside=true;
-
- } else {
-
-
- if (status.press_attempt &&status.pressing_inside) {
-
- if (!toggle_mode) { //mouse press attempt
-
- pressed();
- emit_signal("pressed");
-
- } else {
-
- status.pressed=!status.pressed;
-
- pressed();
- emit_signal("pressed");
-
- toggled(status.pressed);
- emit_signal("toggled",status.pressed);
-
- }
-
- }
-
- 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)
- update();
- }
- } break;
- case InputEvent::JOYSTICK_BUTTON:
- case InputEvent::KEY: {
-
- if (p_event.is_echo()) {
- break;
- }
- if (status.disabled) {
- break;
- }
- if (status.press_attempt && status.pressing_button==0) {
- break;
- }
- if (p_event.is_action("ui_accept")) {
-
- if (p_event.is_pressed()) {
- status.pressing_button++;
- status.press_attempt=true;
- status.pressing_inside=true;
- } else if (status.press_attempt) {
- if (status.pressing_button)
- status.pressing_button--;
- if (status.pressing_button)
- break;
- status.press_attempt=false;
- status.pressing_inside=false;
-
- if (!toggle_mode) { //mouse press attempt
-
- pressed();
- emit_signal("pressed");
- } else {
-
- status.pressed=!status.pressed;
-
- pressed();
- emit_signal("pressed");
-
- toggled(status.pressed);
- emit_signal("toggled",status.pressed);
- }
- }
-
- accept_event();
- update();
- }
- }
-
- }
- }
- void BaseButton::_notification(int p_what) {
-
- if (p_what==NOTIFICATION_MOUSE_ENTER) {
-
- status.hovering=true;
- update();
- }
-
- if (p_what==NOTIFICATION_MOUSE_EXIT) {
- status.hovering=false;
- update();
- }
- if (p_what==NOTIFICATION_FOCUS_EXIT) {
- if (status.pressing_button && status.press_attempt) {
- status.press_attempt=false;
- status.pressing_button=0;
- }
- }
- if (p_what==NOTIFICATION_ENTER_SCENE) {
- CanvasItem *ci=this;
- while(ci) {
- ButtonGroup *bg = ci->cast_to<ButtonGroup>();
- if (bg) {
- group=bg;
- group->_add_button(this);
- }
- ci=ci->get_parent_item();
- }
- }
- if (p_what==NOTIFICATION_EXIT_SCENE) {
- if (group)
- group->_remove_button(this);
- }
- }
- void BaseButton::pressed() {
- if (get_script_instance())
- get_script_instance()->call("pressed");
- }
- void BaseButton::toggled(bool p_pressed) {
- if (get_script_instance())
- get_script_instance()->call("toggled",p_pressed);
- }
- void BaseButton::set_disabled(bool p_disabled) {
-
- status.disabled = p_disabled;
- update();
- _change_notify("disabled");
- if (p_disabled)
- set_focus_mode(FOCUS_NONE);
- else
- set_focus_mode(FOCUS_ALL);
- };
- bool BaseButton::is_disabled() const {
- return status.disabled;
- };
- void BaseButton::set_pressed(bool p_pressed) {
-
- if (!toggle_mode)
- return;
- if (status.pressed==p_pressed)
- return;
- _change_notify("pressed");
- status.pressed=p_pressed;
- update();
- }
- bool BaseButton::is_pressing() const{
-
- return status.press_attempt;
- }
- bool BaseButton::is_pressed() const {
-
- return toggle_mode?status.pressed:status.press_attempt;
- }
- bool BaseButton::is_hovered() const {
- return status.hovering;
- }
- BaseButton::DrawMode BaseButton::get_draw_mode() const {
-
- if (status.disabled) {
- return DRAW_DISABLED;
- };
-
- //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) {
- return DRAW_HOVER;
- } else {
- /* determine if pressed or not */
-
- bool pressing;
- if (status.press_attempt) {
-
- pressing=status.pressing_inside;
- if (status.pressed)
- pressing=!pressing;
- } else {
-
- pressing=status.pressed;
- }
-
- if (pressing)
- return DRAW_PRESSED;
- else
- return DRAW_NORMAL;
- }
- return DRAW_NORMAL;
- }
- void BaseButton::set_toggle_mode(bool p_on) {
-
- toggle_mode=p_on;
- }
- bool BaseButton::is_toggle_mode() const {
-
- return toggle_mode;
- }
- void BaseButton::set_click_on_press(bool p_click_on_press) {
- status.click_on_press=p_click_on_press;
- }
- bool BaseButton::get_click_on_press() const {
- return status.click_on_press;
- }
- void BaseButton::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("_input_event"),&BaseButton::_input_event);
- 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);
- ADD_SIGNAL( MethodInfo("pressed" ) );
- ADD_SIGNAL( MethodInfo("toggled", PropertyInfo( Variant::BOOL,"pressed") ) );
- ADD_PROPERTY( 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_PROPERTY( PropertyInfo( Variant::BOOL, "click_on_press"), _SCS("set_click_on_press"), _SCS("get_click_on_press"));
- }
- BaseButton::BaseButton() {
-
- 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 );
- group=NULL;
-
- }
- BaseButton::~BaseButton()
- {
- }
|