2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* rich_text_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 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 "rich_text_editor_plugin.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2015-04-09 14:48:46 +02:00
|
|
|
#include "canvas_item_editor_plugin.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "os/file_access.h"
|
2015-04-09 14:48:46 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void RichTextEditor::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case NOTIFICATION_FIXED_PROCESS: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void RichTextEditor::_node_removed(Node *p_node) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_node == node) {
|
|
|
|
node = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RichTextEditor::_file_selected(const String &p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
CharString cs;
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *fa = FileAccess::open(p_path, FileAccess::READ);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!fa) {
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (!fa->eof_reached())
|
2014-02-10 02:10:30 +01:00
|
|
|
cs.push_back(fa->get_8());
|
|
|
|
cs.push_back(0);
|
|
|
|
memdelete(fa);
|
|
|
|
|
|
|
|
String bbcode;
|
|
|
|
bbcode.parse_utf8(&cs[0]);
|
|
|
|
node->parse_bbcode(bbcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichTextEditor::_menu_option(int p_option) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_option) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case PARSE_BBCODE: {
|
|
|
|
|
|
|
|
file_dialog->popup_centered_ratio();
|
|
|
|
} break;
|
|
|
|
case CLEAR: {
|
|
|
|
|
|
|
|
node->clear();
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichTextEditor::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_menu_option"), &RichTextEditor::_menu_option);
|
|
|
|
ClassDB::bind_method(D_METHOD("_file_selected"), &RichTextEditor::_file_selected);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichTextEditor::edit(Node *p_rich_text) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
node = p_rich_text->cast_to<RichTextLabel>();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
RichTextEditor::RichTextEditor() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
options = memnew(MenuButton);
|
2015-04-09 14:48:46 +02:00
|
|
|
//add_child(options);
|
|
|
|
CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
|
2014-02-10 02:10:30 +01:00
|
|
|
options->set_area_as_parent_rect();
|
|
|
|
|
2016-05-19 00:08:12 +02:00
|
|
|
options->set_text("RichText");
|
2017-03-05 16:44:50 +01:00
|
|
|
options->get_popup()->add_item(TTR("Parse BBCode"), PARSE_BBCODE);
|
|
|
|
options->get_popup()->add_item(TTR("Clear"), CLEAR);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
options->get_popup()->connect("id_pressed", this, "_menu_option");
|
|
|
|
file_dialog = memnew(EditorFileDialog);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(file_dialog);
|
|
|
|
file_dialog->add_filter("*.txt");
|
2015-06-06 14:44:38 +02:00
|
|
|
file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
|
2017-03-05 16:44:50 +01:00
|
|
|
file_dialog->connect("file_selected", this, "_file_selected");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichTextEditorPlugin::edit(Object *p_object) {
|
|
|
|
|
|
|
|
rich_text_editor->edit(p_object->cast_to<Node>());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichTextEditorPlugin::handles(Object *p_object) const {
|
|
|
|
|
2017-01-03 03:03:46 +01:00
|
|
|
return p_object->is_class("RichTextLabel");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichTextEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
|
|
|
|
if (p_visible) {
|
2015-04-09 14:48:46 +02:00
|
|
|
rich_text_editor->options->show();
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
2015-04-09 14:48:46 +02:00
|
|
|
rich_text_editor->options->hide();
|
2014-02-10 02:10:30 +01:00
|
|
|
rich_text_editor->edit(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RichTextEditorPlugin::RichTextEditorPlugin(EditorNode *p_node) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
editor = p_node;
|
|
|
|
rich_text_editor = memnew(RichTextEditor);
|
2014-02-10 02:10:30 +01:00
|
|
|
editor->get_viewport()->add_child(rich_text_editor);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rich_text_editor->set_margin(MARGIN_LEFT, 184);
|
|
|
|
rich_text_editor->set_margin(MARGIN_RIGHT, 230);
|
|
|
|
rich_text_editor->set_margin(MARGIN_TOP, 0);
|
|
|
|
rich_text_editor->set_margin(MARGIN_BOTTOM, 10);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-09 14:48:46 +02:00
|
|
|
rich_text_editor->options->hide();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RichTextEditorPlugin::~RichTextEditorPlugin() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|