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 "CGUIScrollBarVertical.h" 21 : 22 : #include "gui/CGUI.h" 23 : #include "gui/CGUISprite.h" 24 : #include "gui/IGUIScrollBar.h" 25 : #include "gui/SGUIMessage.h" 26 : #include "maths/Rect.h" 27 : #include "maths/Vector2D.h" 28 : #include "ps/CLogger.h" 29 : 30 0 : CGUIScrollBarVertical::CGUIScrollBarVertical(CGUI& pGUI) 31 0 : : IGUIScrollBar(pGUI) 32 : { 33 0 : } 34 : 35 0 : CGUIScrollBarVertical::~CGUIScrollBarVertical() 36 : { 37 0 : } 38 : 39 0 : void CGUIScrollBarVertical::SetPosFromMousePos(const CVector2D& mouse) 40 : { 41 0 : if (!GetStyle()) 42 : return; 43 : 44 : /** 45 : * Calculate the position for the top of the item being scrolled 46 : */ 47 0 : float emptyBackground = m_Length - m_BarSize; 48 : 49 0 : if (GetStyle()->m_UseEdgeButtons) 50 0 : emptyBackground -= GetStyle()->m_Width * 2; 51 : 52 0 : m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.Y - m_BarPressedAtPos.Y) / emptyBackground; 53 : } 54 : 55 0 : void CGUIScrollBarVertical::Draw(CCanvas2D& canvas) 56 : { 57 0 : if (!GetStyle()) 58 : { 59 0 : LOGWARNING("Attempt to draw scrollbar without a style."); 60 0 : return; 61 : } 62 : 63 0 : if (IsVisible()) 64 : { 65 0 : CRect outline = GetOuterRect(); 66 : 67 0 : m_pGUI.DrawSprite( 68 0 : GetStyle()->m_SpriteBackVertical, 69 : canvas, 70 0 : CRect( 71 : outline.left, 72 0 : outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0), 73 : outline.right, 74 0 : outline.bottom - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0) 75 : ) 76 : ); 77 : 78 0 : if (GetStyle()->m_UseEdgeButtons) 79 : { 80 0 : const CGUISpriteInstance* button_top; 81 0 : const CGUISpriteInstance* button_bottom; 82 : 83 0 : if (m_ButtonMinusHovered) 84 : { 85 0 : if (m_ButtonMinusPressed) 86 0 : button_top = &(GetStyle()->m_SpriteButtonTopPressed ? GetStyle()->m_SpriteButtonTopPressed : GetStyle()->m_SpriteButtonTop); 87 : else 88 0 : button_top = &(GetStyle()->m_SpriteButtonTopOver ? GetStyle()->m_SpriteButtonTopOver : GetStyle()->m_SpriteButtonTop); 89 : } 90 : else 91 0 : button_top = &GetStyle()->m_SpriteButtonTop; 92 : 93 0 : if (m_ButtonPlusHovered) 94 : { 95 0 : if (m_ButtonPlusPressed) 96 0 : button_bottom = &(GetStyle()->m_SpriteButtonBottomPressed ? GetStyle()->m_SpriteButtonBottomPressed : GetStyle()->m_SpriteButtonBottom); 97 : else 98 0 : button_bottom = &(GetStyle()->m_SpriteButtonBottomOver ? GetStyle()->m_SpriteButtonBottomOver : GetStyle()->m_SpriteButtonBottom); 99 : } 100 : else 101 0 : button_bottom = &GetStyle()->m_SpriteButtonBottom; 102 : 103 0 : m_pGUI.DrawSprite( 104 : *button_top, 105 : canvas, 106 0 : CRect( 107 : outline.left, 108 : outline.top, 109 : outline.right, 110 0 : outline.top+GetStyle()->m_Width 111 : ) 112 : ); 113 : 114 0 : m_pGUI.DrawSprite( 115 : *button_bottom, 116 : canvas, 117 0 : CRect( 118 : outline.left, 119 0 : outline.bottom-GetStyle()->m_Width, 120 : outline.right, 121 : outline.bottom 122 : ) 123 : ); 124 : } 125 : 126 0 : m_pGUI.DrawSprite( 127 0 : GetStyle()->m_SpriteSliderVertical, 128 : canvas, 129 0 : GetBarRect() 130 : ); 131 : } 132 : } 133 : 134 0 : void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message) 135 : { 136 0 : switch (Message.type) 137 : { 138 0 : case GUIM_MOUSE_WHEEL_UP: 139 0 : { 140 0 : ScrollMinus(); 141 : // Since the scroll was changed, let's simulate a mouse movement 142 : // to check if scrollbar now is hovered 143 0 : SGUIMessage msg(GUIM_MOUSE_MOTION); 144 0 : HandleMessage(msg); 145 0 : Message.Skip(false); 146 0 : break; 147 0 : } 148 : 149 0 : case GUIM_MOUSE_WHEEL_DOWN: 150 0 : { 151 0 : ScrollPlus(); 152 : // Since the scroll was changed, let's simulate a mouse movement 153 : // to check if scrollbar now is hovered 154 0 : SGUIMessage msg(GUIM_MOUSE_MOTION); 155 0 : HandleMessage(msg); 156 0 : Message.Skip(false); 157 0 : break; 158 0 : } 159 : 160 0 : default: 161 0 : IGUIScrollBar::HandleMessage(Message); 162 0 : break; 163 : } 164 0 : } 165 : 166 0 : CRect CGUIScrollBarVertical::GetBarRect() const 167 : { 168 0 : CRect ret; 169 0 : if (!GetStyle()) 170 : return ret; 171 : 172 : // Get from where the scroll area begins to where it ends 173 0 : float from = m_Y; 174 0 : float to = m_Y + m_Length - m_BarSize; 175 : 176 0 : if (GetStyle()->m_UseEdgeButtons) 177 : { 178 0 : from += GetStyle()->m_Width; 179 0 : to -= GetStyle()->m_Width; 180 : } 181 : 182 0 : ret.top = from + (to - from) * m_Pos / GetMaxPos(); 183 0 : ret.bottom = ret.top + m_BarSize; 184 0 : ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width); 185 0 : ret.left = ret.right - GetStyle()->m_Width; 186 : 187 0 : return ret; 188 : } 189 : 190 0 : CRect CGUIScrollBarVertical::GetOuterRect() const 191 : { 192 0 : CRect ret; 193 0 : if (!GetStyle()) 194 : return ret; 195 : 196 0 : ret.top = m_Y; 197 0 : ret.bottom = m_Y+m_Length; 198 0 : ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width); 199 0 : ret.left = ret.right - GetStyle()->m_Width; 200 : 201 0 : return ret; 202 : } 203 : 204 0 : bool CGUIScrollBarVertical::HoveringButtonMinus(const CVector2D& mouse) 205 : { 206 0 : if (!GetStyle()) 207 : return false; 208 : 209 0 : float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X; 210 : 211 0 : return mouse.X >= StartX && 212 0 : mouse.X <= StartX + GetStyle()->m_Width && 213 0 : mouse.Y >= m_Y && 214 0 : mouse.Y <= m_Y + GetStyle()->m_Width; 215 : } 216 : 217 0 : bool CGUIScrollBarVertical::HoveringButtonPlus(const CVector2D& mouse) 218 : { 219 0 : if (!GetStyle()) 220 : return false; 221 : 222 0 : float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X; 223 : 224 0 : return mouse.X > StartX && 225 0 : mouse.X < StartX + GetStyle()->m_Width && 226 0 : mouse.Y > m_Y + m_Length - GetStyle()->m_Width && 227 0 : mouse.Y < m_Y + m_Length; 228 : } 229 : 230 0 : void CGUIScrollBarVertical::SetScrollPlentyFromMousePos(const CVector2D& mouse) 231 : { 232 : // Scroll plus or minus a lot, this might change, it doesn't 233 : // have to be fancy though. 234 0 : if (mouse.Y < GetBarRect().top) 235 0 : ScrollMinusPlenty(); 236 : else 237 0 : ScrollPlusPlenty(); 238 0 : }