gwenUserInterface.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. #include "gwenUserInterface.h"
  2. #include "gwenInternalData.h"
  3. #include "Gwen/Controls/ImagePanel.h"
  4. #include "Gwen/Controls/ColorPicker.h"
  5. //#include "Gwen/Controls/HSVColorPicker.h"
  6. class MyGraphWindow* graphWindow = 0;
  7. GwenUserInterface::GwenUserInterface()
  8. {
  9. m_data = new GwenInternalData();
  10. m_data->m_toggleButtonCallback = 0;
  11. m_data->m_comboBoxCallback = 0;
  12. }
  13. class MyMenuItems : public Gwen::Controls::Base
  14. {
  15. public:
  16. b3FileOpenCallback m_fileOpenCallback;
  17. b3QuitCallback m_quitCallback;
  18. MyMenuItems() :Gwen::Controls::Base(0), m_fileOpenCallback(0)
  19. {
  20. }
  21. void myQuitApp(Gwen::Controls::Base* pControl)
  22. {
  23. if (m_quitCallback)
  24. {
  25. (*m_quitCallback)();
  26. }
  27. }
  28. void fileOpen(Gwen::Controls::Base* pControl)
  29. {
  30. if (m_fileOpenCallback)
  31. {
  32. (*m_fileOpenCallback)();
  33. }
  34. }
  35. };
  36. struct MyTestMenuBar : public Gwen::Controls::MenuStrip
  37. {
  38. Gwen::Controls::MenuItem* m_fileMenu;
  39. Gwen::Controls::MenuItem* m_viewMenu;
  40. MyMenuItems* m_menuItems;
  41. MyTestMenuBar(Gwen::Controls::Base* pParent)
  42. :Gwen::Controls::MenuStrip(pParent)
  43. {
  44. // Gwen::Controls::MenuStrip* menu = new Gwen::Controls::MenuStrip( pParent );
  45. {
  46. m_menuItems = new MyMenuItems();
  47. m_menuItems->m_fileOpenCallback = 0;
  48. m_menuItems->m_quitCallback = 0;
  49. m_fileMenu = AddItem(L"File");
  50. m_fileMenu->GetMenu()->AddItem(L"Open", m_menuItems, (Gwen::Event::Handler::Function)&MyMenuItems::fileOpen);
  51. m_fileMenu->GetMenu()->AddItem(L"Quit", m_menuItems, (Gwen::Event::Handler::Function)&MyMenuItems::myQuitApp);
  52. m_viewMenu = AddItem(L"View");
  53. }
  54. }
  55. virtual ~MyTestMenuBar()
  56. {
  57. delete m_menuItems;
  58. }
  59. };
  60. void GwenUserInterface::exit()
  61. {
  62. //m_data->m_menubar->RemoveAllChildren();
  63. delete m_data->m_tab;
  64. delete m_data->m_windowRight;
  65. delete m_data->m_leftStatusBar;
  66. delete m_data->m_TextOutput;
  67. delete m_data->m_rightStatusBar;
  68. delete m_data->m_bar;
  69. delete m_data->m_menubar;
  70. m_data->m_menubar = 0;
  71. delete m_data->pCanvas;
  72. m_data->pCanvas = 0;
  73. }
  74. GwenUserInterface::~GwenUserInterface()
  75. {
  76. for (int i=0;i<m_data->m_handlers.size();i++)
  77. {
  78. delete m_data->m_handlers[i];
  79. }
  80. m_data->m_handlers.clear();
  81. delete m_data;
  82. }
  83. void GwenUserInterface::resize(int width, int height)
  84. {
  85. m_data->pCanvas->SetSize(width,height);
  86. }
  87. struct MyComboBoxHander :public Gwen::Event::Handler
  88. {
  89. GwenInternalData* m_data;
  90. int m_buttonId;
  91. MyComboBoxHander (GwenInternalData* data, int buttonId)
  92. :m_data(data),
  93. m_buttonId(buttonId)
  94. {
  95. }
  96. void onSelect( Gwen::Controls::Base* pControl )
  97. {
  98. Gwen::Controls::ComboBox* but = (Gwen::Controls::ComboBox*) pControl;
  99. Gwen::String str = Gwen::Utility::UnicodeToString( but->GetSelectedItem()->GetText());
  100. if (m_data->m_comboBoxCallback)
  101. (*m_data->m_comboBoxCallback)(m_buttonId,str.c_str());
  102. }
  103. };
  104. struct MyButtonHander :public Gwen::Event::Handler
  105. {
  106. GwenInternalData* m_data;
  107. int m_buttonId;
  108. MyButtonHander (GwenInternalData* data, int buttonId)
  109. :m_data(data),
  110. m_buttonId(buttonId)
  111. {
  112. }
  113. void onButtonA( Gwen::Controls::Base* pControl )
  114. {
  115. Gwen::Controls::Button* but = (Gwen::Controls::Button*) pControl;
  116. // int dep = but->IsDepressed();
  117. int tog = but->GetToggleState();
  118. if (m_data->m_toggleButtonCallback)
  119. (*m_data->m_toggleButtonCallback)(m_buttonId,tog);
  120. }
  121. };
  122. void GwenUserInterface::textOutput(const char* message)
  123. {
  124. Gwen::UnicodeString msg = Gwen::Utility::StringToUnicode(message);
  125. m_data->m_TextOutput->AddItem( msg );
  126. m_data->m_TextOutput->Scroller()->ScrollToBottom();
  127. }
  128. void GwenUserInterface::setExampleDescription(const char* message)
  129. {
  130. //Gwen apparently doesn't have text/word wrap, so do rudimentary brute-force implementation here.
  131. std::string wrapmessage=message;
  132. int startPos = 0;
  133. std::string lastFit = "";
  134. bool hasSpace = false;
  135. std::string lastFitSpace = "";
  136. int spacePos = 0;
  137. m_data->m_exampleInfoTextOutput->Clear();
  138. int fixedWidth = m_data->m_exampleInfoTextOutput->GetBounds().w-25;
  139. int wrapLen = int(wrapmessage.length());
  140. for (int endPos=0;endPos<=wrapLen;endPos++)
  141. {
  142. std::string sub = wrapmessage.substr(startPos,(endPos-startPos));
  143. Gwen::Point pt = m_data->pRenderer->MeasureText(m_data->pCanvas->GetSkin()->GetDefaultFont(),sub);
  144. if (pt.x <= fixedWidth)
  145. {
  146. lastFit = sub;
  147. if (message[endPos]==' ' ||message[endPos]=='.' || message[endPos]==',' )
  148. {
  149. hasSpace = true;
  150. lastFitSpace = sub;
  151. spacePos = endPos;
  152. }
  153. } else
  154. {
  155. //submit and
  156. if (hasSpace)
  157. {
  158. endPos = spacePos+1;
  159. hasSpace = false;
  160. lastFit = lastFitSpace;
  161. startPos = endPos;
  162. } else
  163. {
  164. startPos = endPos-1;
  165. }
  166. Gwen::UnicodeString msg = Gwen::Utility::StringToUnicode(lastFit);
  167. m_data->m_exampleInfoTextOutput->AddItem( msg );
  168. m_data->m_exampleInfoTextOutput->Scroller()->ScrollToBottom();
  169. }
  170. }
  171. if (lastFit.length())
  172. {
  173. Gwen::UnicodeString msg = Gwen::Utility::StringToUnicode(lastFit);
  174. m_data->m_exampleInfoTextOutput->AddItem( msg );
  175. m_data->m_exampleInfoTextOutput->Scroller()->ScrollToBottom();
  176. }
  177. }
  178. void GwenUserInterface::setStatusBarMessage(const char* message, bool isLeft)
  179. {
  180. Gwen::UnicodeString msg = Gwen::Utility::StringToUnicode(message);
  181. if (isLeft)
  182. {
  183. m_data->m_leftStatusBar->SetText( msg);
  184. } else
  185. {
  186. m_data->m_rightStatusBar->SetText( msg);
  187. }
  188. }
  189. void GwenUserInterface::registerFileOpenCallback(b3FileOpenCallback callback)
  190. {
  191. m_data->m_menuItems->m_fileOpenCallback = callback;
  192. }
  193. void GwenUserInterface::registerQuitCallback(b3QuitCallback callback)
  194. {
  195. m_data->m_menuItems->m_quitCallback = callback;
  196. }
  197. void GwenUserInterface::init(int width, int height,Gwen::Renderer::Base* renderer,float retinaScale)
  198. {
  199. m_data->m_curYposition = 20;
  200. //m_data->m_primRenderer = new GLPrimitiveRenderer(width,height);
  201. m_data->pRenderer = renderer;//new GwenOpenGL3CoreRenderer(m_data->m_primRenderer,stash,width,height,retinaScale);
  202. m_data->skin.SetRender( m_data->pRenderer );
  203. m_data->pCanvas= new Gwen::Controls::Canvas( &m_data->skin );
  204. m_data->pCanvas->SetSize( width,height);
  205. m_data->pCanvas->SetDrawBackground( false);
  206. m_data->pCanvas->SetBackgroundColor( Gwen::Color( 150, 170, 170, 255 ) );
  207. MyTestMenuBar* menubar = new MyTestMenuBar(m_data->pCanvas);
  208. m_data->m_viewMenu = menubar->m_viewMenu;
  209. m_data->m_menuItems = menubar->m_menuItems;
  210. m_data->m_menubar = menubar;
  211. Gwen::Controls::StatusBar* bar = new Gwen::Controls::StatusBar(m_data->pCanvas);
  212. m_data->m_bar = bar;
  213. m_data->m_rightStatusBar = new Gwen::Controls::Label( bar );
  214. m_data->m_rightStatusBar->SetWidth(width/2);
  215. //m_data->m_rightStatusBar->SetText( L"Label Added to Right" );
  216. bar->AddControl( m_data->m_rightStatusBar, true );
  217. m_data->m_TextOutput = new Gwen::Controls::ListBox( m_data->pCanvas );
  218. m_data->m_TextOutput->Dock( Gwen::Pos::Bottom );
  219. m_data->m_TextOutput->SetHeight( 100 );
  220. m_data->m_leftStatusBar = new Gwen::Controls::Label( bar );
  221. //m_data->m_leftStatusBar->SetText( L"Label Added to Left" );
  222. m_data->m_leftStatusBar->SetWidth(width/2);
  223. bar->AddControl( m_data->m_leftStatusBar,false);
  224. //Gwen::KeyboardFocus
  225. /*Gwen::Controls::GroupBox* box = new Gwen::Controls::GroupBox(m_data->pCanvas);
  226. box->SetText("text");
  227. box->SetName("name");
  228. box->SetHeight(500);
  229. */
  230. Gwen::Controls::ScrollControl* windowRight= new Gwen::Controls::ScrollControl(m_data->pCanvas);
  231. windowRight->Dock(Gwen::Pos::Right);
  232. windowRight->SetWidth(250);
  233. windowRight->SetHeight(250);
  234. windowRight->SetScroll(false,true);
  235. m_data->m_windowRight = windowRight;
  236. //windowLeft->SetSkin(
  237. Gwen::Controls::TabControl* tab = new Gwen::Controls::TabControl(windowRight);
  238. m_data->m_tab = tab;
  239. //tab->SetHeight(300);
  240. tab->SetWidth(240);
  241. tab->SetHeight(1250);
  242. //tab->Dock(Gwen::Pos::Left);
  243. tab->Dock( Gwen::Pos::Fill );
  244. //tab->SetMargin( Gwen::Margin( 2, 2, 2, 2 ) );
  245. Gwen::UnicodeString str1(L"Params");
  246. m_data->m_demoPage = tab->AddPage(str1);
  247. // Gwen::UnicodeString str2(L"OpenCL");
  248. // tab->AddPage(str2);
  249. //Gwen::UnicodeString str3(L"page3");
  250. // tab->AddPage(str3);
  251. //but->onPress.Add(handler, &MyHander::onButtonA);
  252. //box->Dock(Gwen::Pos::Left);
  253. /*Gwen::Controls::WindowControl* windowBottom = new Gwen::Controls::WindowControl(m_data->pCanvas);
  254. windowBottom->SetHeight(100);
  255. windowBottom->Dock(Gwen::Pos::Bottom);
  256. windowBottom->SetTitle("bottom");
  257. */
  258. // Gwen::Controls::Property::Text* prop = new Gwen::Controls::Property::Text(m_data->pCanvas);
  259. //prop->Dock(Gwen::Pos::Bottom);
  260. /*Gwen::Controls::SplitterBar* split = new Gwen::Controls::SplitterBar(m_data->pCanvas);
  261. split->Dock(Gwen::Pos::Center);
  262. split->SetHeight(300);
  263. split->SetWidth(300);
  264. */
  265. /*
  266. */
  267. Gwen::Controls::ScrollControl* windowLeft = new Gwen::Controls::ScrollControl(m_data->pCanvas);
  268. windowLeft->Dock(Gwen::Pos::Left);
  269. // windowLeft->SetTitle("title");
  270. windowLeft->SetScroll(false, false);
  271. windowLeft->SetWidth(250);
  272. windowLeft->SetPos(50, 50);
  273. windowLeft->SetHeight(500);
  274. //windowLeft->SetClosable(false);
  275. // windowLeft->SetShouldDrawBackground(true);
  276. windowLeft->SetTabable(true);
  277. Gwen::Controls::TabControl* explorerTab = new Gwen::Controls::TabControl(windowLeft);
  278. //tab->SetHeight(300);
  279. // explorerTab->SetWidth(230);
  280. explorerTab->SetHeight(250);
  281. //tab->Dock(Gwen::Pos::Left);
  282. explorerTab->Dock(Gwen::Pos::Fill);
  283. //m_data->m_exampleInfoTextOutput->SetBounds(2, 10, 236, 400);
  284. //windowRight
  285. Gwen::UnicodeString explorerStr1(L"Explorer");
  286. m_data->m_explorerPage = explorerTab->AddPage(explorerStr1);
  287. Gwen::UnicodeString shapesStr1(L"Test");
  288. Gwen::Controls::TabButton* shapes = explorerTab->AddPage(shapesStr1);
  289. ///todo(erwincoumans) figure out why the HSV color picker is extremely slow
  290. //Gwen::Controls::HSVColorPicker* color = new Gwen::Controls::HSVColorPicker(shapes->GetPage());
  291. Gwen::Controls::ColorPicker* color = new Gwen::Controls::ColorPicker(shapes->GetPage());
  292. color->SetKeyboardInputEnabled(true);
  293. Gwen::Controls::TreeControl* ctrl = new Gwen::Controls::TreeControl(m_data->m_explorerPage->GetPage());
  294. m_data->m_explorerTreeCtrl = ctrl;
  295. ctrl->SetKeyboardInputEnabled(true);
  296. ctrl->Focus();
  297. ctrl->SetBounds(2, 10, 236, 300);
  298. m_data->m_exampleInfoGroupBox = new Gwen::Controls::Label( m_data->m_explorerPage->GetPage() );
  299. m_data->m_exampleInfoGroupBox->SetPos(2, 314);
  300. m_data->m_exampleInfoGroupBox->SetHeight( 15 );
  301. m_data->m_exampleInfoGroupBox->SetWidth(234);
  302. m_data->m_exampleInfoGroupBox->SetText("Example Description");
  303. m_data->m_exampleInfoTextOutput = new Gwen::Controls::ListBox(m_data->m_explorerPage->GetPage());
  304. //m_data->m_exampleInfoTextOutput->Dock( Gwen::Pos::Bottom );
  305. m_data->m_exampleInfoTextOutput->SetPos(2, 332);
  306. m_data->m_exampleInfoTextOutput->SetHeight( 150 );
  307. m_data->m_exampleInfoTextOutput->SetWidth(233);
  308. }
  309. void GwenUserInterface::forceUpdateScrollBars()
  310. {
  311. b3Assert(m_data);
  312. b3Assert(m_data->m_explorerTreeCtrl);
  313. if (m_data && m_data->m_explorerTreeCtrl)
  314. {
  315. m_data->m_explorerTreeCtrl->ForceUpdateScrollBars();
  316. }
  317. }
  318. void GwenUserInterface::setFocus()
  319. {
  320. b3Assert(m_data);
  321. b3Assert(m_data->m_explorerTreeCtrl);
  322. if (m_data && m_data->m_explorerTreeCtrl)
  323. {
  324. m_data->m_explorerTreeCtrl->Focus();
  325. }
  326. }
  327. b3ToggleButtonCallback GwenUserInterface::getToggleButtonCallback()
  328. {
  329. return m_data->m_toggleButtonCallback;
  330. }
  331. void GwenUserInterface::setToggleButtonCallback(b3ToggleButtonCallback callback)
  332. {
  333. m_data->m_toggleButtonCallback = callback;
  334. }
  335. void GwenUserInterface::registerToggleButton2(int buttonId, const char* name)
  336. {
  337. assert(m_data);
  338. assert(m_data->m_demoPage);
  339. Gwen::Controls::Button* but = new Gwen::Controls::Button(m_data->m_demoPage->GetPage());
  340. ///some heuristic to find the button location
  341. int ypos = m_data->m_curYposition;
  342. but->SetPos(10, ypos );
  343. but->SetWidth( 200 );
  344. //but->SetBounds( 200, 30, 300, 200 );
  345. MyButtonHander* handler = new MyButtonHander(m_data, buttonId);
  346. m_data->m_handlers.push_back(handler);
  347. m_data->m_curYposition+=22;
  348. but->onToggle.Add(handler, &MyButtonHander::onButtonA);
  349. but->SetIsToggle(true);
  350. but->SetToggleState(false);
  351. but->SetText(name);
  352. }
  353. void GwenUserInterface::setComboBoxCallback(b3ComboBoxCallback callback)
  354. {
  355. m_data->m_comboBoxCallback = callback;
  356. }
  357. b3ComboBoxCallback GwenUserInterface::getComboBoxCallback()
  358. {
  359. return m_data->m_comboBoxCallback;
  360. }
  361. void GwenUserInterface::registerComboBox2(int comboboxId, int numItems, const char** items, int startItem)
  362. {
  363. Gwen::Controls::ComboBox* combobox = new Gwen::Controls::ComboBox(m_data->m_demoPage->GetPage());
  364. MyComboBoxHander* handler = new MyComboBoxHander(m_data, comboboxId);
  365. m_data->m_handlers.push_back(handler);
  366. combobox->onSelection.Add(handler,&MyComboBoxHander::onSelect);
  367. int ypos = m_data->m_curYposition;
  368. combobox->SetPos(10, ypos );
  369. combobox->SetWidth( 100 );
  370. //box->SetPos(120,130);
  371. for (int i=0;i<numItems;i++)
  372. {
  373. Gwen::Controls::MenuItem* item = combobox->AddItem(Gwen::Utility::StringToUnicode(items[i]));
  374. if (i==startItem)
  375. combobox->OnItemSelected(item);
  376. }
  377. m_data->m_curYposition+=22;
  378. }
  379. void GwenUserInterface::draw(int width, int height)
  380. {
  381. // printf("width = %d, height=%d\n", width,height);
  382. if (m_data->pCanvas)
  383. {
  384. m_data->pCanvas->SetSize(width,height);
  385. //m_data->m_primRenderer->setScreenSize(width,height);
  386. m_data->pRenderer->Resize(width,height);
  387. m_data->pCanvas->RenderCanvas();
  388. //restoreOpenGLState();
  389. }
  390. }
  391. bool GwenUserInterface::mouseMoveCallback( float x, float y)
  392. {
  393. bool handled = false;
  394. static int m_lastmousepos[2] = {0,0};
  395. static bool isInitialized = false;
  396. if (m_data->pCanvas)
  397. {
  398. if (!isInitialized)
  399. {
  400. isInitialized = true;
  401. m_lastmousepos[0] = x+1;
  402. m_lastmousepos[1] = y+1;
  403. }
  404. handled = m_data->pCanvas->InputMouseMoved(x,y,m_lastmousepos[0],m_lastmousepos[1]);
  405. }
  406. return handled;
  407. }
  408. #include "../CommonInterfaces/CommonWindowInterface.h"
  409. bool GwenUserInterface::keyboardCallback(int bulletKey, int state)
  410. {
  411. int gwenKey = -1;
  412. if (m_data->pCanvas)
  413. {
  414. //convert 'Bullet' keys into 'Gwen' keys
  415. switch (bulletKey)
  416. {
  417. case B3G_RETURN:
  418. {
  419. gwenKey = Gwen::Key::Return;
  420. break;
  421. }
  422. case B3G_LEFT_ARROW:
  423. {
  424. gwenKey = Gwen::Key::Left;
  425. break;
  426. }
  427. case B3G_RIGHT_ARROW:
  428. {
  429. gwenKey = Gwen::Key::Right;
  430. break;
  431. }
  432. case B3G_UP_ARROW:
  433. {
  434. gwenKey = Gwen::Key::Up;
  435. break;
  436. }
  437. case B3G_DOWN_ARROW:
  438. {
  439. gwenKey = Gwen::Key::Down;
  440. break;
  441. }
  442. case B3G_BACKSPACE:
  443. {
  444. gwenKey = Gwen::Key::Backspace;
  445. break;
  446. }
  447. case B3G_DELETE:
  448. {
  449. gwenKey = Gwen::Key::Delete;
  450. break;
  451. }
  452. case B3G_HOME:
  453. {
  454. gwenKey = Gwen::Key::Home;
  455. break;
  456. }
  457. case B3G_END:
  458. {
  459. gwenKey = Gwen::Key::End;
  460. break;
  461. }
  462. case B3G_SHIFT:
  463. {
  464. gwenKey = Gwen::Key::Shift;
  465. break;
  466. }
  467. case B3G_CONTROL:
  468. {
  469. gwenKey = Gwen::Key::Control;
  470. break;
  471. }
  472. default:
  473. {
  474. }
  475. };
  476. if (gwenKey>=0)
  477. {
  478. return m_data->pCanvas->InputKey(gwenKey,state==1);
  479. } else
  480. {
  481. if (bulletKey<256 && state)
  482. {
  483. Gwen::UnicodeChar c = ( Gwen::UnicodeChar ) bulletKey;
  484. return m_data->pCanvas->InputCharacter(c);
  485. }
  486. }
  487. }
  488. return false;
  489. }
  490. bool GwenUserInterface::mouseButtonCallback(int button, int state, float x, float y)
  491. {
  492. bool handled = false;
  493. if (m_data->pCanvas)
  494. {
  495. handled = m_data->pCanvas->InputMouseMoved(x,y,x, y);
  496. if (button>=0)
  497. {
  498. handled = m_data->pCanvas->InputMouseButton(button,(bool)state);
  499. if (handled)
  500. {
  501. //if (!state)
  502. // return false;
  503. }
  504. }
  505. }
  506. return handled;
  507. }