Skip to content

Commit

Permalink
GUI: use size_t for discrete controls' number of states
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiapoirier committed Sep 15, 2022
1 parent 06f88d0 commit 143e922
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 73 deletions.
40 changes: 22 additions & 18 deletions dfxgui/dfxguibutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ To contact the author, use the contact form at http:https://destroyfx.org/
#include <cmath>

#include "dfxguieditor.h"
#include "dfxmath.h"


using namespace std::placeholders;
Expand All @@ -50,10 +51,10 @@ DGButton::DGButton(DfxGuiEditor* inOwnerEditor, long inParamID, DGRect const& in

//-----------------------------------------------------------------------------
DGButton::DGButton(DfxGuiEditor* inOwnerEditor, DGRect const& inRegion, DGImage* inImage,
long inNumStates, Mode inMode, bool inDrawMomentaryState)
size_t inNumStates, Mode inMode, bool inDrawMomentaryState)
: DGButton(inOwnerEditor, dfx::kParameterID_Invalid, inRegion, inImage, inMode, inDrawMomentaryState)
{
constexpr long minNumStates = 2;
constexpr size_t minNumStates = 2;
assert(inNumStates >= minNumStates);
setNumStates(std::max(inNumStates, minNumStates));
}
Expand All @@ -64,7 +65,7 @@ void DGButton::draw(VSTGUI::CDrawContext* inContext)
if (auto const image = getDrawBackground())
{
long const xoff = (mDrawMomentaryState && mMouseIsDown) ? (std::lround(image->getWidth()) / 2) : 0;
long const yoff = getValue_i() * (std::lround(image->getHeight()) / getNumStates());
long const yoff = getValue_i() * (std::lround(image->getHeight()) / dfx::math::ToSigned(getNumStates()));

image->draw(inContext, getViewSize(), VSTGUI::CPoint(xoff, yoff));
}
Expand All @@ -87,17 +88,15 @@ void DGButton::onMouseDownEvent(VSTGUI::MouseDownEvent& ioEvent)
beginEdit();

mEntryValue = mNewValue = getValue_i();
constexpr long min = 0;
long const max = getNumStates() - 1;
auto const isDirectionReversed = ioEvent.modifiers.has(VSTGUI::ModifierKey::Alt);

setMouseIsDown(true);

switch (mMode)
{
case Mode::Momentary:
mNewValue = max;
mEntryValue = 0; // just to make sure it's like that
mNewValue = getMaxValue();
mEntryValue = kMinValue; // just to make sure it's like that
break;
case Mode::Increment:
mNewValue = mEntryValue + (isDirectionReversed ? -1 : 1);
Expand All @@ -106,7 +105,7 @@ void DGButton::onMouseDownEvent(VSTGUI::MouseDownEvent& ioEvent)
mNewValue = mEntryValue + (isDirectionReversed ? 1 : -1);
break;
case Mode::Radio:
mNewValue = getRadioValue(ioEvent.mousePosition) + min;
mNewValue = getRadioValue(ioEvent.mousePosition) + kMinValue;
break;
default:
break;
Expand Down Expand Up @@ -236,13 +235,13 @@ void DGButton::onMouseWheelEvent(VSTGUI::MouseWheelEvent& ioEvent)
long newValue {};
if (mMode == Mode::Momentary)
{
newValue = getNumStates() - 1;
newValue = getMaxValue();
setValue_i(newValue);
if (isDirty())
{
valueChanged();
}
setValue_i(0);
setValue_i(kMinValue);
}
else
{
Expand Down Expand Up @@ -326,35 +325,40 @@ void DGButton::setOrientation(dfx::Axis inOrientation) noexcept
//-----------------------------------------------------------------------------
long DGButton::constrainValue(long inValue) const
{
constexpr long min = 0;
long const max = getNumStates() - 1;
auto const max = getMaxValue();

if (mWraparoundValues)
{
if (inValue > max)
{
return min;
return kMinValue;
}
else if (inValue < min)
else if (inValue < kMinValue)
{
return max;
}
}

return std::clamp(inValue, min, max);
return std::clamp(inValue, kMinValue, max);
}

//-----------------------------------------------------------------------------
void DGButton::setRadioThresholds(std::vector<VSTGUI::CCoord> const& inThresholds)
{
assert(mMode == Mode::Radio);
assert(static_cast<long>(inThresholds.size() + 1) == getNumStates());
assert((inThresholds.size() + 1) == getNumStates());
assert(std::all_of(inThresholds.cbegin(), inThresholds.cend(), std::bind(std::less<>{}, _1, getRange())));
assert(std::all_of(inThresholds.cbegin(), inThresholds.cend(), std::bind(std::greater<>{}, _1, 0)));

mRadioThresholds = inThresholds;
}

//-----------------------------------------------------------------------------
long DGButton::getMaxValue() const
{
return static_cast<long>(getNumStates()) - 1;
}

//-----------------------------------------------------------------------------
long DGButton::getRadioValue(VSTGUI::CPoint const& inPos) const
{
Expand All @@ -367,9 +371,9 @@ long DGButton::getRadioValue(VSTGUI::CPoint const& inPos) const
{
return std::count_if(mRadioThresholds.cbegin(), mRadioThresholds.cend(), std::bind(std::less_equal<>{}, _1, pos));
}
return std::lround(pos) / (getRange() / getNumStates());
return std::lround(pos) / (getRange() / dfx::math::ToSigned(getNumStates()));
}();
return std::clamp(result, 0L, getNumStates() - 1);
return std::clamp(result, kMinValue, getMaxValue());
}

//-----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions dfxgui/dfxguibutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DGButton : public DGControl<VSTGUI::CControl>
DGButton(DfxGuiEditor* inOwnerEditor, long inParamID, DGRect const& inRegion, DGImage* inImage,
Mode inMode, bool inDrawMomentaryState = false);
DGButton(DfxGuiEditor* inOwnerEditor, DGRect const& inRegion, DGImage* inImage,
long inNumStates, Mode inMode, bool inDrawMomentaryState = false);
size_t inNumStates, Mode inMode, bool inDrawMomentaryState = false);

void draw(VSTGUI::CDrawContext* inContext) override;
void onMouseDownEvent(VSTGUI::MouseDownEvent& ioEvent) override;
Expand Down Expand Up @@ -90,6 +90,8 @@ class DGButton : public DGControl<VSTGUI::CControl>
dfx::Axis mOrientation = dfx::kAxis_Horizontal;

private:
static constexpr long kMinValue = 0;
long getMaxValue() const;
long getRadioValue(VSTGUI::CPoint const& inPos) const;
long getRange() const; // the salient view dimension per the orientation

Expand All @@ -113,7 +115,7 @@ class DGToggleImageButton : public DGButton
CLASS_METHODS(DGToggleImageButton, DGButton)

private:
static constexpr long kNumStates = 2;
static constexpr size_t kNumStates = 2;

static DGRect makeRegion(VSTGUI::CCoord inXpos, VSTGUI::CCoord inYpos, DGImage* inImage, bool inDrawMomentaryState);
};
Expand Down
6 changes: 3 additions & 3 deletions dfxgui/dfxguicontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class DGControl : public IDGControl, public T
void setParameterID(long inParameterID) final;
bool isParameterAttached() const final;

long getNumStates() const final
size_t getNumStates() const final
{
return mNumStates;
}
void setNumStates(long inNumStates);
void setNumStates(size_t inNumStates);

float getFineTuneFactor() const override
{
Expand Down Expand Up @@ -178,7 +178,7 @@ class DGControl : public IDGControl, public T

DfxGuiEditor* const mOwnerEditor;

long mNumStates = 0;
size_t mNumStates = 0;

class MouseWheelEditingSupport : protected VSTGUI::CMouseWheelEditingSupport
{
Expand Down
19 changes: 6 additions & 13 deletions dfxgui/dfxguicontrol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void DGControl<T>::setValue_i(long inValue)
}
else
{
auto const maxValue = getNumStates() - 1;
auto const maxValue = static_cast<long>(getNumStates()) - 1;
if (inValue >= maxValue)
{
newValue_f = 1.0f;
Expand Down Expand Up @@ -148,17 +148,10 @@ bool DGControl<T>::isParameterAttached() const

//-----------------------------------------------------------------------------
template <class T>
void DGControl<T>::setNumStates(long inNumStates)
void DGControl<T>::setNumStates(size_t inNumStates)
{
if (inNumStates >= 0)
{
mNumStates = inNumStates;
T::setDirty();
}
else
{
assert(false);
}
mNumStates = inNumStates;
T::setDirty();
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -275,12 +268,12 @@ void DGControl<T>::pullNumStatesFromParameter()
{
if (isParameterAttached() && mOwnerEditor)
{
long numStates = 0;
size_t numStates = 0;
auto const paramID = getParameterID();
if (mOwnerEditor->GetParameterValueType(paramID) != DfxParam::ValueType::Float)
{
auto const valueRange = mOwnerEditor->GetParameter_maxValue(paramID) - mOwnerEditor->GetParameter_minValue(paramID);
numStates = std::lround(valueRange) + 1;
numStates = static_cast<size_t>(std::max(std::lround(valueRange), 0L) + 1);
}
setNumStates(numStates);
}
Expand Down
2 changes: 1 addition & 1 deletion dfxgui/dfxguislider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace

//-----------------------------------------------------------------------------
// TODO: migrate fully to VSTGUI::Modifiers
static VSTGUI::CButtonState ConstrainButtons(VSTGUI::MouseEvent const& inEvent, long inNumStates)
static VSTGUI::CButtonState ConstrainButtons(VSTGUI::MouseEvent const& inEvent, size_t inNumStates)
{
auto const buttons = VSTGUI::buttonStateFromMouseEvent(inEvent);
if (inNumStates > 0)
Expand Down
17 changes: 9 additions & 8 deletions dfxgui/dfxguitextdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,20 +494,21 @@ void DGStaticTextDisplay::drawPlatformText(VSTGUI::CDrawContext* inContext, VSTG
// Static Text Display
//-----------------------------------------------------------------------------
DGTextArrayDisplay::DGTextArrayDisplay(DfxGuiEditor* inOwnerEditor, long inParamID, DGRect const& inRegion,
long inNumStrings, dfx::TextAlignment inTextAlignment, DGImage* inBackground,
size_t inNumStrings, dfx::TextAlignment inTextAlignment, DGImage* inBackground,
float inFontSize, DGColor inFontColor, char const* inFontName)
: DGTextDisplay(inOwnerEditor, inParamID, inRegion, nullptr, nullptr, inBackground,
inTextAlignment, inFontSize, inFontColor, inFontName),
mDisplayStrings(std::max(inNumStrings, 1L))
// TODO C++23: integer literal suffix UZ
mDisplayStrings(std::max(inNumStrings, size_t(1)))
{
setMouseEnabled(false);
}

//-----------------------------------------------------------------------------
void DGTextArrayDisplay::setText(long inStringNum, char const* inText)
void DGTextArrayDisplay::setText(size_t inStringNum, char const* inText)
{
assert(inText);
if ((inStringNum < 0) || (inStringNum >= static_cast<long>(mDisplayStrings.size())))
if (inStringNum >= mDisplayStrings.size())
{
return;
}
Expand All @@ -525,18 +526,18 @@ void DGTextArrayDisplay::draw(VSTGUI::CDrawContext* inContext)
}

// TODO: consolidate this logic and DGButton::getValue_i into DGControl?
long stringIndex = 0;
size_t stringIndex = 0;
if (isParameterAttached())
{
stringIndex = std::lround(getOwnerEditor()->dfxgui_ExpandParameterValue(getParameterID(), getValueNormalized()));
stringIndex = dfx::math::ToIndex(std::lround(getOwnerEditor()->dfxgui_ExpandParameterValue(getParameterID(), getValueNormalized())));
}
else
{
auto const maxValue_f = static_cast<float>(mDisplayStrings.size() - 1);
stringIndex = static_cast<long>((getValueNormalized() * maxValue_f) + DfxParam::kIntegerPadding);
stringIndex = static_cast<size_t>((getValueNormalized() * maxValue_f) + DfxParam::kIntegerPadding);
}

if ((stringIndex >= 0) && (static_cast<size_t>(stringIndex) < mDisplayStrings.size()))
if (stringIndex < mDisplayStrings.size())
{
drawPlatformText(inContext, VSTGUI::UTF8String(mDisplayStrings[stringIndex]).getPlatformString(), getViewSize());
}
Expand Down
4 changes: 2 additions & 2 deletions dfxgui/dfxguitextdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ class DGStaticTextDisplay : public DGControl<VSTGUI::CTextLabel>
class DGTextArrayDisplay : public DGTextDisplay
{
public:
DGTextArrayDisplay(DfxGuiEditor* inOwnerEditor, long inParamID, DGRect const& inRegion, long inNumStrings,
DGTextArrayDisplay(DfxGuiEditor* inOwnerEditor, long inParamID, DGRect const& inRegion, size_t inNumStrings,
dfx::TextAlignment inTextAlignment = dfx::TextAlignment::Left, DGImage* inBackground = nullptr,
float inFontSize = 12.0f, DGColor inFontColor = VSTGUI::kBlackCColor, char const* inFontName = nullptr);

void draw(VSTGUI::CDrawContext* inContext) override;

using VSTGUI::CTextEdit::setText;
void setText(long inStringNum, char const* inText);
void setText(size_t inStringNum, char const* inText);

CLASS_METHODS(DGTextArrayDisplay, DGTextDisplay)

Expand Down
4 changes: 2 additions & 2 deletions dfxgui/idfxguicontrol.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*------------------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2018-2021 Sophia Poirier
Copyright (C) 2018-2022 Sophia Poirier
This file is part of the Destroy FX Library (version 1.0).
Expand Down Expand Up @@ -55,7 +55,7 @@ class IDGControl
virtual long getParameterID() const = 0;
virtual void setParameterID(long inParameterID) = 0;
virtual bool isParameterAttached() const = 0;
virtual long getNumStates() const = 0;
virtual size_t getNumStates() const = 0;
virtual float getFineTuneFactor() const = 0;
virtual bool notifyIfChanged() = 0;
virtual void onMouseWheelEditing() = 0;
Expand Down
6 changes: 3 additions & 3 deletions geometer/gui/geometereditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ long GeometerEditor::OpenEditor() {
dfx::TextAlignment::Center, nullptr,
dfx::kFontSize_Snooty10px, fontcolor_labels,
dfx::kFontName_Snooty10px);
long j = 0;
size_t j = 0;
for (auto const& labelstring : *labelstrings) {
label->setText(j, labelstring);
j++;
Expand Down Expand Up @@ -397,7 +397,7 @@ void GeometerEditor::mouseovercontrolchanged(IDGControl * /*currentControlUnderM
//-----------------------------------------------------------------------------
std::string GeometerEditor::changehelp(IDGControl * currentControlUnderMouse) {

auto const updatehelp = [this](int category, int item, long numitems) {
auto const updatehelp = [this](size_t category, int item, size_t numitems) {
if (helpicon) {
helpicon->setNumStates(numitems);
helpicon->setButtonImage(g_helpicons[category]);
Expand Down Expand Up @@ -440,7 +440,7 @@ std::string GeometerEditor::changehelp(IDGControl * currentControlUnderMouse) {
}

//--------------------------------------------------------------------------
std::string GeometerEditor::helptext(int inHelpCategory, int inItemNum) {
std::string GeometerEditor::helptext(size_t inHelpCategory, int inItemNum) {

if (inItemNum < 0) {
return {};
Expand Down
4 changes: 2 additions & 2 deletions geometer/gui/geometereditor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------
Copyright (C) 2002-2021 Tom Murphy 7 and Sophia Poirier
Copyright (C) 2002-2022 Tom Murphy 7 and Sophia Poirier
This file is part of Geometer.
Expand Down Expand Up @@ -38,7 +38,7 @@ class GeometerEditor final : public DfxGuiEditor {

private:
std::string changehelp(IDGControl * currentControlUnderMouse);
static std::string helptext(int inHelpCategory, int inItemNum);
static std::string helptext(size_t inHelpCategory, int inItemNum);
long choose_multiparam(long baseParamID) {
return getparameter_i(baseParamID) + baseParamID + 1;
}
Expand Down
Loading

0 comments on commit 143e922

Please sign in to comment.