diff --git a/ui/view.cpp b/ui/view.cpp index 6234d6533a..7cff1d5d12 100644 --- a/ui/view.cpp +++ b/ui/view.cpp @@ -12,7 +12,7 @@ namespace UI { static View *focusedView; static bool focusMovementEnabled; -const float ITEM_HEIGHT = 48.f; +const float ITEM_HEIGHT = 64.f; View *GetFocusedView() { return focusedView; @@ -49,7 +49,6 @@ void MeasureBySpec(Size sz, float contentWidth, MeasureSpec spec, float *measure } } - void Event::Add(std::function func) { HandlerRegistration reg; reg.func = func; @@ -135,6 +134,16 @@ void Clickable::Update(const InputState &input_state) { } } +Item::Item(LayoutParams *layoutParams) : InertView(layoutParams) { + layoutParams_->width = FILL_PARENT; + layoutParams_->height = ITEM_HEIGHT; +} + +ClickableItem::ClickableItem(LayoutParams *layoutParams) : Clickable(layoutParams) { + layoutParams_->width = FILL_PARENT; + layoutParams_->height = ITEM_HEIGHT; +} + void ClickableItem::Draw(UIContext &dc) { if (down_) { dc.Draw()->DrawImageStretch(dc.theme->whiteImage, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y2(), dc.theme->itemDownStyle.bgColor); diff --git a/ui/view.h b/ui/view.h index 7d5f4466a3..813231eff0 100644 --- a/ui/view.h +++ b/ui/view.h @@ -352,11 +352,7 @@ private: class Item : public InertView { public: - Item(LayoutParams *layoutParams) : InertView(layoutParams) { - layoutParams_->width = FILL_PARENT; - layoutParams_->height = 80; - } - + Item(LayoutParams *layoutParams); virtual void GetContentDimensions(const UIContext &dc, float &w, float &h) const { w = 0.0f; h = 0.0f; @@ -365,11 +361,7 @@ public: class ClickableItem : public Clickable { public: - ClickableItem(LayoutParams *layoutParams) : Clickable(layoutParams) { - layoutParams_->width = FILL_PARENT; - layoutParams_->height = 80; - } - + ClickableItem(LayoutParams *layoutParams); virtual void GetContentDimensions(const UIContext &dc, float &w, float &h) const { w = 0.0f; h = 0.0f; diff --git a/ui/viewgroup.cpp b/ui/viewgroup.cpp index 03ec7757a3..51377993e4 100644 --- a/ui/viewgroup.cpp +++ b/ui/viewgroup.cpp @@ -145,7 +145,6 @@ NeighborResult ViewGroup::FindNeighbor(View *view, FocusDirection direction, Nei if (score > result.score) { result.score = score; result.view = views_[i]; - result.parent = this; } } @@ -451,8 +450,20 @@ bool ScrollView::SubviewFocused(View *view) { } void ScrollView::ScrollTo(float newScrollPos) { - // TODO: Smooth scrolling - scrollPos_ = newScrollPos; + scrollTarget_ = newScrollPos; + scrollToTarget_ = true; +} + +void ScrollView::Update(const InputState &input_state) { + ViewGroup::Update(input_state); + if (scrollToTarget_) { + if (fabsf(scrollTarget_ - scrollPos_) < 0.5f) { + scrollPos_ = scrollTarget_; + scrollToTarget_ = false; + } else { + scrollPos_ += (scrollTarget_ - scrollPos_) * 0.3f; + } + } } void GridLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) { @@ -577,21 +588,25 @@ void LayoutViewHierarchy(const UIContext &dc, ViewGroup *root) { void UpdateViewHierarchy(const InputState &input_state, ViewGroup *root) { if (input_state.pad_buttons_down & (PAD_BUTTON_LEFT | PAD_BUTTON_RIGHT | PAD_BUTTON_UP | PAD_BUTTON_DOWN)) + { EnableFocusMovement(true); + if (!GetFocusedView()) { + root->SetFocus(); + root->SubviewFocused(GetFocusedView()); + } else { + if (input_state.pad_buttons_down & PAD_BUTTON_RIGHT) + MoveFocus(root, FOCUS_RIGHT); + if (input_state.pad_buttons_down & PAD_BUTTON_UP) + MoveFocus(root, FOCUS_UP); + if (input_state.pad_buttons_down & PAD_BUTTON_LEFT) + MoveFocus(root, FOCUS_LEFT); + if (input_state.pad_buttons_down & PAD_BUTTON_DOWN) + MoveFocus(root, FOCUS_DOWN); + } + } if (input_state.pointer_down[0]) EnableFocusMovement(false); - //if (input_state.pad_last_buttons == 0) { - if (input_state.pad_buttons_down & PAD_BUTTON_RIGHT) - MoveFocus(root, FOCUS_RIGHT); - if (input_state.pad_buttons_down & PAD_BUTTON_UP) - MoveFocus(root, FOCUS_UP); - if (input_state.pad_buttons_down & PAD_BUTTON_LEFT) - MoveFocus(root, FOCUS_LEFT); - if (input_state.pad_buttons_down & PAD_BUTTON_DOWN) - MoveFocus(root, FOCUS_DOWN); - //} - root->Update(input_state); } diff --git a/ui/viewgroup.h b/ui/viewgroup.h index 8c55a3387a..a3fe54470e 100644 --- a/ui/viewgroup.h +++ b/ui/viewgroup.h @@ -10,7 +10,6 @@ struct NeighborResult { NeighborResult(View *v, float s) : view(v), score(s) {} View *view; - ViewGroup *parent; float score; }; @@ -150,7 +149,7 @@ private: class ScrollView : public ViewGroup { public: ScrollView(Orientation orientation, LayoutParams *layoutParams = 0) : - ViewGroup(layoutParams), orientation_(orientation), scrollPos_(0) {} + ViewGroup(layoutParams), orientation_(orientation), scrollPos_(0), scrollTarget_(0), scrollToTarget_(false) {} void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert); void Layout(); @@ -159,6 +158,7 @@ public: void Draw(UIContext &dc); void ScrollTo(float newScrollPos); + void Update(const InputState &input_state); // Override so that we can scroll to the active one after moving the focus. virtual bool SubviewFocused(View *view); @@ -169,6 +169,8 @@ private: float scrollPos_; float scrollStart_; float scrollMax_; + float scrollTarget_; + bool scrollToTarget_; }; class ViewPager : public ScrollView {