libgui-maschell/source/gui/GuiScrollbar.cpp

244 lines
6.1 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
/***************************************************************************
* Copyright (C) 2011
* by Dimok
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
***************************************************************************/
#include "GuiScrollbar.h"
#include "resources/Resources.h"
2017-10-31 17:18:42 +01:00
GuiScrollbar::GuiScrollbar(s32 h)
2017-10-29 10:28:14 +01:00
: touchTrigger(GuiTrigger::CHANNEL_1, GuiTrigger::VPAD_TOUCH)
, wpadTouchTrigger(GuiTrigger::CHANNEL_2 | GuiTrigger::CHANNEL_3 | GuiTrigger::CHANNEL_4 | GuiTrigger::CHANNEL_5, GuiTrigger::BUTTON_A)
{
SelItem = 0;
SelInd = 0;
PageSize = 0;
EntrieCount = 0;
SetScrollSpeed(15);
2017-10-29 10:28:14 +01:00
ScrollState = 0;
2017-10-31 17:18:42 +01:00
listChanged.connect(this, &GuiScrollbar::setScrollboxPosition);
2017-10-29 10:28:14 +01:00
height = h;
arrowUpBtn = new GuiButton(50, 50);
2017-10-29 10:28:14 +01:00
arrowUpBtn->setParent(this);
arrowUpBtn->setAlignment(ALIGN_CENTER | ALIGN_TOP);
arrowUpBtn->setPosition(0, 0);
arrowUpBtn->setTrigger(&touchTrigger, 0);
arrowUpBtn->setTrigger(&wpadTouchTrigger, 1);
arrowUpBtn->setEffectGrow();
2017-10-31 17:18:42 +01:00
arrowUpBtn->clicked.connect(this, &GuiScrollbar::OnUpButtonClick);
2017-10-29 10:28:14 +01:00
arrowDownBtn = new GuiButton(50, 50);
2017-10-29 10:28:14 +01:00
arrowDownBtn->setParent(this);
arrowDownBtn->setAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
arrowDownBtn->setPosition(0, 0);
arrowDownBtn->setTrigger(&touchTrigger, 0);
arrowDownBtn->setTrigger(&wpadTouchTrigger, 1);
arrowDownBtn->setEffectGrow();
2017-10-31 17:18:42 +01:00
arrowDownBtn->clicked.connect(this, &GuiScrollbar::OnDownButtonClick);
2017-10-29 10:28:14 +01:00
scrollbarBoxBtn = new GuiButton(50, height);
2017-10-29 10:28:14 +01:00
scrollbarBoxBtn->setParent(this);
scrollbarBoxBtn->setAlignment(ALIGN_CENTER | ALIGN_TOP);
scrollbarBoxBtn->setPosition(0, MaxHeight);
scrollbarBoxBtn->setHoldable(true);
scrollbarBoxBtn->setTrigger(&touchTrigger, 0);
scrollbarBoxBtn->setTrigger(&wpadTouchTrigger, 1);
scrollbarBoxBtn->setEffectGrow();
2017-10-31 17:18:42 +01:00
scrollbarBoxBtn->held.connect(this, &GuiScrollbar::OnBoxButtonHold);
2017-10-29 10:28:14 +01:00
}
2017-10-31 17:18:42 +01:00
GuiScrollbar::~GuiScrollbar()
2017-10-29 10:28:14 +01:00
{
delete arrowUpBtn;
delete arrowDownBtn;
delete scrollbarBoxBtn;
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::ScrollOneUp()
2017-10-29 10:28:14 +01:00
{
if(SelItem == 0 && SelInd > 0)
{
// move list up by 1
--SelInd;
}
else if(SelInd+SelItem > 0)
{
--SelItem;
}
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::ScrollOneDown()
2017-10-29 10:28:14 +01:00
{
if(SelInd+SelItem + 1 < EntrieCount)
{
if(SelItem == PageSize-1)
{
// move list down by 1
SelInd++;
}
else
{
SelItem++;
}
}
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::OnUpButtonClick(GuiButton *button, const GuiController *controller, GuiTrigger *trigger)
2017-10-29 10:28:14 +01:00
{
if(ScrollState < ScrollSpeed)
return;
ScrollOneUp();
ScrollState = 0;
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::OnDownButtonClick(GuiButton *button, const GuiController *controller, GuiTrigger *trigger)
2017-10-29 10:28:14 +01:00
{
if(ScrollState < ScrollSpeed)
return;
ScrollOneDown();
ScrollState = 0;
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::OnBoxButtonHold(GuiButton *button, const GuiController *controller, GuiTrigger *trigger)
2017-10-29 10:28:14 +01:00
{
if(EntrieCount == 0){
return;
}
if(!controller->data.validPointer){
return;
}
s32 y = controller->data.y - this->getCenterY();
s32 positionWiimote = LIMIT(y - MinHeight, 0, MaxHeight - MinHeight);
s32 newSelected = (EntrieCount - 1) - (s32) ((float) positionWiimote / (float) (MaxHeight-MinHeight) * (float) (EntrieCount-1));
s32 diff = newSelected-SelInd-SelItem;
if(newSelected <= 0)
{
SelItem = 0;
SelInd = 0;
}
else if(newSelected >= EntrieCount-1)
{
SelItem = (PageSize-1 < EntrieCount-1) ? PageSize-1 : EntrieCount-1;
SelInd = EntrieCount-PageSize;
}
else if(newSelected < PageSize && SelInd == 0 && diff < 0)
{
SelItem = std::max(SelItem+diff, (s32)0);
}
else if(EntrieCount-newSelected < PageSize && SelInd == EntrieCount-PageSize && diff > 0)
{
SelItem = std::min(SelItem+diff, PageSize-1);
}
else
{
SelInd = LIMIT(SelInd+diff, 0, ((EntrieCount-PageSize < 0) ? 0 : EntrieCount-PageSize));
}
ScrollState = 0;
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::SetPageSize(s32 size)
2017-10-29 10:28:14 +01:00
{
if(PageSize == size)
return;
PageSize = size;
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::SetSelectedItem(s32 pos)
2017-10-29 10:28:14 +01:00
{
if(SelItem == pos)
return;
SelItem = LIMIT(pos, 0, EntrieCount-1);
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::SetSelectedIndex(s32 pos)
2017-10-29 10:28:14 +01:00
{
if(SelInd == pos)
return;
SelInd = pos;
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::SetEntrieCount(s32 cnt)
2017-10-29 10:28:14 +01:00
{
if(EntrieCount == cnt)
return;
EntrieCount = cnt;
listChanged(SelItem, SelInd);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::setScrollboxPosition(s32 SelItem, s32 SelInd)
2017-10-29 10:28:14 +01:00
{
s32 position = MaxHeight-(MaxHeight-MinHeight)*(SelInd+SelItem)/(EntrieCount-1);
if(position < MinHeight || (SelInd+SelItem >= EntrieCount-1))
position = MinHeight;
else if(position > MaxHeight || (SelInd+SelItem) == 0)
position = MaxHeight;
scrollbarBoxBtn->setPosition(0, position);
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::draw(CVideo * video)
2017-10-29 10:28:14 +01:00
{
if(scrollbarLineImage){ scrollbarLineImage->draw(video); }
2017-10-29 10:28:14 +01:00
arrowUpBtn->draw(video);
arrowDownBtn->draw(video);
scrollbarBoxBtn->draw(video);
updateEffects();
}
2017-10-31 17:18:42 +01:00
void GuiScrollbar::update(GuiController * t)
2017-10-29 10:28:14 +01:00
{
if(this->isStateSet(STATE_DISABLED))
return;
arrowUpBtn->update(t);
arrowDownBtn->update(t);
scrollbarBoxBtn->update(t);
++ScrollState;
}