| 1 | /**************************************************************************/ |
| 2 | /* project_tag.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "project_tag.h" |
| 32 | |
| 33 | #include "editor/themes/editor_scale.h" |
| 34 | #include "scene/gui/button.h" |
| 35 | #include "scene/gui/color_rect.h" |
| 36 | |
| 37 | void ProjectTag::_notification(int p_what) { |
| 38 | if (display_close && p_what == NOTIFICATION_THEME_CHANGED) { |
| 39 | button->set_button_icon(get_theme_icon(SNAME("close"), SNAME("TabBar"))); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void ProjectTag::connect_button_to(const Callable &p_callable) { |
| 44 | button->connect(SceneStringName(pressed), p_callable, CONNECT_DEFERRED); |
| 45 | } |
| 46 | |
| 47 | const String ProjectTag::get_tag() const { |
| 48 | return tag_string; |
| 49 | } |
| 50 | |
| 51 | ProjectTag::ProjectTag(const String &p_text, bool p_display_close) { |
| 52 | add_theme_constant_override(SNAME("separation"), 0); |
| 53 | set_v_size_flags(SIZE_SHRINK_CENTER); |
| 54 | tag_string = p_text; |
| 55 | display_close = p_display_close; |
| 56 | |
| 57 | Color tag_color = Color(1, 0, 0); |
| 58 | tag_color.set_ok_hsl_s(0.8); |
| 59 | tag_color.set_ok_hsl_h(float(p_text.hash() * 10001 % UINT32_MAX) / float(UINT32_MAX)); |
| 60 | set_self_modulate(tag_color); |
| 61 | |
| 62 | ColorRect *cr = memnew(ColorRect); |
| 63 | add_child(cr); |
| 64 | cr->set_custom_minimum_size(Vector2(4, 0) * EDSCALE); |
| 65 | cr->set_color(tag_color); |
| 66 | cr->set_mouse_filter(MOUSE_FILTER_PASS); |
| 67 | |
| 68 | button = memnew(Button); |
| 69 | add_child(button); |
| 70 | button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 71 | button->set_text(p_text.capitalize()); |
| 72 | button->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 73 | button->set_accessibility_name(vformat(TTR("Project Tag: %s"), p_text)); |
| 74 | button->set_icon_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
| 75 | button->set_theme_type_variation(SNAME("ProjectTagButton")); |
| 76 | button->set_mouse_filter(MOUSE_FILTER_PASS); |
| 77 | } |