aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/views/SkWidget.cpp
blob: e7de522107776c562e7b28e6515588ed88bd8d7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkWidget.h"
#include "SkCanvas.h"
#include "SkInterpolator.h"
#include "SkTime.h"
#include "SkParsePaint.h"

#if 0
SkWidgetView::SkWidgetView(U32 flags) : SkView(flags)
{
}

SkWidgetView::~SkWidgetView()
{
}

const char* SkWidgetView::GetEventType()
{
    return "SkWidgetView";
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

class SkTextView::Interp {
public:
    Interp(const SkString& old, SkMSec now, SkMSec dur, AnimaDir dir) : fOldText(old), fInterp(1, 2)
    {
        SkScalar x = 0;
        fInterp.setKeyFrame(0, now, &x, 0);
        x = SK_Scalar1;
        if (dir == kBackward_AnimDir)
            x = -x;
        fInterp.setKeyFrame(1, now + dur, &x);
    }
    bool draw(SkCanvas* canvas, const SkString& newText, SkScalar x, SkScalar y, SkPaint& paint)
    {
        SkScalar scale;

        if (fInterp.timeToValues(SkTime::GetMSecs(), &scale) == SkInterpolator::kFreezeEnd_Result)
        {
            canvas->drawText(newText.c_str(), newText.size(), x, y, paint);
            return false;
        }
        else
        {
            U8 alpha = paint.getAlpha();
            SkScalar above, below;
            (void)paint.measureText(NULL, 0, &above, &below);
            SkScalar height = below - above;
            SkScalar dy = SkScalarMul(height, scale);
            if (scale < 0)
                height = -height;

            // draw the old
            paint.setAlpha((U8)SkScalarMul(alpha, SK_Scalar1 - SkScalarAbs(scale)));
            canvas->drawText(fOldText.c_str(), fOldText.size(), x, y - dy, paint);
            // draw the new
            paint.setAlpha((U8)SkScalarMul(alpha, SkScalarAbs(scale)));
            canvas->drawText(newText.c_str(), newText.size(), x, y + height - dy, paint);
            // restore the paint
            paint.setAlpha(alpha);
            return true;
        }
    }

private:
    SkString        fOldText;
    SkInterpolator    fInterp;
};

SkTextView::SkTextView(U32 flags) : SkView(flags), fInterp(NULL), fDoInterp(false)
{
    fMargin.set(0, 0);
}

SkTextView::~SkTextView()
{
    delete fInterp;
}

void SkTextView::getText(SkString* str) const
{
    if (str)
        str->set(fText);
}

void SkTextView::setText(const char text[], AnimaDir dir)
{
    if (!fText.equals(text))
    {
        SkString tmp(text);
        this->privSetText(tmp, dir);
    }
}

void SkTextView::setText(const char text[], size_t len, AnimaDir dir)
{
    if (!fText.equals(text))
    {
        SkString tmp(text, len);
        this->privSetText(tmp, dir);
    }
}

void SkTextView::setText(const SkString& src, AnimaDir dir)
{
    if (fText != src)
        this->privSetText(src, dir);
}

void SkTextView::privSetText(const SkString& src, AnimaDir dir)
{
    SkASSERT(fText != src);

    if (fDoInterp)
    {
        if (fInterp)
            delete fInterp;
        fInterp = new Interp(fText, SkTime::GetMSecs(), 500, dir);
    }
    fText = src;
    this->inval(NULL);
}

/////////////////////////////////////////////////////////////////

void SkTextView::getMargin(SkPoint* margin) const
{
    if (margin)
        *margin = fMargin;
}

void SkTextView::setMargin(const SkPoint& margin)
{
    if (fMargin != margin)
    {
        fMargin = margin;
        this->inval(NULL);
    }
}

void SkTextView::onDraw(SkCanvas* canvas)
{
    this->INHERITED::onDraw(canvas);

    if (fText.size() == 0)
        return;

    SkPaint::Align    align = fPaint.getTextAlign();
    SkScalar        x, y;

    switch (align) {
    case SkPaint::kLeft_Align:
        x = fMargin.fX;
        break;
    case SkPaint::kCenter_Align:
        x = SkScalarHalf(this->width());
        break;
    default:
        SkASSERT(align == SkPaint::kRight_Align);
        x = this->width() - fMargin.fX;
        break;
    }

    fPaint.measureText(NULL, 0, &y, NULL);
    y = fMargin.fY - y;

    if (fInterp)
    {
        if (fInterp->draw(canvas, fText, x, y, fPaint))
            this->inval(NULL);
        else
        {
            delete fInterp;
            fInterp = NULL;
        }
    }
    else
        canvas->drawText(fText.c_str(), fText.size(), x, y, fPaint);
}

//////////////////////////////////////////////////////////////////////////////////////

void SkTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
{
    this->INHERITED::onInflate(dom, node);

    const char* text = dom.findAttr(node, "text");
    if (text)
        this->setText(text);

    SkPoint    margin;
    if (dom.findScalars(node, "margin", (SkScalar*)&margin, 2))
        this->setMargin(margin);
    (void)dom.findBool(node, "do-interp", &fDoInterp);

    SkPaint_Inflate(&fPaint, dom, node);
}

//////////////////////////////////////////////////////////////////////////////////////

SkSliderView::SkSliderView(U32 flags) : SkWidgetView(flags)
{
    fValue = 0;
    fMax = 0;
}

static U16 actual_value(U16CPU value, U16CPU max)
{
    return SkToU16(SkMax32(0, SkMin32(value, max)));
}

void SkSliderView::setMax(U16CPU max)
{
    if (fMax != max)
    {
        fMax = SkToU16(max);
        if (fValue > 0)
            this->inval(NULL);
    }
}

void SkSliderView::setValue(U16CPU value)
{
    if (fValue != value)
    {
        U16 prev = actual_value(fValue, fMax);
        U16 next = actual_value(value, fMax);

        fValue = SkToU16(value);
        if (prev != next)
        {
            this->inval(NULL);

            if (this->hasListeners())
            {
                SkEvent    evt;

                evt.setType(SkWidgetView::GetEventType());
                evt.setFast32(this->getSinkID());
                evt.setS32("sliderValue", next);
                this->postToListeners(evt);
            }
        }
    }
}

#include "SkGradientShader.h"

static void setgrad(SkPaint* paint, const SkRect& r)
{
    SkPoint    pts[2];
    SkColor    colors[2];

#if 0
    pts[0].set(r.fLeft, r.fTop);
    pts[1].set(r.fLeft + r.height(), r.fBottom);
#else
    pts[0].set(r.fRight, r.fBottom);
    pts[1].set(r.fRight - r.height(), r.fTop);
#endif
    colors[0] = SK_ColorBLUE;
    colors[1] = SK_ColorWHITE;

    paint->setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kMirror_TileMode))->unref();
}

void SkSliderView::onDraw(SkCanvas* canvas)
{
    this->INHERITED::onDraw(canvas);

    U16CPU value = SkMax32(0, SkMin32(fValue, fMax));

    SkRect    r;
    SkPaint    p;

    r.set(0, 0, this->width(), this->height());

    p.setAntiAliasOn(true);
    p.setStyle(SkPaint::kStroke_Style);
    p.setStrokeWidth(SK_Scalar1);
    r.inset(SK_Scalar1/2, SK_Scalar1/2);
    canvas->drawRect(r, p);

    if (fMax)
    {
        SkFixed percent = SkFixedDiv(value, fMax);

        r.inset(SK_Scalar1/2, SK_Scalar1/2);
        r.fRight = r.fLeft + SkScalarMul(r.width(), SkFixedToScalar(percent));
        p.setStyle(SkPaint::kFill_Style);
        setgrad(&p, r);
        canvas->drawRect(r, p);
    }

#if 0
    r.set(0, 0, this->width(), this->height());
    r.inset(SK_Scalar1, SK_Scalar1);
    r.inset(r.width()/2, 0);
    p.setColor(SK_ColorBLACK);
    canvas->drawLine(*(SkPoint*)&r.fLeft, *(SkPoint*)&r.fRight, p);
#endif
}

SkView::Click* SkSliderView::onFindClickHandler(SkScalar x, SkScalar y)
{
    return new Click(this);
}

bool SkSliderView::onClick(Click* click)
{
    if (fMax)
    {
        SkScalar percent = SkScalarDiv(click->fCurr.fX + SK_Scalar1, this->width() - SK_Scalar1*2);
        percent = SkMaxScalar(0, SkMinScalar(percent, SK_Scalar1));
        this->setValue(SkScalarRound(percent * fMax));
        return true;
    }
    return false;
}

#endif