godot/scene/gui/option_button.cpp

320 lines
9.6 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* option_button.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00: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 "option_button.h"
#include "print_string.h"
Size2 OptionButton::get_minimum_size() const {
Size2 minsize = Button::get_minimum_size();
if (has_icon("arrow"))
minsize.width += Control::get_icon("arrow")->get_width();
2014-02-10 01:10:30 +00:00
return minsize;
}
void OptionButton::_notification(int p_what) {
switch (p_what) {
2014-02-10 01:10:30 +00:00
case NOTIFICATION_DRAW: {
if (!has_icon("arrow"))
return;
RID ci = get_canvas_item();
Ref<Texture> arrow = Control::get_icon("arrow");
Ref<StyleBox> normal = get_stylebox("normal");
2014-02-10 01:10:30 +00:00
2016-03-08 23:00:52 +00:00
Size2 size = get_size();
2014-02-10 01:10:30 +00:00
Point2 ofs(size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height - arrow->get_height()) / 2)));
arrow->draw(ci, ofs);
2014-02-10 01:10:30 +00:00
} break;
}
}
void OptionButton::_selected(int p_which) {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
int selid = -1;
for (int i = 0; i < popup->get_item_count(); i++) {
2014-02-10 01:10:30 +00:00
bool is_clicked = popup->get_item_ID(i) == p_which;
2014-02-10 01:10:30 +00:00
if (is_clicked) {
selid = i;
2014-02-10 01:10:30 +00:00
break;
}
}
if (selid == -1 && p_which >= 0 && p_which < popup->get_item_count()) {
_select(p_which, true);
2015-12-13 04:08:36 +00:00
} else {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(selid == -1);
2015-12-13 04:08:36 +00:00
_select(selid, true);
2015-12-13 04:08:36 +00:00
}
2014-02-10 01:10:30 +00:00
}
void OptionButton::pressed() {
2016-03-08 23:00:52 +00:00
Size2 size = get_size();
popup->set_global_pos(get_global_pos() + Size2(0, size.height));
popup->set_size(Size2(size.width, 0));
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
popup->popup();
}
void OptionButton::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID) {
2016-03-08 23:00:52 +00:00
popup->add_icon_check_item(p_icon, p_label, p_ID);
if (popup->get_item_count() == 1)
2014-02-10 01:10:30 +00:00
select(0);
}
void OptionButton::add_item(const String &p_label, int p_ID) {
2016-03-08 23:00:52 +00:00
popup->add_check_item(p_label, p_ID);
if (popup->get_item_count() == 1)
2016-03-08 23:00:52 +00:00
select(0);
2014-02-10 01:10:30 +00:00
}
void OptionButton::set_item_text(int p_idx, const String &p_text) {
2016-03-08 23:00:52 +00:00
popup->set_item_text(p_idx, p_text);
2014-02-10 01:10:30 +00:00
}
void OptionButton::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
2016-03-08 23:00:52 +00:00
popup->set_item_icon(p_idx, p_icon);
2014-02-10 01:10:30 +00:00
}
void OptionButton::set_item_ID(int p_idx, int p_ID) {
2016-03-08 23:00:52 +00:00
popup->set_item_ID(p_idx, p_ID);
2014-02-10 01:10:30 +00:00
}
void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
2014-02-10 01:10:30 +00:00
popup->set_item_metadata(p_idx, p_metadata);
2014-02-10 01:10:30 +00:00
}
void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
2014-02-10 01:10:30 +00:00
popup->set_item_disabled(p_idx, p_disabled);
2014-02-10 01:10:30 +00:00
}
String OptionButton::get_item_text(int p_idx) const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return popup->get_item_text(p_idx);
}
Ref<Texture> OptionButton::get_item_icon(int p_idx) const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return popup->get_item_icon(p_idx);
}
int OptionButton::get_item_ID(int p_idx) const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return popup->get_item_ID(p_idx);
}
Variant OptionButton::get_item_metadata(int p_idx) const {
return popup->get_item_metadata(p_idx);
}
bool OptionButton::is_item_disabled(int p_idx) const {
return popup->is_item_disabled(p_idx);
}
int OptionButton::get_item_count() const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return popup->get_item_count();
}
void OptionButton::add_separator() {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
popup->add_separator();
}
void OptionButton::clear() {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
popup->clear();
set_text("");
current = -1;
2014-02-10 01:10:30 +00:00
}
void OptionButton::_select(int p_idx, bool p_emit) {
2014-02-10 01:10:30 +00:00
if (p_idx < 0)
2014-02-10 01:10:30 +00:00
return;
if (p_idx == current)
2014-02-10 01:10:30 +00:00
return;
ERR_FAIL_INDEX(p_idx, popup->get_item_count());
2014-02-10 01:10:30 +00:00
for (int i = 0; i < popup->get_item_count(); i++) {
2014-02-10 01:10:30 +00:00
popup->set_item_checked(i, i == p_idx);
2014-02-10 01:10:30 +00:00
}
current = p_idx;
set_text(popup->get_item_text(current));
set_icon(popup->get_item_icon(current));
2014-02-10 01:10:30 +00:00
if (is_inside_tree() && p_emit)
emit_signal("item_selected", current);
2014-02-10 01:10:30 +00:00
}
void OptionButton::_select_int(int p_which) {
if (p_which < 0 || p_which >= popup->get_item_count())
2014-02-10 01:10:30 +00:00
return;
_select(p_which, false);
2014-02-10 01:10:30 +00:00
}
void OptionButton::select(int p_idx) {
2016-03-08 23:00:52 +00:00
_select(p_idx, false);
2014-02-10 01:10:30 +00:00
}
int OptionButton::get_selected() const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return current;
}
int OptionButton::get_selected_ID() const {
int idx = get_selected();
if (idx < 0)
2014-02-10 01:10:30 +00:00
return 0;
return get_item_ID(current);
}
Variant OptionButton::get_selected_metadata() const {
int idx = get_selected();
if (idx < 0)
2014-02-10 01:10:30 +00:00
return Variant();
return get_item_metadata(current);
}
void OptionButton::remove_item(int p_idx) {
popup->remove_item(p_idx);
}
Array OptionButton::_get_items() const {
Array items;
for (int i = 0; i < get_item_count(); i++) {
2014-02-10 01:10:30 +00:00
items.push_back(get_item_text(i));
items.push_back(get_item_icon(i));
items.push_back(is_item_disabled(i));
items.push_back(get_item_ID(i));
items.push_back(get_item_metadata(i));
}
return items;
}
void OptionButton::_set_items(const Array &p_items) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND(p_items.size() % 5);
clear();
for (int i = 0; i < p_items.size(); i += 5) {
2014-02-10 01:10:30 +00:00
String text = p_items[i + 0];
Ref<Texture> icon = p_items[i + 1];
bool disabled = p_items[i + 2];
int id = p_items[i + 3];
Variant meta = p_items[i + 4];
2014-02-10 01:10:30 +00:00
int idx = get_item_count();
add_item(text, id);
set_item_icon(idx, icon);
set_item_disabled(idx, disabled);
set_item_metadata(idx, meta);
2014-02-10 01:10:30 +00:00
}
}
void OptionButton::get_translatable_strings(List<String> *p_strings) const {
return popup->get_translatable_strings(p_strings);
}
void OptionButton::_bind_methods() {
2016-03-08 23:00:52 +00:00
ObjectTypeDB::bind_method(_MD("_selected"), &OptionButton::_selected);
ObjectTypeDB::bind_method(_MD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
ObjectTypeDB::bind_method(_MD("add_icon_item", "texture:Texture", "label", "id"), &OptionButton::add_icon_item);
ObjectTypeDB::bind_method(_MD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
ObjectTypeDB::bind_method(_MD("set_item_icon", "idx", "texture:Texture"), &OptionButton::set_item_icon);
ObjectTypeDB::bind_method(_MD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
ObjectTypeDB::bind_method(_MD("set_item_ID", "idx", "id"), &OptionButton::set_item_ID);
ObjectTypeDB::bind_method(_MD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
ObjectTypeDB::bind_method(_MD("get_item_text", "idx"), &OptionButton::get_item_text);
ObjectTypeDB::bind_method(_MD("get_item_icon:Texture", "idx"), &OptionButton::get_item_icon);
ObjectTypeDB::bind_method(_MD("get_item_ID", "idx"), &OptionButton::get_item_ID);
ObjectTypeDB::bind_method(_MD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
ObjectTypeDB::bind_method(_MD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
ObjectTypeDB::bind_method(_MD("get_item_count"), &OptionButton::get_item_count);
ObjectTypeDB::bind_method(_MD("add_separator"), &OptionButton::add_separator);
ObjectTypeDB::bind_method(_MD("clear"), &OptionButton::clear);
ObjectTypeDB::bind_method(_MD("select", "idx"), &OptionButton::select);
ObjectTypeDB::bind_method(_MD("get_selected"), &OptionButton::get_selected);
ObjectTypeDB::bind_method(_MD("get_selected_ID"), &OptionButton::get_selected_ID);
ObjectTypeDB::bind_method(_MD("get_selected_metadata"), &OptionButton::get_selected_metadata);
ObjectTypeDB::bind_method(_MD("remove_item", "idx"), &OptionButton::remove_item);
ObjectTypeDB::bind_method(_MD("_select_int"), &OptionButton::_select_int);
ObjectTypeDB::bind_method(_MD("_set_items"), &OptionButton::_set_items);
ObjectTypeDB::bind_method(_MD("_get_items"), &OptionButton::_get_items);
ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), _SCS("_select_int"), _SCS("get_selected"));
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_items"), _SCS("_get_items"));
ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "ID")));
2014-02-10 01:10:30 +00:00
}
OptionButton::OptionButton() {
2016-03-08 23:00:52 +00:00
popup = memnew(PopupMenu);
2014-02-10 01:10:30 +00:00
popup->hide();
popup->set_as_toplevel(true);
add_child(popup);
popup->connect("item_pressed", this, "_selected");
2016-03-08 23:00:52 +00:00
current = -1;
2014-02-10 01:10:30 +00:00
set_text_align(ALIGN_LEFT);
}
OptionButton::~OptionButton() {
}