diff --git a/ui/view.cpp b/ui/view.cpp index 68f295510a..537d98ee2f 100644 --- a/ui/view.cpp +++ b/ui/view.cpp @@ -208,6 +208,42 @@ void TextView::Draw(DrawContext &dc) { dc.draw->DrawTextRect(dc.theme->uiFont, text_.c_str(), bounds_.x, bounds_.y, bounds_.w, bounds_.h, 0xFFFFFFFF); } +void TriggerButton::Touch(const TouchInput &input) { + if (input.flags & TOUCH_DOWN) { + if (bounds_.Contains(input.x, input.y)) { + down_ |= 1 << input.id; + } + } + if (input.flags & TOUCH_MOVE) { + if (bounds_.Contains(input.x, input.y)) + down_ |= 1 << input.id; + else + down_ &= ~(1 << input.id); + } + + if (input.flags & TOUCH_UP) { + down_ &= ~(1 << input.id); + } + + if (down_ != 0) { + *bitField_ |= bit_; + } else { + *bitField_ &= ~bit_; + } +} + +void TriggerButton::Draw(DrawContext &dc) { + dc.draw->DrawImage(imageBackground_, bounds_.centerX(), bounds_.centerY(), 1.0f, 0xFFFFFFFF, ALIGN_CENTER); + dc.draw->DrawImage(imageForeground_, bounds_.centerX(), bounds_.centerY(), 1.0f, 0xFFFFFFFF, ALIGN_CENTER); +} + +void TriggerButton::GetContentDimensions(const DrawContext &dc, float &w, float &h) const { + const AtlasImage &image = dc.draw->GetAtlas()->images[imageBackground_]; + w = image.w; + h = image.h; +} + + /* TabStrip::TabStrip() : selected_(0) { diff --git a/ui/view.h b/ui/view.h index f251c5f785..602041c3a2 100644 --- a/ui/view.h +++ b/ui/view.h @@ -218,29 +218,6 @@ public: Size height; }; -class LinearLayoutParams : public LayoutParams { -public: - LinearLayoutParams() - : LayoutParams(), weight(0.0f), gravity(G_TOPLEFT), hasMargins_(false) {} - explicit LinearLayoutParams(float wgt) - : LayoutParams(), weight(wgt), gravity(G_TOPLEFT), hasMargins_(false) {} - LinearLayoutParams(Size w, Size h, float wgt = 0.0f, Gravity grav = G_TOPLEFT) - : LayoutParams(w, h), weight(wgt), gravity(grav), hasMargins_(false) {} - LinearLayoutParams(Size w, Size h, float wgt, Gravity grav, const Margins &mgn) - : LayoutParams(w, h), weight(wgt), gravity(grav), margins(mgn), hasMargins_(true) {} - LinearLayoutParams(const Margins &mgn) - : LayoutParams(WRAP_CONTENT, WRAP_CONTENT), weight(0.0f), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {} - - float weight; - Gravity gravity; - Margins margins; - - bool HasMargins() const { return hasMargins_; } - -private: - bool hasMargins_; -}; - View *GetFocusedView(); class View { @@ -356,6 +333,31 @@ private: std::string text_; }; +// Basic button that modifies a bitfield based on the pressed status. Supports multitouch. +// Suitable for controller simulation (ABXY etc). +class TriggerButton : public View { +public: + TriggerButton(uint32_t *bitField, uint32_t bit, int imageBackground, int imageForeground, LayoutParams *layoutParams) + : View(layoutParams), down_(0.0), bitField_(bitField), bit_(bit), imageBackground_(imageBackground), imageForeground_(imageForeground) {} + + virtual void Touch(const TouchInput &input); + virtual void Draw(DrawContext &dc); + virtual void GetContentDimensions(const DrawContext &dc, float &w, float &h) const; + +private: + int down_; // bitfield of pressed fingers, translates into bitField + + uint32_t *bitField_; + uint32_t bit_; + + int imageBackground_; + int imageForeground_; +}; + + +// The following classes are mostly suitable as items in ListView which +// really is just a LinearLayout in a ScrollView, possibly with some special optimizations. + class Item : public InertView { public: Item(LayoutParams *layoutParams) : InertView(layoutParams) { @@ -385,9 +387,6 @@ public: virtual void Draw(DrawContext &dc); }; -// The following classes are mostly suitable as items in ListView which -// really is just a LinearLayout in a ScrollView, possibly with some special optimizations. - // Use to trigger something or open a submenu screen. class Choice : public ClickableItem { public: @@ -489,6 +488,8 @@ private: ImageSizeMode sizeMode_; }; + + // This tab strip is a little special. /* class TabStrip : public View { diff --git a/ui/viewgroup.cpp b/ui/viewgroup.cpp index 1d160f1dce..d40bd278fe 100644 --- a/ui/viewgroup.cpp +++ b/ui/viewgroup.cpp @@ -472,6 +472,62 @@ void GridLayout::Measure(const DrawContext &dc, MeasureSpec horiz, MeasureSpec v MeasureBySpec(layoutParams_->height, estimatedHeight, vert, &measuredHeight_); } +void AnchorLayout::Measure(const DrawContext &dc, MeasureSpec horiz, MeasureSpec vert) { + MeasureBySpec(layoutParams_->width, 0.0f, horiz, &measuredWidth_); + MeasureBySpec(layoutParams_->height, 0.0f, horiz, &measuredHeight_); + + for (size_t i = 0; i < views_.size(); i++) { + Size width = WRAP_CONTENT; + Size height = WRAP_CONTENT; + + MeasureSpec specW(UNSPECIFIED, 0.0f); + MeasureSpec specH(UNSPECIFIED, 0.0f); + + const AnchorLayoutParams *params = dynamic_cast(views_[i]->GetLayoutParams()); + if (params) { + width = params->width; + height = params->height; + + if (params->left >= 0 && params->right >= 0) { + width = measuredWidth_ - params->left - params->right; + specW = MeasureSpec(EXACTLY, width); + } + if (params->top >= 0 && params->bottom >= 0) { + height = measuredHeight_ - params->top - params->bottom; + specH = MeasureSpec(EXACTLY, height); + } + } + + views_[i]->Measure(dc, specW, specH); + } +} + +void AnchorLayout::Layout() { + for (size_t i = 0; i < views_.size(); i++) { + const AnchorLayoutParams *params = dynamic_cast(views_[i]->GetLayoutParams()); + + Bounds vBounds; + vBounds.w = views_[i]->GetMeasuredWidth(); + vBounds.h = views_[i]->GetMeasuredHeight(); + + float left = 0, top = 0, right = 0, bottom = 0; + + if (params) { + left = params->left; + top = params->top; + right = params->right; + bottom = params->bottom; + } + + if (left >= 0) vBounds.x = bounds_.x + left; + if (top >= 0) vBounds.y = bounds_.y + top; + if (right >= 0) vBounds.x = bounds_.x2() - right - vBounds.w; + if (bottom >= 0) vBounds.y = bounds_.y2() - bottom - vBounds.y; + + views_[i]->SetBounds(vBounds); + } +} + void GridLayout::Layout() { int y = 0; int x = 0; @@ -500,10 +556,6 @@ void GridLayout::Layout() { } } -void RelativeLayout::Layout() { - -} - void LayoutViewHierarchy(const DrawContext &dc, ViewGroup *root) { Bounds rootBounds; rootBounds.x = 0; diff --git a/ui/viewgroup.h b/ui/viewgroup.h index 4d16a71ba1..de4c691198 100644 --- a/ui/viewgroup.h +++ b/ui/viewgroup.h @@ -55,12 +55,54 @@ public: void Layout(); }; -class RelativeLayout : public ViewGroup { + +enum { + NONE = -1, +}; + +class AnchorLayoutParams : public LayoutParams { public: + AnchorLayoutParams(Size w, Size h, float l, float t, float r, float b) + : LayoutParams(w, h), left(l), top(t), right(r), bottom(b) {} + + AnchorLayoutParams(float l, float t, float r, float b) + : LayoutParams(WRAP_CONTENT, WRAP_CONTENT), left(l), top(t), right(r), bottom(b) {} + + // These are not bounds, but distances from the container edges. + // Set to NONE to not attach this edge to the container. + float left, top, right, bottom; +}; + +class AnchorLayout : public ViewGroup { +public: + AnchorLayout(LayoutParams *layoutParams = 0) : ViewGroup(layoutParams) {} void Measure(const DrawContext &dc, MeasureSpec horiz, MeasureSpec vert); void Layout(); }; +class LinearLayoutParams : public LayoutParams { +public: + LinearLayoutParams() + : LayoutParams(), weight(0.0f), gravity(G_TOPLEFT), hasMargins_(false) {} + explicit LinearLayoutParams(float wgt) + : LayoutParams(), weight(wgt), gravity(G_TOPLEFT), hasMargins_(false) {} + LinearLayoutParams(Size w, Size h, float wgt = 0.0f, Gravity grav = G_TOPLEFT) + : LayoutParams(w, h), weight(wgt), gravity(grav), hasMargins_(false) {} + LinearLayoutParams(Size w, Size h, float wgt, Gravity grav, const Margins &mgn) + : LayoutParams(w, h), weight(wgt), gravity(grav), margins(mgn), hasMargins_(true) {} + LinearLayoutParams(const Margins &mgn) + : LayoutParams(WRAP_CONTENT, WRAP_CONTENT), weight(0.0f), gravity(G_TOPLEFT), margins(mgn), hasMargins_(true) {} + + float weight; + Gravity gravity; + Margins margins; + + bool HasMargins() const { return hasMargins_; } + +private: + bool hasMargins_; +}; + class LinearLayout : public ViewGroup { public: LinearLayout(Orientation orientation, LayoutParams *layoutParams = 0)