1 /*************************************************************************/
2 /*  rich_text_editor_plugin.cpp                                          */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 "rich_text_editor_plugin.h"
31 #include "canvas_item_editor_plugin.h"
32 #include "os/file_access.h"
33 
_notification(int p_what)34 void RichTextEditor::_notification(int p_what) {
35 
36 	switch (p_what) {
37 
38 		case NOTIFICATION_FIXED_PROCESS: {
39 
40 		} break;
41 	}
42 }
_node_removed(Node * p_node)43 void RichTextEditor::_node_removed(Node *p_node) {
44 
45 	if (p_node == node) {
46 		node = NULL;
47 		hide();
48 	}
49 }
50 
_file_selected(const String & p_path)51 void RichTextEditor::_file_selected(const String &p_path) {
52 
53 	CharString cs;
54 	FileAccess *fa = FileAccess::open(p_path, FileAccess::READ);
55 	if (!fa) {
56 		ERR_FAIL();
57 	}
58 
59 	while (!fa->eof_reached())
60 		cs.push_back(fa->get_8());
61 	cs.push_back(0);
62 	memdelete(fa);
63 
64 	String bbcode;
65 	bbcode.parse_utf8(&cs[0]);
66 	node->parse_bbcode(bbcode);
67 }
68 
_menu_option(int p_option)69 void RichTextEditor::_menu_option(int p_option) {
70 
71 	switch (p_option) {
72 
73 		case PARSE_BBCODE: {
74 
75 			file_dialog->popup_centered_ratio();
76 		} break;
77 		case CLEAR: {
78 
79 			node->clear();
80 
81 		} break;
82 	}
83 }
84 
_bind_methods()85 void RichTextEditor::_bind_methods() {
86 
87 	ObjectTypeDB::bind_method(_MD("_menu_option"), &RichTextEditor::_menu_option);
88 	ObjectTypeDB::bind_method(_MD("_file_selected"), &RichTextEditor::_file_selected);
89 }
90 
edit(Node * p_rich_text)91 void RichTextEditor::edit(Node *p_rich_text) {
92 
93 	node = p_rich_text->cast_to<RichTextLabel>();
94 }
RichTextEditor()95 RichTextEditor::RichTextEditor() {
96 
97 	options = memnew(MenuButton);
98 	//add_child(options);
99 	CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
100 	options->set_area_as_parent_rect();
101 
102 	options->set_text("RichText");
103 	options->get_popup()->add_item(TTR("Parse BBCode"), PARSE_BBCODE);
104 	options->get_popup()->add_item(TTR("Clear"), CLEAR);
105 
106 	options->get_popup()->connect("item_pressed", this, "_menu_option");
107 	file_dialog = memnew(EditorFileDialog);
108 	add_child(file_dialog);
109 	file_dialog->add_filter("*.txt");
110 	file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
111 	file_dialog->connect("file_selected", this, "_file_selected");
112 }
113 
edit(Object * p_object)114 void RichTextEditorPlugin::edit(Object *p_object) {
115 
116 	rich_text_editor->edit(p_object->cast_to<Node>());
117 }
118 
handles(Object * p_object) const119 bool RichTextEditorPlugin::handles(Object *p_object) const {
120 
121 	return p_object->is_type("RichTextLabel");
122 }
123 
make_visible(bool p_visible)124 void RichTextEditorPlugin::make_visible(bool p_visible) {
125 
126 	if (p_visible) {
127 		rich_text_editor->options->show();
128 	} else {
129 
130 		rich_text_editor->options->hide();
131 		rich_text_editor->edit(NULL);
132 	}
133 }
134 
RichTextEditorPlugin(EditorNode * p_node)135 RichTextEditorPlugin::RichTextEditorPlugin(EditorNode *p_node) {
136 
137 	editor = p_node;
138 	rich_text_editor = memnew(RichTextEditor);
139 	editor->get_viewport()->add_child(rich_text_editor);
140 
141 	rich_text_editor->set_margin(MARGIN_LEFT, 184);
142 	rich_text_editor->set_margin(MARGIN_RIGHT, 230);
143 	rich_text_editor->set_margin(MARGIN_TOP, 0);
144 	rich_text_editor->set_margin(MARGIN_BOTTOM, 10);
145 
146 	rich_text_editor->options->hide();
147 }
148 
~RichTextEditorPlugin()149 RichTextEditorPlugin::~RichTextEditorPlugin() {
150 }
151