Qt: Add clamp function, using stl if available

This commit is contained in:
Vicki Pfau 2021-01-16 00:47:41 -08:00
parent 8947b80a1c
commit b6dbbb14ab
4 changed files with 17 additions and 7 deletions

View File

@ -10,6 +10,7 @@
#include "GamepadButtonEvent.h"
#include "InputProfile.h"
#include "LogController.h"
#include "utils.h"
#include <QApplication>
#include <QTimer>
@ -733,7 +734,7 @@ void InputController::decreaseLuminanceLevel() {
void InputController::setLuminanceLevel(int level) {
int value = 0x16;
level = std::max(0, std::min(10, level));
level = clamp(level, 0, 10);
if (level > 0) {
value += GBA_LUX_LEVELS[level - 1];
}

View File

@ -9,6 +9,7 @@
#include "CoreController.h"
#include "LogController.h"
#include "VFileDevice.h"
#include "utils.h"
#include <QAction>
#include <QApplication>
@ -628,11 +629,7 @@ void MemoryModel::keyPressEvent(QKeyEvent* event) {
}
void MemoryModel::boundsCheck() {
if (m_top < 0) {
m_top = 0;
} else if (m_top > (m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight) {
m_top = (m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight;
}
m_top = clamp(m_top, 0, static_cast<int32_t>(m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight);
}
bool MemoryModel::isInSelection(uint32_t address) {

View File

@ -8,6 +8,7 @@
#include "CoreController.h"
#include "GamepadAxisEvent.h"
#include "InputController.h"
#include "utils.h"
#include <mgba/core/core.h>
#include <mgba/internal/gba/gba.h>
@ -129,7 +130,7 @@ void SensorView::updateSensors() {
}
void SensorView::setLuminanceValue(int value) {
value = std::max(0, std::min(value, 255));
value = clamp(value, 0, 255);
if (m_input) {
m_input->setLuminanceValue(value);
}

View File

@ -11,6 +11,8 @@
#include <QSize>
#include <QString>
#include <algorithm>
namespace QGBA {
QString niceSizeFormat(size_t filesize);
@ -45,4 +47,13 @@ inline QRect clampSize(const QSize& ref, const QSize& size, bool aspectRatio, bo
return QRect(origin, ds);
}
#if __cplusplus >= 201703L
using std::clamp;
#else
template<class T>
constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
return std::max(lo, std::min(hi, v));
}
#endif
}