diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index e69f6865c8..cb9666e951 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -31,13 +31,13 @@ void ApplyGravity(const Bounds outer, const Margins &margins, float w, float h, switch (gravity & G_HORIZMASK) { case G_LEFT: inner.x = outer.x + margins.left; break; case G_RIGHT: inner.x = outer.x + outer.w - w - margins.right; break; - case G_HCENTER: inner.x = outer.x + (outer.w - w) / 2; break; + case G_HCENTER: inner.x = outer.x + (outer.w - w) * 0.5f; break; } switch (gravity & G_VERTMASK) { case G_TOP: inner.y = outer.y + margins.top; break; case G_BOTTOM: inner.y = outer.y + outer.h - h - margins.bottom; break; - case G_VCENTER: inner.y = outer.y + (outer.h - h) / 2; break; + case G_VCENTER: inner.y = outer.y + (outer.h - h) * 0.5f; break; } } @@ -267,11 +267,12 @@ static float HorizontalOverlap(const Bounds &a, const Bounds &b) { // okay they do overlap. Let's clip. float maxMin = std::max(a.x, b.x); float minMax = std::min(a.x2(), b.x2()); + float minW = std::min(a.w, b.w); float overlap = minMax - maxMin; - if (overlap < 0.0f) + if (overlap < 0.0f || minW <= 0.0f) return 0.0f; else - return std::min(1.0f, overlap / std::min(a.w, b.w)); + return std::min(1.0f, overlap / minW); } // Returns the percentage the smaller one overlaps the bigger one. @@ -281,11 +282,12 @@ static float VerticalOverlap(const Bounds &a, const Bounds &b) { // okay they do overlap. Let's clip. float maxMin = std::max(a.y, b.y); float minMax = std::min(a.y2(), b.y2()); + float minH = std::min(a.h, b.h); float overlap = minMax - maxMin; - if (overlap < 0.0f) + if (overlap < 0.0f || minH <= 0.0f) return 0.0f; else - return std::min(1.0f, overlap / std::min(a.h, b.h)); + return std::min(1.0f, overlap / minH); } float GetTargetScore(const Point &originPos, int originIndex, View *origin, View *destination, FocusDirection direction) { @@ -303,6 +305,9 @@ float GetTargetScore(const Point &originPos, int originIndex, View *origin, View float dy = destPos.y - originPos.y; float distance = sqrtf(dx*dx + dy*dy); + if (distance == 0.0f) { + distance = 0.001f; + } float overlap = 0.0f; float dirX = dx / distance; float dirY = dy / distance; @@ -594,6 +599,10 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v if (v.type == UNSPECIFIED && measuredHeight_ != 0.0f) v = MeasureSpec(AT_MOST, measuredHeight_); float unit = (allowedWidth - weightZeroSum) / weightSum; + if (weightSum == 0.0f) { + // We must have gotten an inf. + unit = 1.0f; + } MeasureSpec h(AT_MOST, unit * linLayoutParams->weight - margins.horiz()); if (horiz.type == EXACTLY) { h.type = EXACTLY; @@ -635,6 +644,9 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v if (h.type == UNSPECIFIED && measuredWidth_ != 0.0f) h = MeasureSpec(AT_MOST, measuredWidth_); float unit = (allowedHeight - weightZeroSum) / weightSum; + if (weightSum == 0.0f) { + unit = 1.0f; + } MeasureSpec v(AT_MOST, unit * linLayoutParams->weight - margins.vert()); if (vert.type == EXACTLY) { v.type = EXACTLY; @@ -735,8 +747,8 @@ void FrameLayout::Layout() { bounds.w = w; bounds.h = h; - bounds.x = bounds_.x + (measuredWidth_ - w) / 2; - bounds.y = bounds_.y + (measuredWidth_ - h) / 2; + bounds.x = bounds_.x + (measuredWidth_ - w) * 0.5f; + bounds.y = bounds_.y + (measuredWidth_ - h) * 0.5f; views_[i]->SetBounds(bounds); } } @@ -912,7 +924,7 @@ void ScrollView::Draw(UIContext &dc) { float childHeight = views_[0]->GetBounds().h; float scrollMax = std::max(0.0f, childHeight - bounds_.h); - float ratio = bounds_.h / views_[0]->GetBounds().h; + float ratio = bounds_.h / std::max(0.01f, views_[0]->GetBounds().h); float bobWidth = 5; if (ratio < 1.0f && scrollMax > 0.0f) { @@ -1054,7 +1066,7 @@ void ScrollView::ScrollRelative(float distance) { } float ScrollView::ClampedScrollPos(float pos) { - if (!views_.size()) { + if (!views_.size() || bounds_.h == 0.0f) { return 0.0f; } @@ -1063,7 +1075,7 @@ float ScrollView::ClampedScrollPos(float pos) { Gesture gesture = orientation_ == ORIENT_VERTICAL ? GESTURE_DRAG_VERTICAL : GESTURE_DRAG_HORIZONTAL; - if (scrollTouchId_ >= 0 && gesture_.IsGestureActive(gesture, scrollTouchId_) && bounds_.h > 0) { + if (scrollTouchId_ >= 0 && gesture_.IsGestureActive(gesture, scrollTouchId_) && bounds_.h > 0.0f) { float maxPull = bounds_.h * 0.1f; if (pos < 0.0f) { float dist = std::min(-pos * (1.0f / bounds_.h), 1.0f); @@ -1275,7 +1287,7 @@ void AnchorLayout::Layout() { } GridLayout::GridLayout(GridLayoutSettings settings, LayoutParams *layoutParams) - : ViewGroup(layoutParams), settings_(settings), numColumns_(1) { + : ViewGroup(layoutParams), settings_(settings) { if (settings.orientation != ORIENT_HORIZONTAL) ERROR_LOG(SYSTEM, "GridLayout: Vertical layouts not yet supported"); } @@ -1456,7 +1468,7 @@ void TabHolder::PersistData(PersistStatus status, std::string anonId, PersistMap } ChoiceStrip::ChoiceStrip(Orientation orientation, LayoutParams *layoutParams) - : LinearLayout(orientation, layoutParams), selected_(0), topTabs_(false) { + : LinearLayout(orientation, layoutParams) { SetSpacing(0.0f); } diff --git a/Common/UI/ViewGroup.h b/Common/UI/ViewGroup.h index c5ee4032dd..b789935592 100644 --- a/Common/UI/ViewGroup.h +++ b/Common/UI/ViewGroup.h @@ -113,9 +113,7 @@ const float NONE = -FLT_MAX; class AnchorLayoutParams : public LayoutParams { public: AnchorLayoutParams(Size w, Size h, float l, float t, float r, float b, bool c = false) - : LayoutParams(w, h, LP_ANCHOR), left(l), top(t), right(r), bottom(b), center(c) { - - } + : LayoutParams(w, h, LP_ANCHOR), left(l), top(t), right(r), bottom(b), center(c) {} // There's a small hack here to make this behave more intuitively - AnchorLayout ordinarily ignores FILL_PARENT. AnchorLayoutParams(Size w, Size h, bool c = false) : LayoutParams(w, h, LP_ANCHOR), left(0), top(0), right(w == FILL_PARENT ? 0 : NONE), bottom(h == FILL_PARENT ? 0 : NONE), center(c) { @@ -135,7 +133,7 @@ public: class AnchorLayout : public ViewGroup { public: - AnchorLayout(LayoutParams *layoutParams = 0) : ViewGroup(layoutParams), overflow_(true) {} + AnchorLayout(LayoutParams *layoutParams = 0) : ViewGroup(layoutParams) {} void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override; void Layout() override; void Overflow(bool allow) { @@ -145,7 +143,7 @@ public: private: void MeasureViews(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert); - bool overflow_; + bool overflow_ = true; }; class LinearLayoutParams : public LayoutParams { @@ -157,7 +155,7 @@ public: LinearLayoutParams(float wgt, const Margins &mgn) : LayoutParams(LP_LINEAR), weight(wgt), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {} LinearLayoutParams(Size w, Size h, float wgt = 0.0f, Gravity grav = G_TOPLEFT) - : LayoutParams(w, h, LP_LINEAR), weight(wgt), gravity(grav), hasMargins_(false) {} + : LayoutParams(w, h, LP_LINEAR), weight(wgt), gravity(grav), margins(0), hasMargins_(false) {} LinearLayoutParams(Size w, Size h, float wgt, Gravity grav, const Margins &mgn) : LayoutParams(w, h, LP_LINEAR), weight(wgt), gravity(grav), margins(mgn), hasMargins_(true) {} LinearLayoutParams(Size w, Size h, const Margins &mgn) @@ -184,7 +182,7 @@ private: class LinearLayout : public ViewGroup { public: LinearLayout(Orientation orientation, LayoutParams *layoutParams = 0) - : ViewGroup(layoutParams), orientation_(orientation), defaultMargins_(0), spacing_(10) {} + : ViewGroup(layoutParams), orientation_(orientation), defaultMargins_(0) {} void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override; void Layout() override; @@ -197,7 +195,7 @@ protected: Orientation orientation_; private: Margins defaultMargins_; - float spacing_; + float spacing_ = 10.0f; }; class LinearLayoutList : public LinearLayout { @@ -249,7 +247,7 @@ public: private: GridLayoutSettings settings_; - int numColumns_; + int numColumns_ = 1; }; class GridLayoutList : public GridLayout { @@ -317,11 +315,6 @@ private: static float lastScrollPosY; }; -class ViewPager : public ScrollView { -public: -}; - - class ChoiceStrip : public LinearLayout { public: ChoiceStrip(Orientation orientation, LayoutParams *layoutParams = 0); @@ -348,8 +341,8 @@ private: StickyChoice *Choice(int index); EventReturn OnChoiceClick(EventParams &e); - int selected_; - bool topTabs_; // Can be controlled with L/R. + int selected_ = 0; // Can be controlled with L/R. + bool topTabs_ = false; }; diff --git a/UI/TouchControlLayoutScreen.cpp b/UI/TouchControlLayoutScreen.cpp index 12012332a1..53931db685 100644 --- a/UI/TouchControlLayoutScreen.cpp +++ b/UI/TouchControlLayoutScreen.cpp @@ -618,7 +618,7 @@ void TouchControlLayoutScreen::CreateViews() { InitPadLayout(bounds.w, bounds.h); const float leftColumnWidth = 170.0f; - layoutAreaScale = 1.0-(leftColumnWidth+10)/bounds.w; + layoutAreaScale = 1.0f - (leftColumnWidth + 10.0f) / std::max(bounds.w, 1.0f); auto co = GetI18NCategory("Controls"); auto di = GetI18NCategory("Dialog");