theme_editor_plugin.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*************************************************************************/
  2. /* theme_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "theme_editor_plugin.h"
  31. #include "os/file_access.h"
  32. #include "version.h"
  33. void ThemeEditor::edit(const Ref<Theme> &p_theme) {
  34. theme = p_theme;
  35. main_vb->set_theme(p_theme);
  36. }
  37. void ThemeEditor::_propagate_redraw(Control *p_at) {
  38. p_at->notification(NOTIFICATION_THEME_CHANGED);
  39. p_at->minimum_size_changed();
  40. p_at->update();
  41. for (int i = 0; i < p_at->get_child_count(); i++) {
  42. Control *a = Object::cast_to<Control>(p_at->get_child(i));
  43. if (a)
  44. _propagate_redraw(a);
  45. }
  46. }
  47. void ThemeEditor::_refresh_interval() {
  48. _propagate_redraw(main_vb);
  49. }
  50. void ThemeEditor::_type_menu_cbk(int p_option) {
  51. type_edit->set_text(type_menu->get_popup()->get_item_text(p_option));
  52. }
  53. void ThemeEditor::_name_menu_about_to_show() {
  54. String fromtype = type_edit->get_text();
  55. List<StringName> names;
  56. if (popup_mode == POPUP_ADD) {
  57. switch (type_select->get_selected()) {
  58. case 0: Theme::get_default()->get_icon_list(fromtype, &names); break;
  59. case 1: Theme::get_default()->get_stylebox_list(fromtype, &names); break;
  60. case 2: Theme::get_default()->get_font_list(fromtype, &names); break;
  61. case 3: Theme::get_default()->get_color_list(fromtype, &names); break;
  62. case 4: Theme::get_default()->get_constant_list(fromtype, &names); break;
  63. }
  64. } else if (popup_mode == POPUP_REMOVE) {
  65. theme->get_icon_list(fromtype, &names);
  66. theme->get_stylebox_list(fromtype, &names);
  67. theme->get_font_list(fromtype, &names);
  68. theme->get_color_list(fromtype, &names);
  69. theme->get_constant_list(fromtype, &names);
  70. }
  71. name_menu->get_popup()->clear();
  72. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  73. name_menu->get_popup()->add_item(E->get());
  74. }
  75. }
  76. void ThemeEditor::_name_menu_cbk(int p_option) {
  77. name_edit->set_text(name_menu->get_popup()->get_item_text(p_option));
  78. }
  79. struct _TECategory {
  80. template <class T>
  81. struct RefItem {
  82. Ref<T> item;
  83. StringName name;
  84. bool operator<(const RefItem<T> &p) const { return item->get_instance_id() < p.item->get_instance_id(); }
  85. };
  86. template <class T>
  87. struct Item {
  88. T item;
  89. String name;
  90. bool operator<(const Item<T> &p) const { return name < p.name; }
  91. };
  92. Set<RefItem<StyleBox> > stylebox_items;
  93. Set<RefItem<Font> > font_items;
  94. Set<RefItem<Texture> > icon_items;
  95. Set<Item<Color> > color_items;
  96. Set<Item<int> > constant_items;
  97. };
  98. void ThemeEditor::_save_template_cbk(String fname) {
  99. String filename = file_dialog->get_current_path();
  100. Map<String, _TECategory> categories;
  101. //fill types
  102. List<StringName> type_list;
  103. Theme::get_default()->get_type_list(&type_list);
  104. for (List<StringName>::Element *E = type_list.front(); E; E = E->next()) {
  105. categories.insert(E->get(), _TECategory());
  106. }
  107. //fill default theme
  108. for (Map<String, _TECategory>::Element *E = categories.front(); E; E = E->next()) {
  109. _TECategory &tc = E->get();
  110. List<StringName> stylebox_list;
  111. Theme::get_default()->get_stylebox_list(E->key(), &stylebox_list);
  112. for (List<StringName>::Element *F = stylebox_list.front(); F; F = F->next()) {
  113. _TECategory::RefItem<StyleBox> it;
  114. it.name = F->get();
  115. it.item = Theme::get_default()->get_stylebox(F->get(), E->key());
  116. tc.stylebox_items.insert(it);
  117. }
  118. List<StringName> font_list;
  119. Theme::get_default()->get_font_list(E->key(), &font_list);
  120. for (List<StringName>::Element *F = font_list.front(); F; F = F->next()) {
  121. _TECategory::RefItem<Font> it;
  122. it.name = F->get();
  123. it.item = Theme::get_default()->get_font(F->get(), E->key());
  124. tc.font_items.insert(it);
  125. }
  126. List<StringName> icon_list;
  127. Theme::get_default()->get_icon_list(E->key(), &icon_list);
  128. for (List<StringName>::Element *F = icon_list.front(); F; F = F->next()) {
  129. _TECategory::RefItem<Texture> it;
  130. it.name = F->get();
  131. it.item = Theme::get_default()->get_icon(F->get(), E->key());
  132. tc.icon_items.insert(it);
  133. }
  134. List<StringName> color_list;
  135. Theme::get_default()->get_color_list(E->key(), &color_list);
  136. for (List<StringName>::Element *F = color_list.front(); F; F = F->next()) {
  137. _TECategory::Item<Color> it;
  138. it.name = F->get();
  139. it.item = Theme::get_default()->get_color(F->get(), E->key());
  140. tc.color_items.insert(it);
  141. }
  142. List<StringName> constant_list;
  143. Theme::get_default()->get_constant_list(E->key(), &constant_list);
  144. for (List<StringName>::Element *F = constant_list.front(); F; F = F->next()) {
  145. _TECategory::Item<int> it;
  146. it.name = F->get();
  147. it.item = Theme::get_default()->get_constant(F->get(), E->key());
  148. tc.constant_items.insert(it);
  149. }
  150. }
  151. FileAccess *file = FileAccess::open(filename, FileAccess::WRITE);
  152. if (!file) {
  153. ERR_EXPLAIN(TTR("Can't save theme to file:") + " " + filename);
  154. return;
  155. }
  156. file->store_line("; ******************* ");
  157. file->store_line("; Template Theme File ");
  158. file->store_line("; ******************* ");
  159. file->store_line("; ");
  160. file->store_line("; Theme Syntax: ");
  161. file->store_line("; ------------- ");
  162. file->store_line("; ");
  163. file->store_line("; Must be placed in section [theme]");
  164. file->store_line("; ");
  165. file->store_line("; Type.item = [value] ");
  166. file->store_line("; ");
  167. file->store_line("; [value] examples:");
  168. file->store_line("; ");
  169. file->store_line("; Type.item = 6 ; numeric constant. ");
  170. file->store_line("; Type.item = #FF00FF ; HTML color ");
  171. file->store_line("; Type.item = #55FF00FF ; HTML color with alpha 55.");
  172. file->store_line("; Type.item = icon(image.png) ; icon in a png file (relative to theme file).");
  173. file->store_line("; Type.item = font(font.xres) ; font in a resource (relative to theme file).");
  174. file->store_line("; Type.item = sbox(stylebox.xres) ; stylebox in a resource (relative to theme file).");
  175. file->store_line("; Type.item = sboxf(2,#FF00FF) ; flat stylebox with margin 2.");
  176. file->store_line("; Type.item = sboxf(2,#FF00FF,#FFFFFF) ; flat stylebox with margin 2 and border.");
  177. file->store_line("; Type.item = sboxf(2,#FF00FF,#FFFFFF,#000000) ; flat stylebox with margin 2, light & dark borders.");
  178. file->store_line("; Type.item = sboxt(base.png,2,2,2,2) ; textured stylebox with 3x3 stretch and stretch margins.");
  179. file->store_line("; -Additionally, 4 extra integers can be added to sboxf and sboxt to specify custom padding of contents:");
  180. file->store_line("; Type.item = sboxt(base.png,2,2,2,2,5,4,2,4) ;");
  181. file->store_line("; -Order for all is always left, top, right, bottom.");
  182. file->store_line("; ");
  183. file->store_line("; Special values:");
  184. file->store_line("; Type.item = default ; use the value in the default theme (must exist there).");
  185. file->store_line("; Type.item = @somebutton_color ; reference to a library value previously defined.");
  186. file->store_line("; ");
  187. file->store_line("; Library Syntax: ");
  188. file->store_line("; --------------- ");
  189. file->store_line("; ");
  190. file->store_line("; Must be placed in section [library], but usage is optional.");
  191. file->store_line("; ");
  192. file->store_line("; item = [value] ; same as Theme, but assign to library.");
  193. file->store_line("; ");
  194. file->store_line("; examples:");
  195. file->store_line("; ");
  196. file->store_line("; [library]");
  197. file->store_line("; ");
  198. file->store_line("; default_button_color = #FF00FF");
  199. file->store_line("; ");
  200. file->store_line("; [theme]");
  201. file->store_line("; ");
  202. file->store_line("; Button.color = @default_button_color ; used reference.");
  203. file->store_line("; ");
  204. file->store_line("; ******************* ");
  205. file->store_line("; ");
  206. file->store_line("; Template Generated Using: " + String(VERSION_MKSTRING));
  207. file->store_line("; ");
  208. file->store_line("; ");
  209. file->store_line("");
  210. file->store_line("[library]");
  211. file->store_line("");
  212. file->store_line("; place library stuff here");
  213. file->store_line("");
  214. file->store_line("[theme]");
  215. file->store_line("");
  216. file->store_line("");
  217. //write default theme
  218. for (Map<String, _TECategory>::Element *E = categories.front(); E; E = E->next()) {
  219. _TECategory &tc = E->get();
  220. String underline = "; ";
  221. for (int i = 0; i < E->key().length(); i++)
  222. underline += "*";
  223. file->store_line("");
  224. file->store_line(underline);
  225. file->store_line("; " + E->key());
  226. file->store_line(underline);
  227. if (tc.stylebox_items.size())
  228. file->store_line("\n; StyleBox Items:\n");
  229. for (Set<_TECategory::RefItem<StyleBox> >::Element *F = tc.stylebox_items.front(); F; F = F->next()) {
  230. file->store_line(E->key() + "." + F->get().name + " = default");
  231. }
  232. if (tc.font_items.size())
  233. file->store_line("\n; Font Items:\n");
  234. for (Set<_TECategory::RefItem<Font> >::Element *F = tc.font_items.front(); F; F = F->next()) {
  235. file->store_line(E->key() + "." + F->get().name + " = default");
  236. }
  237. if (tc.icon_items.size())
  238. file->store_line("\n; Icon Items:\n");
  239. for (Set<_TECategory::RefItem<Texture> >::Element *F = tc.icon_items.front(); F; F = F->next()) {
  240. file->store_line(E->key() + "." + F->get().name + " = default");
  241. }
  242. if (tc.color_items.size())
  243. file->store_line("\n; Color Items:\n");
  244. for (Set<_TECategory::Item<Color> >::Element *F = tc.color_items.front(); F; F = F->next()) {
  245. file->store_line(E->key() + "." + F->get().name + " = default");
  246. }
  247. if (tc.constant_items.size())
  248. file->store_line("\n; Constant Items:\n");
  249. for (Set<_TECategory::Item<int> >::Element *F = tc.constant_items.front(); F; F = F->next()) {
  250. file->store_line(E->key() + "." + F->get().name + " = default");
  251. }
  252. }
  253. file->close();
  254. memdelete(file);
  255. }
  256. void ThemeEditor::_dialog_cbk() {
  257. switch (popup_mode) {
  258. case POPUP_ADD: {
  259. switch (type_select->get_selected()) {
  260. case 0: theme->set_icon(name_edit->get_text(), type_edit->get_text(), Ref<Texture>()); break;
  261. case 1: theme->set_stylebox(name_edit->get_text(), type_edit->get_text(), Ref<StyleBox>()); break;
  262. case 2: theme->set_font(name_edit->get_text(), type_edit->get_text(), Ref<Font>()); break;
  263. case 3: theme->set_color(name_edit->get_text(), type_edit->get_text(), Color()); break;
  264. case 4: theme->set_constant(name_edit->get_text(), type_edit->get_text(), 0); break;
  265. }
  266. } break;
  267. case POPUP_CLASS_ADD: {
  268. StringName fromtype = type_edit->get_text();
  269. List<StringName> names;
  270. {
  271. names.clear();
  272. Theme::get_default()->get_icon_list(fromtype, &names);
  273. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  274. theme->set_icon(E->get(), fromtype, Ref<Texture>());
  275. }
  276. }
  277. {
  278. names.clear();
  279. Theme::get_default()->get_stylebox_list(fromtype, &names);
  280. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  281. theme->set_stylebox(E->get(), fromtype, Ref<StyleBox>());
  282. }
  283. }
  284. {
  285. names.clear();
  286. Theme::get_default()->get_font_list(fromtype, &names);
  287. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  288. theme->set_font(E->get(), fromtype, Ref<Font>());
  289. }
  290. }
  291. {
  292. names.clear();
  293. Theme::get_default()->get_color_list(fromtype, &names);
  294. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  295. theme->set_color(E->get(), fromtype, Theme::get_default()->get_color(E->get(), fromtype));
  296. }
  297. }
  298. {
  299. names.clear();
  300. Theme::get_default()->get_constant_list(fromtype, &names);
  301. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  302. theme->set_constant(E->get(), fromtype, Theme::get_default()->get_constant(E->get(), fromtype));
  303. }
  304. }
  305. } break;
  306. case POPUP_REMOVE: {
  307. switch (type_select->get_selected()) {
  308. case 0: theme->clear_icon(name_edit->get_text(), type_edit->get_text()); break;
  309. case 1: theme->clear_stylebox(name_edit->get_text(), type_edit->get_text()); break;
  310. case 2: theme->clear_font(name_edit->get_text(), type_edit->get_text()); break;
  311. case 3: theme->clear_color(name_edit->get_text(), type_edit->get_text()); break;
  312. case 4: theme->clear_constant(name_edit->get_text(), type_edit->get_text()); break;
  313. }
  314. } break;
  315. case POPUP_CLASS_REMOVE: {
  316. StringName fromtype = type_edit->get_text();
  317. List<StringName> names;
  318. {
  319. names.clear();
  320. Theme::get_default()->get_icon_list(fromtype, &names);
  321. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  322. theme->clear_icon(E->get(), fromtype);
  323. }
  324. }
  325. {
  326. names.clear();
  327. Theme::get_default()->get_stylebox_list(fromtype, &names);
  328. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  329. theme->clear_stylebox(E->get(), fromtype);
  330. }
  331. }
  332. {
  333. names.clear();
  334. Theme::get_default()->get_font_list(fromtype, &names);
  335. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  336. theme->clear_font(E->get(), fromtype);
  337. }
  338. }
  339. {
  340. names.clear();
  341. Theme::get_default()->get_color_list(fromtype, &names);
  342. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  343. theme->clear_color(E->get(), fromtype);
  344. }
  345. }
  346. {
  347. names.clear();
  348. Theme::get_default()->get_constant_list(fromtype, &names);
  349. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  350. theme->clear_constant(E->get(), fromtype);
  351. }
  352. }
  353. } break;
  354. }
  355. }
  356. void ThemeEditor::_theme_menu_cbk(int p_option) {
  357. if (p_option == POPUP_CREATE_EMPTY || p_option == POPUP_CREATE_EDITOR_EMPTY || p_option == POPUP_IMPORT_EDITOR_THEME) {
  358. bool import = (p_option == POPUP_IMPORT_EDITOR_THEME);
  359. Ref<Theme> base_theme;
  360. if (p_option == POPUP_CREATE_EMPTY) {
  361. base_theme = Theme::get_default();
  362. } else {
  363. base_theme = EditorNode::get_singleton()->get_theme_base()->get_theme();
  364. }
  365. {
  366. List<StringName> types;
  367. base_theme->get_type_list(&types);
  368. for (List<StringName>::Element *T = types.front(); T; T = T->next()) {
  369. StringName type = T->get();
  370. List<StringName> icons;
  371. base_theme->get_icon_list(type, &icons);
  372. for (List<StringName>::Element *E = icons.front(); E; E = E->next()) {
  373. theme->set_icon(E->get(), type, import ? base_theme->get_icon(E->get(), type) : Ref<Texture>());
  374. }
  375. List<StringName> shaders;
  376. base_theme->get_shader_list(type, &shaders);
  377. for (List<StringName>::Element *E = shaders.front(); E; E = E->next()) {
  378. theme->set_shader(E->get(), type, import ? base_theme->get_shader(E->get(), type) : Ref<Shader>());
  379. }
  380. List<StringName> styleboxs;
  381. base_theme->get_stylebox_list(type, &styleboxs);
  382. for (List<StringName>::Element *E = styleboxs.front(); E; E = E->next()) {
  383. theme->set_stylebox(E->get(), type, import ? base_theme->get_stylebox(E->get(), type) : Ref<StyleBox>());
  384. }
  385. List<StringName> fonts;
  386. base_theme->get_font_list(type, &fonts);
  387. for (List<StringName>::Element *E = fonts.front(); E; E = E->next()) {
  388. theme->set_font(E->get(), type, Ref<Font>());
  389. }
  390. List<StringName> colors;
  391. base_theme->get_color_list(type, &colors);
  392. for (List<StringName>::Element *E = colors.front(); E; E = E->next()) {
  393. theme->set_color(E->get(), type, import ? base_theme->get_color(E->get(), type) : Color());
  394. }
  395. List<StringName> constants;
  396. base_theme->get_constant_list(type, &constants);
  397. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  398. theme->set_constant(E->get(), type, base_theme->get_constant(E->get(), type));
  399. }
  400. }
  401. }
  402. return;
  403. }
  404. Ref<Theme> base_theme;
  405. name_select_label->show();
  406. name_hbc->show();
  407. type_select_label->show();
  408. type_select->show();
  409. if (p_option == POPUP_ADD) { //add
  410. add_del_dialog->set_title(TTR("Add Item"));
  411. add_del_dialog->get_ok()->set_text(TTR("Add"));
  412. add_del_dialog->popup_centered(Size2(490, 85) * EDSCALE);
  413. base_theme = Theme::get_default();
  414. } else if (p_option == POPUP_CLASS_ADD) { //add
  415. add_del_dialog->set_title(TTR("Add All Items"));
  416. add_del_dialog->get_ok()->set_text(TTR("Add All"));
  417. add_del_dialog->popup_centered(Size2(240, 85) * EDSCALE);
  418. base_theme = Theme::get_default();
  419. name_select_label->hide();
  420. name_hbc->hide();
  421. type_select_label->hide();
  422. type_select->hide();
  423. } else if (p_option == POPUP_REMOVE) {
  424. add_del_dialog->set_title(TTR("Remove Item"));
  425. add_del_dialog->get_ok()->set_text(TTR("Remove"));
  426. add_del_dialog->popup_centered(Size2(490, 85) * EDSCALE);
  427. base_theme = theme;
  428. } else if (p_option == POPUP_CLASS_REMOVE) {
  429. add_del_dialog->set_title(TTR("Remove All Items"));
  430. add_del_dialog->get_ok()->set_text(TTR("Remove All"));
  431. add_del_dialog->popup_centered(Size2(240, 85) * EDSCALE);
  432. base_theme = Theme::get_default();
  433. name_select_label->hide();
  434. name_hbc->hide();
  435. type_select_label->hide();
  436. type_select->hide();
  437. }
  438. popup_mode = p_option;
  439. ERR_FAIL_COND(theme.is_null());
  440. List<StringName> types;
  441. base_theme->get_type_list(&types);
  442. type_menu->get_popup()->clear();
  443. if (p_option == 0 || p_option == 1) { //add
  444. List<StringName> new_types;
  445. theme->get_type_list(&new_types);
  446. //uh kind of sucks
  447. for (List<StringName>::Element *F = new_types.front(); F; F = F->next()) {
  448. bool found = false;
  449. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  450. if (E->get() == F->get()) {
  451. found = true;
  452. break;
  453. }
  454. }
  455. if (!found)
  456. types.push_back(F->get());
  457. }
  458. }
  459. //types.sort();
  460. types.sort_custom<StringName::AlphCompare>();
  461. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  462. type_menu->get_popup()->add_item(E->get());
  463. }
  464. }
  465. void ThemeEditor::_notification(int p_what) {
  466. if (p_what == NOTIFICATION_PROCESS) {
  467. time_left -= get_process_delta_time();
  468. if (time_left < 0) {
  469. time_left = 1.5;
  470. _refresh_interval();
  471. }
  472. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  473. theme_menu->set_icon(get_icon("Theme", "EditorIcons"));
  474. }
  475. }
  476. void ThemeEditor::_bind_methods() {
  477. ClassDB::bind_method("_type_menu_cbk", &ThemeEditor::_type_menu_cbk);
  478. ClassDB::bind_method("_name_menu_about_to_show", &ThemeEditor::_name_menu_about_to_show);
  479. ClassDB::bind_method("_name_menu_cbk", &ThemeEditor::_name_menu_cbk);
  480. ClassDB::bind_method("_theme_menu_cbk", &ThemeEditor::_theme_menu_cbk);
  481. ClassDB::bind_method("_dialog_cbk", &ThemeEditor::_dialog_cbk);
  482. ClassDB::bind_method("_save_template_cbk", &ThemeEditor::_save_template_cbk);
  483. }
  484. ThemeEditor::ThemeEditor() {
  485. time_left = 0;
  486. scroll = memnew(ScrollContainer);
  487. add_child(scroll);
  488. scroll->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 3);
  489. scroll->set_margin(MARGIN_TOP, 30 * EDSCALE);
  490. //scroll->set_enable_h_scroll(true);
  491. scroll->set_enable_v_scroll(true);
  492. scroll->set_enable_h_scroll(false);
  493. Panel *panel = memnew(Panel);
  494. scroll->add_child(panel);
  495. panel->set_custom_minimum_size(Size2(500, 800) * EDSCALE);
  496. panel->set_theme(Theme::get_default());
  497. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  498. main_vb = memnew(VBoxContainer);
  499. panel->add_child(main_vb);
  500. main_vb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 4 * EDSCALE);
  501. HBoxContainer *hb_menu = memnew(HBoxContainer);
  502. main_vb->add_child(hb_menu);
  503. theme_menu = memnew(MenuButton);
  504. theme_menu->set_text(TTR("Edit theme.."));
  505. theme_menu->set_flat(false);
  506. theme_menu->set_tooltip(TTR("Theme editing menu."));
  507. theme_menu->get_popup()->add_item(TTR("Add Item"), POPUP_ADD);
  508. theme_menu->get_popup()->add_item(TTR("Add Class Items"), POPUP_CLASS_ADD);
  509. theme_menu->get_popup()->add_item(TTR("Remove Item"), POPUP_REMOVE);
  510. theme_menu->get_popup()->add_item(TTR("Remove Class Items"), POPUP_CLASS_REMOVE);
  511. theme_menu->get_popup()->add_separator();
  512. theme_menu->get_popup()->add_item(TTR("Create Empty Template"), POPUP_CREATE_EMPTY);
  513. theme_menu->get_popup()->add_item(TTR("Create Empty Editor Template"), POPUP_CREATE_EDITOR_EMPTY);
  514. theme_menu->get_popup()->add_item(TTR("Create From Current Editor Theme"), POPUP_IMPORT_EDITOR_THEME);
  515. add_child(theme_menu);
  516. theme_menu->set_position(Vector2(3, 3) * EDSCALE);
  517. theme_menu->get_popup()->connect("id_pressed", this, "_theme_menu_cbk");
  518. HBoxContainer *main_hb = memnew(HBoxContainer);
  519. main_vb->add_child(main_hb);
  520. VBoxContainer *first_vb = memnew(VBoxContainer);
  521. first_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  522. main_hb->add_child(first_vb);
  523. //main_panel->add_child(panel);
  524. //panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  525. //panel->set_margin( MARGIN_TOP,20 );
  526. first_vb->add_child(memnew(Label("Label")));
  527. first_vb->add_child(memnew(Button("Button")));
  528. ToolButton *tb = memnew(ToolButton);
  529. tb->set_text("ToolButton");
  530. first_vb->add_child(tb);
  531. CheckButton *cb = memnew(CheckButton);
  532. cb->set_text("CheckButton");
  533. first_vb->add_child(cb);
  534. CheckBox *cbx = memnew(CheckBox);
  535. cbx->set_text("CheckBox");
  536. first_vb->add_child(cbx);
  537. VBoxContainer *bg = memnew(VBoxContainer);
  538. bg->set_v_size_flags(SIZE_EXPAND_FILL);
  539. VBoxContainer *gbvb = memnew(VBoxContainer);
  540. gbvb->set_v_size_flags(SIZE_EXPAND_FILL);
  541. CheckBox *rbx1 = memnew(CheckBox);
  542. rbx1->set_text(TTR("CheckBox Radio1"));
  543. rbx1->set_pressed(true);
  544. gbvb->add_child(rbx1);
  545. CheckBox *rbx2 = memnew(CheckBox);
  546. rbx2->set_text(TTR("CheckBox Radio2"));
  547. gbvb->add_child(rbx2);
  548. bg->add_child(gbvb);
  549. first_vb->add_child(bg);
  550. MenuButton *test_menu_button = memnew(MenuButton);
  551. test_menu_button->set_text("MenuButton");
  552. test_menu_button->get_popup()->add_item(TTR("Item"));
  553. test_menu_button->get_popup()->add_separator();
  554. test_menu_button->get_popup()->add_check_item(TTR("Check Item"));
  555. test_menu_button->get_popup()->add_check_item(TTR("Checked Item"));
  556. test_menu_button->get_popup()->set_item_checked(2, true);
  557. first_vb->add_child(test_menu_button);
  558. OptionButton *test_option_button = memnew(OptionButton);
  559. test_option_button->add_item("OptionButton");
  560. test_option_button->add_separator();
  561. test_option_button->add_item(TTR("Has"));
  562. test_option_button->add_item(TTR("Many"));
  563. test_option_button->add_item(TTR("Options"));
  564. first_vb->add_child(test_option_button);
  565. ColorPickerButton *cpb = memnew(ColorPickerButton);
  566. first_vb->add_child(cpb);
  567. first_vb->add_child(memnew(HSeparator));
  568. first_vb->add_child(memnew(HSlider));
  569. first_vb->add_child(memnew(HScrollBar));
  570. first_vb->add_child(memnew(SpinBox));
  571. ProgressBar *pb = memnew(ProgressBar);
  572. pb->set_value(50);
  573. first_vb->add_child(pb);
  574. Panel *pn = memnew(Panel);
  575. pn->set_custom_minimum_size(Size2(40, 40) * EDSCALE);
  576. first_vb->add_child(pn);
  577. first_vb->add_constant_override("separation", 10 * EDSCALE);
  578. VBoxContainer *second_vb = memnew(VBoxContainer);
  579. second_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  580. main_hb->add_child(second_vb);
  581. second_vb->add_constant_override("separation", 10 * EDSCALE);
  582. LineEdit *le = memnew(LineEdit);
  583. le->set_text("LineEdit");
  584. second_vb->add_child(le);
  585. TextEdit *te = memnew(TextEdit);
  586. te->set_text("TextEdit");
  587. //te->set_v_size_flags(SIZE_EXPAND_FILL);
  588. te->set_custom_minimum_size(Size2(0, 160) * EDSCALE);
  589. second_vb->add_child(te);
  590. Tree *test_tree = memnew(Tree);
  591. second_vb->add_child(test_tree);
  592. test_tree->set_custom_minimum_size(Size2(0, 160) * EDSCALE);
  593. TreeItem *item = test_tree->create_item();
  594. item->set_editable(0, true);
  595. item->set_text(0, "Tree");
  596. item = test_tree->create_item(test_tree->get_root());
  597. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  598. item->set_editable(0, true);
  599. item->set_text(0, "check");
  600. item = test_tree->create_item(test_tree->get_root());
  601. item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE);
  602. item->set_editable(0, true);
  603. item->set_range_config(0, 0, 20, 0.1);
  604. item->set_range(0, 2);
  605. item = test_tree->create_item(test_tree->get_root());
  606. item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE);
  607. item->set_editable(0, true);
  608. item->set_text(0, TTR("Have,Many,Several,Options!"));
  609. item->set_range(0, 2);
  610. VBoxContainer *third_vb = memnew(VBoxContainer);
  611. third_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  612. third_vb->add_constant_override("separation", 10);
  613. main_hb->add_child(third_vb);
  614. HBoxContainer *vhb = memnew(HBoxContainer);
  615. vhb->set_custom_minimum_size(Size2(0, 160) * EDSCALE);
  616. vhb->add_child(memnew(VSeparator));
  617. vhb->add_child(memnew(VSlider));
  618. vhb->add_child(memnew(VScrollBar));
  619. third_vb->add_child(vhb);
  620. TabContainer *tc = memnew(TabContainer);
  621. third_vb->add_child(tc);
  622. tc->set_custom_minimum_size(Size2(0, 160) * EDSCALE);
  623. Control *tcc = memnew(Control);
  624. tcc->set_name(TTR("Tab 1"));
  625. tc->add_child(tcc);
  626. tcc = memnew(Control);
  627. tcc->set_name(TTR("Tab 2"));
  628. tc->add_child(tcc);
  629. tcc = memnew(Control);
  630. tcc->set_name(TTR("Tab 3"));
  631. tc->add_child(tcc);
  632. main_hb->add_constant_override("separation", 20 * EDSCALE);
  633. /*
  634. test_h_scroll = memnew( HScrollBar );
  635. test_h_scroll->set_position( Point2( 25, 225 ) );
  636. test_h_scroll->set_size( Point2( 150, 5 ) );
  637. panel->add_child(test_h_scroll);
  638. line_edit = memnew( LineEdit );
  639. line_edit->set_position( Point2( 25, 275 ) );
  640. line_edit->set_size( Point2( 150, 5 ) );
  641. line_edit->set_text("Line Edit");
  642. panel->add_child(line_edit);
  643. test_v_scroll = memnew( VScrollBar );
  644. test_v_scroll->set_position( Point2( 200, 25 ) );
  645. test_v_scroll->set_size( Point2( 5, 150 ) );
  646. panel->add_child(test_v_scroll);
  647. test_tree = memnew(Tree);
  648. test_tree->set_position( Point2( 300, 25 ) );
  649. test_tree->set_size( Point2( 200, 200 ) );
  650. panel->add_child(test_tree);
  651. TreeItem *item = test_tree->create_item();
  652. item->set_editable(0,true);
  653. item->set_text(0,"root");
  654. item = test_tree->create_item( test_tree->get_root() );
  655. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  656. item->set_editable(0,true);
  657. item->set_text(0,"check");
  658. item = test_tree->create_item( test_tree->get_root() );
  659. item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE);
  660. item->set_editable(0,true);
  661. item->set_range_config(0,0,20,0.1);
  662. item->set_range(0,2);
  663. item = test_tree->create_item( test_tree->get_root() );
  664. item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE);
  665. item->set_editable(0,true);
  666. item->set_text(0,"Have,Many,Several,Options!"));
  667. item->set_range(0,2);
  668. Button *fd_button= memnew( Button );
  669. fd_button->set_position(Point2(300,275));
  670. fd_button->set_text("Open File Dialog");
  671. panel->add_child(fd_button);
  672. test_file_dialog = memnew( EditorFileDialog );
  673. panel->add_child(test_file_dialog);
  674. fd_button->connect("pressed", this,"_open_file_dialog");
  675. */
  676. add_del_dialog = memnew(ConfirmationDialog);
  677. add_del_dialog->hide();
  678. add_child(add_del_dialog);
  679. VBoxContainer *dialog_vbc = memnew(VBoxContainer);
  680. add_del_dialog->add_child(dialog_vbc);
  681. Label *l = memnew(Label);
  682. l->set_text(TTR("Type:"));
  683. dialog_vbc->add_child(l);
  684. type_hbc = memnew(HBoxContainer);
  685. dialog_vbc->add_child(type_hbc);
  686. type_edit = memnew(LineEdit);
  687. type_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  688. type_hbc->add_child(type_edit);
  689. type_menu = memnew(MenuButton);
  690. type_menu->set_text("..");
  691. type_hbc->add_child(type_menu);
  692. type_menu->get_popup()->connect("id_pressed", this, "_type_menu_cbk");
  693. l = memnew(Label);
  694. l->set_text(TTR("Name:"));
  695. dialog_vbc->add_child(l);
  696. name_select_label = l;
  697. name_hbc = memnew(HBoxContainer);
  698. dialog_vbc->add_child(name_hbc);
  699. name_edit = memnew(LineEdit);
  700. name_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  701. name_hbc->add_child(name_edit);
  702. name_menu = memnew(MenuButton);
  703. name_menu->set_text("..");
  704. name_hbc->add_child(name_menu);
  705. name_menu->get_popup()->connect("about_to_show", this, "_name_menu_about_to_show");
  706. name_menu->get_popup()->connect("id_pressed", this, "_name_menu_cbk");
  707. type_select_label = memnew(Label);
  708. type_select_label->set_text(TTR("Data Type:"));
  709. dialog_vbc->add_child(type_select_label);
  710. type_select = memnew(OptionButton);
  711. type_select->add_item(TTR("Icon"));
  712. type_select->add_item(TTR("Style"));
  713. type_select->add_item(TTR("Font"));
  714. type_select->add_item(TTR("Color"));
  715. type_select->add_item(TTR("Constant"));
  716. dialog_vbc->add_child(type_select);
  717. add_del_dialog->get_ok()->connect("pressed", this, "_dialog_cbk");
  718. file_dialog = memnew(EditorFileDialog);
  719. file_dialog->add_filter("*.theme ; Theme File");
  720. add_child(file_dialog);
  721. file_dialog->connect("file_selected", this, "_save_template_cbk");
  722. //MenuButton *name_menu;
  723. //LineEdit *name_edit;
  724. }
  725. void ThemeEditorPlugin::edit(Object *p_node) {
  726. if (Object::cast_to<Theme>(p_node)) {
  727. theme_editor->show();
  728. theme_editor->edit(Object::cast_to<Theme>(p_node));
  729. } else {
  730. theme_editor->edit(Ref<Theme>());
  731. theme_editor->hide();
  732. }
  733. }
  734. bool ThemeEditorPlugin::handles(Object *p_node) const {
  735. return p_node->is_class("Theme");
  736. }
  737. void ThemeEditorPlugin::make_visible(bool p_visible) {
  738. if (p_visible) {
  739. theme_editor->set_process(true);
  740. button->show();
  741. editor->make_bottom_panel_item_visible(theme_editor);
  742. } else {
  743. theme_editor->set_process(false);
  744. if (theme_editor->is_visible_in_tree())
  745. editor->hide_bottom_panel();
  746. button->hide();
  747. }
  748. }
  749. ThemeEditorPlugin::ThemeEditorPlugin(EditorNode *p_node) {
  750. editor = p_node;
  751. theme_editor = memnew(ThemeEditor);
  752. theme_editor->set_custom_minimum_size(Size2(0, 200));
  753. //p_node->get_viewport()->add_child(theme_editor);
  754. button = editor->add_bottom_panel_item("Theme", theme_editor);
  755. button->hide();
  756. }