2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* button.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:11:45 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2018-01-02 11:27:24 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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 "button.h"
|
|
|
|
#include "print_string.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "servers/visual_server.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "translation.h"
|
|
|
|
|
|
|
|
Size2 Button::get_minimum_size() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Size2 minsize = get_font("font")->get_string_size(text);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (clip_text)
|
2017-03-19 00:36:26 +01:00
|
|
|
minsize.width = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Ref<Texture> _icon;
|
|
|
|
if (icon.is_null() && has_icon("icon"))
|
2017-03-19 00:36:26 +01:00
|
|
|
_icon = Control::get_icon("icon");
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
_icon = icon;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!_icon.is_null()) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
minsize.height = MAX(minsize.height, _icon->get_height());
|
|
|
|
minsize.width += _icon->get_width();
|
|
|
|
if (text != "")
|
|
|
|
minsize.width += get_constant("hseparation");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return get_stylebox("normal")->get_minimum_size() + minsize;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::_notification(int p_what) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
RID ci = get_canvas_item();
|
2017-03-19 00:36:26 +01:00
|
|
|
Size2 size = get_size();
|
2014-02-10 02:10:30 +01:00
|
|
|
Color color;
|
|
|
|
|
|
|
|
//print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (get_draw_mode()) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
case DRAW_NORMAL: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!flat)
|
2017-04-11 19:45:39 +02:00
|
|
|
get_stylebox("normal")->draw(ci, Rect2(Point2(0, 0), size));
|
2017-03-19 00:36:26 +01:00
|
|
|
color = get_color("font_color");
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case DRAW_PRESSED: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-04-11 19:45:39 +02:00
|
|
|
get_stylebox("pressed")->draw(ci, Rect2(Point2(0, 0), size));
|
2014-02-10 02:10:30 +01:00
|
|
|
if (has_color("font_color_pressed"))
|
2017-03-19 00:36:26 +01:00
|
|
|
color = get_color("font_color_pressed");
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
color = get_color("font_color");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case DRAW_HOVER: {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-04-11 19:45:39 +02:00
|
|
|
get_stylebox("hover")->draw(ci, Rect2(Point2(0, 0), size));
|
2017-03-19 00:36:26 +01:00
|
|
|
color = get_color("font_color_hover");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case DRAW_DISABLED: {
|
|
|
|
|
2017-04-11 19:45:39 +02:00
|
|
|
get_stylebox("disabled")->draw(ci, Rect2(Point2(0, 0), size));
|
2017-03-19 00:36:26 +01:00
|
|
|
color = get_color("font_color_disabled");
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
2015-06-08 15:36:11 +02:00
|
|
|
|
|
|
|
if (has_focus()) {
|
|
|
|
|
|
|
|
Ref<StyleBox> style = get_stylebox("focus");
|
2017-03-19 00:36:26 +01:00
|
|
|
style->draw(ci, Rect2(Point2(), size));
|
2015-06-08 15:36:11 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 19:45:39 +02:00
|
|
|
Ref<StyleBox> style = get_stylebox("normal");
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<Font> font = get_font("font");
|
2014-02-10 02:10:30 +01:00
|
|
|
Ref<Texture> _icon;
|
|
|
|
if (icon.is_null() && has_icon("icon"))
|
2017-03-19 00:36:26 +01:00
|
|
|
_icon = Control::get_icon("icon");
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
_icon = icon;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Point2 icon_ofs = (!_icon.is_null()) ? Point2(_icon->get_width() + get_constant("hseparation"), 0) : Point2();
|
|
|
|
int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
|
|
|
|
Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(text)) / 2.0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (align) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case ALIGN_LEFT: {
|
|
|
|
text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
|
2017-03-19 00:36:26 +01:00
|
|
|
text_ofs.y += style->get_offset().y;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case ALIGN_CENTER: {
|
2017-03-19 00:36:26 +01:00
|
|
|
if (text_ofs.x < 0)
|
|
|
|
text_ofs.x = 0;
|
|
|
|
text_ofs += icon_ofs;
|
|
|
|
text_ofs += style->get_offset();
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case ALIGN_RIGHT: {
|
2017-03-19 00:36:26 +01:00
|
|
|
text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size(text).x;
|
|
|
|
text_ofs.y += style->get_offset().y;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
text_ofs.y += font->get_ascent();
|
|
|
|
font->draw(ci, text_ofs.floor(), text, color, clip_text ? text_clip : -1);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!_icon.is_null()) {
|
2016-01-02 19:51:41 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int valign = size.height - style->get_minimum_size().y;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_icon->draw(ci, style->get_offset() + Point2(0, Math::floor((valign - _icon->get_height()) / 2.0)), is_disabled() ? Color(1, 1, 1, 0.4) : Color(1, 1, 1));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Button::set_text(const String &p_text) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (text == p_text)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2017-03-19 00:36:26 +01:00
|
|
|
text = XL_MESSAGE(p_text);
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
_change_notify("text");
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
String Button::get_text() const {
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Button::set_icon(const Ref<Texture> &p_icon) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (icon == p_icon)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2017-03-19 00:36:26 +01:00
|
|
|
icon = p_icon;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
_change_notify("icon");
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Texture> Button::get_icon() const {
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_flat(bool p_flat) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
flat = p_flat;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
_change_notify("flat");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::is_flat() const {
|
|
|
|
|
|
|
|
return flat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_clip_text(bool p_clip_text) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
clip_text = p_clip_text;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::get_clip_text() const {
|
|
|
|
|
|
|
|
return clip_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_text_align(TextAlign p_align) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
align = p_align;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::TextAlign Button::get_text_align() const {
|
|
|
|
|
|
|
|
return align;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_text", "text"), &Button::set_text);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_text"), &Button::get_text);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_button_icon", "texture:Texture"), &Button::set_icon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_button_icon:Texture"), &Button::get_icon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_flat", "enabled"), &Button::set_flat);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_clip_text", "enabled"), &Button::set_clip_text);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_clip_text"), &Button::get_clip_text);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_text_align", "align"), &Button::set_text_align);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_text_align"), &Button::get_text_align);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_flat"), &Button::is_flat);
|
|
|
|
|
|
|
|
BIND_CONSTANT(ALIGN_LEFT);
|
|
|
|
BIND_CONSTANT(ALIGN_CENTER);
|
|
|
|
BIND_CONSTANT(ALIGN_RIGHT);
|
|
|
|
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), _SCS("set_text"), _SCS("get_text"));
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_button_icon"), _SCS("get_button_icon"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), _SCS("set_flat"), _SCS("is_flat"));
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "clip_text"), _SCS("set_clip_text"), _SCS("get_clip_text"));
|
|
|
|
ADD_PROPERTYNO(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), _SCS("set_text_align"), _SCS("get_text_align"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Button::Button(const String &p_text) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
flat = false;
|
|
|
|
clip_text = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
set_stop_mouse(true);
|
|
|
|
set_text(p_text);
|
2017-03-19 00:36:26 +01:00
|
|
|
align = ALIGN_CENTER;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Button::~Button() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|