Use of clamp

master
conde2 11 years ago
parent 7ff73d1064
commit 4dcb30110f

@ -50,7 +50,7 @@ TexturePtr LightView::generateLightBubble(float centerFactor)
for(int x = 0; x < bubbleDiameter; x++) { for(int x = 0; x < bubbleDiameter; x++) {
for(int y = 0; y < bubbleDiameter; y++) { for(int y = 0; y < bubbleDiameter; y++) {
float radius = std::sqrt((bubbleRadius - x)*(bubbleRadius - x) + (bubbleRadius - y)*(bubbleRadius - y)); float radius = std::sqrt((bubbleRadius - x)*(bubbleRadius - x) + (bubbleRadius - y)*(bubbleRadius - y));
float intensity = std::max<float>(std::min<float>((bubbleRadius-radius)/(float)(bubbleRadius-centerRadius), 1.0f), 0.0f); float intensity = stdext::clamp<float>((bubbleRadius - radius) / (float)(bubbleRadius - centerRadius), 0.0f, 1.0f);
// light intensity varies inversely with the square of the distance // light intensity varies inversely with the square of the distance
intensity = intensity * intensity; intensity = intensity * intensity;

@ -644,7 +644,7 @@ int MapView::calcFirstVisibleFloor()
} }
// just ensure the that the floor is in the valid range // just ensure the that the floor is in the valid range
z = std::min<int>(std::max<int>(z, 0), (int)Otc::MAX_Z); z = stdext::clamp<int>(z, 0, (int)Otc::MAX_Z);
return z; return z;
} }
@ -669,7 +669,7 @@ int MapView::calcLastVisibleFloor()
z = std::max<int>(m_lockedFirstVisibleFloor, z); z = std::max<int>(m_lockedFirstVisibleFloor, z);
// just ensure the that the floor is in the valid range // just ensure the that the floor is in the valid range
z = std::min<int>(std::max<int>(z, 0), (int)Otc::MAX_Z); z = stdext::clamp<int>(z, 0, (int)Otc::MAX_Z);
return z; return z;
} }

@ -77,7 +77,7 @@ void UIMap::movePixels(int x, int y)
bool UIMap::setZoom(int zoom) bool UIMap::setZoom(int zoom)
{ {
m_zoom = std::min<int>(std::max<int>(zoom, m_maxZoomIn), m_maxZoomOut); m_zoom = stdext::clamp<int>(zoom, m_maxZoomOut, m_maxZoomIn);
updateVisibleDimension(); updateVisibleDimension();
return false; return false;
} }

@ -84,7 +84,7 @@ void UIProgressRect::drawSelf(Fw::DrawPane drawPane)
void UIProgressRect::setPercent(float percent) void UIProgressRect::setPercent(float percent)
{ {
m_percent = std::max<float>(std::min<float>((double)percent, 100.0), 0.0); m_percent = stdext::clamp<float>((double)percent, 0.0, 100.0);
} }
void UIProgressRect::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode) void UIProgressRect::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)

@ -89,7 +89,7 @@ bool AdaptativeFrameCounter::update()
void AdaptativeFrameCounter::setMaxFps(int maxFps) void AdaptativeFrameCounter::setMaxFps(int maxFps)
{ {
maxFps = std::max<int>(std::min<int>(maxFps, 1000), 0); maxFps = stdext::clamp<int>(maxFps, 0, 1000);
if(maxFps != 0) { if(maxFps != 0) {
m_bestFrameDelay = 1000000 / maxFps; m_bestFrameDelay = 1000000 / maxFps;

@ -45,6 +45,9 @@ float random_range(float min, float max);
double round(double r); double round(double r);
template<typename T>
T clamp(T x, T min, T max) { return std::max<T>(min, std::min<T>(x, max)); }
} }
#endif #endif

@ -358,8 +358,8 @@ void UITextEdit::setSelection(int start, int end)
if(end == -1) if(end == -1)
end = m_text.length(); end = m_text.length();
m_selectionStart = std::min<int>(std::max<int>(start, 0), (int)m_text.length()); m_selectionStart = stdext::clamp<int>(start, 0, (int)m_text.length());
m_selectionEnd = std::min<int>(std::max<int>(end, 0), (int)m_text.length()); m_selectionEnd = stdext::clamp<int>(end, 0, (int)m_text.length());
} }
void UITextEdit::setTextHidden(bool hidden) void UITextEdit::setTextHidden(bool hidden)

@ -192,7 +192,7 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child)
if(!(index >= 0 && (uint)index <= m_children.size())) { if(!(index >= 0 && (uint)index <= m_children.size())) {
//g_logger.traceWarning("attempt to insert a child UIWidget into an invalid index, using nearest index..."); //g_logger.traceWarning("attempt to insert a child UIWidget into an invalid index, using nearest index...");
index = std::min<int>(std::max<int>(index, 0), (int)m_children.size()); index = stdext::clamp<int>(index, 0, (int)m_children.size());
} }
// retrieve child by index // retrieve child by index

@ -346,7 +346,7 @@ public:
void setPaddingRight(int padding) { m_padding.right = padding; updateLayout(); } void setPaddingRight(int padding) { m_padding.right = padding; updateLayout(); }
void setPaddingBottom(int padding) { m_padding.bottom = padding; updateLayout(); } void setPaddingBottom(int padding) { m_padding.bottom = padding; updateLayout(); }
void setPaddingLeft(int padding) { m_padding.left = padding; updateLayout(); } void setPaddingLeft(int padding) { m_padding.left = padding; updateLayout(); }
void setOpacity(float opacity) { m_opacity = std::min<float>(std::max<float>(opacity, 0.0f), 1.0f); } void setOpacity(float opacity) { m_opacity = stdext::clamp<float>(opacity, 0.0f, 1.0f); }
void setRotation(float degrees) { m_rotation = degrees; } void setRotation(float degrees) { m_rotation = degrees; }
int getX() { return m_rect.x(); } int getX() { return m_rect.x(); }

Loading…
Cancel
Save