Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "IGUIScrollBar.h" 21 : 22 : #include "gui/CGUI.h" 23 : #include "gui/ObjectBases/IGUIScrollBarOwner.h" 24 : #include "gui/SGUIMessage.h" 25 : #include "maths/MathUtil.h" 26 : #include "maths/Rect.h" 27 : 28 0 : IGUIScrollBar::IGUIScrollBar(CGUI& pGUI) 29 0 : : m_pGUI(pGUI), 30 0 : m_pStyle(nullptr), 31 0 : m_X(300.f), m_Y(300.f), 32 0 : m_ScrollRange(1.f), m_ScrollSpace(0.f), // MaxPos: not 0, due to division. 33 0 : m_Length(200.f), m_Width(20.f), 34 0 : m_BarSize(0.f), m_Pos(0.f), 35 0 : m_ButtonPlusPressed(false), 36 0 : m_ButtonMinusPressed(false), 37 0 : m_ButtonPlusHovered(false), 38 0 : m_ButtonMinusHovered(false), 39 0 : m_BarHovered(false), 40 0 : m_BarPressed(false) 41 : { 42 0 : } 43 : 44 0 : IGUIScrollBar::~IGUIScrollBar() 45 : { 46 0 : } 47 : 48 0 : void IGUIScrollBar::SetupBarSize() 49 : { 50 0 : if (!GetStyle()) 51 0 : return; 52 : 53 0 : float min = GetStyle()->m_MinimumBarSize; 54 0 : float max = GetStyle()->m_MaximumBarSize; 55 0 : float length = m_Length; 56 : 57 : // Check for edge buttons 58 0 : if (GetStyle()->m_UseEdgeButtons) 59 0 : length -= GetStyle()->m_Width * 2.f; 60 : 61 : // Check min and max are valid 62 0 : if (min > length) 63 0 : min = 0.f; 64 0 : if (max < min) 65 0 : max = length; 66 : 67 : // Clamp size to not exceed a minimum or maximum. 68 0 : m_BarSize = Clamp(length * std::min(m_ScrollSpace / m_ScrollRange, 1.f), min, max); 69 : } 70 : 71 0 : const SGUIScrollBarStyle* IGUIScrollBar::GetStyle() const 72 : { 73 0 : if (!m_pHostObject) 74 : return nullptr; 75 : 76 0 : return m_pHostObject->GetScrollBarStyle(m_ScrollBarStyle); 77 : } 78 : 79 0 : void IGUIScrollBar::UpdatePosBoundaries() 80 : { 81 0 : if (m_Pos < 0.f || 82 0 : m_ScrollRange < m_ScrollSpace) // <= scrolling not applicable 83 0 : m_Pos = 0.f; 84 0 : else if (m_Pos > GetMaxPos()) 85 0 : m_Pos = GetMaxPos(); 86 0 : } 87 : 88 0 : void IGUIScrollBar::HandleMessage(SGUIMessage& Message) 89 : { 90 0 : switch (Message.type) 91 : { 92 0 : case GUIM_MOUSE_MOTION: 93 0 : { 94 : // TODO Gee: Optimizations needed! 95 : 96 0 : const CVector2D& mouse = m_pGUI.GetMousePos(); 97 : 98 : // If bar is being dragged 99 0 : if (m_BarPressed) 100 : { 101 0 : SetPosFromMousePos(mouse); 102 0 : UpdatePosBoundaries(); 103 : } 104 : 105 : // check if components are being hovered 106 0 : m_BarHovered = GetBarRect().PointInside(mouse); 107 0 : m_ButtonMinusHovered = HoveringButtonMinus(mouse); 108 0 : m_ButtonPlusHovered = HoveringButtonPlus(mouse); 109 : 110 0 : if (!m_ButtonMinusHovered) 111 0 : m_ButtonMinusPressed = false; 112 : 113 0 : if (!m_ButtonPlusHovered) 114 0 : m_ButtonPlusPressed = false; 115 : break; 116 : } 117 : 118 0 : case GUIM_MOUSE_PRESS_LEFT: 119 0 : { 120 0 : if (!m_pHostObject) 121 : break; 122 : 123 0 : const CVector2D& mouse = m_pGUI.GetMousePos(); 124 : 125 : // if bar is pressed 126 0 : if (GetBarRect().PointInside(mouse)) 127 : { 128 0 : m_BarPressed = true; 129 0 : m_BarPressedAtPos = mouse; 130 0 : m_PosWhenPressed = m_Pos; 131 : } 132 : // if button-minus is pressed 133 0 : else if (m_ButtonMinusHovered) 134 : { 135 0 : m_ButtonMinusPressed = true; 136 0 : ScrollMinus(); 137 : } 138 : // if button-plus is pressed 139 0 : else if (m_ButtonPlusHovered) 140 : { 141 0 : m_ButtonPlusPressed = true; 142 0 : ScrollPlus(); 143 : } 144 : // Pressing the background of the bar, to scroll 145 : // notice the if-sentence alone does not admit that, 146 : // it must be after the above if/elses 147 : else 148 : { 149 0 : if (GetOuterRect().PointInside(mouse)) 150 : { 151 0 : SetScrollPlentyFromMousePos(mouse); 152 : 153 : // Simulate mouse movement to see if bar now is hovered 154 0 : SGUIMessage msg(GUIM_MOUSE_MOTION); 155 0 : HandleMessage(msg); 156 0 : } 157 : } 158 : break; 159 : } 160 : 161 0 : case GUIM_MOUSE_RELEASE_LEFT: 162 0 : m_ButtonMinusPressed = false; 163 0 : m_ButtonPlusPressed = false; 164 0 : break; 165 : default: 166 : break; 167 : } 168 0 : }