test_gui.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*************************************************************************/
  2. /* test_gui.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. #ifndef _3D_DISABLED
  31. #include "test_gui.h"
  32. #include "core/io/image_loader.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "scene/2d/sprite.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/gui/line_edit.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/option_button.h"
  42. #include "scene/gui/panel.h"
  43. #include "scene/gui/popup_menu.h"
  44. #include "scene/gui/progress_bar.h"
  45. #include "scene/gui/rich_text_label.h"
  46. #include "scene/gui/scroll_bar.h"
  47. #include "scene/gui/spin_box.h"
  48. #include "scene/gui/tab_container.h"
  49. #include "scene/gui/texture_rect.h"
  50. #include "scene/gui/tree.h"
  51. #include "scene/main/scene_tree.h"
  52. #include "scene/3d/camera.h"
  53. #include "scene/main/viewport.h"
  54. namespace TestGUI {
  55. class TestMainLoop : public SceneTree {
  56. public:
  57. virtual void request_quit() {
  58. quit();
  59. }
  60. virtual void init() {
  61. SceneTree::init();
  62. Panel *frame = memnew(Panel);
  63. frame->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
  64. frame->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_END);
  65. frame->set_end(Point2(0, 0));
  66. Ref<Theme> t = memnew(Theme);
  67. frame->set_theme(t);
  68. get_root()->add_child(frame);
  69. Label *label = memnew(Label);
  70. label->set_position(Point2(80, 90));
  71. label->set_size(Point2(170, 80));
  72. label->set_align(Label::ALIGN_FILL);
  73. label->set_text("There was once upon a time a beautiful unicorn that loved to play with little girls...");
  74. frame->add_child(label);
  75. Button *button = memnew(Button);
  76. button->set_position(Point2(20, 20));
  77. button->set_size(Point2(1, 1));
  78. button->set_text("This is a biggie button");
  79. frame->add_child(button);
  80. Tree *tree = memnew(Tree);
  81. tree->set_columns(2);
  82. tree->set_position(Point2(230, 210));
  83. tree->set_size(Point2(150, 250));
  84. TreeItem *item = tree->create_item();
  85. item->set_editable(0, true);
  86. item->set_text(0, "root");
  87. item = tree->create_item(tree->get_root());
  88. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  89. item->set_editable(0, true);
  90. item->set_text(0, "check");
  91. item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);
  92. item->set_editable(1, true);
  93. item->set_text(1, "check2");
  94. item = tree->create_item(tree->get_root());
  95. item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE);
  96. item->set_editable(0, true);
  97. item->set_range_config(0, 0, 20, 0.1);
  98. item->set_range(0, 2);
  99. item->add_button(0, Theme::get_default()->get_icon("folder", "FileDialog"));
  100. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  101. item->set_editable(1, true);
  102. item->set_range_config(1, 0, 20, 0.1);
  103. item->set_range(1, 3);
  104. item = tree->create_item(tree->get_root());
  105. item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE);
  106. item->set_editable(0, true);
  107. item->set_text(0, "Have,Many,Several,Options!");
  108. item->set_range(0, 2);
  109. item = tree->create_item(item);
  110. item->set_editable(0, true);
  111. item->set_text(0, "Gershwin!");
  112. frame->add_child(tree);
  113. LineEdit *line_edit = memnew(LineEdit);
  114. line_edit->set_position(Point2(30, 190));
  115. line_edit->set_size(Point2(180, 1));
  116. frame->add_child(line_edit);
  117. HScrollBar *hscroll = memnew(HScrollBar);
  118. hscroll->set_position(Point2(30, 290));
  119. hscroll->set_size(Point2(180, 1));
  120. hscroll->set_max(10);
  121. hscroll->set_page(4);
  122. frame->add_child(hscroll);
  123. SpinBox *spin = memnew(SpinBox);
  124. spin->set_position(Point2(30, 260));
  125. spin->set_size(Point2(120, 1));
  126. frame->add_child(spin);
  127. hscroll->share(spin);
  128. ProgressBar *progress = memnew(ProgressBar);
  129. progress->set_position(Point2(30, 330));
  130. progress->set_size(Point2(120, 1));
  131. frame->add_child(progress);
  132. hscroll->share(progress);
  133. MenuButton *menu_button = memnew(MenuButton);
  134. menu_button->set_text("I'm a menu!");
  135. menu_button->set_position(Point2(30, 380));
  136. menu_button->set_size(Point2(1, 1));
  137. frame->add_child(menu_button);
  138. PopupMenu *popup = menu_button->get_popup();
  139. popup->add_item("Hello, testing");
  140. popup->add_item("My Dearest");
  141. popup->add_separator();
  142. popup->add_item("Popup");
  143. popup->add_check_item("Check Popup");
  144. popup->set_item_checked(4, true);
  145. popup->add_separator();
  146. popup->add_radio_check_item("Option A");
  147. popup->set_item_checked(6, true);
  148. popup->add_radio_check_item("Option B");
  149. OptionButton *options = memnew(OptionButton);
  150. options->add_item("Hello, testing");
  151. options->add_item("My Dearest");
  152. options->set_position(Point2(230, 180));
  153. options->set_size(Point2(1, 1));
  154. frame->add_child(options);
  155. RichTextLabel *richtext = memnew(RichTextLabel);
  156. richtext->set_position(Point2(600, 210));
  157. richtext->set_size(Point2(180, 250));
  158. richtext->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -20);
  159. frame->add_child(richtext);
  160. richtext->add_text("Hello, My Friends!\n\nWelcome to the amazing world of ");
  161. richtext->add_newline();
  162. richtext->add_newline();
  163. richtext->push_color(Color(1, 0.5, 0.5));
  164. richtext->add_text("leprechauns");
  165. richtext->pop();
  166. richtext->add_text(" and ");
  167. richtext->push_color(Color(0, 1.0, 0.5));
  168. richtext->add_text("faeries.\n");
  169. richtext->pop();
  170. richtext->add_text("In this new episode, we will attempt to ");
  171. richtext->push_font(richtext->get_font("mono_font", "Fonts"));
  172. richtext->push_color(Color(0.7, 0.5, 1.0));
  173. richtext->add_text("deliver something nice");
  174. richtext->pop();
  175. richtext->pop();
  176. richtext->add_text(" to all the viewers! Unfortunately, I need to ");
  177. richtext->push_underline();
  178. richtext->add_text("keep writing a lot of text");
  179. richtext->pop();
  180. richtext->add_text(" so the label control overflows and the scrollbar appears.\n");
  181. richtext->push_meta("http://www.scrollingcapabilities.xz");
  182. richtext->add_text("This allows to test for the scrolling capabilities ");
  183. richtext->pop();
  184. richtext->add_text("of the rich text label for huge text (not like this text will really be huge but, you know).\nAs long as it is so long that it will work nicely for a test/demo, then it's welcomed in my book...\nChanging subject, the day is cloudy today and I'm wondering if I'll get che chance to travel somewhere nice. Sometimes, watching the clouds from satellite images may give a nice insight about how pressure zones in our planet work, although it also makes it pretty obvious to see why most weather forecasts get it wrong so often.\nClouds are so difficult to predict!\nBut it's pretty cool how our civilization has adapted to having water falling from the sky each time it rains...");
  185. TabContainer *tabc = memnew(TabContainer);
  186. Control *ctl = memnew(Control);
  187. ctl->set_name("tab 1");
  188. tabc->add_child(ctl);
  189. ctl = memnew(Control);
  190. ctl->set_name("tab 2");
  191. tabc->add_child(ctl);
  192. label = memnew(Label);
  193. label->set_text("Some Label");
  194. label->set_position(Point2(20, 20));
  195. ctl->add_child(label);
  196. ctl = memnew(Control);
  197. ctl->set_name("tab 3");
  198. button = memnew(Button);
  199. button->set_text("Some Button");
  200. button->set_position(Point2(30, 50));
  201. ctl->add_child(button);
  202. tabc->add_child(ctl);
  203. frame->add_child(tabc);
  204. tabc->set_position(Point2(400, 210));
  205. tabc->set_size(Point2(180, 250));
  206. }
  207. };
  208. MainLoop *test() {
  209. return memnew(TestMainLoop);
  210. }
  211. } // namespace TestGUI
  212. #endif