More networking and UI stuff

This commit is contained in:
Henrik Rydgard 2013-06-01 18:59:03 +02:00
parent e2378dd970
commit 4267d9903c
7 changed files with 59 additions and 29 deletions

View File

@ -82,7 +82,8 @@ public:
}
~JsonReader() {
free(buffer_);
if (buffer_)
free(buffer_);
}
void parse() {

View File

@ -370,6 +370,17 @@ void DrawBuffer::DoAlign(int flags, float *x, float *y, float *w, float *h) {
// U+30A0U+30FF Katakana
void DrawBuffer::DrawTextRect(int font, const char *text, float x, float y, float w, float h, Color color, int align) {
if (align & ALIGN_HCENTER) {
x += w / 2;
} else if (align & ALIGN_RIGHT) {
x += w;
}
if (align & ALIGN_VCENTER) {
y += h / 2;
} else if (align & ALIGN_BOTTOM) {
y += h;
}
DrawText(font, text, x, y, color, align);
}

View File

@ -201,7 +201,7 @@ void Download::Do() {
if (client.GET(fileUrl.Resource().c_str(), &buffer_)) {
progress_ = 1.0f;
ILOG("Completed downloading %s to %s", url_.c_str(), outfile_.c_str());
if (!buffer_.FlushToFile(outfile_.c_str())) {
if (!outfile_.empty() && !buffer_.FlushToFile(outfile_.c_str())) {
ELOG("Failed writing download to %s", outfile_.c_str());
}
} else {

View File

@ -74,13 +74,18 @@ public:
Download(const std::string &url, const std::string &outfile);
~Download();
// Returns 1.0 when done. That one value can be compared exactly.
// Returns 1.0 when done. That one value can be compared exactly - or just use Done().
float Progress() const { return progress_; }
bool Done() const { return progress_ == 1.0f; }
bool Failed() const { return failed_; }
std::string url() const { return url_; }
std::string outfile() const { return outfile_; }
// If not downloading to a file, access this to get the result.
Buffer &buffer() { return buffer_; }
private:
void Do(); // Actually does the download. Runs on thread.

View File

@ -49,14 +49,14 @@ void MeasureBySpec(Size sz, float contentWidth, MeasureSpec spec, float *measure
}
}
void Event::Add(std::function<EventReturn(const EventParams&)> func) {
void Event::Add(std::function<EventReturn(EventParams&)> func) {
HandlerRegistration reg;
reg.func = func;
handlers_.push_back(reg);
}
// Call this from input thread or whatever, it doesn't matter
void Event::Trigger(const EventParams &e) {
void Event::Trigger(EventParams &e) {
lock_guard guard(mutex_);
if (!triggered_) {
triggered_ = true;
@ -96,6 +96,12 @@ void Clickable::Click() {
};
void Clickable::Touch(const TouchInput &input) {
if (!enabled_) {
dragging_ = false;
down_ = false;
return;
}
if (input.flags & TOUCH_DOWN) {
if (bounds_.Contains(input.x, input.y)) {
if (IsFocusMovementEnabled())
@ -128,6 +134,7 @@ void Clickable::Update(const InputState &input_state) {
} else if (input_state.pad_buttons_up & PAD_BUTTON_A) {
if (down_) {
UI::EventParams e;
e.v = this;
OnClick.Trigger(e);
}
down_ = false;
@ -210,12 +217,15 @@ void ImageView::Draw(UIContext &dc) {
}
void TextView::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
dc.Draw()->SetFontScale(textScale_, textScale_);
dc.Draw()->MeasureText(dc.theme->uiFont, text_.c_str(), &w, &h);
dc.Draw()->SetFontScale(1.0f, 1.0f);
}
void TextView::Draw(UIContext &dc) {
// TODO: involve sizemode
dc.Draw()->DrawTextRect(dc.theme->uiFont, text_.c_str(), bounds_.x, bounds_.y, bounds_.w, bounds_.h, 0xFFFFFFFF);
dc.Draw()->SetFontScale(textScale_, textScale_);
dc.Draw()->DrawTextRect(dc.theme->uiFont, text_.c_str(), bounds_.x, bounds_.y, bounds_.w, bounds_.h, 0xFFFFFFFF, textAlign_);
dc.Draw()->SetFontScale(1.0f, 1.0f);
}
void TriggerButton::Touch(const TouchInput &input) {

View File

@ -9,17 +9,9 @@
#include <string>
#include <vector>
#include <functional>
#include <cmath>
// <functional> fix
#if defined(IOS) || defined(MACGNUSTD)
#include <tr1/functional>
namespace std {
using tr1::bind;
}
#endif
#include "base/functional.h"
#include "base/mutex.h"
#include "base/basictypes.h"
#include "base/scoped_ptr.h"
@ -165,17 +157,17 @@ struct EventParams {
};
struct HandlerRegistration {
std::function<EventReturn(const EventParams&)> func;
std::function<EventReturn(EventParams&)> func;
};
class Event {
public:
Event() : triggered_(false) {}
void Add(std::function<EventReturn(const EventParams&)> func);
void Add(std::function<EventReturn(EventParams&)> func);
// Call this from input thread or whatever, it doesn't matter
void Trigger(const EventParams &e);
void Trigger(EventParams &e);
// Call this from UI thread
void Update();
@ -215,7 +207,7 @@ View *GetFocusedView();
class View {
public:
View(LayoutParams *layoutParams = 0) : layoutParams_(layoutParams) {
View(LayoutParams *layoutParams = 0) : layoutParams_(layoutParams), enabled_(true) {
if (!layoutParams)
layoutParams_.reset(new LayoutParams());
}
@ -262,10 +254,15 @@ public:
return GetFocusedView() == this;
}
void SetEnabled(bool enabled) { enabled_ = enabled; }
bool Enabled() const { return enabled_; }
protected:
// Inputs to layout
scoped_ptr<LayoutParams> layoutParams_;
bool enabled_;
// Results of measure pass. Set these in Measure.
float measuredWidth_;
float measuredHeight_;
@ -295,7 +292,7 @@ public:
class Clickable : public View {
public:
Clickable(LayoutParams *layoutParams)
: View(layoutParams), downCountDown_(0), down_(false), dragging_(false) {}
: View(layoutParams), downCountDown_(0), dragging_(false), down_(false) {}
virtual void Touch(const TouchInput &input);
virtual void Update(const InputState &input_state);
@ -413,12 +410,12 @@ class CheckBox : public ClickableItem {
public:
CheckBox(bool *toggle, const std::string &text, const std::string &smallText = "", LayoutParams *layoutParams = 0)
: ClickableItem(layoutParams), text_(text), smallText_(smallText) {
OnClick.Add(std::bind(&CheckBox::OnClicked, this, std::placeholders::_1));
OnClick.Add(std::bind(&CheckBox::OnClicked, this, p::_1));
}
virtual void Draw(UIContext &dc);
EventReturn OnClicked(const EventParams &e) {
EventReturn OnClicked(EventParams &e) {
if (toggle_)
*toggle_ = !(*toggle_);
return EVENT_DONE;
@ -445,15 +442,17 @@ public:
class TextView : public InertView {
public:
TextView(int font, const std::string &text, LayoutParams *layoutParams = 0)
: InertView(layoutParams), font_(font), text_(text) {}
TextView(int font, const std::string &text, int textAlign, float textScale, LayoutParams *layoutParams = 0)
: InertView(layoutParams), font_(font), text_(text), textScale_(textScale), textAlign_(textAlign) {}
virtual void GetContentDimensions(const UIContext &dc, float &w, float &h) const;
virtual void Draw(UIContext &dc);
private:
std::string text_;
int font_;
std::string text_;
float textScale_;
int textAlign_;
};
enum ImageSizeMode {

View File

@ -1,5 +1,6 @@
#pragma once
#include "base/logging.h"
#include "ui/view.h"
#include "input/gesture_detector.h"
@ -33,7 +34,8 @@ public:
virtual float GetContentHeight() const { return 0.0f; }
// Takes ownership! DO NOT add a view to multiple parents!
void Add(View *view) { views_.push_back(view); }
template <class T>
T *Add(T *view) { views_.push_back(view); return view; }
virtual bool SetFocus();
virtual bool SubviewFocused(View *view);
@ -83,8 +85,10 @@ 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) {}
explicit LinearLayoutParams(float wgt, Gravity grav = G_TOPLEFT)
: LayoutParams(), weight(wgt), gravity(grav), hasMargins_(false) {}
LinearLayoutParams(float wgt, const Margins &mgn)
: LayoutParams(), 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), weight(wgt), gravity(grav), hasMargins_(false) {}
LinearLayoutParams(Size w, Size h, float wgt, Gravity grav, const Margins &mgn)