option_button.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*************************************************************************/
  2. /* option_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "option_button.h"
  31. #include "core/string/print_string.h"
  32. static const int NONE_SELECTED = -1;
  33. Size2 OptionButton::get_minimum_size() const {
  34. Size2 minsize;
  35. if (fit_to_longest_item) {
  36. minsize = _cached_size;
  37. } else {
  38. minsize = Button::get_minimum_size();
  39. }
  40. if (has_theme_icon(SNAME("arrow"))) {
  41. const Size2 padding = get_theme_stylebox(SNAME("normal"))->get_minimum_size();
  42. const Size2 arrow_size = Control::get_theme_icon(SNAME("arrow"))->get_size();
  43. Size2 content_size = minsize - padding;
  44. content_size.width += arrow_size.width + MAX(0, get_theme_constant(SNAME("h_separation")));
  45. content_size.height = MAX(content_size.height, arrow_size.height);
  46. minsize = content_size + padding;
  47. }
  48. return minsize;
  49. }
  50. void OptionButton::_notification(int p_what) {
  51. switch (p_what) {
  52. case NOTIFICATION_DRAW: {
  53. if (!has_theme_icon(SNAME("arrow"))) {
  54. return;
  55. }
  56. RID ci = get_canvas_item();
  57. Ref<Texture2D> arrow = Control::get_theme_icon(SNAME("arrow"));
  58. Color clr = Color(1, 1, 1);
  59. if (get_theme_constant(SNAME("modulate_arrow"))) {
  60. switch (get_draw_mode()) {
  61. case DRAW_PRESSED:
  62. clr = get_theme_color(SNAME("font_pressed_color"));
  63. break;
  64. case DRAW_HOVER:
  65. clr = get_theme_color(SNAME("font_hover_color"));
  66. break;
  67. case DRAW_HOVER_PRESSED:
  68. clr = get_theme_color(SNAME("font_hover_pressed_color"));
  69. break;
  70. case DRAW_DISABLED:
  71. clr = get_theme_color(SNAME("font_disabled_color"));
  72. break;
  73. default:
  74. if (has_focus()) {
  75. clr = get_theme_color(SNAME("font_focus_color"));
  76. } else {
  77. clr = get_theme_color(SNAME("font_color"));
  78. }
  79. }
  80. }
  81. Size2 size = get_size();
  82. Point2 ofs;
  83. if (is_layout_rtl()) {
  84. ofs = Point2(get_theme_constant(SNAME("arrow_margin")), int(Math::abs((size.height - arrow->get_height()) / 2)));
  85. } else {
  86. ofs = Point2(size.width - arrow->get_width() - get_theme_constant(SNAME("arrow_margin")), int(Math::abs((size.height - arrow->get_height()) / 2)));
  87. }
  88. arrow->draw(ci, ofs, clr);
  89. } break;
  90. case NOTIFICATION_TRANSLATION_CHANGED:
  91. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  92. popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  93. [[fallthrough]];
  94. }
  95. case NOTIFICATION_THEME_CHANGED: {
  96. if (has_theme_icon(SNAME("arrow"))) {
  97. if (is_layout_rtl()) {
  98. _set_internal_margin(SIDE_LEFT, Control::get_theme_icon(SNAME("arrow"))->get_width());
  99. _set_internal_margin(SIDE_RIGHT, 0.f);
  100. } else {
  101. _set_internal_margin(SIDE_LEFT, 0.f);
  102. _set_internal_margin(SIDE_RIGHT, Control::get_theme_icon(SNAME("arrow"))->get_width());
  103. }
  104. }
  105. _refresh_size_cache();
  106. } break;
  107. case NOTIFICATION_VISIBILITY_CHANGED: {
  108. if (!is_visible_in_tree()) {
  109. popup->hide();
  110. }
  111. } break;
  112. }
  113. }
  114. bool OptionButton::_set(const StringName &p_name, const Variant &p_value) {
  115. Vector<String> components = String(p_name).split("/", true, 2);
  116. if (components.size() >= 2 && components[0] == "popup") {
  117. String property = components[2];
  118. if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") {
  119. return false;
  120. }
  121. bool valid;
  122. popup->set(String(p_name).trim_prefix("popup/"), p_value, &valid);
  123. int idx = components[1].get_slice("_", 1).to_int();
  124. if (idx == current) {
  125. // Force refreshing currently displayed item.
  126. current = NONE_SELECTED;
  127. _select(idx, false);
  128. }
  129. if (property == "text" || property == "icon") {
  130. _queue_refresh_cache();
  131. }
  132. return valid;
  133. }
  134. return false;
  135. }
  136. bool OptionButton::_get(const StringName &p_name, Variant &r_ret) const {
  137. Vector<String> components = String(p_name).split("/", true, 2);
  138. if (components.size() >= 2 && components[0] == "popup") {
  139. String property = components[2];
  140. if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") {
  141. return false;
  142. }
  143. bool valid;
  144. r_ret = popup->get(String(p_name).trim_prefix("popup/"), &valid);
  145. return valid;
  146. }
  147. return false;
  148. }
  149. void OptionButton::_get_property_list(List<PropertyInfo> *p_list) const {
  150. for (int i = 0; i < popup->get_item_count(); i++) {
  151. p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text", i)));
  152. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  153. pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  154. p_list->push_back(pi);
  155. pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id", i), PROPERTY_HINT_RANGE, "0,10,1,or_greater");
  156. p_list->push_back(pi);
  157. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled", i));
  158. pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  159. p_list->push_back(pi);
  160. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator", i));
  161. pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0);
  162. p_list->push_back(pi);
  163. }
  164. }
  165. void OptionButton::_focused(int p_which) {
  166. emit_signal(SNAME("item_focused"), p_which);
  167. }
  168. void OptionButton::_selected(int p_which) {
  169. _select(p_which, true);
  170. }
  171. void OptionButton::pressed() {
  172. if (popup->is_visible()) {
  173. popup->hide();
  174. return;
  175. }
  176. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  177. popup->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  178. popup->set_size(Size2(size.width, 0));
  179. // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
  180. if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
  181. if (!_was_pressed_by_mouse()) {
  182. popup->set_current_index(current);
  183. } else {
  184. popup->scroll_to_item(current);
  185. }
  186. } else {
  187. for (int i = 0; i < popup->get_item_count(); i++) {
  188. if (!popup->is_item_disabled(i)) {
  189. if (!_was_pressed_by_mouse()) {
  190. popup->set_current_index(i);
  191. } else {
  192. popup->scroll_to_item(i);
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. popup->popup();
  199. }
  200. void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) {
  201. bool first_selectable = !has_selectable_items();
  202. popup->add_icon_radio_check_item(p_icon, p_label, p_id);
  203. if (first_selectable) {
  204. select(get_item_count() - 1);
  205. }
  206. _queue_refresh_cache();
  207. }
  208. void OptionButton::add_item(const String &p_label, int p_id) {
  209. bool first_selectable = !has_selectable_items();
  210. popup->add_radio_check_item(p_label, p_id);
  211. if (first_selectable) {
  212. select(get_item_count() - 1);
  213. }
  214. _queue_refresh_cache();
  215. }
  216. void OptionButton::set_item_text(int p_idx, const String &p_text) {
  217. popup->set_item_text(p_idx, p_text);
  218. if (current == p_idx) {
  219. set_text(p_text);
  220. }
  221. _queue_refresh_cache();
  222. }
  223. void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  224. popup->set_item_icon(p_idx, p_icon);
  225. if (current == p_idx) {
  226. set_icon(p_icon);
  227. }
  228. _queue_refresh_cache();
  229. }
  230. void OptionButton::set_item_id(int p_idx, int p_id) {
  231. popup->set_item_id(p_idx, p_id);
  232. }
  233. void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
  234. popup->set_item_metadata(p_idx, p_metadata);
  235. }
  236. void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) {
  237. popup->set_item_tooltip(p_idx, p_tooltip);
  238. }
  239. void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
  240. popup->set_item_disabled(p_idx, p_disabled);
  241. }
  242. String OptionButton::get_item_text(int p_idx) const {
  243. return popup->get_item_text(p_idx);
  244. }
  245. Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const {
  246. return popup->get_item_icon(p_idx);
  247. }
  248. int OptionButton::get_item_id(int p_idx) const {
  249. if (p_idx == NONE_SELECTED) {
  250. return NONE_SELECTED;
  251. }
  252. return popup->get_item_id(p_idx);
  253. }
  254. int OptionButton::get_item_index(int p_id) const {
  255. return popup->get_item_index(p_id);
  256. }
  257. Variant OptionButton::get_item_metadata(int p_idx) const {
  258. return popup->get_item_metadata(p_idx);
  259. }
  260. String OptionButton::get_item_tooltip(int p_idx) const {
  261. return popup->get_item_tooltip(p_idx);
  262. }
  263. bool OptionButton::is_item_disabled(int p_idx) const {
  264. return popup->is_item_disabled(p_idx);
  265. }
  266. bool OptionButton::is_item_separator(int p_idx) const {
  267. return popup->is_item_separator(p_idx);
  268. }
  269. void OptionButton::set_item_count(int p_count) {
  270. ERR_FAIL_COND(p_count < 0);
  271. int count_old = get_item_count();
  272. if (p_count == count_old) {
  273. return;
  274. }
  275. popup->set_item_count(p_count);
  276. if (p_count > count_old) {
  277. for (int i = count_old; i < p_count; i++) {
  278. popup->set_item_as_radio_checkable(i, true);
  279. }
  280. }
  281. _refresh_size_cache();
  282. notify_property_list_changed();
  283. }
  284. bool OptionButton::has_selectable_items() const {
  285. for (int i = 0; i < get_item_count(); i++) {
  286. if (!is_item_disabled(i) && !is_item_separator(i)) {
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. int OptionButton::get_selectable_item(bool p_from_last) const {
  293. if (!p_from_last) {
  294. for (int i = 0; i < get_item_count(); i++) {
  295. if (!is_item_disabled(i) && !is_item_separator(i)) {
  296. return i;
  297. }
  298. }
  299. } else {
  300. for (int i = get_item_count() - 1; i >= 0; i--) {
  301. if (!is_item_disabled(i) && !is_item_separator(i)) {
  302. return i;
  303. }
  304. }
  305. }
  306. return -1;
  307. }
  308. int OptionButton::get_item_count() const {
  309. return popup->get_item_count();
  310. }
  311. void OptionButton::set_fit_to_longest_item(bool p_fit) {
  312. if (p_fit == fit_to_longest_item) {
  313. return;
  314. }
  315. fit_to_longest_item = p_fit;
  316. _refresh_size_cache();
  317. }
  318. bool OptionButton::is_fit_to_longest_item() const {
  319. return fit_to_longest_item;
  320. }
  321. void OptionButton::add_separator(const String &p_text) {
  322. popup->add_separator(p_text);
  323. }
  324. void OptionButton::clear() {
  325. popup->clear();
  326. set_text("");
  327. current = NONE_SELECTED;
  328. _refresh_size_cache();
  329. }
  330. void OptionButton::_select(int p_which, bool p_emit) {
  331. if (p_which == current) {
  332. return;
  333. }
  334. if (p_which == NONE_SELECTED) {
  335. for (int i = 0; i < popup->get_item_count(); i++) {
  336. popup->set_item_checked(i, false);
  337. }
  338. current = NONE_SELECTED;
  339. set_text("");
  340. set_icon(nullptr);
  341. } else {
  342. ERR_FAIL_INDEX(p_which, popup->get_item_count());
  343. for (int i = 0; i < popup->get_item_count(); i++) {
  344. popup->set_item_checked(i, i == p_which);
  345. }
  346. current = p_which;
  347. set_text(popup->get_item_text(current));
  348. set_icon(popup->get_item_icon(current));
  349. }
  350. if (is_inside_tree() && p_emit) {
  351. emit_signal(SNAME("item_selected"), current);
  352. }
  353. }
  354. void OptionButton::_select_int(int p_which) {
  355. if (p_which < NONE_SELECTED || p_which >= popup->get_item_count()) {
  356. return;
  357. }
  358. _select(p_which, false);
  359. }
  360. void OptionButton::_refresh_size_cache() {
  361. cache_refresh_pending = false;
  362. if (!fit_to_longest_item) {
  363. return;
  364. }
  365. _cached_size = Vector2();
  366. for (int i = 0; i < get_item_count(); i++) {
  367. _cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(get_item_text(i), get_item_icon(i)));
  368. }
  369. update_minimum_size();
  370. }
  371. void OptionButton::_queue_refresh_cache() {
  372. if (cache_refresh_pending) {
  373. return;
  374. }
  375. cache_refresh_pending = true;
  376. callable_mp(this, &OptionButton::_refresh_size_cache).call_deferredp(nullptr, 0);
  377. }
  378. void OptionButton::select(int p_idx) {
  379. _select(p_idx, false);
  380. }
  381. int OptionButton::get_selected() const {
  382. return current;
  383. }
  384. int OptionButton::get_selected_id() const {
  385. return get_item_id(current);
  386. }
  387. Variant OptionButton::get_selected_metadata() const {
  388. int idx = get_selected();
  389. if (idx < 0) {
  390. return Variant();
  391. }
  392. return get_item_metadata(current);
  393. }
  394. void OptionButton::remove_item(int p_idx) {
  395. popup->remove_item(p_idx);
  396. if (current == p_idx) {
  397. _select(NONE_SELECTED);
  398. }
  399. _queue_refresh_cache();
  400. }
  401. PopupMenu *OptionButton::get_popup() const {
  402. return popup;
  403. }
  404. void OptionButton::get_translatable_strings(List<String> *p_strings) const {
  405. popup->get_translatable_strings(p_strings);
  406. }
  407. void OptionButton::_validate_property(PropertyInfo &p_property) const {
  408. if (p_property.name == "text" || p_property.name == "icon") {
  409. p_property.usage = PROPERTY_USAGE_NONE;
  410. }
  411. }
  412. void OptionButton::_bind_methods() {
  413. ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
  414. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
  415. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
  416. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
  417. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
  418. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
  419. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
  420. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
  421. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
  422. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
  423. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
  424. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
  425. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
  426. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
  427. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
  428. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
  429. ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
  430. ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
  431. ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
  432. ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
  433. ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
  434. ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
  435. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
  436. ClassDB::bind_method(D_METHOD("_select_int", "idx"), &OptionButton::_select_int);
  437. ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
  438. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count);
  439. ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
  440. ClassDB::bind_method(D_METHOD("has_selectable_items"), &OptionButton::has_selectable_items);
  441. ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false));
  442. ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item);
  443. ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
  444. // "selected" property must come after "item_count", otherwise GH-10213 occurs.
  445. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  446. ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
  447. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
  448. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  449. ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
  450. }
  451. OptionButton::OptionButton(const String &p_text) :
  452. Button(p_text) {
  453. set_toggle_mode(true);
  454. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  455. if (is_layout_rtl()) {
  456. if (has_theme_icon(SNAME("arrow"))) {
  457. _set_internal_margin(SIDE_LEFT, Control::get_theme_icon(SNAME("arrow"))->get_width());
  458. }
  459. } else {
  460. if (has_theme_icon(SNAME("arrow"))) {
  461. _set_internal_margin(SIDE_RIGHT, Control::get_theme_icon(SNAME("arrow"))->get_width());
  462. }
  463. }
  464. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  465. popup = memnew(PopupMenu);
  466. popup->hide();
  467. add_child(popup, false, INTERNAL_MODE_FRONT);
  468. popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected));
  469. popup->connect("id_focused", callable_mp(this, &OptionButton::_focused));
  470. popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false));
  471. _refresh_size_cache();
  472. }
  473. OptionButton::~OptionButton() {
  474. }