fork(1) download
  1. #include <irrlicht.h>
  2. #include <iostream>
  3.  
  4. using namespace irr;
  5. using namespace core;
  6. using namespace scene;
  7. using namespace video;
  8. using namespace io;
  9. using namespace gui;
  10.  
  11. #ifdef _IRR_WINDOWS_
  12. #pragma comment(lib, "Irrlicht.lib")
  13. #endif
  14.  
  15. enum
  16. {
  17. GUI_ID_CHECKBOX_VISIBLE_PARENT,
  18. GUI_ID_CHECKBOX_VISIBLE_ELEMENTS,
  19. GUI_ID_CHECKBOX_ENABLED_PARENT,
  20. GUI_ID_CHECKBOX_ENABLED_ELEMENTS,
  21. GUI_ID_TRANSPARENCY_SCROLL_BAR,
  22. GUI_ID_BUTTON_CLEAR,
  23. GUI_ID_BUTTON_SAVE,
  24. GUI_ID_BUTTON_LOAD,
  25. GUI_ID_BUTTON_CREATE_ELEMENTS
  26. };
  27.  
  28.  
  29. struct SAppContext
  30. {
  31. IrrlichtDevice * device;
  32. IGUIElement* mGuiParent;
  33. array<IGUIElement*> mGuiElements;
  34. };
  35.  
  36.  
  37. void ClearAllTestGuiElements(SAppContext & context)
  38. {
  39. const core::list<IGUIElement*>& children = context.mGuiParent->getChildren();
  40. while (!children.empty())
  41. (*children.getLast())->remove();
  42. context.mGuiElements.clear();
  43. }
  44.  
  45. void SetTabStopsForAllElements(SAppContext & context)
  46. {
  47. for ( u32 i=0; i < context.mGuiElements.size(); ++i )
  48. {
  49. context.mGuiElements[i]->setTabStop(true);
  50. context.mGuiElements[i]->setTabOrder((s32)i);
  51. }
  52. }
  53.  
  54. void AddTestGuiElements(IGUIEnvironment* env, IGUIElement * parent, SAppContext & context)
  55. {
  56. context.mGuiElements.push_back( env->addToolBar (parent, /*s32 id=*/-1) );
  57.  
  58. s32 top = 40;
  59. s32 default_height = 15;
  60. s32 default_width = 150;
  61. s32 default_gap = 20;
  62. core::rect<s32> rect(10, top, 10 + default_width, top + default_height);
  63.  
  64. IGUIButton * button = env->addButton (rect, parent, /*s32 id=*/-1, /*const wchar_t *text=*/L"button", /*const wchar_t *tooltiptext=*/L"tooltip");
  65. context.mGuiElements.push_back(button);
  66.  
  67. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  68. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height;
  69. IGUICheckBox * checkbox = env->addCheckBox (/*bool checked*/true, rect, parent, /*s32 id=*/-1, /*const wchar_t *text=*/L"checkbox");
  70. checkbox->setToolTipText(L"tooltip");
  71. context.mGuiElements.push_back(checkbox);
  72.  
  73. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  74. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height;
  75. IGUIComboBox * combo = env->addComboBox (rect, parent, /*s32 id=*/-1);
  76. for ( int i = 0; i < 100; ++i)
  77. {
  78. core::stringw wstr(i);
  79. wstr += L"Alpha";
  80. combo->addItem(wstr.c_str());
  81. wstr += L"-Beta";
  82. combo->addItem(wstr.c_str());
  83. }
  84. combo->setToolTipText(L"tooltip");
  85. context.mGuiElements.push_back( combo );
  86.  
  87. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  88. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 2* default_height;
  89. IGUIEditBox * multiLineEditBox = env->addEditBox (/*const wchar_t *text*/L"editbox", rect, /*bool border=*/true, parent, /*s32 id=*/-1);
  90. multiLineEditBox->setMultiLine(true);
  91. multiLineEditBox->setWordWrap(true);
  92. multiLineEditBox->setToolTipText(L"tooltip");
  93. context.mGuiElements.push_back( multiLineEditBox );
  94.  
  95. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  96. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height;
  97. IGUIEditBox * editboxnotext = env->addEditBox (/*const wchar_t *text*/L"", rect, /*bool border=*/true, parent, /*s32 id=*/-1);
  98. editboxnotext->setToolTipText(L"tooltip");
  99. context.mGuiElements.push_back(editboxnotext);
  100.  
  101. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  102. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height*3;
  103. IGUIListBox * lb = env->addListBox (rect, parent, /*s32 id=*/-1, /*bool drawBackground=*/true);
  104. for ( int i = 0; i < 100; ++i)
  105. {
  106. core::stringw wstr(i);
  107. wstr += L"Alpha";
  108. lb->addItem(wstr.c_str());
  109. wstr += L"-Beta";
  110. lb->addItem(wstr.c_str());
  111. }
  112. lb->setToolTipText(L"tooltip");
  113. context.mGuiElements.push_back( lb );
  114.  
  115. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  116. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height;
  117. IGUIScrollBar * scrollbar = env->addScrollBar (/*bool horizontal*/true, rect, parent, /*s32 id=*/-1);
  118. scrollbar->setToolTipText(L"tooltip");
  119. context.mGuiElements.push_back(scrollbar);
  120.  
  121. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  122. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height;
  123. IGUISpinBox * spinbox = env->addSpinBox (/*const wchar_t *text*/L"0", rect, /*bool border=*/true, parent, /*s32 id=*/-1);
  124. spinbox->setToolTipText(L"tooltip");
  125. context.mGuiElements.push_back(spinbox);
  126.  
  127. rect.UpperLeftCorner.X = 10 + default_width + default_gap;
  128. rect.LowerRightCorner.X = rect.UpperLeftCorner.X + default_width;
  129. rect.UpperLeftCorner.Y = top;
  130. rect.LowerRightCorner.Y = top + default_height;
  131. IGUIStaticText * statictext = env->addStaticText (/*const wchar_t *text*/L"static", rect, /*bool border=*/false, /*bool wordWrap=*/true, parent, /*s32 id=*/-1,/* bool fillBackground=*/true);
  132. statictext->setToolTipText(L"tooltip");
  133. context.mGuiElements.push_back(statictext);
  134.  
  135. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  136. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height;
  137. IGUIStaticText * statictextnotext = env->addStaticText (/*const wchar_t *text*/L"", rect, /*bool border=*/true, /*bool wordWrap=*/true, parent, /*s32 id=*/-1,/* bool fillBackground=*/true);
  138. statictextnotext->setToolTipText(L"tooltip");
  139. context.mGuiElements.push_back(statictextnotext); // no text
  140.  
  141. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  142. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 4*default_height;
  143. IGUITabControl * tabctrl = env->addTabControl (rect, parent, /*bool fillbackground=*/true, /*bool border=*/true, /*s32 id=*/-1);
  144. tabctrl->addTab(/*const wchar_t *caption*/L"tab1", /*s32 id=*/-1);
  145. tabctrl->addTab(/*const wchar_t *caption*/L"tab2", /*s32 id=*/-1);
  146. tabctrl->addTab(/*const wchar_t *caption*/L"tab3", /*s32 id=*/-1);
  147. tabctrl->addTab(/*const wchar_t *caption*/L"tab4", /*s32 id=*/-1);
  148. tabctrl->addTab(/*const wchar_t *caption*/L"tab5", /*s32 id=*/-1);
  149. tabctrl->setToolTipText(L"tooltip");
  150. context.mGuiElements.push_back( tabctrl );
  151.  
  152. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  153. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + default_height*3;
  154. IGUITable * table = env->addTable (rect, parent, /*s32 id=*/-1, /*bool drawBackground=*/true);
  155. table->addColumn(L"col1");
  156. table->addColumn(L"col2");
  157. table->addRow(0);
  158. table->addRow(1);
  159. table->setCellText(/*u32 rowIndex*/1, /*u32 columnIndex*/1, /*const wchar_t *text*/L"text");
  160. table->setToolTipText(L"tooltip");
  161. context.mGuiElements.push_back(table );
  162.  
  163. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  164. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 4*default_height;
  165. IGUIWindow * window = env->addWindow (rect, /*bool modal=*/false, /*const wchar_t *text=*/L"window long titletext for colortesting", parent, /*s32 id=*/-1);
  166. window->setToolTipText(L"tooltip");
  167. context.mGuiElements.push_back(window);
  168.  
  169. rect.UpperLeftCorner.Y = rect.LowerRightCorner.Y + default_gap;
  170. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + 4*default_height;
  171. bool treeViewBackground = false;
  172. bool treeScrollBarVertical=true;
  173. bool treeScrollBarHorizontal=true;
  174. IGUITreeView * tree = env->addTreeView(rect, parent, -1, treeViewBackground, treeScrollBarVertical, treeScrollBarHorizontal);
  175. IGUITreeViewNode * treeNode = tree->getRoot();
  176. for ( int i=0; i < 10; ++i )
  177. treeNode->addChildBack(L"child");
  178. for ( int i=0; i < 10; ++i )
  179. {
  180. treeNode = treeNode->addChildBack(L"branch");
  181. treeNode->setExpanded(true);
  182. }
  183. context.mGuiElements.push_back(tree);
  184.  
  185. IGUIContextMenu * contextMenu = env->addMenu(parent);
  186. contextMenu->addItem(L"File", -1, false, true);
  187. contextMenu->getSubMenu (0)->addItem(L"One", -1, false, true);
  188. contextMenu->getSubMenu (0)->addItem(L"Two", -1, true, true);
  189. contextMenu->addItem(L"Options", -1, true, true);
  190. contextMenu->getSubMenu (1)->addItem(L"One", -1, false, true);
  191. contextMenu->getSubMenu (1)->addItem(L"Two", -1, true, true);
  192. contextMenu->addItem(L"Help", -1, true, true);
  193. contextMenu->getSubMenu (2)->addItem(L"One", -1, false, true);
  194. contextMenu->getSubMenu (2)->addItem(L"Two", -1, true, true);
  195. context.mGuiElements.push_back(contextMenu);
  196. }
  197.  
  198. void AddControlElements(IGUIEnvironment* env, IGUIElement * parent)
  199. {
  200. env->addStaticText (L"", rect<s32>(500, 50, 630, 480), /*bool border=*/true, /*bool wordWrap=*/false, parent, -1,/* bool fillBackground=*/false);
  201. env->addCheckBox (true, rect<s32>(510, 60, 620, 80), parent, GUI_ID_CHECKBOX_VISIBLE_PARENT, L"parent visible");
  202. env->addCheckBox (true, rect<s32>(510, 100, 620, 120), parent, GUI_ID_CHECKBOX_ENABLED_PARENT, L"parent enabled");
  203. env->addCheckBox (true, rect<s32>(510, 140, 620, 160), parent, GUI_ID_CHECKBOX_VISIBLE_ELEMENTS, L"elements visible");
  204. env->addCheckBox (true, rect<s32>(510, 180, 620, 200), parent, GUI_ID_CHECKBOX_ENABLED_ELEMENTS, L"elements enabled");
  205.  
  206. env->addStaticText(L"Transparent Control:", rect<s32>(510,220,620,240), true);
  207. IGUIScrollBar* scrollbar = env->addScrollBar(true, rect<s32>(510, 250, 620, 270), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
  208. scrollbar->setMax(255);
  209. scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha()); // set scrollbar position to alpha value of an arbitrary element
  210.  
  211. env->addButton(rect<s32>(510, 280, 620, 300), parent, GUI_ID_BUTTON_CLEAR, L"clear", L"remove gui elements");
  212. env->addButton(rect<s32>(510, 310, 620, 330), parent, GUI_ID_BUTTON_SAVE, L"save", L"save gui");
  213. env->addButton(rect<s32>(510, 340, 620, 360), parent, GUI_ID_BUTTON_LOAD, L"load", L"clear & load gui");
  214. env->addButton(rect<s32>(510, 370, 620, 390), parent, GUI_ID_BUTTON_CREATE_ELEMENTS, L"create elements", L"clear & create new");
  215. }
  216.  
  217. class MyEventReceiver : public IEventReceiver
  218. {
  219. public:
  220. MyEventReceiver(SAppContext & context) : Context(context) { }
  221.  
  222. virtual bool OnEvent(const SEvent& event)
  223. {
  224. if (event.EventType == EET_GUI_EVENT)
  225. {
  226. s32 id = event.GUIEvent.Caller->getID();
  227. switch(event.GUIEvent.EventType)
  228. {
  229. case EGET_BUTTON_CLICKED:
  230. {
  231. switch ( id )
  232. {
  233. case GUI_ID_BUTTON_CLEAR:
  234. ClearAllTestGuiElements(Context);
  235. break;
  236. case GUI_ID_BUTTON_SAVE:
  237. Context.device->getGUIEnvironment()->saveGUI("gui_all_elements.xml", Context.mGuiParent);
  238. break;
  239. case GUI_ID_BUTTON_LOAD:
  240. ClearAllTestGuiElements(Context);
  241. Context.device->getGUIEnvironment()->loadGUI("gui_all_elements.xml", Context.mGuiParent);
  242. break;
  243. case GUI_ID_BUTTON_CREATE_ELEMENTS:
  244. ClearAllTestGuiElements(Context);
  245. AddTestGuiElements(Context.device->getGUIEnvironment(), Context.mGuiParent, Context);
  246. break;
  247. }
  248. }
  249. break;
  250. case EGET_CHECKBOX_CHANGED:
  251. {
  252. IGUICheckBox *cb = static_cast<IGUICheckBox *>(event.GUIEvent.Caller);
  253. switch ( id )
  254. {
  255. case GUI_ID_CHECKBOX_VISIBLE_PARENT:
  256. Context.mGuiParent->setVisible( cb->isChecked() );
  257. break;
  258. case GUI_ID_CHECKBOX_VISIBLE_ELEMENTS:
  259. for ( u32 i=0; i < Context.mGuiElements.size(); ++i )
  260. {
  261. Context.mGuiElements[i]->setVisible( cb->isChecked() );
  262. }
  263. break;
  264. case GUI_ID_CHECKBOX_ENABLED_PARENT:
  265. Context.mGuiParent->setEnabled( cb->isChecked() );
  266. break;
  267. case GUI_ID_CHECKBOX_ENABLED_ELEMENTS:
  268. for ( u32 i=0; i < Context.mGuiElements.size(); ++i )
  269. {
  270. Context.mGuiElements[i]->setEnabled( cb->isChecked() );
  271. }
  272. break;
  273. default:
  274. break;
  275. }
  276. break;
  277. }
  278. case EGET_SCROLL_BAR_CHANGED:
  279. {
  280. if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
  281. {
  282. s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
  283.  
  284. for (u32 i=0; i<EGDC_COUNT ; ++i)
  285. {
  286. IGUIEnvironment * env = Context.device->getGUIEnvironment();
  287. SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
  288. col.setAlpha(pos);
  289. env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
  290. }
  291.  
  292. }
  293. break;
  294. }
  295. default:
  296. break;
  297. }
  298. }
  299.  
  300. return false;
  301. }
  302.  
  303. private:
  304. SAppContext & Context;
  305. };
  306.  
  307. int main()
  308. {
  309. video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
  310. IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
  311. if (device == 0)
  312. return 1; // could not create selected driver.
  313.  
  314. video::IVideoDriver* driver = device->getVideoDriver();
  315. IGUIEnvironment* env = device->getGUIEnvironment();
  316.  
  317. SAppContext context;
  318. context.device = device;
  319.  
  320. // context.mGuiParent = env->addStaticText (L"", core::rect<s32>(10, 10, 500, 450), true, false, 0, -1,false);
  321. context.mGuiParent = env->addWindow(irr::core::recti(0,0,500,450), false, L"sometext");
  322.  
  323. AddTestGuiElements(env, context.mGuiParent, context);
  324. SetTabStopsForAllElements(context);
  325.  
  326. MyEventReceiver receiver(context);
  327. device->setEventReceiver(&receiver);
  328.  
  329. AddControlElements(env, 0);
  330.  
  331. while(device->run() && driver)
  332. {
  333. if (device->isWindowActive())
  334. {
  335. driver->beginScene(true, true, SColor(0,200,200,200));
  336.  
  337. env->drawAll();
  338.  
  339. driver->endScene();
  340. }
  341. }
  342.  
  343. device->drop();
  344.  
  345. return 0;
  346. }
  347.  
  348.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty