Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.75 KB | None | 0 0
  1.  
  2. #include "GUI.h"
  3.  
  4. #include "DrawManager.h"
  5. #include "MetaInfo.h"
  6. #include "Menu.h"
  7. #include <algorithm>
  8. #include "tinyxml2.h"
  9. #include "Controls.h"
  10.  
  11. #include <sstream>
  12.  
  13. CGUI GUI;
  14.  
  15. CGUI::CGUI()
  16. {
  17.  
  18. }
  19.  
  20. // Draws all windows
  21. void CGUI::Draw()
  22. {
  23. bool ShouldDrawCursor = false;
  24.  
  25. for (auto window : Windows)
  26. {
  27. if (window->m_bIsOpen)
  28. {
  29. ShouldDrawCursor = true;
  30. DrawWindow(window, 1);
  31. }
  32. }
  33.  
  34. if (ShouldDrawCursor)
  35. {
  36. draw.rect(Mouse.x + 1, Mouse.y, 1, 17, Color(3, 6, 26, 255));
  37. for (int i = 0; i < 11; i++)
  38. draw.rect(Mouse.x + 2 + i, Mouse.y + 1 + i, 1, 1, Color(3, 6, 26, 255));
  39. draw.rect(Mouse.x + 8, Mouse.y + 12, 5, 1, Color(3, 6, 26, 255));
  40. draw.rect(Mouse.x + 8, Mouse.y + 13, 1, 1, Color(3, 6, 26, 255));
  41. draw.rect(Mouse.x + 9, Mouse.y + 14, 1, 2, Color(3, 6, 26, 255));
  42. draw.rect(Mouse.x + 10, Mouse.y + 16, 1, 2, Color(3, 6, 26, 255));
  43. draw.rect(Mouse.x + 8, Mouse.y + 18, 2, 1, Color(3, 6, 26, 255));
  44. draw.rect(Mouse.x + 7, Mouse.y + 16, 1, 2, Color(3, 6, 26, 255));
  45. draw.rect(Mouse.x + 6, Mouse.y + 14, 1, 2, Color(3, 6, 26, 255));
  46. draw.rect(Mouse.x + 5, Mouse.y + 13, 1, 1, Color(3, 6, 26, 255));
  47. draw.rect(Mouse.x + 4, Mouse.y + 14, 1, 1, Color(3, 6, 26, 255));
  48. draw.rect(Mouse.x + 3, Mouse.y + 15, 1, 1, Color(3, 6, 26, 255));
  49. draw.rect(Mouse.x + 2, Mouse.y + 16, 1, 1, Color(3, 6, 26, 255));
  50. for (int i = 0; i < 4; i++)
  51. draw.rect(Mouse.x + 2 + i, Mouse.y + 2 + i, 1, 14 - (i * 2), game::globals.forecolor);
  52. draw.rect(Mouse.x + 6, Mouse.y + 6, 1, 8, game::globals.forecolor);
  53. draw.rect(Mouse.x + 7, Mouse.y + 7, 1, 9, game::globals.forecolor);
  54. for (int i = 0; i < 4; i++)
  55. draw.rect(Mouse.x + 8 + i, Mouse.y + 8 + i, 1, 4 - i, game::globals.forecolor);
  56. draw.rect(Mouse.x + 8, Mouse.y + 14, 1, 4, game::globals.forecolor);
  57. draw.rect(Mouse.x + 9, Mouse.y + 16, 1, 2, game::globals.forecolor);
  58. }
  59. }
  60.  
  61. // Handle all input etc
  62. void CGUI::Update()
  63. {
  64. //Key Array
  65. std::copy(keys, keys + 255, oldKeys);
  66. for (int x = 0; x < 255; x++)
  67. {
  68. //oldKeys[x] = oldKeys[x] & keys[x];
  69. keys[x] = (GetAsyncKeyState(x));
  70. }
  71.  
  72. // Mouse Location
  73. POINT mp; GetCursorPos(&mp);
  74. Mouse.x = mp.x; Mouse.y = mp.y;
  75.  
  76. RECT Viewport = { 0, 0, 0, 0 };
  77. int w, h;
  78. m_pEngine->GetScreenSize(w, h);
  79. Viewport.right = w; Viewport.bottom = h;
  80. RECT Screen = Viewport;
  81.  
  82. // Window Binds
  83. for (auto& bind : WindowBinds)
  84. {
  85. if (GetKeyPress(miscconfig.iMenuKey))
  86. {
  87. bind.second->Toggle();
  88. }
  89. }
  90.  
  91. // Stop dragging
  92. if (IsDraggingWindow && !GetKeyState(VK_LBUTTON))
  93. {
  94. IsDraggingWindow = false;
  95. DraggingWindow = nullptr;
  96. }
  97.  
  98. // If we are in the proccess of dragging a window
  99. if (IsDraggingWindow && GetKeyState(VK_LBUTTON) && !GetKeyPress(VK_LBUTTON))
  100. {
  101. if (DraggingWindow)
  102. {
  103. DraggingWindow->m_x = Mouse.x - DragOffsetX;
  104. DraggingWindow->m_y = Mouse.y - DragOffsetY;
  105. }
  106. }
  107.  
  108. // Process some windows
  109. for (auto window : Windows)
  110. {
  111. if (window->m_bIsOpen)
  112. {
  113. // Used to tell the widget processing that there could be a click
  114. bool bCheckWidgetClicks = false;
  115.  
  116. // If the user clicks inside the window
  117. if (GetKeyPress(VK_LBUTTON))
  118. {
  119. if (IsMouseInRegion(window->m_x, window->m_y, window->m_x + window->m_iWidth, window->m_y + window->m_iHeight))
  120. {
  121. // Is it inside the client area?
  122. if (IsMouseInRegion(window->GetClientArea()))
  123. {
  124. // User is selecting a new tab
  125. if (!IsMouseInRegion(window->GetTabArea()))
  126. bCheckWidgetClicks = true;
  127. }
  128. }
  129. else
  130. {
  131. // Loose focus on the control
  132. window->IsFocusingControl = false;
  133. window->FocusedControl = nullptr;
  134. }
  135. }
  136.  
  137. if (IsMouseInRegion(window->GetDragArea()))
  138. {
  139. // Must be in the around the title or side of the window
  140. // So we assume the user is trying to drag the window
  141. IsDraggingWindow = true;
  142. DraggingWindow = window;
  143. DragOffsetX = Mouse.x - window->m_x;
  144. DragOffsetY = Mouse.y - window->m_y;
  145.  
  146. // Loose focus on the control
  147. window->IsFocusingControl = false;
  148. window->FocusedControl = nullptr;
  149. }
  150.  
  151. // Controls
  152. if (window->SelectedTab != nullptr)
  153. {
  154. // Focused widget
  155. bool SkipWidget = false;
  156. CControl* SkipMe = nullptr;
  157.  
  158. // this window is focusing on a widget??
  159. if (window->IsFocusingControl)
  160. {
  161. if (window->FocusedControl != nullptr)
  162. {
  163. CControl* control = window->FocusedControl;
  164. CGroupBox* group;
  165. if (control->FileControlType != UIControlTypes::UIC_GroupBox) group = control->parent_group ? (CGroupBox*)control->parent_group : nullptr;
  166.  
  167. if (group != nullptr && control->FileControlType != UIControlTypes::UIC_GroupBox)
  168. {
  169. if ((group->group_tabs.size() > 0 && control->g_tab == group->selected_tab) || group->group_tabs.size() == 0)
  170. {
  171. // We've processed it once, skip it later
  172. SkipWidget = true;
  173. SkipMe = window->FocusedControl;
  174.  
  175. POINT cAbs = window->FocusedControl->GetAbsolutePos();
  176. RECT controlRect = { cAbs.x, cAbs.y, window->FocusedControl->m_iWidth, window->FocusedControl->m_iHeight };
  177. window->FocusedControl->OnUpdate();
  178.  
  179. if (window->FocusedControl->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
  180. {
  181. window->FocusedControl->OnClick();
  182.  
  183. bCheckWidgetClicks = false;
  184. }
  185. }
  186. }
  187. else if (control->FileControlType == UIControlTypes::UIC_GroupBox || control->FileControlType != UIControlTypes::UIC_GroupBox && !control->parent_group)
  188. {
  189. // We've processed it once, skip it later
  190. SkipWidget = true;
  191. SkipMe = window->FocusedControl;
  192.  
  193. POINT cAbs = window->FocusedControl->GetAbsolutePos();
  194. RECT controlRect = { cAbs.x, cAbs.y, window->FocusedControl->m_iWidth, window->FocusedControl->m_iHeight };
  195. window->FocusedControl->OnUpdate();
  196.  
  197. if (window->FocusedControl->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
  198. {
  199. window->FocusedControl->OnClick();
  200.  
  201. // If it gets clicked we loose focus
  202. window->IsFocusingControl = false;
  203. window->FocusedControl = nullptr;
  204. bCheckWidgetClicks = false;
  205. }
  206. }
  207. }
  208. }
  209.  
  210. // Itterate over the rest of the control
  211. for (auto control : window->SelectedTab->Controls)
  212. {
  213. if (control != nullptr)
  214. {
  215. CGroupBox* group;
  216. if (control->FileControlType != UIControlTypes::UIC_GroupBox) group = control->parent_group ? (CGroupBox*)control->parent_group : nullptr;
  217.  
  218. if (group != nullptr && control->FileControlType != UIControlTypes::UIC_GroupBox)
  219. {
  220. if (group->group_tabs.size() > 0 && control->g_tab == group->selected_tab || group->group_tabs.size() == 0)
  221. {
  222. if (SkipWidget && SkipMe == control)
  223. continue;
  224.  
  225. POINT cAbs = control->GetAbsolutePos();
  226. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  227. control->OnUpdate();
  228.  
  229. if (control->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
  230. {
  231. control->OnClick();
  232. bCheckWidgetClicks = false;
  233.  
  234. // Change of focus
  235. if (control->Flag(UIFlags::UI_Focusable))
  236. {
  237. window->IsFocusingControl = true;
  238. window->FocusedControl = control;
  239. }
  240. else
  241. {
  242. window->IsFocusingControl = false;
  243. window->FocusedControl = nullptr;
  244. }
  245. }
  246. }
  247. }
  248. else if (control->FileControlType == UIControlTypes::UIC_GroupBox || control->FileControlType != UIControlTypes::UIC_GroupBox && !control->parent_group)
  249. {
  250. if (SkipWidget && SkipMe == control)
  251. continue;
  252.  
  253. POINT cAbs = control->GetAbsolutePos();
  254. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  255. control->OnUpdate();
  256.  
  257. if (control->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
  258. {
  259. control->OnClick();
  260. bCheckWidgetClicks = false;
  261.  
  262. // Change of focus
  263. if (control->Flag(UIFlags::UI_Focusable))
  264. {
  265. window->IsFocusingControl = true;
  266. window->FocusedControl = control;
  267. }
  268. else
  269. {
  270. window->IsFocusingControl = false;
  271. window->FocusedControl = nullptr;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. // We must have clicked whitespace
  278. if (bCheckWidgetClicks)
  279. {
  280. // Loose focus on the control
  281. window->IsFocusingControl = false;
  282. window->FocusedControl = nullptr;
  283. }
  284. }
  285. }
  286. }
  287. }
  288.  
  289. // Returns
  290. bool CGUI::GetKeyPress(unsigned int key)
  291. {
  292. if (keys[key] == true && oldKeys[key] == false)
  293. return true;
  294. else
  295. return false;
  296. }
  297.  
  298. bool CGUI::GetKeyState(unsigned int key)
  299. {
  300. return keys[key];
  301. }
  302.  
  303. bool CGUI::IsMouseInRegion(int x, int y, int x2, int y2)
  304. {
  305. if (Mouse.x > x && Mouse.y > y && Mouse.x < x2 && Mouse.y < y2)
  306. return true;
  307. else
  308. return false;
  309. }
  310.  
  311. bool CGUI::IsMouseInRegion(RECT region)
  312. {
  313. return IsMouseInRegion(region.left, region.top, region.left + region.right, region.top + region.bottom);
  314. }
  315.  
  316. POINT CGUI::GetMouse()
  317. {
  318. return Mouse;
  319. }
  320.  
  321. bool CGUI::DrawWindow(CWindow* window, int menu)
  322. {
  323. if (menu == 1)
  324. {
  325. int _x = window->m_x + 8;
  326. int _tab_x = window->m_x + 8 - 90;
  327. int _y = window->m_y + 29 * 2;
  328. int _width = window->m_iWidth - 16;
  329. int _height = window->m_iHeight - 236;
  330.  
  331. //Inner
  332. draw.rect(_tab_x, _y, _width + 90, _height, Color(28, 28, 28, 255));
  333. draw.rect(_tab_x, _y - 6, _width + 90, 6, Color(40, 40, 40, 255));
  334. draw.rect(_tab_x, _y + _height, _width + 90, 6, Color(40, 40, 40, 255));
  335. draw.rect(_tab_x - 6, _y - 6, 6, _height + 12, Color(40, 40, 40, 255));
  336. draw.rect(_x + _width, _y - 6, 6, _height + 12, Color(40, 40, 40, 255));
  337.  
  338. //Tab
  339. draw.rect(_tab_x, _y, 90, _height, Color(21, 21, 19, 255));
  340.  
  341. draw.outline(_tab_x, _y, _width + 90, _height, Color(48, 48, 48, 255));
  342. draw.outline(_x - 6 - 90, _y - 6, _width + 90 + 12, _height + 12, Color(48, 48, 48, 255));
  343. draw.outline(_tab_x, _y, 90, _height, Color(48, 48, 48, 255));
  344.  
  345. int iProportion = _width + 90 - 2;
  346. iProportion = iProportion / 4;
  347. draw.gradient_horizontal(_tab_x + 1, _y + 1, iProportion, 1, Color(30, 220, 255, 255), Color(147, 112, 219, 255));
  348. draw.gradient_horizontal(_tab_x + 1 + iProportion, _y + 1, iProportion, 1, Color(147, 112, 219, 255), Color(255, 105, 180, 255));
  349. draw.gradient_horizontal(_tab_x + 1 + (iProportion * 2), _y + 1, iProportion, 1, Color(255, 105, 180, 255), Color(250, 128, 114, 255));
  350. draw.gradient_horizontal(_tab_x + 1 + (iProportion * 3), _y + 1, iProportion, 1, Color(250, 128, 114, 255), Color(124, 252, 0, 255));
  351.  
  352. // we are using the dimensions of the picture we got from Photoshop
  353.  
  354. int tabcount = window->Tabs.size();
  355. if (tabcount) // If there are some tabs
  356. {
  357. bool isOut = true;
  358.  
  359. for (int i = 0; i < tabcount; i++)
  360. {
  361. CTab *tab = window->Tabs[i];
  362.  
  363. float xAxis;
  364. float yAxis;
  365. float yWinPos = _y;
  366. float yWinHeight = _height;
  367.  
  368. float intercept = (yWinHeight - 40) / (tabcount + 1);
  369. int factor = i;
  370.  
  371. yAxis = yWinPos + 16 + (factor * intercept) - 10 + 20;
  372.  
  373. RECT TabDrawArea = { _tab_x + 1, yAxis - 5, 89, intercept };
  374.  
  375. RECT TextSize;
  376. TextSize = draw.get_text_size(tab->Title.c_str(), draw.fonts.font_icons);
  377.  
  378. RECT ClickTabArea = { xAxis,
  379. yAxis,
  380. TextSize.right,
  381. TextSize.bottom };
  382.  
  383. if (GetAsyncKeyState(VK_LBUTTON))
  384. {
  385. if (IsMouseInRegion(TabDrawArea))
  386. {
  387. window->SelectedTab = window->Tabs[i];
  388. window->IsFocusingControl = false;
  389. window->FocusedControl = nullptr;
  390. }
  391. }
  392.  
  393. xAxis = _x - (45 + TextSize.right / 2);
  394. if (IsMouseInRegion(TabDrawArea) && window->SelectedTab != tab) {
  395. draw.text(TabDrawArea.left + (TabDrawArea.right / 2) - (TextSize.right / 2), TabDrawArea.top + (TabDrawArea.bottom / 2) - (TextSize.bottom / 2), tab->Title.c_str(), draw.fonts.font_icons, Color(190, 190, 190, 255));
  396. }
  397. else if (window->SelectedTab == tab) {
  398. draw.rect(TabDrawArea.left, TabDrawArea.top, TabDrawArea.right, TabDrawArea.bottom, Color(28, 28, 28, 255));
  399. draw.line(TabDrawArea.left, TabDrawArea.top, TabDrawArea.left + TabDrawArea.right, TabDrawArea.top, Color(48, 48, 48, 255));
  400. draw.line(TabDrawArea.left, TabDrawArea.top + TabDrawArea.bottom, TabDrawArea.left + TabDrawArea.right, TabDrawArea.top + TabDrawArea.bottom, Color(48, 48, 48, 255));
  401. draw.text(TabDrawArea.left + (TabDrawArea.right / 2) - (TextSize.right / 2), TabDrawArea.top + (TabDrawArea.bottom / 2) - (TextSize.bottom / 2), tab->Title.c_str(), draw.fonts.font_icons, Color(200, 200, 200, 255));
  402. }
  403. else {
  404. draw.text(TabDrawArea.left + (TabDrawArea.right / 2) - (TextSize.right / 2), TabDrawArea.top + (TabDrawArea.bottom / 2) - (TextSize.bottom / 2), tab->Title.c_str(), draw.fonts.font_icons, Color(92, 92, 92, 255));
  405. }
  406.  
  407. int width = _width;
  408. }
  409. }
  410. }
  411.  
  412. // Controls
  413. if (window->SelectedTab != nullptr)
  414. {
  415. // Focused widget
  416. bool SkipWidget = false;
  417. CControl* SkipMe = nullptr;
  418.  
  419. // this window is focusing on a widget??
  420. if (window->IsFocusingControl)
  421. {
  422. if (window->FocusedControl != nullptr)
  423. {
  424. CControl* control = window->FocusedControl;
  425. CGroupBox* group;
  426. if (control->FileControlType != UIControlTypes::UIC_GroupBox) group = control->parent_group ? (CGroupBox*)control->parent_group : nullptr;
  427.  
  428. if (group != nullptr && control->FileControlType != UIControlTypes::UIC_GroupBox)
  429. {
  430. if (group->group_tabs.size() > 0 && control->g_tab == group->selected_tab || group->group_tabs.size() == 0)
  431. {
  432. SkipWidget = true;
  433. SkipMe = window->FocusedControl;
  434. }
  435. }
  436. else if (control->FileControlType == UIControlTypes::UIC_GroupBox || control->FileControlType != UIControlTypes::UIC_GroupBox && !control->parent_group)
  437. {
  438.  
  439. SkipWidget = true;
  440. SkipMe = window->FocusedControl;
  441. }
  442. }
  443. }
  444.  
  445.  
  446. // Itterate over all the other controls
  447. for (auto control : window->SelectedTab->Controls)
  448. {
  449. if (SkipWidget && SkipMe == control)
  450. continue;
  451.  
  452. if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
  453. {
  454. CGroupBox* group;
  455. if (control->FileControlType != UIControlTypes::UIC_GroupBox) group = control->parent_group ? (CGroupBox*)control->parent_group : nullptr;
  456.  
  457. if (group != nullptr && control->FileControlType != UIControlTypes::UIC_GroupBox)
  458. {
  459. if (group->group_tabs.size() > 0 && control->g_tab == group->selected_tab || group->group_tabs.size() == 0)
  460. {
  461. POINT cAbs = control->GetAbsolutePos();
  462. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  463. bool hover = false;
  464. if (IsMouseInRegion(controlRect))
  465. {
  466. hover = true;
  467. }
  468. control->Draw(hover);
  469. }
  470. }
  471. else if (control->FileControlType == UIControlTypes::UIC_GroupBox || control->FileControlType != UIControlTypes::UIC_GroupBox && !control->parent_group)
  472. {
  473.  
  474. POINT cAbs = control->GetAbsolutePos();
  475. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  476. bool hover = false;
  477. if (IsMouseInRegion(controlRect))
  478. {
  479. hover = true;
  480. }
  481. control->Draw(hover);
  482. }
  483. }
  484. }
  485.  
  486. // Draw the skipped widget last
  487. if (SkipWidget)
  488. {
  489. auto control = window->FocusedControl;
  490.  
  491. if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
  492. {
  493. CControl* control = window->FocusedControl;
  494. CGroupBox* group;
  495. if (control->FileControlType != UIControlTypes::UIC_GroupBox) group = control->parent_group ? (CGroupBox*)control->parent_group : nullptr;
  496.  
  497. if (group != nullptr && control->FileControlType != UIControlTypes::UIC_GroupBox)
  498. {
  499. if (group->group_tabs.size() > 0 && control->g_tab == group->selected_tab || group->group_tabs.size() == 0)
  500. {
  501. POINT cAbs = control->GetAbsolutePos();
  502. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  503. bool hover = false;
  504. if (IsMouseInRegion(controlRect))
  505. {
  506. hover = true;
  507. }
  508. control->Draw(hover);
  509. }
  510. }
  511. else if (control->FileControlType == UIControlTypes::UIC_GroupBox || control->FileControlType != UIControlTypes::UIC_GroupBox && !control->parent_group)
  512. {
  513.  
  514. POINT cAbs = control->GetAbsolutePos();
  515. RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
  516. bool hover = false;
  517. if (IsMouseInRegion(controlRect))
  518. {
  519. hover = true;
  520. }
  521. control->Draw(hover);
  522. }
  523. }
  524. }
  525.  
  526. }
  527.  
  528.  
  529. return true;
  530. }
  531.  
  532. void CGUI::RegisterWindow(CWindow* window)
  533. {
  534. Windows.push_back(window);
  535.  
  536. // Resorting to put groupboxes at the start
  537. for (auto tab : window->Tabs)
  538. {
  539. for (auto control : tab->Controls)
  540. {
  541. if (control->Flag(UIFlags::UI_RenderFirst))
  542. {
  543. CControl * c = control;
  544. tab->Controls.erase(std::remove(tab->Controls.begin(), tab->Controls.end(), control), tab->Controls.end());
  545. tab->Controls.insert(tab->Controls.begin(), control);
  546. }
  547. }
  548. }
  549. }
  550.  
  551. void CGUI::BindWindow(unsigned char Key, CWindow* window)
  552. {
  553. if (window)
  554. WindowBinds[Key] = window;
  555. else
  556. WindowBinds.erase(Key);
  557. }
  558.  
  559. void CGUI::SaveWindowState(CWindow* window, std::string Filename)
  560. {
  561. // Create a whole new document and we'll just save over top of the old one
  562. tinyxml2::XMLDocument Doc;
  563.  
  564. // Root Element is called "ayy"
  565. tinyxml2::XMLElement *Root = Doc.NewElement("Skol0");
  566. Doc.LinkEndChild(Root);
  567.  
  568. // If the window has some tabs..
  569. if (Root && window->Tabs.size() > 0)
  570. {
  571. for (auto Tab : window->Tabs)
  572. {
  573. // Add a new section for this tab
  574. tinyxml2::XMLElement *TabElement = Doc.NewElement(Tab->Title.c_str());
  575. Root->LinkEndChild(TabElement);
  576.  
  577. // Now we itterate the controls this tab contains
  578. if (TabElement && Tab->Controls.size() > 0)
  579. {
  580. for (auto Control : Tab->Controls)
  581. {
  582. // If the control is ok to be saved
  583. if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
  584. {
  585. // Create an element for the control
  586. tinyxml2::XMLElement *ControlElement = Doc.NewElement(Control->FileIdentifier.c_str());
  587. TabElement->LinkEndChild(ControlElement);
  588.  
  589. if (!ControlElement)
  590. {
  591. return;
  592. }
  593.  
  594. CCheckBox* cbx = nullptr;
  595. CComboBox* cbo = nullptr;
  596. CKeyBind* key = nullptr;
  597. CSlider* sld = nullptr;
  598. CItemSelector* itms = nullptr;
  599. CMultiBox* mtbx = nullptr;
  600. CListBox* lsbox = nullptr;
  601. CColorSelector* clse = nullptr;
  602.  
  603. // Figure out what kind of control and data this is
  604. switch (Control->FileControlType)
  605. {
  606. case UIControlTypes::UIC_CheckBox:
  607. cbx = (CCheckBox*)Control;
  608. ControlElement->SetText(cbx->GetState());
  609. break;
  610. case UIControlTypes::UIC_ComboBox:
  611. cbo = (CComboBox*)Control;
  612. ControlElement->SetText(cbo->GetIndex());
  613. break;
  614. case UIControlTypes::UIC_KeyBind:
  615. key = (CKeyBind*)Control;
  616. ControlElement->SetText(key->GetKey());
  617. break;
  618. case UIControlTypes::UIC_Slider:
  619. sld = (CSlider*)Control;
  620. ControlElement->SetText(sld->GetValue());
  621. break;
  622. case UIControlTypes::UIC_ItemSelector:
  623. itms = (CItemSelector*)Control;
  624. ControlElement->SetText(itms->GetInt());
  625. break;
  626. case UIControlTypes::UIC_ListBox:
  627. lsbox = (CListBox*)Control;
  628. ControlElement->SetText(lsbox->GetIndex());
  629. break;
  630. case UIControlTypes::UIC_MultiBox:
  631. {
  632. mtbx = (CMultiBox*)Control;
  633. std::string x;
  634. for (int i = 0; i < mtbx->items.size(); i++)
  635. {
  636. std::string status;
  637. status = mtbx->items[i].bselected ? "1" : "0";
  638. x = x + status;
  639. }
  640. ControlElement->SetText(x.c_str());
  641. break;
  642. }
  643. case UIControlTypes::UIC_ColorSelector:
  644. clse = (CColorSelector*)Control;
  645. char buffer[128];
  646. float r, g, b, a;
  647. r = clse->GetValue()[0];
  648. g = clse->GetValue()[1];
  649. b = clse->GetValue()[2];
  650. a = clse->GetValue()[3];
  651. sprintf_s(buffer, "%1.f %1.f %1.f %1.f", r, g, b, a);
  652. ControlElement->SetText(buffer);
  653. break;
  654. }
  655. }
  656. }
  657. }
  658. }
  659. }
  660.  
  661. //Save the file
  662. if (Doc.SaveFile(Filename.c_str()) != tinyxml2::XML_NO_ERROR)
  663. {
  664. MessageBox(NULL, "Failed To Save Config File!", "Skol0mc", MB_OK);
  665. }
  666.  
  667. }
  668.  
  669. void CGUI::LoadWindowState(CWindow* window, std::string Filename)
  670. {
  671. // Lets load our meme
  672. tinyxml2::XMLDocument Doc;
  673. if (Doc.LoadFile(Filename.c_str()) == tinyxml2::XML_NO_ERROR)
  674. {
  675. tinyxml2::XMLElement *Root = Doc.RootElement();
  676.  
  677. // The root "ayy" element
  678. if (Root)
  679. {
  680. // If the window has some tabs..
  681. if (Root && window->Tabs.size() > 0)
  682. {
  683. for (auto Tab : window->Tabs)
  684. {
  685. // We find the corresponding element for this tab
  686. tinyxml2::XMLElement *TabElement = Root->FirstChildElement(Tab->Title.c_str());
  687. if (TabElement)
  688. {
  689. // Now we itterate the controls this tab contains
  690. if (TabElement && Tab->Controls.size() > 0)
  691. {
  692. for (auto Control : Tab->Controls)
  693. {
  694. // If the control is ok to be saved
  695. if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
  696. {
  697. // Get the controls element
  698. tinyxml2::XMLElement *ControlElement = TabElement->FirstChildElement(Control->FileIdentifier.c_str());
  699.  
  700. if (ControlElement)
  701. {
  702. CCheckBox* cbx = nullptr;
  703. CComboBox* cbo = nullptr;
  704. CKeyBind* key = nullptr;
  705. CSlider* sld = nullptr;
  706. CItemSelector* itms = nullptr;
  707. CMultiBox* mtbx = nullptr;
  708. CListBox* lsbox = nullptr;
  709. CColorSelector* clse = nullptr;
  710.  
  711. // Figure out what kind of control and data this is
  712. switch (Control->FileControlType)
  713. {
  714. case UIControlTypes::UIC_CheckBox:
  715. cbx = (CCheckBox*)Control;
  716. cbx->SetState(ControlElement->GetText()[0] == '1' ? true : false);
  717. break;
  718. case UIControlTypes::UIC_ComboBox:
  719. cbo = (CComboBox*)Control;
  720. cbo->SelectIndex(atoi(ControlElement->GetText()));
  721. break;
  722. case UIControlTypes::UIC_KeyBind:
  723. key = (CKeyBind*)Control;
  724. key->SetKey(atoi(ControlElement->GetText()));
  725. break;
  726. case UIControlTypes::UIC_Slider:
  727. sld = (CSlider*)Control;
  728. sld->SetValue(atof(ControlElement->GetText()));
  729. break;
  730. case UIControlTypes::UIC_ItemSelector:
  731. itms = (CItemSelector*)Control;
  732. itms->SetValue(atof(ControlElement->GetText()));
  733. break;
  734. case UIControlTypes::UIC_ListBox:
  735. lsbox = (CListBox*)Control;
  736. lsbox->SelectItem(atoi(ControlElement->GetText()));
  737. break;
  738. case UIControlTypes::UIC_MultiBox:
  739. mtbx = (CMultiBox*)Control;
  740. for (int i = 0; i < mtbx->items.size(); i++)
  741. {
  742. mtbx->items[i].bselected = ControlElement->GetText()[i] == '1' ? true : false;
  743. }
  744. break;
  745. case UIControlTypes::UIC_ColorSelector:
  746. clse = (CColorSelector*)Control;
  747. int r, g, b, a;
  748. std::stringstream ss(ControlElement->GetText());
  749. ss >> r >> g >> b >> a;
  750. clse->SetColor(r, g, b, a);
  751. break;
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement