From b059027359d69dec21fc5b392378fd00a1d45043 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sat, 20 Jul 2013 12:04:24 +0200 Subject: [PATCH] Add centering mode to AnchorLayout. --- ui/ui_screen.cpp | 6 +++++- ui/viewgroup.cpp | 43 ++++++++++++++++++++++++++++++++----------- ui/viewgroup.h | 9 +++++---- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/ui/ui_screen.cpp b/ui/ui_screen.cpp index 97143bfb39..38f5b64c81 100644 --- a/ui/ui_screen.cpp +++ b/ui/ui_screen.cpp @@ -16,7 +16,11 @@ void UIScreen::update(InputState &input) { recreateViews_ = false; } - UpdateViewHierarchy(input, root_); + if (root_) { + UpdateViewHierarchy(input, root_); + } else { + ELOG("Tried to update without a view root"); + } } void UIScreen::render() { diff --git a/ui/viewgroup.cpp b/ui/viewgroup.cpp index 99b8b7c81a..936e7285db 100644 --- a/ui/viewgroup.cpp +++ b/ui/viewgroup.cpp @@ -608,11 +608,13 @@ void AnchorLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v width = params->width; height = params->height; - if (params->left >= 0 && params->right >= 0) { - width = measuredWidth_ - params->left - params->right; - } - if (params->top >= 0 && params->bottom >= 0) { - height = measuredHeight_ - params->top - params->bottom; + if (!params->center) { + if (params->left >= 0 && params->right >= 0) { + width = measuredWidth_ - params->left - params->right; + } + if (params->top >= 0 && params->bottom >= 0) { + height = measuredHeight_ - params->top - params->bottom; + } } specW = width < 0 ? MeasureSpec(UNSPECIFIED) : MeasureSpec(EXACTLY, width); specH = height < 0 ? MeasureSpec(UNSPECIFIED) : MeasureSpec(EXACTLY, height); @@ -631,24 +633,35 @@ void AnchorLayout::Layout() { vBounds.w = views_[i]->GetMeasuredWidth(); vBounds.h = views_[i]->GetMeasuredHeight(); - float left = 0, top = 0, right = 0, bottom = 0; - + float left = 0, top = 0, right = 0, bottom = 0, center = false; if (params) { left = params->left; top = params->top; right = params->right; bottom = params->bottom; + center = params->center; } - if (left >= 0) + if (left >= 0) { vBounds.x = bounds_.x + left; - else if (right >= 0) + if (center) + vBounds.x -= vBounds.w * 0.5f; + } else if (right >= 0) { vBounds.x = bounds_.x2() - right - vBounds.w; + if (center) { + vBounds.x += vBounds.w * 0.5f; + } + } - if (top >= 0) + if (top >= 0) { vBounds.y = bounds_.y + top; - else if (bottom >= 0) + if (center) + vBounds.y -= vBounds.h * 0.5f; + } else if (bottom >= 0) { vBounds.y = bounds_.y2() - bottom - vBounds.h; + if (center) + vBounds.y += vBounds.h * 0.5f; + } views_[i]->SetBounds(vBounds); views_[i]->Layout(); @@ -796,6 +809,10 @@ bool StringVectorListAdaptor::AddEventCallback(View *view, std::function