editor_preview_plugins.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /**************************************************************************/
  2. /* editor_preview_plugins.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. #include "editor_preview_plugins.h"
  31. #include "core/io/file_access_memory.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/os.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "scene/resources/bit_map.h"
  38. #include "scene/resources/dynamic_font.h"
  39. #include "scene/resources/material.h"
  40. #include "scene/resources/mesh.h"
  41. #include "servers/audio/audio_stream.h"
  42. void post_process_preview(Ref<Image> p_image) {
  43. if (p_image->get_format() != Image::FORMAT_RGBA8) {
  44. p_image->convert(Image::FORMAT_RGBA8);
  45. }
  46. p_image->lock();
  47. const int w = p_image->get_width();
  48. const int h = p_image->get_height();
  49. const int r = MIN(w, h) / 32;
  50. const int r2 = r * r;
  51. Color transparent = Color(0, 0, 0, 0);
  52. for (int i = 0; i < r; i++) {
  53. for (int j = 0; j < r; j++) {
  54. int dx = i - r;
  55. int dy = j - r;
  56. if (dx * dx + dy * dy > r2) {
  57. p_image->set_pixel(i, j, transparent);
  58. p_image->set_pixel(w - 1 - i, j, transparent);
  59. p_image->set_pixel(w - 1 - i, h - 1 - j, transparent);
  60. p_image->set_pixel(i, h - 1 - j, transparent);
  61. } else {
  62. break;
  63. }
  64. }
  65. }
  66. p_image->unlock();
  67. }
  68. bool EditorTexturePreviewPlugin::handles(const String &p_type) const {
  69. return ClassDB::is_parent_class(p_type, "Texture");
  70. }
  71. bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const {
  72. return true;
  73. }
  74. Ref<Texture> EditorTexturePreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  75. Ref<Image> img;
  76. Ref<AtlasTexture> atex = p_from;
  77. Ref<LargeTexture> ltex = p_from;
  78. if (atex.is_valid()) {
  79. Ref<Texture> tex = atex->get_atlas();
  80. if (!tex.is_valid()) {
  81. return Ref<Texture>();
  82. }
  83. Ref<Image> atlas = tex->get_data();
  84. if (!atlas.is_valid()) {
  85. return Ref<Texture>();
  86. }
  87. img = atlas->get_rect(atex->get_region());
  88. } else if (ltex.is_valid()) {
  89. img = ltex->to_image();
  90. } else {
  91. Ref<Texture> tex = p_from;
  92. if (tex.is_valid()) {
  93. img = tex->get_data();
  94. if (img.is_valid()) {
  95. img = img->duplicate();
  96. }
  97. }
  98. }
  99. if (img.is_null() || img->empty()) {
  100. return Ref<Texture>();
  101. }
  102. img->clear_mipmaps();
  103. if (img->is_compressed()) {
  104. if (img->decompress() != OK) {
  105. return Ref<Texture>();
  106. }
  107. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  108. img->convert(Image::FORMAT_RGBA8);
  109. }
  110. Vector2 new_size = img->get_size();
  111. if (new_size.x > p_size.x) {
  112. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  113. }
  114. if (new_size.y > p_size.y) {
  115. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  116. }
  117. Vector2i new_size_i(MAX(1, (int)new_size.x), MAX(1, (int)new_size.y));
  118. img->resize(new_size_i.x, new_size_i.y, Image::INTERPOLATE_CUBIC);
  119. post_process_preview(img);
  120. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  121. ptex->create_from_image(img, 0);
  122. return ptex;
  123. }
  124. EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() {
  125. }
  126. ////////////////////////////////////////////////////////////////////////////
  127. bool EditorImagePreviewPlugin::handles(const String &p_type) const {
  128. return p_type == "Image";
  129. }
  130. Ref<Texture> EditorImagePreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  131. Ref<Image> img = p_from;
  132. if (img.is_null() || img->empty()) {
  133. return Ref<Image>();
  134. }
  135. img = img->duplicate();
  136. img->clear_mipmaps();
  137. if (img->is_compressed()) {
  138. if (img->decompress() != OK) {
  139. return Ref<Image>();
  140. }
  141. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  142. img->convert(Image::FORMAT_RGBA8);
  143. }
  144. Vector2 new_size = img->get_size();
  145. if (new_size.x > p_size.x) {
  146. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  147. }
  148. if (new_size.y > p_size.y) {
  149. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  150. }
  151. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  152. post_process_preview(img);
  153. Ref<ImageTexture> ptex;
  154. ptex.instance();
  155. ptex->create_from_image(img, 0);
  156. return ptex;
  157. }
  158. EditorImagePreviewPlugin::EditorImagePreviewPlugin() {
  159. }
  160. bool EditorImagePreviewPlugin::generate_small_preview_automatically() const {
  161. return true;
  162. }
  163. ////////////////////////////////////////////////////////////////////////////
  164. /////////////////////////////////////////////////
  165. bool EditorBitmapPreviewPlugin::handles(const String &p_type) const {
  166. return ClassDB::is_parent_class(p_type, "BitMap");
  167. }
  168. Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  169. Ref<BitMap> bm = p_from;
  170. if (bm->get_size() == Size2()) {
  171. return Ref<Texture>();
  172. }
  173. PoolVector<uint8_t> data;
  174. data.resize(bm->get_size().width * bm->get_size().height);
  175. {
  176. PoolVector<uint8_t>::Write w = data.write();
  177. for (int i = 0; i < bm->get_size().width; i++) {
  178. for (int j = 0; j < bm->get_size().height; j++) {
  179. if (bm->get_bit(Point2i(i, j))) {
  180. w[j * bm->get_size().width + i] = 255;
  181. } else {
  182. w[j * bm->get_size().width + i] = 0;
  183. }
  184. }
  185. }
  186. }
  187. Ref<Image> img;
  188. img.instance();
  189. img->create(bm->get_size().width, bm->get_size().height, false, Image::FORMAT_L8, data);
  190. if (img->is_compressed()) {
  191. if (img->decompress() != OK) {
  192. return Ref<Texture>();
  193. }
  194. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  195. img->convert(Image::FORMAT_RGBA8);
  196. }
  197. Vector2 new_size = img->get_size();
  198. if (new_size.x > p_size.x) {
  199. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  200. }
  201. if (new_size.y > p_size.y) {
  202. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  203. }
  204. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  205. post_process_preview(img);
  206. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  207. ptex->create_from_image(img, 0);
  208. return ptex;
  209. }
  210. bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const {
  211. return true;
  212. }
  213. EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() {
  214. }
  215. ///////////////////////////////////////////////////////////////////////////
  216. bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {
  217. return ClassDB::is_parent_class(p_type, "PackedScene");
  218. }
  219. Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  220. return generate_from_path(p_from->get_path(), p_size);
  221. }
  222. Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size) const {
  223. String temp_path = EditorSettings::get_singleton()->get_cache_dir();
  224. String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();
  225. cache_base = temp_path.plus_file("resthumb-" + cache_base);
  226. //does not have it, try to load a cached thumbnail
  227. String path = cache_base + ".png";
  228. if (!FileAccess::exists(path)) {
  229. return Ref<Texture>();
  230. }
  231. Ref<Image> img;
  232. img.instance();
  233. Error err = img->load(path);
  234. if (err == OK) {
  235. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  236. post_process_preview(img);
  237. ptex->create_from_image(img, 0);
  238. return ptex;
  239. } else {
  240. return Ref<Texture>();
  241. }
  242. }
  243. EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() {
  244. }
  245. //////////////////////////////////////////////////////////////////
  246. void EditorMaterialPreviewPlugin::_preview_done(const Variant &p_udata) {
  247. preview_done.set();
  248. }
  249. void EditorMaterialPreviewPlugin::_bind_methods() {
  250. ClassDB::bind_method("_preview_done", &EditorMaterialPreviewPlugin::_preview_done);
  251. }
  252. bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
  253. return ClassDB::is_parent_class(p_type, "Material"); //any material
  254. }
  255. bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const {
  256. return true;
  257. }
  258. Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  259. Ref<Material> material = p_from;
  260. ERR_FAIL_COND_V(material.is_null(), Ref<Texture>());
  261. if (material->get_shader_mode() == Shader::MODE_SPATIAL) {
  262. VS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid());
  263. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
  264. preview_done.clear();
  265. VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorMaterialPreviewPlugin *>(this), "_preview_done", Variant());
  266. while (!preview_done.is_set()) {
  267. OS::get_singleton()->delay_usec(10);
  268. }
  269. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  270. VS::get_singleton()->mesh_surface_set_material(sphere, 0, RID());
  271. ERR_FAIL_COND_V(!img.is_valid(), Ref<ImageTexture>());
  272. img->convert(Image::FORMAT_RGBA8);
  273. int thumbnail_size = MAX(p_size.x, p_size.y);
  274. img->resize(thumbnail_size, thumbnail_size, Image::INTERPOLATE_CUBIC);
  275. post_process_preview(img);
  276. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  277. ptex->create_from_image(img, 0);
  278. return ptex;
  279. }
  280. return Ref<Texture>();
  281. }
  282. EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
  283. scenario = RID_PRIME(VS::get_singleton()->scenario_create());
  284. viewport = RID_PRIME(VS::get_singleton()->viewport_create());
  285. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED);
  286. VS::get_singleton()->viewport_set_scenario(viewport, scenario);
  287. VS::get_singleton()->viewport_set_size(viewport, 128, 128);
  288. VS::get_singleton()->viewport_set_transparent_background(viewport, true);
  289. VS::get_singleton()->viewport_set_active(viewport, true);
  290. VS::get_singleton()->viewport_set_vflip(viewport, true);
  291. viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  292. camera = RID_PRIME(VS::get_singleton()->camera_create());
  293. VS::get_singleton()->viewport_attach_camera(viewport, camera);
  294. VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3)));
  295. VS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10);
  296. light = RID_PRIME(VS::get_singleton()->directional_light_create());
  297. light_instance = VS::get_singleton()->instance_create2(light, scenario);
  298. VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  299. light2 = RID_PRIME(VS::get_singleton()->directional_light_create());
  300. VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  301. //VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  302. light_instance2 = VS::get_singleton()->instance_create2(light2, scenario);
  303. VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  304. sphere = RID_PRIME(VS::get_singleton()->mesh_create());
  305. sphere_instance = VS::get_singleton()->instance_create2(sphere, scenario);
  306. int lats = 32;
  307. int lons = 32;
  308. float radius = 1.0;
  309. PoolVector<Vector3> vertices;
  310. PoolVector<Vector3> normals;
  311. PoolVector<Vector2> uvs;
  312. PoolVector<float> tangents;
  313. Basis tt = Basis(Vector3(0, 1, 0), Math_PI * 0.5);
  314. for (int i = 1; i <= lats; i++) {
  315. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / lats);
  316. double z0 = Math::sin(lat0);
  317. double zr0 = Math::cos(lat0);
  318. double lat1 = Math_PI * (-0.5 + (double)i / lats);
  319. double z1 = Math::sin(lat1);
  320. double zr1 = Math::cos(lat1);
  321. for (int j = lons; j >= 1; j--) {
  322. double lng0 = 2 * Math_PI * (double)(j - 1) / lons;
  323. double x0 = Math::cos(lng0);
  324. double y0 = Math::sin(lng0);
  325. double lng1 = 2 * Math_PI * (double)(j) / lons;
  326. double x1 = Math::cos(lng1);
  327. double y1 = Math::sin(lng1);
  328. Vector3 v[4] = {
  329. Vector3(x1 * zr0, z0, y1 * zr0),
  330. Vector3(x1 * zr1, z1, y1 * zr1),
  331. Vector3(x0 * zr1, z1, y0 * zr1),
  332. Vector3(x0 * zr0, z0, y0 * zr0)
  333. };
  334. #define ADD_POINT(m_idx) \
  335. normals.push_back(v[m_idx]); \
  336. vertices.push_back(v[m_idx] * radius); \
  337. { \
  338. Vector2 uv(Math::atan2(v[m_idx].x, v[m_idx].z), Math::atan2(-v[m_idx].y, v[m_idx].z)); \
  339. uv /= Math_PI; \
  340. uv *= 4.0; \
  341. uv = uv * 0.5 + Vector2(0.5, 0.5); \
  342. uvs.push_back(uv); \
  343. } \
  344. { \
  345. Vector3 t = tt.xform(v[m_idx]); \
  346. tangents.push_back(t.x); \
  347. tangents.push_back(t.y); \
  348. tangents.push_back(t.z); \
  349. tangents.push_back(1.0); \
  350. }
  351. ADD_POINT(0);
  352. ADD_POINT(1);
  353. ADD_POINT(2);
  354. ADD_POINT(2);
  355. ADD_POINT(3);
  356. ADD_POINT(0);
  357. }
  358. }
  359. Array arr;
  360. arr.resize(VS::ARRAY_MAX);
  361. arr[VS::ARRAY_VERTEX] = vertices;
  362. arr[VS::ARRAY_NORMAL] = normals;
  363. arr[VS::ARRAY_TANGENT] = tangents;
  364. arr[VS::ARRAY_TEX_UV] = uvs;
  365. VS::get_singleton()->mesh_add_surface_from_arrays(sphere, VS::PRIMITIVE_TRIANGLES, arr);
  366. }
  367. EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() {
  368. VS::get_singleton()->free(sphere);
  369. VS::get_singleton()->free(sphere_instance);
  370. VS::get_singleton()->free(viewport);
  371. VS::get_singleton()->free(light);
  372. VS::get_singleton()->free(light_instance);
  373. VS::get_singleton()->free(light2);
  374. VS::get_singleton()->free(light_instance2);
  375. VS::get_singleton()->free(camera);
  376. VS::get_singleton()->free(scenario);
  377. }
  378. ///////////////////////////////////////////////////////////////////////////
  379. static bool _is_text_char(CharType c) {
  380. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  381. }
  382. bool EditorScriptPreviewPlugin::handles(const String &p_type) const {
  383. return ClassDB::is_parent_class(p_type, "Script");
  384. }
  385. Ref<Texture> EditorScriptPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  386. Ref<Script> scr = p_from;
  387. if (scr.is_null()) {
  388. return Ref<Texture>();
  389. }
  390. String code = scr->get_source_code().strip_edges();
  391. if (code == "") {
  392. return Ref<Texture>();
  393. }
  394. List<String> kwors;
  395. scr->get_language()->get_reserved_words(&kwors);
  396. Set<String> control_flow_keywords;
  397. Set<String> keywords;
  398. for (List<String>::Element *E = kwors.front(); E; E = E->next()) {
  399. if (scr->get_language()->is_control_flow_keyword(E->get())) {
  400. control_flow_keywords.insert(E->get());
  401. } else {
  402. keywords.insert(E->get());
  403. }
  404. }
  405. int line = 0;
  406. int col = 0;
  407. Ref<Image> img;
  408. img.instance();
  409. int thumbnail_size = MAX(p_size.x, p_size.y);
  410. img->create(thumbnail_size, thumbnail_size, false, Image::FORMAT_RGBA8);
  411. Color bg_color = EditorSettings::get_singleton()->get("text_editor/highlighting/background_color");
  412. Color keyword_color = EditorSettings::get_singleton()->get("text_editor/highlighting/keyword_color");
  413. Color control_flow_keyword_color = EditorSettings::get_singleton()->get("text_editor/highlighting/control_flow_keyword_color");
  414. Color text_color = EditorSettings::get_singleton()->get("text_editor/highlighting/text_color");
  415. Color symbol_color = EditorSettings::get_singleton()->get("text_editor/highlighting/symbol_color");
  416. Color comment_color = EditorSettings::get_singleton()->get("text_editor/highlighting/comment_color");
  417. img->lock();
  418. if (bg_color.a == 0) {
  419. bg_color = Color(0, 0, 0, 0);
  420. }
  421. bg_color.a = MAX(bg_color.a, 0.2); // some background
  422. for (int i = 0; i < thumbnail_size; i++) {
  423. for (int j = 0; j < thumbnail_size; j++) {
  424. img->set_pixel(i, j, bg_color);
  425. }
  426. }
  427. const int x0 = thumbnail_size / 8;
  428. const int y0 = thumbnail_size / 8;
  429. const int available_height = thumbnail_size - 2 * y0;
  430. col = x0;
  431. bool prev_is_text = false;
  432. bool in_control_flow_keyword = false;
  433. bool in_keyword = false;
  434. bool in_comment = false;
  435. for (int i = 0; i < code.length(); i++) {
  436. CharType c = code[i];
  437. if (c > 32) {
  438. if (col < thumbnail_size) {
  439. Color color = text_color;
  440. if (c == '#') {
  441. in_comment = true;
  442. }
  443. if (in_comment) {
  444. color = comment_color;
  445. } else {
  446. if (c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t')) {
  447. //make symbol a little visible
  448. color = symbol_color;
  449. in_control_flow_keyword = false;
  450. in_keyword = false;
  451. } else if (!prev_is_text && _is_text_char(c)) {
  452. int pos = i;
  453. while (_is_text_char(code[pos])) {
  454. pos++;
  455. }
  456. const String word = code.substr(i, pos - i);
  457. if (control_flow_keywords.has(word)) {
  458. in_control_flow_keyword = true;
  459. } else if (keywords.has(word)) {
  460. in_keyword = true;
  461. }
  462. } else if (!_is_text_char(c)) {
  463. in_control_flow_keyword = false;
  464. in_keyword = false;
  465. }
  466. if (in_control_flow_keyword) {
  467. color = control_flow_keyword_color;
  468. } else if (in_keyword) {
  469. color = keyword_color;
  470. }
  471. }
  472. Color ul = color;
  473. ul.a *= 0.5;
  474. img->set_pixel(col, y0 + line * 2, bg_color.blend(ul));
  475. img->set_pixel(col, y0 + line * 2 + 1, color);
  476. prev_is_text = _is_text_char(c);
  477. }
  478. col++;
  479. } else {
  480. prev_is_text = false;
  481. in_control_flow_keyword = false;
  482. in_keyword = false;
  483. if (c == '\n') {
  484. in_comment = false;
  485. col = x0;
  486. line++;
  487. if (line >= available_height / 2) {
  488. break;
  489. }
  490. } else if (c == '\t') {
  491. col += 3;
  492. } else {
  493. col++;
  494. }
  495. }
  496. }
  497. img->unlock();
  498. post_process_preview(img);
  499. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  500. ptex->create_from_image(img, 0);
  501. return ptex;
  502. }
  503. EditorScriptPreviewPlugin::EditorScriptPreviewPlugin() {
  504. }
  505. ///////////////////////////////////////////////////////////////////
  506. bool EditorAudioStreamPreviewPlugin::handles(const String &p_type) const {
  507. return ClassDB::is_parent_class(p_type, "AudioStream");
  508. }
  509. Ref<Texture> EditorAudioStreamPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  510. Ref<AudioStream> stream = p_from;
  511. ERR_FAIL_COND_V(stream.is_null(), Ref<Texture>());
  512. PoolVector<uint8_t> img;
  513. int w = p_size.x;
  514. int h = p_size.y;
  515. img.resize(w * h * 3);
  516. PoolVector<uint8_t>::Write imgdata = img.write();
  517. uint8_t *imgw = imgdata.ptr();
  518. Ref<AudioStreamPlayback> playback = stream->instance_playback();
  519. ERR_FAIL_COND_V(playback.is_null(), Ref<Texture>());
  520. float len_s = stream->get_length();
  521. if (len_s == 0) {
  522. len_s = 60; //one minute audio if no length specified
  523. }
  524. int frame_length = AudioServer::get_singleton()->get_mix_rate() * len_s;
  525. Vector<AudioFrame> frames;
  526. frames.resize(frame_length);
  527. playback->start();
  528. playback->mix(frames.ptrw(), 1, frames.size());
  529. playback->stop();
  530. for (int i = 0; i < w; i++) {
  531. float max = -1000;
  532. float min = 1000;
  533. int from = uint64_t(i) * frame_length / w;
  534. int to = (uint64_t(i) + 1) * frame_length / w;
  535. to = MIN(to, frame_length);
  536. from = MIN(from, frame_length - 1);
  537. if (to == from) {
  538. to = from + 1;
  539. }
  540. for (int j = from; j < to; j++) {
  541. max = MAX(max, frames[j].l);
  542. max = MAX(max, frames[j].r);
  543. min = MIN(min, frames[j].l);
  544. min = MIN(min, frames[j].r);
  545. }
  546. int pfrom = CLAMP((min * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
  547. int pto = CLAMP((max * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
  548. for (int j = 0; j < h; j++) {
  549. uint8_t *p = &imgw[(j * w + i) * 3];
  550. if (j < pfrom || j > pto) {
  551. p[0] = 100;
  552. p[1] = 100;
  553. p[2] = 100;
  554. } else {
  555. p[0] = 180;
  556. p[1] = 180;
  557. p[2] = 180;
  558. }
  559. }
  560. }
  561. imgdata.release();
  562. //post_process_preview(img);
  563. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  564. Ref<Image> image;
  565. image.instance();
  566. image->create(w, h, false, Image::FORMAT_RGB8, img);
  567. ptex->create_from_image(image, 0);
  568. return ptex;
  569. }
  570. EditorAudioStreamPreviewPlugin::EditorAudioStreamPreviewPlugin() {
  571. }
  572. ///////////////////////////////////////////////////////////////////////////
  573. void EditorMeshPreviewPlugin::_preview_done(const Variant &p_udata) {
  574. preview_done.set();
  575. }
  576. void EditorMeshPreviewPlugin::_bind_methods() {
  577. ClassDB::bind_method("_preview_done", &EditorMeshPreviewPlugin::_preview_done);
  578. }
  579. bool EditorMeshPreviewPlugin::handles(const String &p_type) const {
  580. return ClassDB::is_parent_class(p_type, "Mesh"); //any Mesh
  581. }
  582. Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  583. Ref<Mesh> mesh = p_from;
  584. ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture>());
  585. VS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid());
  586. AABB aabb = mesh->get_aabb();
  587. Vector3 ofs = aabb.position + aabb.size * 0.5;
  588. aabb.position -= ofs;
  589. Transform xform;
  590. xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.125);
  591. xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.125) * xform.basis;
  592. AABB rot_aabb = xform.xform(aabb);
  593. float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
  594. if (m == 0) {
  595. return Ref<Texture>();
  596. }
  597. m = 1.0 / m;
  598. m *= 0.5;
  599. xform.basis.scale(Vector3(m, m, m));
  600. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  601. xform.origin.z -= rot_aabb.size.z * 2;
  602. VS::get_singleton()->instance_set_transform(mesh_instance, xform);
  603. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
  604. preview_done.clear();
  605. VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorMeshPreviewPlugin *>(this), "_preview_done", Variant());
  606. while (!preview_done.is_set()) {
  607. OS::get_singleton()->delay_usec(10);
  608. }
  609. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  610. ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());
  611. VS::get_singleton()->instance_set_base(mesh_instance, RID());
  612. img->convert(Image::FORMAT_RGBA8);
  613. Vector2 new_size = img->get_size();
  614. if (new_size.x > p_size.x) {
  615. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  616. }
  617. if (new_size.y > p_size.y) {
  618. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  619. }
  620. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  621. post_process_preview(img);
  622. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  623. ptex->create_from_image(img, 0);
  624. return ptex;
  625. }
  626. EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() {
  627. scenario = RID_PRIME(VS::get_singleton()->scenario_create());
  628. viewport = RID_PRIME(VS::get_singleton()->viewport_create());
  629. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED);
  630. VS::get_singleton()->viewport_set_vflip(viewport, true);
  631. VS::get_singleton()->viewport_set_scenario(viewport, scenario);
  632. VS::get_singleton()->viewport_set_size(viewport, 128, 128);
  633. VS::get_singleton()->viewport_set_transparent_background(viewport, true);
  634. VS::get_singleton()->viewport_set_active(viewport, true);
  635. viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  636. camera = VS::get_singleton()->camera_create();
  637. VS::get_singleton()->viewport_attach_camera(viewport, camera);
  638. VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3)));
  639. //VS::get_singleton()->camera_set_perspective(camera,45,0.1,10);
  640. VS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0);
  641. light = VS::get_singleton()->directional_light_create();
  642. light_instance = VS::get_singleton()->instance_create2(light, scenario);
  643. VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  644. light2 = VS::get_singleton()->directional_light_create();
  645. VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  646. //VS::get_singleton()->light_set_color(light2, VS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0));
  647. light_instance2 = VS::get_singleton()->instance_create2(light2, scenario);
  648. VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  649. //sphere = RID_PRIME(VS::get_singleton()->mesh_create());
  650. mesh_instance = RID_PRIME(VS::get_singleton()->instance_create());
  651. VS::get_singleton()->instance_set_scenario(mesh_instance, scenario);
  652. }
  653. EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() {
  654. //VS::get_singleton()->free(sphere);
  655. VS::get_singleton()->free(mesh_instance);
  656. VS::get_singleton()->free(viewport);
  657. VS::get_singleton()->free(light);
  658. VS::get_singleton()->free(light_instance);
  659. VS::get_singleton()->free(light2);
  660. VS::get_singleton()->free(light_instance2);
  661. VS::get_singleton()->free(camera);
  662. VS::get_singleton()->free(scenario);
  663. }
  664. ///////////////////////////////////////////////////////////////////////////
  665. void EditorFontPreviewPlugin::_preview_done(const Variant &p_udata) {
  666. preview_done.set();
  667. }
  668. void EditorFontPreviewPlugin::_bind_methods() {
  669. ClassDB::bind_method("_preview_done", &EditorFontPreviewPlugin::_preview_done);
  670. }
  671. bool EditorFontPreviewPlugin::handles(const String &p_type) const {
  672. return ClassDB::is_parent_class(p_type, "DynamicFontData") || ClassDB::is_parent_class(p_type, "DynamicFont");
  673. }
  674. Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size) const {
  675. Ref<ResourceInteractiveLoader> ril = ResourceLoader::load_interactive(p_path);
  676. ril.ptr()->wait();
  677. RES res = ril.ptr()->get_resource();
  678. Ref<DynamicFont> sampled_font;
  679. sampled_font.instance();
  680. if (res->is_class("DynamicFont")) {
  681. Ref<DynamicFont> font = res;
  682. if (font->get_font_data().is_valid()) {
  683. sampled_font->set_font_data(font->get_font_data()->duplicate());
  684. }
  685. for (int i = 0; i < font->get_fallback_count(); i++) {
  686. sampled_font->add_fallback(font->get_fallback(i)->duplicate());
  687. }
  688. } else if (res->is_class("DynamicFontData")) {
  689. sampled_font->set_font_data(res->duplicate());
  690. }
  691. sampled_font->set_size(50);
  692. String sampled_text = "Abg";
  693. Vector2 size = sampled_font->get_string_size(sampled_text);
  694. Vector2 pos;
  695. pos.x = 64 - size.x / 2;
  696. pos.y = 80;
  697. Ref<Font> font = sampled_font;
  698. const Color c = GLOBAL_GET("rendering/environment/default_clear_color");
  699. const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;
  700. font->draw(canvas_item, pos, sampled_text, Color(fg, fg, fg));
  701. preview_done.clear();
  702. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
  703. VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorFontPreviewPlugin *>(this), "_preview_done", Variant());
  704. while (!preview_done.is_set()) {
  705. OS::get_singleton()->delay_usec(10);
  706. }
  707. VS::get_singleton()->canvas_item_clear(canvas_item);
  708. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  709. ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());
  710. img->convert(Image::FORMAT_RGBA8);
  711. Vector2 new_size = img->get_size();
  712. if (new_size.x > p_size.x) {
  713. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  714. }
  715. if (new_size.y > p_size.y) {
  716. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  717. }
  718. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  719. post_process_preview(img);
  720. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  721. ptex->create_from_image(img, 0);
  722. return ptex;
  723. }
  724. Ref<Texture> EditorFontPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  725. String path = p_from->get_path();
  726. if (!FileAccess::exists(path)) {
  727. return Ref<Texture>();
  728. }
  729. return generate_from_path(path, p_size);
  730. }
  731. EditorFontPreviewPlugin::EditorFontPreviewPlugin() {
  732. viewport = RID_PRIME(VS::get_singleton()->viewport_create());
  733. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED);
  734. VS::get_singleton()->viewport_set_vflip(viewport, true);
  735. VS::get_singleton()->viewport_set_size(viewport, 128, 128);
  736. VS::get_singleton()->viewport_set_active(viewport, true);
  737. viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  738. canvas = RID_PRIME(VS::get_singleton()->canvas_create());
  739. canvas_item = RID_PRIME(VS::get_singleton()->canvas_item_create());
  740. VS::get_singleton()->viewport_attach_canvas(viewport, canvas);
  741. VS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  742. }
  743. EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {
  744. VS::get_singleton()->free(canvas_item);
  745. VS::get_singleton()->free(canvas);
  746. VS::get_singleton()->free(viewport);
  747. }
  748. ///////////////////////////////////////////////////////////////////////////
  749. static const real_t GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR = 4.0;
  750. bool EditorGradientPreviewPlugin::handles(const String &p_type) const {
  751. return ClassDB::is_parent_class(p_type, "Gradient");
  752. }
  753. bool EditorGradientPreviewPlugin::generate_small_preview_automatically() const {
  754. return true;
  755. }
  756. Ref<Texture> EditorGradientPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size) const {
  757. Ref<Gradient> gradient = p_from;
  758. if (gradient.is_valid()) {
  759. Ref<GradientTexture> ptex = Ref<GradientTexture>(memnew(GradientTexture));
  760. ptex->set_width(p_size.width * GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR * EDSCALE);
  761. ptex->set_gradient(gradient);
  762. Ref<ImageTexture> image = Ref<ImageTexture>(memnew(ImageTexture));
  763. image->create_from_image(ptex->get_data());
  764. return image;
  765. }
  766. return Ref<Texture>();
  767. }
  768. EditorGradientPreviewPlugin::EditorGradientPreviewPlugin() {
  769. }