Advertisement
weebwaredotnet

Untitled

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