libgui/source/gui/GuiScrollbar.cpp

216 lines
6.3 KiB
C++
Raw Permalink 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.
***************************************************************************/
2018-06-21 20:44:58 +02:00
#include <gui/GuiScrollbar.h>
#include <resources/Resources.h>
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
GuiScrollbar::GuiScrollbar(int32_t h)
2017-10-29 10:28:14 +01:00
: touchTrigger(GuiTrigger::CHANNEL_1, GuiTrigger::VPAD_TOUCH)
2018-06-21 20:44:58 +02:00
, 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);
ScrollState = 0;
listChanged.connect(this, &GuiScrollbar::setScrollboxPosition);
height = h;
arrowUpBtn = new GuiButton(50, 50);
arrowUpBtn->setParent(this);
arrowUpBtn->setAlignment(ALIGN_CENTER | ALIGN_TOP);
arrowUpBtn->setPosition(0, 0);
arrowUpBtn->setTrigger(&touchTrigger, 0);
arrowUpBtn->setTrigger(&wpadTouchTrigger, 1);
arrowUpBtn->setEffectGrow();
arrowUpBtn->clicked.connect(this, &GuiScrollbar::OnUpButtonClick);
arrowDownBtn = new GuiButton(50, 50);
arrowDownBtn->setParent(this);
arrowDownBtn->setAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
arrowDownBtn->setPosition(0, 0);
arrowDownBtn->setTrigger(&touchTrigger, 0);
arrowDownBtn->setTrigger(&wpadTouchTrigger, 1);
arrowDownBtn->setEffectGrow();
arrowDownBtn->clicked.connect(this, &GuiScrollbar::OnDownButtonClick);
scrollbarBoxBtn = new GuiButton(50, height);
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();
scrollbarBoxBtn->held.connect(this, &GuiScrollbar::OnBoxButtonHold);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
GuiScrollbar::~GuiScrollbar() {
delete arrowUpBtn;
delete arrowDownBtn;
delete scrollbarBoxBtn;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::ScrollOneUp() {
if(SelItem == 0 && SelInd > 0) {
2017-10-29 10:28:14 +01:00
// move list up by 1
--SelInd;
2018-06-21 20:44:58 +02:00
} else if(SelInd+SelItem > 0) {
2017-10-29 10:28:14 +01:00
--SelItem;
}
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::ScrollOneDown() {
if(SelInd+SelItem + 1 < EntrieCount) {
if(SelItem == PageSize-1) {
2017-10-29 10:28:14 +01:00
// move list down by 1
SelInd++;
2018-06-21 20:44:58 +02:00
} else {
2017-10-29 10:28:14 +01:00
SelItem++;
}
}
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::OnUpButtonClick(GuiButton *button, const GuiController *controller, GuiTrigger *trigger) {
if(ScrollState < ScrollSpeed)
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
ScrollOneUp();
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
ScrollState = 0;
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::OnDownButtonClick(GuiButton *button, const GuiController *controller, GuiTrigger *trigger) {
if(ScrollState < ScrollSpeed)
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
ScrollOneDown();
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
ScrollState = 0;
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::OnBoxButtonHold(GuiButton *button, const GuiController *controller, GuiTrigger *trigger) {
if(EntrieCount == 0) {
2017-10-29 10:28:14 +01:00
return;
}
2018-06-21 20:44:58 +02:00
if(!controller->data.validPointer) {
return;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
int32_t y = controller->data.y - this->getCenterY();
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
int32_t positionWiimote = LIMIT(y - MinHeight, 0, MaxHeight - MinHeight);
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
int32_t newSelected = (EntrieCount - 1) - (int32_t) ((float) positionWiimote / (float) (MaxHeight-MinHeight) * (float) (EntrieCount-1));
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
int32_t diff = newSelected-SelInd-SelItem;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
if(newSelected <= 0) {
2017-10-29 10:28:14 +01:00
SelItem = 0;
SelInd = 0;
2018-06-21 20:44:58 +02:00
} else if(newSelected >= EntrieCount-1) {
2017-10-29 10:28:14 +01:00
SelItem = (PageSize-1 < EntrieCount-1) ? PageSize-1 : EntrieCount-1;
SelInd = EntrieCount-PageSize;
2018-06-21 20:44:58 +02:00
} else if(newSelected < PageSize && SelInd == 0 && diff < 0) {
SelItem = std::max(SelItem+diff, (int32_t)0);
} else if(EntrieCount-newSelected < PageSize && SelInd == EntrieCount-PageSize && diff > 0) {
2017-10-29 10:28:14 +01:00
SelItem = std::min(SelItem+diff, PageSize-1);
2018-06-21 20:44:58 +02:00
} else {
2017-10-29 10:28:14 +01:00
SelInd = LIMIT(SelInd+diff, 0, ((EntrieCount-PageSize < 0) ? 0 : EntrieCount-PageSize));
}
2018-06-21 20:44:58 +02:00
ScrollState = 0;
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::SetPageSize(int32_t size) {
if(PageSize == size)
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
PageSize = size;
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::SetSelectedItem(int32_t pos) {
if(SelItem == pos)
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
SelItem = LIMIT(pos, 0, EntrieCount-1);
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::SetSelectedIndex(int32_t pos) {
if(SelInd == pos)
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
SelInd = pos;
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::SetEntrieCount(int32_t cnt) {
if(EntrieCount == cnt)
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
EntrieCount = cnt;
listChanged(SelItem, SelInd);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::setScrollboxPosition(int32_t SelItem, int32_t SelInd) {
int32_t position = MaxHeight-(MaxHeight-MinHeight)*(SelInd+SelItem)/(EntrieCount-1);
2017-10-29 10:28:14 +01:00
if(position < MinHeight || (SelInd+SelItem >= EntrieCount-1))
position = MinHeight;
else if(position > MaxHeight || (SelInd+SelItem) == 0)
position = MaxHeight;
scrollbarBoxBtn->setPosition(0, position);
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::draw(CVideo * video) {
if(scrollbarLineImage) {
scrollbarLineImage->draw(video);
}
arrowUpBtn->draw(video);
arrowDownBtn->draw(video);
scrollbarBoxBtn->draw(video);
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
updateEffects();
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiScrollbar::update(GuiController * t) {
if(this->isStateSet(STATE_DISABLED))
return;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
arrowUpBtn->update(t);
arrowDownBtn->update(t);
scrollbarBoxBtn->update(t);
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
++ScrollState;
2017-10-29 10:28:14 +01:00
}