Checkpoint/3ds/source/clickable.cpp

78 lines
2.7 KiB
C++
Raw Normal View History

2018-05-14 06:52:41 +00:00
/*
* This file is part of Checkpoint
* Copyright (C) 2017-2018 Bernardo Giordano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
2017-09-29 07:45:56 +00:00
*/
2018-05-14 06:52:41 +00:00
#include "clickable.hpp"
2017-09-29 07:45:56 +00:00
2018-05-14 06:52:41 +00:00
bool Clickable::held()
2017-09-29 07:45:56 +00:00
{
2018-05-14 06:52:41 +00:00
touchPosition touch;
hidTouchRead(&touch);
return ((hidKeysHeld() & KEY_TOUCH) && touch.px > mx && touch.px < mx+mw && touch.py > my && touch.py < my+mh);
}
bool Clickable::released(void)
{
touchPosition touch;
hidTouchRead(&touch);
const bool on = touch.px > mx && touch.px < mx+mw && touch.py > my && touch.py < my+mh;
if (on)
{
mOldPressed = true;
}
else
{
if (mOldPressed && !(touch.px > 0 || touch.py > 0))
{
mOldPressed = false;
return true;
}
mOldPressed = false;
}
return false;
2017-09-29 07:45:56 +00:00
}
void Clickable::draw(void)
{
2018-05-14 06:52:41 +00:00
static const float size = 0.7f;
static const float messageHeight = ceilf(size*fontGetInfo()->lineFeed);
const float messageWidth = mCentered ? mC2dText.width * size : mw - 8;
const u32 bgColor = held() ? mColorText : mColorBg;
const u32 msgColor = bgColor == mColorBg ? mColorText : mColorBg;
C2D_DrawRectSolid(mx, my, 0.5f, mw, mh, bgColor);
C2D_DrawText(&mC2dText, C2D_WithColor, mx + (mw - messageWidth)/2, my + (mh - messageHeight)/2, 0.5f, size, size, msgColor);
2017-09-29 07:45:56 +00:00
}
2018-05-14 06:52:41 +00:00
void Clickable::draw(float size)
2017-09-29 07:45:56 +00:00
{
2018-05-14 06:52:41 +00:00
static const float messageHeight = ceilf(size*fontGetInfo()->lineFeed);
const float messageWidth = mCentered ? mC2dText.width * size : mw - 8;
C2D_DrawRectSolid(mx, my, 0.5f, mw, mh, mColorBg);
C2D_DrawText(&mC2dText, C2D_WithColor, mx + (mw - messageWidth)/2, my + (mh - messageHeight)/2, 0.5f, size, size, mColorText);
2017-09-29 07:45:56 +00:00
}