Skip to content

Instantly share code, notes, and snippets.

@SammyPeiren
Created November 1, 2020 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SammyPeiren/6c56db9d08fbb200acc3dcc89c9c531b to your computer and use it in GitHub Desktop.
Save SammyPeiren/6c56db9d08fbb200acc3dcc89c9c531b to your computer and use it in GitHub Desktop.
multiline drawlabel
//this works if you use label names with \n such as "Attack \n Rate"
void MySliderControl::DrawLabel(IGraphics& g)
{
if (mLabelBounds.H() && mStyle.showLabel)
{
IBlend blend = mControl->GetBlend();
std::string labelStr(mLabelStr.Get());
std::string delimiter("\n");
int numberOfLines = std::count(labelStr.begin(), labelStr.end(), '\n')+1;
IRECT textRect;
mControl->GetUI()->MeasureText(mStyle.labelText, mLabelStr.Get(), textRect);
IRECT mLabelBoundsNew = mLabelBounds;
while (labelStr.find(delimiter)!= std::string::npos)
{
std::size_t charPos = labelStr.find(delimiter);
std::string test = labelStr.substr(0, charPos);
g.DrawText(mStyle.labelText, (labelStr.substr(0, charPos)).c_str(), mLabelBoundsNew, &blend);
labelStr = labelStr.substr(charPos+ delimiter.length(), labelStr.length() - charPos);
mLabelBoundsNew = mLabelBoundsNew.GetVShifted(textRect.H());
}
g.DrawText(mStyle.labelText, labelStr.c_str(), mLabelBoundsNew, &blend);
}
}
void MySliderControl::OnResize()
{
SetTargetRECT(MakeRects(mRECT));
if (mDirection == EDirection::Vertical)
mTrackBounds = mWidgetBounds.GetPadded(-mHandleSize).GetMidHPadded(mTrackSize);
else
mTrackBounds = mWidgetBounds.GetPadded(-mHandleSize).GetMidVPadded(mTrackSize);
SetDirty(false);
}
IRECT MySliderControl::MakeRects(const IRECT& parent, bool hasHandle)
{
IRECT clickableArea = parent;
if (!mLabelInWidget)
{
if (mStyle.showLabel && CStringHasContents(mLabelStr.Get()))
{
IRECT textRect;
mControl->GetUI()->MeasureText(mStyle.labelText, mLabelStr.Get(), textRect);
mLabelBounds = parent.GetFromTop(textRect.H()).GetCentredInside(textRect.W(), textRect.H());
//multiply mLabelBounds height by number of lines
std::string labelStr(mLabelStr.Get());
int numberOfExtraLines = std::count(labelStr.begin(), labelStr.end(), '\n');
mLabelBounds.B = mLabelBounds.B + (mLabelBounds.H() * numberOfExtraLines);
}
else
mLabelBounds = IRECT();
if (mLabelBounds.H())
clickableArea = parent.GetReducedFromTop(mLabelBounds.H());
}
if (mStyle.showValue && !mValueInWidget)
{
IRECT textRect;
if (CStringHasContents(mValueStr.Get()))
mControl->GetUI()->MeasureText(mStyle.valueText, mValueStr.Get(), textRect);
const float valueDisplayWidth = textRect.W() * mValueDisplayFrac;
switch (mStyle.valueText.mVAlign)
{
case EVAlign::Middle:
mValueBounds = clickableArea.GetMidVPadded(textRect.H() / 2.f).GetMidHPadded(valueDisplayWidth);
mWidgetBounds = clickableArea.GetScaledAboutCentre(mStyle.widgetFrac);
break;
case EVAlign::Bottom:
{
mValueBounds = clickableArea.GetFromBottom(textRect.H()).GetMidHPadded(valueDisplayWidth);
mWidgetBounds = clickableArea.GetReducedFromBottom(textRect.H()).GetScaledAboutCentre(mStyle.widgetFrac);
break;
}
case EVAlign::Top:
mValueBounds = clickableArea.GetFromTop(textRect.H()).GetMidHPadded(valueDisplayWidth);
mWidgetBounds = clickableArea.GetReducedFromTop(textRect.H()).GetScaledAboutCentre(mStyle.widgetFrac);
break;
default:
break;
}
}
else
{
mWidgetBounds = clickableArea.GetScaledAboutCentre(mStyle.widgetFrac);
}
if (hasHandle)
mWidgetBounds = GetAdjustedHandleBounds(clickableArea).GetScaledAboutCentre(mStyle.widgetFrac);
if (mLabelInWidget)
mLabelBounds = mWidgetBounds;
if (mValueInWidget)
mValueBounds = mWidgetBounds;
return clickableArea;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment