Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CGUI GUI;
- float MenuAlpha = 0.f;
- float Globals::MenuAlpha2 = MenuAlpha;
- CGUI::CGUI()
- {
- }
- // Draws all windows
- void CGUI::Draw()
- {
- bool ShouldDrawCursor = false;
- for (auto window : Windows)
- {
- if (window->m_bIsOpen)
- {
- ShouldDrawCursor = true;
- DrawWindow(window);
- }
- }
- if (ShouldDrawCursor)
- {
- Render::Clear(Mouse.x + 1, Mouse.y, 1, 17, Color(3, 6, 26, 255));
- for (int i = 0; i < 11; i++)
- Render::Clear(Mouse.x + 2 + i, Mouse.y + 1 + i, 1, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 8, Mouse.y + 12, 5, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 8, Mouse.y + 13, 1, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 9, Mouse.y + 14, 1, 2, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 10, Mouse.y + 16, 1, 2, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 8, Mouse.y + 18, 2, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 7, Mouse.y + 16, 1, 2, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 6, Mouse.y + 14, 1, 2, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 5, Mouse.y + 13, 1, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 4, Mouse.y + 14, 1, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 3, Mouse.y + 15, 1, 1, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 2, Mouse.y + 16, 1, 1, Color(30, 200, 225, 255));
- for (int i = 0; i < 4; i++)
- Render::Clear(Mouse.x + 2 + i, Mouse.y + 2 + i, 1, 14 - (i * 2), Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 6, Mouse.y + 6, 1, 8, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 7, Mouse.y + 7, 1, 9, Color(30, 200, 225, 255));
- for (int i = 0; i < 4; i++)
- Render::Clear(Mouse.x + 8 + i, Mouse.y + 8 + i, 1, 4 - i, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 8, Mouse.y + 14, 1, 4, Color(30, 200, 225, 255));
- Render::Clear(Mouse.x + 9, Mouse.y + 16, 1, 2, Color(30, 200, 225, 255));
- }
- }
- // Handle all input etc
- void CGUI::Update()
- {
- //Key Arra
- std::copy(keys, keys + 255, oldKeys);
- for (int x = 0; x < 255; x++)
- {
- //oldKeys[x] = oldKeys[x] & keys[x];
- keys[x] = (GetAsyncKeyState(x));
- }
- // Mouse Location
- POINT mp; GetCursorPos(&mp);
- Mouse.x = mp.x; Mouse.y = mp.y;
- RECT Screen = Render::GetViewport();
- // Window Binds
- for (auto& bind : WindowBinds)
- {
- if (GetKeyPress(bind.first))
- {
- bind.second->Toggle();
- }
- }
- // Stop dragging
- if (IsDraggingWindow && !GetKeyState(VK_LBUTTON))
- {
- IsDraggingWindow = false;
- DraggingWindow = nullptr;
- }
- // If we are in the proccess of dragging a window
- if (IsDraggingWindow && GetKeyState(VK_LBUTTON) && !GetKeyPress(VK_LBUTTON))
- {
- if (DraggingWindow)
- {
- DraggingWindow->m_x = Mouse.x - DragOffsetX;
- DraggingWindow->m_y = Mouse.y - DragOffsetY;
- }
- }
- // Process some windows
- for (auto window : Windows)
- {
- if (window->m_bIsOpen)
- MenuAlpha = min(MenuAlpha + 6, 255);
- else
- MenuAlpha = max(MenuAlpha - 6, 0);
- {
- // Used to tell the widget processing that there could be a click
- bool bCheckWidgetClicks = false;
- // If the user clicks inside the window
- if (GetKeyPress(VK_LBUTTON))
- {
- bCheckWidgetClicks = false;
- if (IsMouseInRegion(window->m_y, window->m_x + window->m_iWidth, window->m_y + window->m_iHeight, window->m_x))
- {
- // Close Button
- 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))
- {
- window->Toggle();
- }
- else
- // User is selecting a new tab
- if (IsMouseInRegion(window->GetTabArea()))
- {
- bCheckWidgetClicks = true;
- int iTab = 0;
- int TabCount = window->Tabs.size();
- if (TabCount) // If there are some tabs
- {
- int TabSize = UI_TAB_HEIGHT;
- int Dist = Mouse.y - (window->m_y + UI_WIN_TITLEHEIGHT + UI_WIN_TOPHEIGHT);
- if (Dist < (UI_TAB_HEIGHT*TabCount))
- {
- while (Dist > TabSize)
- {
- if (Dist > TabSize)
- {
- iTab++;
- Dist -= TabSize;
- }
- if (iTab == (TabCount - 1))
- {
- break;
- }
- }
- window->SelectedTab = window->Tabs[iTab];
- // Loose focus on the control
- bCheckWidgetClicks = false;
- window->IsFocusingControl = false;
- window->FocusedControl = nullptr;
- }
- }
- }
- // Is it inside the client area?
- else if (IsMouseInRegion(window->GetClientArea()))
- {
- bCheckWidgetClicks = true;
- }
- else
- {
- // Must be in the around the title or side of the window
- // So we assume the user is trying to drag the window
- IsDraggingWindow = true;
- DraggingWindow = window;
- DragOffsetX = Mouse.x - window->m_x;
- DragOffsetY = Mouse.y - window->m_y;
- // Loose focus on the control
- window->IsFocusingControl = false;
- window->FocusedControl = nullptr;
- }
- }
- else
- {
- // Loose focus on the control
- window->IsFocusingControl = false;
- window->FocusedControl = nullptr;
- }
- }
- // Controls
- if (window->SelectedTab != nullptr)
- {
- // Focused widget
- bool SkipWidget = false;
- CControl* SkipMe = nullptr;
- // this window is focusing on a widget??
- if (window->IsFocusingControl)
- {
- if (window->FocusedControl != nullptr)
- {
- // We've processed it once, skip it later
- SkipWidget = true;
- SkipMe = window->FocusedControl;
- POINT cAbs = window->FocusedControl->GetAbsolutePos();
- RECT controlRect = { cAbs.x, cAbs.y, window->FocusedControl->m_iWidth, window->FocusedControl->m_iHeight };
- window->FocusedControl->OnUpdate();
- if (window->FocusedControl->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
- {
- window->FocusedControl->OnClick();
- // If it gets clicked we loose focus
- window->IsFocusingControl = false;
- window->FocusedControl = nullptr;
- bCheckWidgetClicks = false;
- }
- }
- }
- for (auto control : window->SelectedTab->Controls)
- {
- if (control != nullptr)
- {
- if (SkipWidget && SkipMe == control)
- continue;
- control->parent = window;
- POINT cAbs = control->GetAbsolutePos();
- RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
- control->OnUpdate();
- if (control->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
- {
- control->OnClick();
- bCheckWidgetClicks = false;
- // Change of focus
- if (control->Flag(UIFlags::UI_Focusable))
- {
- window->IsFocusingControl = true;
- window->FocusedControl = control;
- }
- else
- {
- window->IsFocusingControl = false;
- window->FocusedControl = nullptr;
- }
- }
- }
- }
- // We must have clicked whitespace
- if (bCheckWidgetClicks)
- {
- // Loose focus on the control
- window->IsFocusingControl = false;
- window->FocusedControl = nullptr;
- }
- }
- }
- }
- }
- // Returns
- bool CGUI::IsMouseInRegion(int y, int x2, int y2, int x)
- {
- if (Mouse.x > x && Mouse.y > y && Mouse.x < x2 && Mouse.y < y2)
- return true;
- else
- return false;
- }
- bool CGUI::IsMouseInRegion(RECT region)
- {
- return IsMouseInRegion(region.top, region.left + region.right, region.top + region.bottom, region.left);
- }
- POINT CGUI::GetMouse()
- {
- return Mouse;
- }
- bool CGUI::DrawWindow(CWindow* window)
- {
- Render::Clear(window->m_x, window->m_y, window->m_iWidth, window->m_iHeight, Color(25, 25, 25, 254));
- //Inner
- //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));
- //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));
- Render::Outline(window->m_x, window->m_y, window->m_iWidth, window->m_iHeight, Color(0, 0, 0, 255));
- Render::Outline(window->m_x + 1, window->m_y + 1, window->m_iWidth - 2, window->m_iHeight - 2, Color(65, 65, 65, 254));
- Render::Outline(window->m_x + 2, window->m_y + 2, window->m_iWidth - 4, window->m_iHeight - 4, Color(45, 45, 45, 254));
- Render::Outline(window->m_x + 3, window->m_y + 3, window->m_iWidth - 6, window->m_iHeight - 6, Color(45, 45, 45, 254));
- Render::Outline(window->m_x + 4, window->m_y + 4, window->m_iWidth - 8, window->m_iHeight - 8, Color(45, 45, 45, 254));
- Render::Outline(window->m_x + 5, window->m_y + 5, window->m_iWidth - 10, window->m_iHeight - 10, Color(65, 65, 65, 254));
- int TabCount = window->Tabs.size();
- if (TabCount) // If there are some tabs
- {
- for (int i = 0; i < TabCount; i++)
- {
- RECT TabArea = { window->m_x, window->m_y + UI_WIN_TITLEHEIGHT + UI_WIN_TOPHEIGHT + (i*UI_TAB_HEIGHT) , UI_TAB_WIDTH, UI_TAB_HEIGHT };
- CTab *tab = window->Tabs[i];
- Color txtColor = UI_COL_SHADOW;
- if (window->SelectedTab == tab)
- {
- // Selected
- txtColor = UI_COL_TABSEPERATOR;
- }
- else if (IsMouseInRegion(TabArea))
- {
- // Hover
- txtColor = Color(220, 220, 220, 255);
- }
- Render::Text(TabArea.left + 15, TabArea.top + 8, txtColor, Render::Fonts::Tabs, tab->Title.c_str());
- //Render::Clear(window->m_x + 8, window->m_y + 1 + 27, window->m_iWidth - 4 - 12, 2, Color(150, 0, 0, 255));
- }
- }
- // Controls
- if (window->SelectedTab != nullptr)
- {
- // Focused widget
- bool SkipWidget = false;
- CControl* SkipMe = nullptr;
- // this window is focusing on a widget??
- if (window->IsFocusingControl)
- {
- if (window->FocusedControl != nullptr)
- {
- // We need to draw it last, so skip it in the regular loop
- SkipWidget = true;
- SkipMe = window->FocusedControl;
- }
- }
- // Itterate over all the other controls
- for (auto control : window->SelectedTab->Controls)
- {
- if (SkipWidget && SkipMe == control)
- continue;
- if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
- {
- POINT cAbs = control->GetAbsolutePos();
- RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
- bool hover = false;
- if (IsMouseInRegion(controlRect))
- {
- hover = true;
- }
- control->Draw(hover);
- }
- }
- // Draw the skipped widget last
- if (SkipWidget)
- {
- auto control = window->FocusedControl;
- if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
- {
- POINT cAbs = control->GetAbsolutePos();
- RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
- bool hover = false;
- if (IsMouseInRegion(controlRect))
- {
- hover = true;
- }
- control->Draw(hover);
- }
- }
- }
- return true;
- }
- void CGUI::RegisterWindow(CWindow* window)
- {
- Windows.push_back(window);
- // Resorting to put groupboxes at the start
- for (auto tab : window->Tabs)
- {
- for (auto control : tab->Controls)
- {
- if (control->Flag(UIFlags::UI_RenderFirst))
- {
- CControl * c = control;
- tab->Controls.erase(std::remove(tab->Controls.begin(), tab->Controls.end(), control), tab->Controls.end());
- tab->Controls.insert(tab->Controls.begin(), control);
- }
- }
- }
- }
- void CGUI::BindWindow(unsigned char Key, CWindow* window)
- {
- if (window)
- WindowBinds[Key] = window;
- else
- WindowBinds.erase(Key);
- }
- void CGUI::SaveWindowState(CWindow* window, std::string Filename)
- {
- tinyxml2::XMLDocument Doc;
- tinyxml2::XMLElement *Root = Doc.NewElement("Rivotril");
- Doc.LinkEndChild(Root);
- if (Root && window->Tabs.size() > 0)
- {
- for (auto Tab : window->Tabs)
- {
- tinyxml2::XMLElement *TabElement = Doc.NewElement(Tab->Title.c_str());
- Root->LinkEndChild(TabElement);
- if (TabElement && Tab->Controls.size() > 1)
- {
- for (auto Control : Tab->Controls)
- {
- if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
- {
- tinyxml2::XMLElement *ControlElement = Doc.NewElement(Control->FileIdentifier.c_str());
- TabElement->LinkEndChild(ControlElement);
- if (!ControlElement)
- {
- return;
- }
- CCheckBox* cbx = nullptr;
- CComboBox* cbo = nullptr;
- CKeyBind* key = nullptr;
- CSlider* sld = nullptr;
- switch (Control->FileControlType)
- {
- case UIControlTypes::UIC_CheckBox:
- cbx = (CCheckBox*)Control;
- ControlElement->SetText(cbx->GetState());
- break;
- case UIControlTypes::UIC_ComboBox:
- cbo = (CComboBox*)Control;
- ControlElement->SetText(cbo->GetIndex());
- break;
- case UIControlTypes::UIC_KeyBind:
- key = (CKeyBind*)Control;
- ControlElement->SetText(key->GetKey());
- break;
- case UIControlTypes::UIC_Slider:
- sld = (CSlider*)Control;
- ControlElement->SetText(sld->GetValue());
- break;
- }
- }
- }
- }
- }
- }
- if (Doc.SaveFile(Filename.c_str()) != tinyxml2::XML_NO_ERROR)
- {
- MessageBox(NULL, "Failed To Save Config File!", "Rivotril", MB_OK);
- }
- }
- void CGUI::LoadWindowState(CWindow* window, std::string Filename)
- {
- tinyxml2::XMLDocument Doc;
- if (Doc.LoadFile(Filename.c_str()) == tinyxml2::XML_NO_ERROR)
- {
- tinyxml2::XMLElement *Root = Doc.RootElement();
- if (Root)
- {
- if (Root && window->Tabs.size() > 0)
- {
- for (auto Tab : window->Tabs)
- {
- tinyxml2::XMLElement *TabElement = Root->FirstChildElement(Tab->Title.c_str());
- if (TabElement)
- {
- if (TabElement && Tab->Controls.size() > 0)
- {
- for (auto Control : Tab->Controls)
- {
- if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
- {
- tinyxml2::XMLElement *ControlElement = TabElement->FirstChildElement(Control->FileIdentifier.c_str());
- if (ControlElement)
- {
- CCheckBox* cbx = nullptr;
- CComboBox* cbo = nullptr;
- CKeyBind* key = nullptr;
- CSlider* sld = nullptr;
- switch (Control->FileControlType)
- {
- case UIControlTypes::UIC_CheckBox:
- cbx = (CCheckBox*)Control;
- cbx->SetState(ControlElement->GetText()[0] == '1' ? true : false);
- break;
- case UIControlTypes::UIC_ComboBox:
- cbo = (CComboBox*)Control;
- cbo->SelectIndex(atoi(ControlElement->GetText()));
- break;
- case UIControlTypes::UIC_KeyBind:
- key = (CKeyBind*)Control;
- key->SetKey(atoi(ControlElement->GetText()));
- break;
- case UIControlTypes::UIC_Slider:
- sld = (CSlider*)Control;
- sld->SetValue(atof(ControlElement->GetText()));
- break;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment