Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CGUIScrollBarVertical.cpp
/* Copyright (C) 2016 Wildfire Games. | /* Copyright (C) 2017 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "CGUIScrollBarVertical.h" | #include "CGUIScrollBarVertical.h" | ||||
#include "GUI.h" | #include "GUI.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
CGUIScrollBarVertical::CGUIScrollBarVertical() | CGUIScrollBarVertical::CGUIScrollBarVertical() | ||||
{ | { | ||||
} | } | ||||
CGUIScrollBarVertical::~CGUIScrollBarVertical() | CGUIScrollBarVertical::~CGUIScrollBarVertical() | ||||
{ | { | ||||
} | } | ||||
void CGUIScrollBarVertical::Setup() | |||||
vladislavbelov: It looks like that these 2 functions have not small common part. It'd be good to minimize the… | |||||
{ | |||||
CRect host = m_pHostObject->GetCachedSize(); | |||||
CRect content = m_pHostObject->GetContentSize(); | |||||
SetScrollRange(content.GetHeight()); | |||||
Setup(host); | |||||
int length = host.bottom - host.top; | |||||
if (content.GetWidth() > host.GetWidth()) | |||||
length -= GetStyle()->m_Breadth; | |||||
SetLength(length); | |||||
} | |||||
void CGUIScrollBarVertical::Setup(const CRect& content) | |||||
{ | |||||
SetScrollSpace(content.GetHeight()); | |||||
SetX(m_RightAligned ? content.right : content.left); | |||||
SetY(content.top); | |||||
SetZ(m_pHostObject->GetBufferedZ()); | |||||
Done Inline Actionsconst CRect& content vladislavbelov: ```lang=cpp
const CRect& content
``` | |||||
SetLength(content.bottom - content.top); | |||||
} | |||||
void CGUIScrollBarVertical::SetPosFromMousePos(const CPos& mouse) | void CGUIScrollBarVertical::SetPosFromMousePos(const CPos& mouse) | ||||
Done Inline ActionsIt could be replaced by: SetX(m_RightAligned ? content.right : content.left); vladislavbelov: It could be replaced by:
```lang=cpp
SetX(m_RightAligned ? content.right : content.left);
``` | |||||
{ | { | ||||
if (!GetStyle()) | if (!GetStyle()) | ||||
return; | return; | ||||
/** | /** | ||||
* Calculate the position for the top of the item being scrolled | * Calculate the position for the top of the item being scrolled | ||||
*/ | */ | ||||
float emptyBackground = m_Length - m_BarSize; | float emptyBackground = m_Length - m_BarSize; | ||||
if (GetStyle()->m_UseEdgeButtons) | if (GetStyle()->m_UseEdgeButtons) | ||||
emptyBackground -= GetStyle()->m_Width * 2; | emptyBackground -= GetStyle()->m_Breadth * 2; | ||||
m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.y - m_BarPressedAtPos.y) / emptyBackground; | m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.y - m_BarPressedAtPos.y) / emptyBackground; | ||||
} | } | ||||
void CGUIScrollBarVertical::Draw() | void CGUIScrollBarVertical::Draw() | ||||
{ | { | ||||
if (!GetStyle()) | if (!GetStyle()) | ||||
{ | { | ||||
LOGWARNING("Attempt to draw scrollbar without a style."); | LOGWARNING("Attempt to draw a vertical scrollbar without a style."); | ||||
return; | return; | ||||
} | } | ||||
if (GetGUI() && IsVisible()) | if (GetGUI() && IsNeeded()) | ||||
Not Done Inline ActionsEarly return, cache GetGUI() ? Stan: Early return, cache GetGUI() ? | |||||
{ | { | ||||
CRect outline = GetOuterRect(); | CRect outline = GetOuterRect(); | ||||
GetGUI()->DrawSprite( | GetGUI()->DrawSprite( | ||||
GetStyle()->m_SpriteBackVertical, | GetStyle()->m_SpriteBackVertical, | ||||
0, | 0, | ||||
m_Z+0.1f, | m_Z + 0.1f, | ||||
CRect( | CRect( | ||||
outline.left, | outline.left, | ||||
outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0), | outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Breadth : 0), | ||||
outline.right, | outline.right, | ||||
outline.bottom - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0) | outline.bottom - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Breadth : 0) | ||||
) | ) | ||||
); | ); | ||||
if (GetStyle()->m_UseEdgeButtons) | if (GetStyle()->m_UseEdgeButtons) | ||||
{ | { | ||||
const CGUISpriteInstance* button_top; | const CGUISpriteInstance* button_top; | ||||
const CGUISpriteInstance* button_bottom; | const CGUISpriteInstance* button_bottom; | ||||
Show All 15 Lines | if (GetStyle()->m_UseEdgeButtons) | ||||
button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomOver, GetStyle()->m_SpriteButtonBottom); | button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomOver, GetStyle()->m_SpriteButtonBottom); | ||||
} | } | ||||
else | else | ||||
button_bottom = &GetStyle()->m_SpriteButtonBottom; | button_bottom = &GetStyle()->m_SpriteButtonBottom; | ||||
GetGUI()->DrawSprite( | GetGUI()->DrawSprite( | ||||
*button_top, | *button_top, | ||||
0, | 0, | ||||
m_Z+0.2f, | m_Z + 0.2f, | ||||
CRect( | CRect( | ||||
outline.left, | outline.left, | ||||
outline.top, | outline.top, | ||||
outline.right, | outline.right, | ||||
outline.top+GetStyle()->m_Width | outline.top + GetStyle()->m_Breadth | ||||
) | ) | ||||
); | ); | ||||
GetGUI()->DrawSprite( | GetGUI()->DrawSprite( | ||||
*button_bottom, | *button_bottom, | ||||
0, | 0, | ||||
m_Z+0.2f, | m_Z + 0.2f, | ||||
CRect( | CRect( | ||||
outline.left, | outline.left, | ||||
outline.bottom-GetStyle()->m_Width, | outline.bottom - GetStyle()->m_Breadth, | ||||
outline.right, | outline.right, | ||||
outline.bottom | outline.bottom | ||||
) | ) | ||||
); | ); | ||||
} | } | ||||
GetGUI()->DrawSprite( | GetGUI()->DrawSprite( | ||||
GetStyle()->m_SpriteBarVertical, | GetStyle()->m_SpriteBarVertical, | ||||
0, | 0, | ||||
m_Z + 0.2f, | m_Z + 0.2f, | ||||
GetBarRect() | GetBarRect() | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message) | void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message) | ||||
Not Done Inline ActionsIf the message was handled, why do you go deeper and call the IGUIScrollbar handler? vladislavbelov: If the message was handled, why do you go deeper and call the `IGUIScrollbar` handler? | |||||
Not Done Inline ActionsThe handling in this function is specific to vertical scrollbars. The handling in IGUIScrollbar is generic to all scrollbars: vertical, horizontal, diagonal, etc. Rather than having the generic handling duplicated, we have it in one place. Or in other words, the message is only partially handled here. Further handling is done in the IGUIScrollbar handler. s0600204: The handling in this function is specific to vertical scrollbars. The handling in… | |||||
{ | { | ||||
switch (Message.type) | |||||
{ | |||||
case GUIM_MOUSE_WHEEL_UP: | |||||
ScrollMinus(); | |||||
break; | |||||
case GUIM_MOUSE_WHEEL_DOWN: | |||||
ScrollPlus(); | |||||
break; | |||||
default: | |||||
break; | |||||
} | |||||
IGUIScrollBar::HandleMessage(Message); | IGUIScrollBar::HandleMessage(Message); | ||||
} | } | ||||
CRect CGUIScrollBarVertical::GetBarRect() const | CRect CGUIScrollBarVertical::GetBarRect() const | ||||
{ | { | ||||
CRect ret; | CRect ret; | ||||
if (!GetStyle()) | if (!GetStyle()) | ||||
return ret; | return ret; | ||||
// Get from where the scroll area begins to where it ends | // Get from where the scroll area begins to where it ends | ||||
float from = m_Y; | float from = m_Y; | ||||
float to = m_Y + m_Length - m_BarSize; | float to = m_Y + m_Length - m_BarSize; | ||||
if (GetStyle()->m_UseEdgeButtons) | if (GetStyle()->m_UseEdgeButtons) | ||||
{ | { | ||||
from += GetStyle()->m_Width; | from += GetStyle()->m_Breadth; | ||||
to -= GetStyle()->m_Width; | to -= GetStyle()->m_Breadth; | ||||
} | } | ||||
ret.top = from + (to - from) * m_Pos / GetMaxPos(); | ret.top = from + (to - from) * m_Pos / GetMaxPos(); | ||||
ret.bottom = ret.top + m_BarSize; | ret.bottom = ret.top + m_BarSize; | ||||
ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width); | ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Breadth); | ||||
ret.left = ret.right - GetStyle()->m_Width; | ret.left = ret.right - GetStyle()->m_Breadth; | ||||
return ret; | return ret; | ||||
} | } | ||||
CRect CGUIScrollBarVertical::GetOuterRect() const | CRect CGUIScrollBarVertical::GetOuterRect() const | ||||
{ | { | ||||
CRect ret; | CRect ret; | ||||
if (!GetStyle()) | if (!GetStyle()) | ||||
return ret; | return ret; | ||||
ret.top = m_Y; | ret.top = m_Y; | ||||
ret.bottom = m_Y+m_Length; | ret.bottom = m_Y+m_Length; | ||||
ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width); | ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Breadth); | ||||
ret.left = ret.right - GetStyle()->m_Width; | ret.left = ret.right - GetStyle()->m_Breadth; | ||||
return ret; | return ret; | ||||
} | } | ||||
bool CGUIScrollBarVertical::HoveringButtonMinus(const CPos& mouse) | bool CGUIScrollBarVertical::HoveringButtonMinus(const CPos& mouse) | ||||
{ | { | ||||
if (!GetStyle()) | if (!GetStyle()) | ||||
return false; | return false; | ||||
float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X; | float StartX = m_RightAligned ? m_X-GetStyle()->m_Breadth : m_X; | ||||
return mouse.x >= StartX && | return mouse.x >= StartX && | ||||
mouse.x <= StartX + GetStyle()->m_Width && | mouse.x <= StartX + GetStyle()->m_Breadth && | ||||
mouse.y >= m_Y && | mouse.y >= m_Y && | ||||
mouse.y <= m_Y + GetStyle()->m_Width; | mouse.y <= m_Y + GetStyle()->m_Breadth; | ||||
} | } | ||||
bool CGUIScrollBarVertical::HoveringButtonPlus(const CPos& mouse) | bool CGUIScrollBarVertical::HoveringButtonPlus(const CPos& mouse) | ||||
{ | { | ||||
if (!GetStyle()) | if (!GetStyle()) | ||||
return false; | return false; | ||||
float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X; | float StartX = m_RightAligned ? m_X-GetStyle()->m_Breadth : m_X; | ||||
return mouse.x > StartX && | return mouse.x > StartX && | ||||
mouse.x < StartX + GetStyle()->m_Width && | mouse.x < StartX + GetStyle()->m_Breadth && | ||||
mouse.y > m_Y + m_Length - GetStyle()->m_Width && | mouse.y > m_Y + m_Length - GetStyle()->m_Breadth && | ||||
mouse.y < m_Y + m_Length; | mouse.y < m_Y + m_Length; | ||||
} | } |
Wildfire Games · Phabricator
It looks like that these 2 functions have not small common part. It'd be good to minimize the code duplicating. Probably you just need a host param.