|
| 1 | +/**************************************************************************/ |
| 2 | +/* licenses_dialog.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 "licenses_dialog.h" |
| 32 | + |
| 33 | +#include "core/license.gen.h" |
| 34 | +#include "core/string/string_buffer.h" |
| 35 | +#include "scene/gui/box_container.h" |
| 36 | +#include "scene/gui/button.h" |
| 37 | +#include "scene/gui/label.h" |
| 38 | +#include "scene/gui/margin_container.h" |
| 39 | +#include "scene/gui/panel_container.h" |
| 40 | +#include "scene/gui/rich_text_label.h" |
| 41 | +#include "scene/main/canvas_item.h" |
| 42 | +#include "scene/resources/style_box_flat.h" |
| 43 | + |
| 44 | +void LicensesDialog::_close_button_pressed() { |
| 45 | + SceneTree::get_singleton()->set_licenses_dialog_visible(false); |
| 46 | +} |
| 47 | + |
| 48 | +void LicensesDialog::unhandled_key_input(const Ref<InputEvent> &p_event) { |
| 49 | + if (p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) { |
| 50 | + SceneTree::get_singleton()->set_licenses_dialog_visible(false); |
| 51 | + Node::get_viewport()->set_input_as_handled(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +LicensesDialog::LicensesDialog() { |
| 56 | + // Set on the highest layer, so that nothing else can draw on top. |
| 57 | + set_layer(128); |
| 58 | + |
| 59 | + // Keep UI interactions functional even if the game is paused. |
| 60 | + set_process_mode(Node::PROCESS_MODE_ALWAYS); |
| 61 | + |
| 62 | + set_process_unhandled_key_input(true); |
| 63 | + |
| 64 | + MarginContainer *margin_container = memnew(MarginContainer); |
| 65 | + margin_container->set_anchors_preset(Control::PRESET_FULL_RECT); |
| 66 | + const float default_base_scale = margin_container->get_theme_default_base_scale(); |
| 67 | + const float default_font_size = margin_container->get_theme_default_font_size(); |
| 68 | + margin_container->add_theme_constant_override("margin_top", Math::round(20 * default_base_scale)); |
| 69 | + margin_container->add_theme_constant_override("margin_right", Math::round(20 * default_base_scale)); |
| 70 | + margin_container->add_theme_constant_override("margin_bottom", Math::round(20 * default_base_scale)); |
| 71 | + margin_container->add_theme_constant_override("margin_left", Math::round(20 * default_base_scale)); |
| 72 | + add_child(margin_container); |
| 73 | + |
| 74 | + PanelContainer *panel_container = memnew(PanelContainer); |
| 75 | + margin_container->add_child(panel_container); |
| 76 | + |
| 77 | + MarginContainer *inner_margin_container = memnew(MarginContainer); |
| 78 | + inner_margin_container->add_theme_constant_override("margin_top", Math::round(10 * default_base_scale)); |
| 79 | + inner_margin_container->add_theme_constant_override("margin_right", Math::round(10 * default_base_scale)); |
| 80 | + inner_margin_container->add_theme_constant_override("margin_bottom", Math::round(10 * default_base_scale)); |
| 81 | + inner_margin_container->add_theme_constant_override("margin_left", Math::round(10 * default_base_scale)); |
| 82 | + panel_container->add_child(inner_margin_container); |
| 83 | + |
| 84 | + VBoxContainer *vbox_container = memnew(VBoxContainer); |
| 85 | + vbox_container->add_theme_constant_override("separation", Math::round(10 * default_base_scale)); |
| 86 | + inner_margin_container->add_child(vbox_container); |
| 87 | + |
| 88 | + Label *title_label = memnew(Label); |
| 89 | + title_label->set_text(RTR("Third-party notices")); |
| 90 | + title_label->add_theme_font_size_override(SceneStringName(font_size), Math::round(1.333 * default_font_size)); |
| 91 | + title_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 92 | + vbox_container->add_child(title_label); |
| 93 | + |
| 94 | + // Based on `editor_about.cpp` with references to TreeItem removed, |
| 95 | + // as we only have the "All Components" view here. A preamble is also added. |
| 96 | + StringBuffer<> long_text; |
| 97 | + long_text += RTR("This project is powered by Godot Engine, which relies on a number of third-party free and open source libraries, all compatible with the terms of its MIT license. The following is an exhaustive list of all such third-party components with their respective copyright statements and license terms.") + "\n\n"; |
| 98 | + |
| 99 | + long_text += RTR("Components:") + "\n\n"; |
| 100 | + |
| 101 | + for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) { |
| 102 | + const ComponentCopyright &component = COPYRIGHT_INFO[component_index]; |
| 103 | + const String component_name = String::utf8(component.name); |
| 104 | + long_text += "- " + component_name; |
| 105 | + for (int part_index = 0; part_index < component.part_count; part_index++) { |
| 106 | + const ComponentCopyrightPart &part = component.parts[part_index]; |
| 107 | + String copyright; |
| 108 | + for (int copyright_index = 0; copyright_index < part.copyright_count; copyright_index++) { |
| 109 | + copyright += String::utf8("\n \xc2\xa9 ") + String::utf8(part.copyright_statements[copyright_index]); |
| 110 | + } |
| 111 | + long_text += copyright; |
| 112 | + String license = "\n License: " + String::utf8(part.license) + "\n"; |
| 113 | + long_text += license + "\n\n"; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + long_text += RTR("Licenses:") + "\n\n"; |
| 118 | + |
| 119 | + for (int i = 0; i < LICENSE_COUNT; i++) { |
| 120 | + const String licensename = String::utf8(LICENSE_NAMES[i]); |
| 121 | + long_text += "- " + licensename + "\n"; |
| 122 | + const String licensebody = String::utf8(LICENSE_BODIES[i]); |
| 123 | + long_text += " " + licensebody.replace("\n", "\n ") + "\n\n"; |
| 124 | + } |
| 125 | + |
| 126 | + RichTextLabel *rich_text_label = memnew(RichTextLabel); |
| 127 | + rich_text_label->set_text(long_text); |
| 128 | + rich_text_label->set_threaded(true); |
| 129 | + rich_text_label->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 130 | + rich_text_label->set_focus_mode(Control::FOCUS_ALL); |
| 131 | + rich_text_label->add_theme_font_size_override("normal_font_size", Math::round(0.75 * default_font_size)); |
| 132 | + |
| 133 | + // Add a background to the scrollable area with the license text. |
| 134 | + Ref<StyleBoxFlat> background; |
| 135 | + background.instantiate(); |
| 136 | + background->set_bg_color(Color(0, 0, 0, 0.5)); |
| 137 | + background->set_content_margin_all(Math::round(10 * default_base_scale)); |
| 138 | + rich_text_label->add_theme_style_override(CoreStringName(normal), background); |
| 139 | + |
| 140 | + vbox_container->add_child(rich_text_label); |
| 141 | + // Allow for keyboard navigation by grabbing focus immediately on the scrollable control. |
| 142 | + callable_mp((Control *)rich_text_label, &Control::grab_focus).call_deferred(); |
| 143 | + |
| 144 | + Button *close_button = memnew(Button); |
| 145 | + close_button->set_text(RTR("Close")); |
| 146 | + close_button->set_h_size_flags(Control::SIZE_SHRINK_CENTER); |
| 147 | + close_button->set_custom_minimum_size(Vector2(100, 40) * default_base_scale); |
| 148 | + close_button->connect(SceneStringName(pressed), callable_mp(this, &LicensesDialog::_close_button_pressed)); |
| 149 | + vbox_container->add_child(close_button); |
| 150 | +} |
0 commit comments