tonti666

Untitled

Sep 17th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.94 KB | None | 0 0
  1.  
  2.  
  3. CGUI GUI;
  4. float MenuAlpha = 0.f;
  5.  
  6. float Globals::MenuAlpha2 = MenuAlpha;
  7.  
  8. CGUI::CGUI()
  9. {
  10.  
  11. }
  12.  
  13. // Draws all windows
  14. void CGUI::Draw()
  15. {
  16. bool ShouldDrawCursor = false;
  17.  
  18. for (auto window : Windows)
  19. {
  20. if (window->m_bIsOpen)
  21. {
  22. ShouldDrawCursor = true;
  23. DrawWindow(window);
  24. }
  25.  
  26. }
  27.  
  28. if (ShouldDrawCursor)
  29. {
  30. Render::Clear(Mouse.x + 1, Mouse.y, 1, 17, Color(3, 6, 26, 255));
  31. for (int i = 0; i < 11; i++)
  32. Render::Clear(Mouse.x + 2 + i, Mouse.y + 1 + i, 1, 1, Color(30, 200, 225, 255));
  33. Render::Clear(Mouse.x + 8, Mouse.y + 12, 5, 1, Color(30, 200, 225, 255));
  34. Render::Clear(Mouse.x + 8, Mouse.y + 13, 1, 1, Color(30, 200, 225, 255));
  35. Render::Clear(Mouse.x + 9, Mouse.y + 14, 1, 2, Color(30, 200, 225, 255));
  36. Render::Clear(Mouse.x + 10, Mouse.y + 16, 1, 2, Color(30, 200, 225, 255));
  37. Render::Clear(Mouse.x + 8, Mouse.y + 18, 2, 1, Color(30, 200, 225, 255));
  38. Render::Clear(Mouse.x + 7, Mouse.y + 16, 1, 2, Color(30, 200, 225, 255));
  39. Render::Clear(Mouse.x + 6, Mouse.y + 14, 1, 2, Color(30, 200, 225, 255));
  40. Render::Clear(Mouse.x + 5, Mouse.y + 13, 1, 1, Color(30, 200, 225, 255));
  41. Render::Clear(Mouse.x + 4, Mouse.y + 14, 1, 1, Color(30, 200, 225, 255));
  42. Render::Clear(Mouse.x + 3, Mouse.y + 15, 1, 1, Color(30, 200, 225, 255));
  43. Render::Clear(Mouse.x + 2, Mouse.y + 16, 1, 1, Color(30, 200, 225, 255));
  44. for (int i = 0; i < 4; i++)
  45. Render::Clear(Mouse.x + 2 + i, Mouse.y + 2 + i, 1, 14 - (i * 2), Color(30, 200, 225, 255));
  46. Render::Clear(Mouse.x + 6, Mouse.y + 6, 1, 8, Color(30, 200, 225, 255));
  47. Render::Clear(Mouse.x + 7, Mouse.y + 7, 1, 9, Color(30, 200, 225, 255));
  48. for (int i = 0; i < 4; i++)
  49. Render::Clear(Mouse.x + 8 + i, Mouse.y + 8 + i, 1, 4 - i, Color(30, 200, 225, 255));
  50. Render::Clear(Mouse.x + 8, Mouse.y + 14, 1, 4, Color(30, 200, 225, 255));
  51. Render::Clear(Mouse.x + 9, Mouse.y + 16, 1, 2, Color(30, 200, 225, 255));
  52.  
  53. }
  54. }
  55.  
  56. // Handle all input etc
  57. void CGUI::Update()
  58. {
  59. //Key Arra
  60. std::copy(keys, keys + 255, oldKeys);
  61. for (int x = 0; x < 255; x++)
  62. {
  63. //oldKeys[x] = oldKeys[x] & keys[x];
  64. keys[x] = (GetAsyncKeyState(x));
  65. }
  66.  
  67. // Mouse Location
  68. POINT mp; GetCursorPos(&mp);
  69. Mouse.x = mp.x; Mouse.y = mp.y;
  70.  
  71. RECT Screen = Render::GetViewport();
  72.  
  73. // Window Binds
  74. for (auto& bind : WindowBinds)
  75. {
  76. if (GetKeyPress(bind.first))
  77. {
  78. bind.second->Toggle();
  79. }
  80. }
  81.  
  82. // Stop dragging
  83. if (IsDraggingWindow && !GetKeyState(VK_LBUTTON))
  84. {
  85. IsDraggingWindow = false;
  86. DraggingWindow = nullptr;
  87. }
  88.  
  89. // If we are in the proccess of dragging a window
  90. if (IsDraggingWindow && GetKeyState(VK_LBUTTON) && !GetKeyPress(VK_LBUTTON))
  91. {
  92. if (DraggingWindow)
  93. {
  94. DraggingWindow->m_x = Mouse.x - DragOffsetX;
  95. DraggingWindow->m_y = Mouse.y - DragOffsetY;
  96. }
  97. }
  98.  
  99. // Process some windows
  100. for (auto window : Windows)
  101. {
  102. if (window->m_bIsOpen)
  103. MenuAlpha = min(MenuAlpha + 6, 255);
  104. else
  105. MenuAlpha = max(MenuAlpha - 6, 0);
  106. {
  107. // Used to tell the widget processing that there could be a click
  108. bool bCheckWidgetClicks = false;
  109.  
  110. // If the user clicks inside the window
  111. if (GetKeyPress(VK_LBUTTON))
  112. {
  113. bCheckWidgetClicks = false;
  114. if (IsMouseInRegion(window->m_y, window->m_x + window->m_iWidth, window->m_y + window->m_iHeight, window->m_x))
  115. {
  116. // Close Button
  117. if (IsMouseInRegion(window->m_y, window->m_x + window->m_iWidth, window->m_y + UI_WIN_CLOSE_X, window->m_x + window->m_iWidth - UI_WIN_CLOSE_X))
  118. {
  119. window->Toggle();
  120. }
  121. else
  122.  
  123. // User is selecting a new tab
  124. if (IsMouseInRegion(window->GetTabArea()))
  125. {
  126.  
  127. bCheckWidgetClicks = true;
  128.  
  129. int iTab = 0;
  130. int TabCount = window->Tabs.size();
  131. if (TabCount) // If there are some tabs
  132. {
  133. int TabSize = UI_TAB_HEIGHT;
  134. int Dist = Mouse.y - (window->m_y + UI_WIN_TITLEHEIGHT + UI_WIN_TOPHEIGHT);
  135. if (Dist < (UI_TAB_HEIGHT*TabCount))
  136. {
  137. while (Dist > TabSize)
  138. {
  139. if (Dist > TabSize)
  140. {
  141. iTab++;
  142. Dist -= TabSize;
  143. }
  144. if (iTab == (TabCount - 1))
  145. {
  146. break;
  147. }
  148. }
  149. window->SelectedTab = window->Tabs[iTab];
  150.  
  151. // Loose focus on the control
  152. bCheckWidgetClicks = false;
  153. window->IsFocusingControl = false;
  154. window->FocusedControl = nullptr;
  155. }
  156. }
  157.  
  158. }
  159. // Is it inside the client area?
  160. else if (IsMouseInRegion(window->GetClientArea()))
  161. {
  162. bCheckWidgetClicks = true;
  163. }
  164. else
  165. {
  166. // Must be in the around the title or side of the window
  167. // So we assume the user is trying to drag the window
  168. IsDraggingWindow = true;
  169. DraggingWindow = window;
  170. DragOffsetX = Mouse.x - window->m_x;
  171. DragOffsetY = Mouse.y - window->m_y;
  172.  
  173. // Loose focus on the control
  174. window->IsFocusingControl = false;
  175. window->FocusedControl = nullptr;
  176. }
  177. }
  178. else
  179. {
  180. // Loose focus on the control
  181. window->IsFocusingControl = false;
  182. window->FocusedControl = nullptr;
  183. }
  184. }
  185.  
  186. // Controls
  187. if (window->SelectedTab != nullptr)
  188. {
  189. // Focused widget
  190. bool SkipWidget = false;
  191. CControl* SkipMe = nullptr;
  192.  
  193. // this window is focusing on a widget??
  194. if (window->IsFocusingControl)
  195. {
  196. if (window->FocusedControl != nullptr)
  197. {
  198. // We've processed it once, skip it later
  199. SkipWidget = true;
  200. SkipMe = window->FocusedControl;
  201.  
  202. POINT cAbs = window->FocusedControl->GetAbsolutePos();
  203. RECT controlRect = { cAbs.x, cAbs.y, window->FocusedControl->m_iWidth, window->FocusedControl->m_iHeight };
  204. window->FocusedControl->OnUpdate();
  205.  
  206. if (window->FocusedControl->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
  207. {
  208. window->FocusedControl->OnClick();
  209.  
  210. // If it gets clicked we loose focus
  211. window->IsFocusingControl = false;
  212. window->FocusedControl = nullptr;
  213. bCheckWidgetClicks = false;
  214. }
  215. }
  216. }
  217.  
  218. for (auto control : window->SelectedTab->Controls)
  219. {
  220. if (control != nullptr)
  221. {
  222. if (SkipWidget && SkipMe == control)
  223. continue;
  224.  
  225. control->parent = window;
  226.  
  227. POINT cAbs = control->GetAbsolutePos();
  228. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  229. control->OnUpdate();
  230.  
  231. if (control->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
  232. {
  233. control->OnClick();
  234. bCheckWidgetClicks = false;
  235.  
  236. // Change of focus
  237. if (control->Flag(UIFlags::UI_Focusable))
  238. {
  239. window->IsFocusingControl = true;
  240. window->FocusedControl = control;
  241. }
  242. else
  243. {
  244. window->IsFocusingControl = false;
  245. window->FocusedControl = nullptr;
  246. }
  247.  
  248. }
  249. }
  250. }
  251.  
  252. // We must have clicked whitespace
  253. if (bCheckWidgetClicks)
  254. {
  255. // Loose focus on the control
  256. window->IsFocusingControl = false;
  257. window->FocusedControl = nullptr;
  258. }
  259. }
  260. }
  261. }
  262. }
  263.  
  264.  
  265. // Returns
  266.  
  267. bool CGUI::IsMouseInRegion(int y, int x2, int y2, int x)
  268. {
  269. if (Mouse.x > x && Mouse.y > y && Mouse.x < x2 && Mouse.y < y2)
  270. return true;
  271. else
  272. return false;
  273. }
  274.  
  275. bool CGUI::IsMouseInRegion(RECT region)
  276. {
  277. return IsMouseInRegion(region.top, region.left + region.right, region.top + region.bottom, region.left);
  278. }
  279.  
  280. POINT CGUI::GetMouse()
  281. {
  282. return Mouse;
  283. }
  284.  
  285. bool CGUI::DrawWindow(CWindow* window)
  286. {
  287. Render::Clear(window->m_x, window->m_y, window->m_iWidth, window->m_iHeight, Color(25, 25, 25, 254));
  288.  
  289.  
  290.  
  291. //Inner
  292. //Render::Outline(window->m_x + 7, window->m_y + 1 + 26, window->m_iWidth - 4 - 10, window->m_iHeight - 2 - 6 - 26, Color(0, 150, 0, MenuAlpha));
  293. //Render::Clear(window->m_x + 8, window->m_y + 1 + 27, window->m_iWidth - 4 - 12, window->m_iHeight - 2 - 8 - 26, Color(255, 255, 255, MenuAlpha));
  294.  
  295. Render::Outline(window->m_x, window->m_y, window->m_iWidth, window->m_iHeight, Color(0, 0, 0, 255));
  296. Render::Outline(window->m_x + 1, window->m_y + 1, window->m_iWidth - 2, window->m_iHeight - 2, Color(65, 65, 65, 254));
  297. Render::Outline(window->m_x + 2, window->m_y + 2, window->m_iWidth - 4, window->m_iHeight - 4, Color(45, 45, 45, 254));
  298. Render::Outline(window->m_x + 3, window->m_y + 3, window->m_iWidth - 6, window->m_iHeight - 6, Color(45, 45, 45, 254));
  299. Render::Outline(window->m_x + 4, window->m_y + 4, window->m_iWidth - 8, window->m_iHeight - 8, Color(45, 45, 45, 254));
  300. Render::Outline(window->m_x + 5, window->m_y + 5, window->m_iWidth - 10, window->m_iHeight - 10, Color(65, 65, 65, 254));
  301.  
  302. int TabCount = window->Tabs.size();
  303. if (TabCount) // If there are some tabs
  304. {
  305. for (int i = 0; i < TabCount; i++)
  306. {
  307. RECT TabArea = { window->m_x, window->m_y + UI_WIN_TITLEHEIGHT + UI_WIN_TOPHEIGHT + (i*UI_TAB_HEIGHT) , UI_TAB_WIDTH, UI_TAB_HEIGHT };
  308. CTab *tab = window->Tabs[i];
  309.  
  310.  
  311.  
  312. Color txtColor = UI_COL_SHADOW;
  313.  
  314. if (window->SelectedTab == tab)
  315. {
  316. // Selected
  317. txtColor = UI_COL_TABSEPERATOR;
  318. }
  319. else if (IsMouseInRegion(TabArea))
  320. {
  321. // Hover
  322. txtColor = Color(220, 220, 220, 255);
  323. }
  324.  
  325. Render::Text(TabArea.left + 15, TabArea.top + 8, txtColor, Render::Fonts::Tabs, tab->Title.c_str());
  326. //Render::Clear(window->m_x + 8, window->m_y + 1 + 27, window->m_iWidth - 4 - 12, 2, Color(150, 0, 0, 255));
  327. }
  328. }
  329.  
  330.  
  331.  
  332.  
  333. // Controls
  334. if (window->SelectedTab != nullptr)
  335. {
  336. // Focused widget
  337. bool SkipWidget = false;
  338. CControl* SkipMe = nullptr;
  339.  
  340. // this window is focusing on a widget??
  341. if (window->IsFocusingControl)
  342. {
  343. if (window->FocusedControl != nullptr)
  344. {
  345. // We need to draw it last, so skip it in the regular loop
  346. SkipWidget = true;
  347. SkipMe = window->FocusedControl;
  348. }
  349. }
  350.  
  351.  
  352. // Itterate over all the other controls
  353. for (auto control : window->SelectedTab->Controls)
  354. {
  355. if (SkipWidget && SkipMe == control)
  356. continue;
  357.  
  358. if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
  359. {
  360. POINT cAbs = control->GetAbsolutePos();
  361. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  362. bool hover = false;
  363. if (IsMouseInRegion(controlRect))
  364. {
  365. hover = true;
  366. }
  367. control->Draw(hover);
  368. }
  369. }
  370.  
  371. // Draw the skipped widget last
  372. if (SkipWidget)
  373. {
  374. auto control = window->FocusedControl;
  375.  
  376. if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
  377. {
  378. POINT cAbs = control->GetAbsolutePos();
  379. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  380. bool hover = false;
  381. if (IsMouseInRegion(controlRect))
  382. {
  383. hover = true;
  384. }
  385. control->Draw(hover);
  386. }
  387. }
  388.  
  389. }
  390.  
  391.  
  392. return true;
  393. }
  394.  
  395. void CGUI::RegisterWindow(CWindow* window)
  396. {
  397. Windows.push_back(window);
  398.  
  399. // Resorting to put groupboxes at the start
  400. for (auto tab : window->Tabs)
  401. {
  402. for (auto control : tab->Controls)
  403. {
  404. if (control->Flag(UIFlags::UI_RenderFirst))
  405. {
  406. CControl * c = control;
  407. tab->Controls.erase(std::remove(tab->Controls.begin(), tab->Controls.end(), control), tab->Controls.end());
  408. tab->Controls.insert(tab->Controls.begin(), control);
  409. }
  410. }
  411. }
  412. }
  413.  
  414. void CGUI::BindWindow(unsigned char Key, CWindow* window)
  415. {
  416. if (window)
  417. WindowBinds[Key] = window;
  418. else
  419. WindowBinds.erase(Key);
  420. }
  421.  
  422. void CGUI::SaveWindowState(CWindow* window, std::string Filename)
  423. {
  424. tinyxml2::XMLDocument Doc;
  425.  
  426. tinyxml2::XMLElement *Root = Doc.NewElement("Rivotril");
  427. Doc.LinkEndChild(Root);
  428.  
  429. if (Root && window->Tabs.size() > 0)
  430. {
  431. for (auto Tab : window->Tabs)
  432. {
  433. tinyxml2::XMLElement *TabElement = Doc.NewElement(Tab->Title.c_str());
  434. Root->LinkEndChild(TabElement);
  435.  
  436. if (TabElement && Tab->Controls.size() > 1)
  437. {
  438. for (auto Control : Tab->Controls)
  439. {
  440. if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
  441. {
  442. tinyxml2::XMLElement *ControlElement = Doc.NewElement(Control->FileIdentifier.c_str());
  443. TabElement->LinkEndChild(ControlElement);
  444.  
  445. if (!ControlElement)
  446. {
  447. return;
  448. }
  449.  
  450. CCheckBox* cbx = nullptr;
  451. CComboBox* cbo = nullptr;
  452. CKeyBind* key = nullptr;
  453. CSlider* sld = nullptr;
  454.  
  455. switch (Control->FileControlType)
  456. {
  457. case UIControlTypes::UIC_CheckBox:
  458. cbx = (CCheckBox*)Control;
  459. ControlElement->SetText(cbx->GetState());
  460. break;
  461. case UIControlTypes::UIC_ComboBox:
  462. cbo = (CComboBox*)Control;
  463. ControlElement->SetText(cbo->GetIndex());
  464. break;
  465. case UIControlTypes::UIC_KeyBind:
  466. key = (CKeyBind*)Control;
  467. ControlElement->SetText(key->GetKey());
  468. break;
  469. case UIControlTypes::UIC_Slider:
  470. sld = (CSlider*)Control;
  471. ControlElement->SetText(sld->GetValue());
  472. break;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479.  
  480. if (Doc.SaveFile(Filename.c_str()) != tinyxml2::XML_NO_ERROR)
  481. {
  482. MessageBox(NULL, "Failed To Save Config File!", "Rivotril", MB_OK);
  483. }
  484.  
  485. }
  486.  
  487. void CGUI::LoadWindowState(CWindow* window, std::string Filename)
  488. {
  489. tinyxml2::XMLDocument Doc;
  490. if (Doc.LoadFile(Filename.c_str()) == tinyxml2::XML_NO_ERROR)
  491. {
  492. tinyxml2::XMLElement *Root = Doc.RootElement();
  493.  
  494. if (Root)
  495. {
  496. if (Root && window->Tabs.size() > 0)
  497. {
  498. for (auto Tab : window->Tabs)
  499. {
  500. tinyxml2::XMLElement *TabElement = Root->FirstChildElement(Tab->Title.c_str());
  501. if (TabElement)
  502. {
  503. if (TabElement && Tab->Controls.size() > 0)
  504. {
  505. for (auto Control : Tab->Controls)
  506. {
  507. if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
  508. {
  509. tinyxml2::XMLElement *ControlElement = TabElement->FirstChildElement(Control->FileIdentifier.c_str());
  510.  
  511. if (ControlElement)
  512. {
  513. CCheckBox* cbx = nullptr;
  514. CComboBox* cbo = nullptr;
  515. CKeyBind* key = nullptr;
  516. CSlider* sld = nullptr;
  517.  
  518. switch (Control->FileControlType)
  519. {
  520. case UIControlTypes::UIC_CheckBox:
  521. cbx = (CCheckBox*)Control;
  522. cbx->SetState(ControlElement->GetText()[0] == '1' ? true : false);
  523. break;
  524. case UIControlTypes::UIC_ComboBox:
  525. cbo = (CComboBox*)Control;
  526. cbo->SelectIndex(atoi(ControlElement->GetText()));
  527. break;
  528. case UIControlTypes::UIC_KeyBind:
  529. key = (CKeyBind*)Control;
  530. key->SetKey(atoi(ControlElement->GetText()));
  531. break;
  532. case UIControlTypes::UIC_Slider:
  533. sld = (CSlider*)Control;
  534. sld->SetValue(atof(ControlElement->GetText()));
  535. break;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
Add Comment
Please, Sign In to add comment