2013-05-03 00:21:39 +02:00
|
|
|
#pragma once
|
|
|
|
|
2017-12-10 14:08:20 -08:00
|
|
|
#include <cfloat>
|
2014-01-22 16:29:59 +01:00
|
|
|
#include <vector>
|
2016-02-25 15:39:17 +01:00
|
|
|
#include <set>
|
2017-02-27 21:57:46 +01:00
|
|
|
#include <mutex>
|
2014-01-22 16:29:59 +01:00
|
|
|
|
2020-10-04 00:25:21 +02:00
|
|
|
#include "Common/Math/geom2d.h"
|
2020-10-01 09:36:43 +02:00
|
|
|
#include "Common/Input/GestureDetector.h"
|
2020-10-04 20:48:47 +02:00
|
|
|
#include "Common/UI/View.h"
|
2013-05-03 00:21:39 +02:00
|
|
|
|
|
|
|
namespace UI {
|
|
|
|
|
2017-12-10 14:08:49 -08:00
|
|
|
class AnchorTranslateTween;
|
|
|
|
|
2013-05-27 00:54:02 +02:00
|
|
|
struct NeighborResult {
|
|
|
|
NeighborResult() : view(0), score(0) {}
|
|
|
|
NeighborResult(View *v, float s) : view(v), score(s) {}
|
|
|
|
|
|
|
|
View *view;
|
|
|
|
float score;
|
|
|
|
};
|
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
class ViewGroup : public View {
|
|
|
|
public:
|
2017-12-29 17:55:37 -08:00
|
|
|
ViewGroup(LayoutParams *layoutParams = 0) : View(layoutParams) {}
|
2022-12-10 20:32:12 -08:00
|
|
|
~ViewGroup();
|
2013-05-03 00:21:39 +02:00
|
|
|
|
|
|
|
// Pass through external events to children.
|
2022-12-10 20:32:12 -08:00
|
|
|
bool Key(const KeyInput &input) override;
|
|
|
|
bool Touch(const TouchInput &input) override;
|
|
|
|
void Axis(const AxisInput &input) override;
|
2013-05-03 00:21:39 +02:00
|
|
|
|
|
|
|
// By default, a container will layout to its own bounds.
|
2022-12-10 20:32:12 -08:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override = 0;
|
|
|
|
void Layout() override = 0;
|
|
|
|
void Update() override;
|
|
|
|
void Query(float x, float y, std::vector<View *> &list) override;
|
2018-03-27 23:10:33 +02:00
|
|
|
|
2022-12-10 20:32:12 -08:00
|
|
|
void DeviceLost() override;
|
|
|
|
void DeviceRestored(Draw::DrawContext *draw) override;
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2022-12-10 20:32:12 -08:00
|
|
|
void Draw(UIContext &dc) override;
|
2013-05-03 00:21:39 +02:00
|
|
|
|
|
|
|
// Takes ownership! DO NOT add a view to multiple parents!
|
2013-06-01 18:59:03 +02:00
|
|
|
template <class T>
|
2014-08-17 12:17:59 +02:00
|
|
|
T *Add(T *view) {
|
2017-02-27 21:57:46 +01:00
|
|
|
std::lock_guard<std::mutex> guard(modifyLock_);
|
2014-08-17 12:17:59 +02:00
|
|
|
views_.push_back(view);
|
|
|
|
return view;
|
|
|
|
}
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2022-12-10 20:32:12 -08:00
|
|
|
bool SetFocus() override;
|
|
|
|
bool SubviewFocused(View *view) override;
|
2013-08-16 12:51:57 +02:00
|
|
|
virtual void RemoveSubview(View *view);
|
2013-05-25 12:40:57 +02:00
|
|
|
|
2014-03-03 11:39:06 +01:00
|
|
|
void SetDefaultFocusView(View *view) { defaultFocusView_ = view; }
|
|
|
|
View *GetDefaultFocusView() { return defaultFocusView_; }
|
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
// Assumes that layout has taken place.
|
2013-05-27 00:54:02 +02:00
|
|
|
NeighborResult FindNeighbor(View *view, FocusDirection direction, NeighborResult best);
|
2021-08-08 14:46:05 -07:00
|
|
|
virtual NeighborResult FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best);
|
2013-12-06 16:44:39 +01:00
|
|
|
|
2021-08-08 14:46:05 -07:00
|
|
|
bool CanBeFocused() const override { return false; }
|
|
|
|
bool IsViewGroup() const override { return true; }
|
|
|
|
bool ContainsSubview(const View *view) const override;
|
2013-05-25 12:40:57 +02:00
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
virtual void SetBG(const Drawable &bg) { bg_ = bg; }
|
|
|
|
|
|
|
|
virtual void Clear();
|
2016-01-23 01:59:25 -08:00
|
|
|
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
|
2013-07-16 00:25:08 +02:00
|
|
|
View *GetViewByIndex(int index) { return views_[index]; }
|
2014-11-16 16:44:07 +01:00
|
|
|
int GetNumSubviews() const { return (int)views_.size(); }
|
2013-07-16 00:25:08 +02:00
|
|
|
void SetHasDropShadow(bool has) { hasDropShadow_ = has; }
|
2017-03-19 15:41:48 -07:00
|
|
|
void SetDropShadowExpand(float s) { dropShadowExpand_ = s; }
|
2022-11-27 16:15:16 +01:00
|
|
|
void SetExclusiveTouch(bool exclusive) { exclusiveTouch_ = exclusive; }
|
2022-12-05 13:19:22 +01:00
|
|
|
void SetClickableBackground(bool clickableBackground) { clickableBackground_ = clickableBackground; }
|
2013-07-16 00:25:08 +02:00
|
|
|
|
2013-08-18 22:29:50 +02:00
|
|
|
void Lock() { modifyLock_.lock(); }
|
|
|
|
void Unlock() { modifyLock_.unlock(); }
|
2013-08-20 12:27:42 +02:00
|
|
|
|
|
|
|
void SetClip(bool clip) { clip_ = clip; }
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "ViewGroup: " + View::DescribeLog(); }
|
2021-02-21 16:38:02 -08:00
|
|
|
std::string DescribeText() const override;
|
2013-08-20 12:27:42 +02:00
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
protected:
|
2021-02-21 18:41:08 -08:00
|
|
|
std::string DescribeListUnordered(const char *heading) const;
|
|
|
|
std::string DescribeListOrdered(const char *heading) const;
|
|
|
|
|
2017-02-27 21:57:46 +01:00
|
|
|
std::mutex modifyLock_; // Hold this when changing the subviews.
|
2013-05-03 00:21:39 +02:00
|
|
|
std::vector<View *> views_;
|
2017-12-29 17:55:37 -08:00
|
|
|
View *defaultFocusView_ = nullptr;
|
2013-07-16 00:25:08 +02:00
|
|
|
Drawable bg_;
|
2017-03-19 15:41:48 -07:00
|
|
|
float dropShadowExpand_ = 0.0f;
|
2017-12-29 17:55:37 -08:00
|
|
|
bool hasDropShadow_ = false;
|
2022-12-05 13:19:22 +01:00
|
|
|
bool clickableBackground_ = false;
|
2017-12-29 17:55:37 -08:00
|
|
|
bool clip_ = false;
|
2022-11-27 16:15:16 +01:00
|
|
|
bool exclusiveTouch_ = false;
|
2013-05-03 00:21:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// A frame layout contains a single child view (normally).
|
2013-06-09 11:18:57 +02:00
|
|
|
// It simply centers the child view.
|
2013-05-03 00:21:39 +02:00
|
|
|
class FrameLayout : public ViewGroup {
|
|
|
|
public:
|
2022-12-08 00:01:46 +01:00
|
|
|
FrameLayout(LayoutParams *layoutParams = nullptr) : ViewGroup(layoutParams) {}
|
2015-09-17 20:29:37 +02:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
|
|
|
|
void Layout() override;
|
2013-05-03 00:21:39 +02:00
|
|
|
};
|
|
|
|
|
2017-12-10 14:08:20 -08:00
|
|
|
const float NONE = -FLT_MAX;
|
2013-05-27 22:22:35 +02:00
|
|
|
|
|
|
|
class AnchorLayoutParams : public LayoutParams {
|
2013-05-03 00:21:39 +02:00
|
|
|
public:
|
2013-07-20 12:04:24 +02:00
|
|
|
AnchorLayoutParams(Size w, Size h, float l, float t, float r, float b, bool c = false)
|
2022-11-30 10:05:54 +01:00
|
|
|
: LayoutParams(w, h, LP_ANCHOR), left(l), top(t), right(r), bottom(b), center(c) {}
|
2021-06-05 23:49:39 +02:00
|
|
|
// There's a small hack here to make this behave more intuitively - AnchorLayout ordinarily ignores FILL_PARENT.
|
2013-08-10 23:03:12 +02:00
|
|
|
AnchorLayoutParams(Size w, Size h, bool c = false)
|
2021-06-05 23:49:39 +02:00
|
|
|
: LayoutParams(w, h, LP_ANCHOR), left(0), top(0), right(w == FILL_PARENT ? 0 : NONE), bottom(h == FILL_PARENT ? 0 : NONE), center(c) {
|
2013-08-10 23:03:12 +02:00
|
|
|
}
|
2013-07-20 12:04:24 +02:00
|
|
|
AnchorLayoutParams(float l, float t, float r, float b, bool c = false)
|
|
|
|
: LayoutParams(WRAP_CONTENT, WRAP_CONTENT, LP_ANCHOR), left(l), top(t), right(r), bottom(b), center(c) {}
|
2013-05-27 22:22:35 +02:00
|
|
|
|
|
|
|
// These are not bounds, but distances from the container edges.
|
|
|
|
// Set to NONE to not attach this edge to the container.
|
2022-12-03 19:13:21 +01:00
|
|
|
// If two opposite edges are NONE, centering will happen.
|
2013-05-27 22:22:35 +02:00
|
|
|
float left, top, right, bottom;
|
2013-07-20 12:04:24 +02:00
|
|
|
bool center; // If set, only two "sides" can be set, and they refer to the center, not the edge, of the view being layouted.
|
2015-12-22 20:07:37 -08:00
|
|
|
|
|
|
|
static LayoutParamsType StaticType() {
|
|
|
|
return LP_ANCHOR;
|
|
|
|
}
|
2013-05-27 22:22:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class AnchorLayout : public ViewGroup {
|
|
|
|
public:
|
2022-11-30 10:05:54 +01:00
|
|
|
AnchorLayout(LayoutParams *layoutParams = 0) : ViewGroup(layoutParams) {}
|
2015-10-31 13:43:33 +01:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
|
|
|
|
void Layout() override;
|
2015-12-22 19:52:23 -08:00
|
|
|
void Overflow(bool allow) {
|
|
|
|
overflow_ = allow;
|
|
|
|
}
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "AnchorLayout: " + View::DescribeLog(); }
|
2015-12-22 19:52:23 -08:00
|
|
|
|
|
|
|
private:
|
2017-12-12 00:19:38 -08:00
|
|
|
void MeasureViews(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert);
|
2022-11-30 10:05:54 +01:00
|
|
|
bool overflow_ = true;
|
2013-05-03 00:21:39 +02:00
|
|
|
};
|
|
|
|
|
2013-05-27 22:22:35 +02:00
|
|
|
class LinearLayoutParams : public LayoutParams {
|
|
|
|
public:
|
|
|
|
LinearLayoutParams()
|
2013-06-09 13:01:36 +02:00
|
|
|
: LayoutParams(LP_LINEAR), weight(0.0f), gravity(G_TOPLEFT), hasMargins_(false) {}
|
2013-06-01 18:59:03 +02:00
|
|
|
explicit LinearLayoutParams(float wgt, Gravity grav = G_TOPLEFT)
|
2013-06-09 13:01:36 +02:00
|
|
|
: LayoutParams(LP_LINEAR), weight(wgt), gravity(grav), hasMargins_(false) {}
|
2013-06-01 18:59:03 +02:00
|
|
|
LinearLayoutParams(float wgt, const Margins &mgn)
|
2013-06-09 13:01:36 +02:00
|
|
|
: LayoutParams(LP_LINEAR), weight(wgt), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {}
|
2013-05-27 22:22:35 +02:00
|
|
|
LinearLayoutParams(Size w, Size h, float wgt = 0.0f, Gravity grav = G_TOPLEFT)
|
2022-11-30 10:05:54 +01:00
|
|
|
: LayoutParams(w, h, LP_LINEAR), weight(wgt), gravity(grav), margins(0), hasMargins_(false) {}
|
2013-05-27 22:22:35 +02:00
|
|
|
LinearLayoutParams(Size w, Size h, float wgt, Gravity grav, const Margins &mgn)
|
2013-06-09 13:01:36 +02:00
|
|
|
: LayoutParams(w, h, LP_LINEAR), weight(wgt), gravity(grav), margins(mgn), hasMargins_(true) {}
|
2013-06-08 22:41:17 +02:00
|
|
|
LinearLayoutParams(Size w, Size h, const Margins &mgn)
|
2013-06-09 13:01:36 +02:00
|
|
|
: LayoutParams(w, h, LP_LINEAR), weight(0.0f), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {}
|
2013-10-08 11:06:38 +02:00
|
|
|
LinearLayoutParams(Size w, Size h, float wgt, const Margins &mgn)
|
|
|
|
: LayoutParams(w, h, LP_LINEAR), weight(wgt), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {}
|
2013-05-27 22:22:35 +02:00
|
|
|
LinearLayoutParams(const Margins &mgn)
|
2013-06-09 13:01:36 +02:00
|
|
|
: LayoutParams(WRAP_CONTENT, WRAP_CONTENT, LP_LINEAR), weight(0.0f), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {}
|
2013-05-27 22:22:35 +02:00
|
|
|
|
|
|
|
float weight;
|
|
|
|
Gravity gravity;
|
|
|
|
Margins margins;
|
|
|
|
|
|
|
|
bool HasMargins() const { return hasMargins_; }
|
|
|
|
|
2015-12-22 20:07:37 -08:00
|
|
|
static LayoutParamsType StaticType() {
|
|
|
|
return LP_LINEAR;
|
|
|
|
}
|
|
|
|
|
2013-05-27 22:22:35 +02:00
|
|
|
private:
|
|
|
|
bool hasMargins_;
|
|
|
|
};
|
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
class LinearLayout : public ViewGroup {
|
|
|
|
public:
|
|
|
|
LinearLayout(Orientation orientation, LayoutParams *layoutParams = 0)
|
2022-11-30 10:05:54 +01:00
|
|
|
: ViewGroup(layoutParams), orientation_(orientation), defaultMargins_(0) {}
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2015-10-31 13:43:33 +01:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
|
|
|
|
void Layout() override;
|
2013-07-17 01:03:29 +02:00
|
|
|
void SetSpacing(float spacing) {
|
|
|
|
spacing_ = spacing;
|
|
|
|
}
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return (orientation_ == ORIENT_HORIZONTAL ? "LinearLayoutHoriz: " : "LinearLayoutVert: ") + View::DescribeLog(); }
|
2022-12-08 00:01:46 +01:00
|
|
|
Margins padding;
|
2015-10-31 13:43:33 +01:00
|
|
|
|
2013-08-20 12:27:42 +02:00
|
|
|
protected:
|
2013-05-03 00:21:39 +02:00
|
|
|
Orientation orientation_;
|
2013-08-20 12:27:42 +02:00
|
|
|
private:
|
2013-05-03 00:21:39 +02:00
|
|
|
Margins defaultMargins_;
|
2022-11-30 10:05:54 +01:00
|
|
|
float spacing_ = 10.0f;
|
2013-05-03 00:21:39 +02:00
|
|
|
};
|
|
|
|
|
2021-02-21 18:41:08 -08:00
|
|
|
class LinearLayoutList : public LinearLayout {
|
|
|
|
public:
|
|
|
|
LinearLayoutList(Orientation orientation, LayoutParams *layoutParams = nullptr)
|
|
|
|
: LinearLayout(orientation, layoutParams) {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DescribeText() const override;
|
|
|
|
};
|
|
|
|
|
2013-05-27 00:54:02 +02:00
|
|
|
// GridLayout is a little different from the Android layout. This one has fixed size
|
|
|
|
// rows and columns. Items are not allowed to deviate from the set sizes.
|
|
|
|
// Initially, only horizontal layout is supported.
|
|
|
|
struct GridLayoutSettings {
|
|
|
|
GridLayoutSettings() : orientation(ORIENT_HORIZONTAL), columnWidth(100), rowHeight(50), spacing(5), fillCells(false) {}
|
2013-12-06 16:44:39 +01:00
|
|
|
GridLayoutSettings(int colW, int colH, int spac = 5)
|
|
|
|
: orientation(ORIENT_HORIZONTAL), columnWidth(colW), rowHeight(colH), spacing(spac), fillCells(false) {}
|
2013-05-27 00:54:02 +02:00
|
|
|
|
|
|
|
Orientation orientation;
|
|
|
|
int columnWidth;
|
|
|
|
int rowHeight;
|
|
|
|
int spacing;
|
|
|
|
bool fillCells;
|
|
|
|
};
|
|
|
|
|
2021-08-29 14:10:14 -07:00
|
|
|
class GridLayoutParams : public LayoutParams {
|
|
|
|
public:
|
|
|
|
GridLayoutParams()
|
|
|
|
: LayoutParams(LP_GRID), gravity(G_CENTER) {}
|
|
|
|
explicit GridLayoutParams(Gravity grav)
|
|
|
|
: LayoutParams(LP_GRID), gravity(grav) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Gravity gravity;
|
|
|
|
|
|
|
|
static LayoutParamsType StaticType() {
|
|
|
|
return LP_GRID;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
class GridLayout : public ViewGroup {
|
|
|
|
public:
|
2020-08-15 12:12:57 +02:00
|
|
|
GridLayout(GridLayoutSettings settings, LayoutParams *layoutParams = 0);
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2015-10-31 13:43:33 +01:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
|
|
|
|
void Layout() override;
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "GridLayout: " + View::DescribeLog(); }
|
2013-05-03 00:21:39 +02:00
|
|
|
|
|
|
|
private:
|
2013-05-27 00:54:02 +02:00
|
|
|
GridLayoutSettings settings_;
|
2022-11-30 10:05:54 +01:00
|
|
|
int numColumns_ = 1;
|
2013-05-03 00:21:39 +02:00
|
|
|
};
|
|
|
|
|
2021-02-21 18:41:08 -08:00
|
|
|
class GridLayoutList : public GridLayout {
|
|
|
|
public:
|
|
|
|
GridLayoutList(GridLayoutSettings settings, LayoutParams *layoutParams = nullptr)
|
|
|
|
: GridLayout(settings, layoutParams) {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DescribeText() const override;
|
|
|
|
};
|
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
// A scrollview usually contains just a single child - a linear layout or similar.
|
|
|
|
class ScrollView : public ViewGroup {
|
|
|
|
public:
|
2021-09-27 23:25:41 -07:00
|
|
|
ScrollView(Orientation orientation, LayoutParams *layoutParams = 0)
|
|
|
|
: ViewGroup(layoutParams), orientation_(orientation) {}
|
2021-09-10 02:06:51 +02:00
|
|
|
~ScrollView();
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2015-10-31 13:43:33 +01:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
|
|
|
|
void Layout() override;
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2015-10-31 13:43:33 +01:00
|
|
|
bool Key(const KeyInput &input) override;
|
2022-11-27 16:15:16 +01:00
|
|
|
bool Touch(const TouchInput &input) override;
|
2015-10-31 13:43:33 +01:00
|
|
|
void Draw(UIContext &dc) override;
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "ScrollView: " + View::DescribeLog(); }
|
2013-05-03 00:21:39 +02:00
|
|
|
|
2013-05-27 21:39:56 +02:00
|
|
|
void ScrollTo(float newScrollPos);
|
2015-01-05 01:19:49 +01:00
|
|
|
void ScrollToBottom();
|
2013-07-08 12:34:39 +02:00
|
|
|
void ScrollRelative(float distance);
|
2013-08-10 23:03:12 +02:00
|
|
|
bool CanScroll() const;
|
2017-03-14 22:01:18 -07:00
|
|
|
void Update() override;
|
2013-05-27 21:39:56 +02:00
|
|
|
|
2021-09-27 23:25:41 -07:00
|
|
|
void RememberPosition(float *pos) {
|
|
|
|
rememberPos_ = pos;
|
|
|
|
ScrollTo(*pos);
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:06:51 +02:00
|
|
|
// Get the last moved scroll view position
|
|
|
|
static void GetLastScrollPosition(float &x, float &y);
|
|
|
|
|
2013-05-27 00:54:02 +02:00
|
|
|
// Override so that we can scroll to the active one after moving the focus.
|
2016-01-22 23:02:44 -08:00
|
|
|
bool SubviewFocused(View *view) override;
|
2016-01-23 01:59:25 -08:00
|
|
|
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
|
2016-01-23 09:12:56 -08:00
|
|
|
void SetVisibility(Visibility visibility) override;
|
2013-05-27 00:54:02 +02:00
|
|
|
|
2022-11-30 10:56:29 +01:00
|
|
|
// If the view is smaller than the scroll view, sets whether to align to the bottom/right instead of the left.
|
|
|
|
void SetAlignOpposite(bool alignOpposite) {
|
|
|
|
alignOpposite_ = alignOpposite;
|
|
|
|
}
|
|
|
|
|
2021-08-08 14:46:05 -07:00
|
|
|
NeighborResult FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best) override;
|
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
private:
|
2016-01-25 21:53:41 -08:00
|
|
|
float ClampedScrollPos(float pos);
|
2013-07-08 12:34:39 +02:00
|
|
|
|
2013-05-03 00:21:39 +02:00
|
|
|
GestureDetector gesture_;
|
|
|
|
Orientation orientation_;
|
2017-04-05 09:57:26 +02:00
|
|
|
float scrollPos_ = 0.0f;
|
|
|
|
float scrollStart_ = 0.0f;
|
|
|
|
float scrollTarget_ = 0.0f;
|
|
|
|
int scrollTouchId_ = -1;
|
|
|
|
bool scrollToTarget_ = false;
|
2021-08-29 17:35:58 -07:00
|
|
|
float layoutScrollPos_ = 0.0f;
|
2017-04-05 09:57:26 +02:00
|
|
|
float inertia_ = 0.0f;
|
|
|
|
float pull_ = 0.0f;
|
|
|
|
float lastViewSize_ = 0.0f;
|
2021-09-27 23:25:41 -07:00
|
|
|
float *rememberPos_ = nullptr;
|
2022-11-30 10:56:29 +01:00
|
|
|
bool alignOpposite_ = false;
|
2021-09-10 02:06:51 +02:00
|
|
|
|
|
|
|
static float lastScrollPosX;
|
|
|
|
static float lastScrollPosY;
|
2013-05-03 00:21:39 +02:00
|
|
|
};
|
|
|
|
|
2013-07-15 19:56:36 +02:00
|
|
|
class ChoiceStrip : public LinearLayout {
|
|
|
|
public:
|
2013-12-06 16:44:39 +01:00
|
|
|
ChoiceStrip(Orientation orientation, LayoutParams *layoutParams = 0);
|
2013-07-15 19:56:36 +02:00
|
|
|
|
|
|
|
void AddChoice(const std::string &title);
|
2013-08-20 00:34:54 +02:00
|
|
|
void AddChoice(ImageID buttonImage);
|
2013-10-07 21:45:25 +05:30
|
|
|
|
2013-07-15 19:56:36 +02:00
|
|
|
int GetSelection() const { return selected_; }
|
2021-02-28 12:25:37 -08:00
|
|
|
void SetSelection(int sel, bool triggerClick);
|
2013-10-07 21:45:25 +05:30
|
|
|
|
2021-04-25 22:22:13 -07:00
|
|
|
void EnableChoice(int choice, bool enabled);
|
2013-10-07 21:45:25 +05:30
|
|
|
|
2015-02-03 00:10:38 +01:00
|
|
|
bool Key(const KeyInput &input) override;
|
2013-10-07 21:45:25 +05:30
|
|
|
|
2013-08-17 12:06:08 +02:00
|
|
|
void SetTopTabs(bool tabs) { topTabs_ = tabs; }
|
2015-02-03 00:10:38 +01:00
|
|
|
void Draw(UIContext &dc) override;
|
2013-10-07 21:45:25 +05:30
|
|
|
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "ChoiceStrip: " + View::DescribeLog(); }
|
2021-02-21 18:41:08 -08:00
|
|
|
std::string DescribeText() const override;
|
2015-10-31 13:43:33 +01:00
|
|
|
|
2013-07-15 19:56:36 +02:00
|
|
|
Event OnChoice;
|
|
|
|
|
|
|
|
private:
|
2016-01-23 10:52:16 -08:00
|
|
|
StickyChoice *Choice(int index);
|
2013-07-15 19:56:36 +02:00
|
|
|
EventReturn OnChoiceClick(EventParams &e);
|
|
|
|
|
2022-11-30 10:05:54 +01:00
|
|
|
int selected_ = 0; // Can be controlled with L/R.
|
|
|
|
bool topTabs_ = false;
|
2013-07-15 19:56:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-08 22:41:17 +02:00
|
|
|
class TabHolder : public LinearLayout {
|
|
|
|
public:
|
2015-02-24 00:22:21 +01:00
|
|
|
TabHolder(Orientation orientation, float stripSize, LayoutParams *layoutParams = 0);
|
2013-06-08 22:41:17 +02:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T *AddTab(const std::string &title, T *tabContents) {
|
2017-12-10 14:08:49 -08:00
|
|
|
AddTabContents(title, (View *)tabContents);
|
2013-06-08 22:41:17 +02:00
|
|
|
return tabContents;
|
|
|
|
}
|
2021-04-25 22:22:13 -07:00
|
|
|
void EnableTab(int tab, bool enabled) {
|
|
|
|
tabStrip_->EnableChoice(tab, enabled);
|
|
|
|
}
|
2013-06-08 22:41:17 +02:00
|
|
|
|
2017-12-12 21:34:17 -08:00
|
|
|
void SetCurrentTab(int tab, bool skipTween = false);
|
2013-06-08 22:41:17 +02:00
|
|
|
|
2013-08-27 19:43:40 +02:00
|
|
|
int GetCurrentTab() const { return currentTab_; }
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "TabHolder: " + View::DescribeLog(); }
|
2013-08-27 19:43:40 +02:00
|
|
|
|
2016-01-23 01:59:25 -08:00
|
|
|
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
|
2016-01-22 22:40:36 -08:00
|
|
|
|
2013-06-08 22:41:17 +02:00
|
|
|
private:
|
2017-12-10 14:08:49 -08:00
|
|
|
void AddTabContents(const std::string &title, View *tabContents);
|
2013-06-08 22:41:17 +02:00
|
|
|
EventReturn OnTabClick(EventParams &e);
|
|
|
|
|
2017-12-10 14:08:49 -08:00
|
|
|
ChoiceStrip *tabStrip_ = nullptr;
|
|
|
|
ScrollView *tabScroll_ = nullptr;
|
|
|
|
AnchorLayout *contents_ = nullptr;
|
2013-06-08 22:41:17 +02:00
|
|
|
|
|
|
|
float stripSize_;
|
2017-12-10 14:08:49 -08:00
|
|
|
int currentTab_ = 0;
|
2013-06-08 22:41:17 +02:00
|
|
|
std::vector<View *> tabs_;
|
2017-12-10 14:08:49 -08:00
|
|
|
std::vector<AnchorTranslateTween *> tabTweens_;
|
2013-06-08 22:41:17 +02:00
|
|
|
};
|
|
|
|
|
2013-06-09 11:18:57 +02:00
|
|
|
// Yes, this feels a bit Java-ish...
|
|
|
|
class ListAdaptor {
|
|
|
|
public:
|
|
|
|
virtual ~ListAdaptor() {}
|
|
|
|
virtual View *CreateItemView(int index) = 0;
|
|
|
|
virtual int GetNumItems() = 0;
|
|
|
|
virtual bool AddEventCallback(View *view, std::function<EventReturn(EventParams&)> callback) { return false; }
|
2014-05-19 23:28:11 +02:00
|
|
|
virtual std::string GetTitle(int index) const { return ""; }
|
2013-07-16 00:25:08 +02:00
|
|
|
virtual void SetSelected(int sel) { }
|
|
|
|
virtual int GetSelected() { return -1; }
|
2013-06-09 11:18:57 +02:00
|
|
|
};
|
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
class ChoiceListAdaptor : public ListAdaptor {
|
2013-06-09 11:18:57 +02:00
|
|
|
public:
|
|
|
|
ChoiceListAdaptor(const char *items[], int numItems) : items_(items), numItems_(numItems) {}
|
2022-12-10 20:32:12 -08:00
|
|
|
View *CreateItemView(int index) override;
|
|
|
|
int GetNumItems() override { return numItems_; }
|
|
|
|
bool AddEventCallback(View *view, std::function<EventReturn(EventParams&)> callback) override;
|
2013-06-09 11:18:57 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char **items_;
|
|
|
|
int numItems_;
|
|
|
|
};
|
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
|
|
|
|
// The "selected" item is what was previously selected (optional). This items will be drawn differently.
|
|
|
|
class StringVectorListAdaptor : public ListAdaptor {
|
|
|
|
public:
|
|
|
|
StringVectorListAdaptor() : selected_(-1) {}
|
|
|
|
StringVectorListAdaptor(const std::vector<std::string> &items, int selected = -1) : items_(items), selected_(selected) {}
|
2022-12-10 20:32:12 -08:00
|
|
|
View *CreateItemView(int index) override;
|
|
|
|
int GetNumItems() override { return (int)items_.size(); }
|
|
|
|
bool AddEventCallback(View *view, std::function<EventReturn(EventParams&)> callback) override;
|
2015-09-17 20:29:37 +02:00
|
|
|
void SetSelected(int sel) override { selected_ = sel; }
|
2022-12-10 20:32:12 -08:00
|
|
|
std::string GetTitle(int index) const override { return items_[index]; }
|
|
|
|
int GetSelected() override { return selected_; }
|
2013-07-16 00:25:08 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::string> items_;
|
|
|
|
int selected_;
|
|
|
|
};
|
|
|
|
|
2013-06-09 11:18:57 +02:00
|
|
|
// A list view is a scroll view with autogenerated items.
|
|
|
|
// In the future, it might be smart and load/unload items as they go, but currently not.
|
|
|
|
class ListView : public ScrollView {
|
|
|
|
public:
|
2016-02-25 15:39:17 +01:00
|
|
|
ListView(ListAdaptor *a, std::set<int> hidden = std::set<int>(), LayoutParams *layoutParams = 0);
|
2013-10-23 13:55:11 +02:00
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
int GetSelected() { return adaptor_->GetSelected(); }
|
2022-12-10 20:32:12 -08:00
|
|
|
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
|
2013-12-11 09:32:14 +01:00
|
|
|
virtual void SetMaxHeight(float mh) { maxHeight_ = mh; }
|
2013-06-09 11:18:57 +02:00
|
|
|
Event OnChoice;
|
2021-02-21 12:42:56 -08:00
|
|
|
std::string DescribeLog() const override { return "ListView: " + View::DescribeLog(); }
|
2021-02-21 18:41:08 -08:00
|
|
|
std::string DescribeText() const override;
|
2013-06-09 11:18:57 +02:00
|
|
|
|
|
|
|
private:
|
2013-07-16 00:25:08 +02:00
|
|
|
void CreateAllItems();
|
2013-06-09 11:18:57 +02:00
|
|
|
EventReturn OnItemCallback(int num, EventParams &e);
|
|
|
|
ListAdaptor *adaptor_;
|
2013-07-16 00:25:08 +02:00
|
|
|
LinearLayout *linLayout_;
|
2013-12-11 09:32:14 +01:00
|
|
|
float maxHeight_;
|
2016-02-25 15:39:17 +01:00
|
|
|
std::set<int> hidden_;
|
2013-06-09 11:18:57 +02:00
|
|
|
};
|
|
|
|
|
2013-07-14 13:51:30 +02:00
|
|
|
} // namespace UI
|