Checkpoint/common/ihid.hpp

144 lines
4.1 KiB
C++
Raw Normal View History

2018-05-30 20:56:19 +00:00
/*
2019-05-06 20:51:09 +00:00
* This file is part of Checkpoint
* Copyright (C) 2017-2019 Bernardo Giordano, FlagBrew
*
* 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.
*/
2018-05-30 20:56:19 +00:00
#ifndef IHID_HPP
#define IHID_HPP
#include "common.hpp"
2019-05-06 20:51:09 +00:00
#include <cmath>
#include <cstdint>
2018-05-30 20:56:19 +00:00
typedef uint64_t u64;
2020-02-22 05:58:06 +00:00
enum class HidDirection
{
VERTICAL,
HORIZONTAL
};
template <HidDirection ListDirection, HidDirection PageDirection, u64 Delay>
2019-05-06 20:51:09 +00:00
class IHid {
2018-05-30 20:56:19 +00:00
public:
2020-02-22 05:58:06 +00:00
IHid(size_t entries, size_t columns)
2018-05-30 20:56:19 +00:00
{
2020-02-22 05:58:06 +00:00
mMaxVisibleEntries = entries;
mColumns = columns;
mRows = entries / columns;
mIndex = 0;
mPage = 0;
mMaxPages = 0;
mLastTime = 0;
2018-05-30 20:56:19 +00:00
}
2020-02-22 05:58:06 +00:00
virtual ~IHid() {}
2018-05-30 20:56:19 +00:00
2020-02-22 05:58:06 +00:00
size_t fullIndex(void) const { return mIndex + mPage * mMaxVisibleEntries; }
size_t index(void) const { return mIndex; }
2020-02-23 00:58:23 +00:00
void index(size_t v) { mIndex = v; }
2020-02-22 05:58:06 +00:00
size_t maxVisibleEntries(void) const { return mMaxVisibleEntries; }
int page(void) const { return mPage; }
void page(int v) { mPage = v; }
size_t maxEntries(size_t count) const
{
return (count - mPage * mMaxVisibleEntries) > mMaxVisibleEntries ? mMaxVisibleEntries - 1 : count - mPage * mMaxVisibleEntries - 1;
}
2020-02-23 00:58:23 +00:00
void pageBack()
2020-02-22 05:58:06 +00:00
{
if (mPage > 0)
{
mPage--;
}
else if (mPage == 0)
{
mPage = mMaxPages - 1;
}
}
2020-02-23 00:58:23 +00:00
void pageForward()
2020-02-22 05:58:06 +00:00
{
if (mPage < (int)mMaxPages - 1)
{
mPage++;
}
else if (mPage == (int)mMaxPages - 1)
{
mPage = 0;
}
}
void reset(void)
{
mIndex = 0;
mPage = 0;
}
2020-02-23 00:58:23 +00:00
void correctIndex(size_t count)
{
if (mIndex > maxEntries(count))
{
if constexpr (ListDirection == HidDirection::HORIZONTAL)
{
mIndex = mIndex % mColumns;
}
else
{
mIndex = mIndex % mRows;
}
// If the above doesn't fix, then forcibly fix
if (mIndex > maxEntries(count))
{
mIndex = maxEntries(count);
}
}
}
2018-05-30 20:56:19 +00:00
2020-02-22 05:58:06 +00:00
void update(size_t count);
2018-05-30 20:56:19 +00:00
2020-02-22 05:58:06 +00:00
private:
2018-05-30 20:56:19 +00:00
size_t mIndex;
2019-05-06 20:51:09 +00:00
int mPage;
2018-05-30 20:56:19 +00:00
size_t mMaxPages;
size_t mMaxVisibleEntries;
size_t mColumns;
2020-02-22 05:58:06 +00:00
size_t mRows;
2019-05-06 20:51:09 +00:00
u64 mLastTime;
2018-12-14 20:59:46 +00:00
2020-02-22 05:58:06 +00:00
virtual bool downDown() const = 0;
virtual bool upDown() const = 0;
virtual bool leftDown() const = 0;
virtual bool rightDown() const = 0;
virtual bool leftTriggerDown() const = 0;
virtual bool rightTriggerDown() const = 0;
virtual bool downHeld() const = 0;
virtual bool upHeld() const = 0;
virtual bool leftHeld() const = 0;
virtual bool rightHeld() const = 0;
virtual bool leftTriggerHeld() const = 0;
virtual bool rightTriggerHeld() const = 0;
virtual u64 tick() const = 0;
2018-12-14 20:59:46 +00:00
};
2020-02-22 05:58:06 +00:00
#include "ihid.tcc"
2018-05-30 20:56:19 +00:00
#endif