summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/allapps.rs
blob: c13608c8e7c94ddcd5b01a3ec3412c7891798a1c (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#pragma version(1)

#pragma rs java_package_name(com.android.launcher2)

#include "rs_types.rsh"
#include "rs_math.rsh"
#include "rs_graphics.rsh"

#define PI 3.14159f

// Constants from Java
int COLUMNS_PER_PAGE_PORTRAIT;
int ROWS_PER_PAGE_PORTRAIT;
int COLUMNS_PER_PAGE_LANDSCAPE;
int ROWS_PER_PAGE_LANDSCAPE;

int gIconCount;
int gSelectedIconIndex = -1;
rs_allocation gSelectedIconTexture;
rs_allocation gHomeButton;

rs_program_fragment gPFTexNearest;
rs_program_fragment gPFTexMip;
rs_program_fragment gPFTexMipAlpha;
rs_program_vertex gPVCurve;
rs_program_store gPS;
rs_mesh gSMCell;

rs_allocation *gIconIDs;
rs_allocation *gLabelIDs;

typedef struct VpConsts {
    float4 Position;
    float4 ScaleOffset;
    float2 BendPos;
    float2 ImgSize;
} VpConsts_t;
VpConsts_t *vpConstants;


#pragma rs export_var(COLUMNS_PER_PAGE_PORTRAIT, ROWS_PER_PAGE_PORTRAIT, COLUMNS_PER_PAGE_LANDSCAPE, ROWS_PER_PAGE_LANDSCAPE, gIconCount, gSelectedIconIndex, gSelectedIconTexture, gHomeButton, gTargetPos, gPFTexNearest, gPFTexMip, gPFTexMipAlpha, gPVCurve, gPS, gSMCell, gIconIDs, gLabelIDs, vpConstants)
#pragma rs export_func(move, moveTo, setZoom, fling)


// Attraction to center values from page edge to page center.
static float g_AttractionTable[9] = {20.f, 20.f, 20.f, 10.f, -10.f, -20.f, -20.f, -20.f, -20.f};
static float g_FrictionTable[9] = {10.f, 10.f, 11.f, 15.f, 15.f, 11.f, 10.f, 10.f, 10.f};
static float g_PhysicsTableSize = 7;

static float gZoomTarget;
static float gTargetPos;
static float g_PosPage = 0.f;
static float g_PosVelocity = 0.f;
static float g_LastPositionX = 0.f;
static bool g_LastTouchDown = false;
static float g_DT;
static int64_t g_LastTime;
static int g_PosMax;
static float g_Zoom = 0.f;
static float g_Animation = 1.f;
static float g_OldPosPage;
static float g_OldPosVelocity;
static float g_OldZoom;
static float g_MoveToTotalTime = 0.2f;
static float g_MoveToTime = 0.f;
static float g_MoveToOldPos = 0.f;

static int g_Cols;
static int g_Rows;

// Drawing constants, should be parameters ======
#define VIEW_ANGLE 1.28700222f


static void updateReadback() {
    if ((g_OldPosPage != g_PosPage) ||
        (g_OldPosVelocity != g_PosVelocity) ||
        (g_OldZoom != g_Zoom)) {

        g_OldPosPage = g_PosPage;
        g_OldPosVelocity = g_PosVelocity;
        g_OldZoom = g_Zoom;

        int i[3];
        i[0] = g_PosPage * (1 << 16);
        i[1] = g_PosVelocity * (1 << 16);
        i[2] = g_OldZoom * (1 << 16);
        rsSendToClient(&i[0], 1, 12, 1);
    }
}

void init() {
}

void move(float newPos) {
    if (g_LastTouchDown) {
        float dx = -(newPos - g_LastPositionX);
        g_PosVelocity = 0;
        g_PosPage += dx * 5.2f;

        float pmin = -0.49f;
        float pmax = g_PosMax + 0.49f;
        g_PosPage = clamp(g_PosPage, pmin, pmax);
    }
    g_LastTouchDown = true;
    g_LastPositionX = newPos;
    g_MoveToTime = 0;
}

void moveTo(float targetPos) {
    gTargetPos = targetPos;
    g_MoveToTime = g_MoveToTotalTime;
    g_PosVelocity = 0;
    g_MoveToOldPos = g_PosPage;
}

void setZoom(float z, /*bool*/ int animate) {
    gZoomTarget = z;
    if (gZoomTarget < 0.001f) {
        gZoomTarget = 0;
    }
    if (!animate) {
        g_Zoom = gZoomTarget;
    }
    updateReadback();
}

void fling(float newPos, float vel) {
    move(newPos);

    g_LastTouchDown = false;
    g_PosVelocity = -vel * 4;
    float av = fabs(g_PosVelocity);
    float minVel = 3.5f;

    minVel *= 1.f - (fabs(rsFrac(g_PosPage + 0.5f) - 0.5f) * 0.45f);

    if (av < minVel && av > 0.2f) {
        if (g_PosVelocity > 0) {
            g_PosVelocity = minVel;
        } else {
            g_PosVelocity = -minVel;
        }
    }

    if (g_PosPage <= 0) {
        g_PosVelocity = max(0.f, g_PosVelocity);
    }
    if (g_PosPage > g_PosMax) {
        g_PosVelocity = min(0.f, g_PosVelocity);
    }
}

// Interpolates values in the range 0..1 to a curve that eases in
// and out.
static float getInterpolation(float input) {
    return (cos((input + 1) * PI) * 0.5f) + 0.5f;
}


static void updatePos() {
    if (g_LastTouchDown) {
        return;
    }

    float tablePosNorm = rsFrac(g_PosPage + 0.5f);
    float tablePosF = tablePosNorm * g_PhysicsTableSize;
    int tablePosI = tablePosF;
    float tablePosFrac = tablePosF - tablePosI;
    float accel = mix(g_AttractionTable[tablePosI],
                        g_AttractionTable[tablePosI + 1],
                        tablePosFrac) * g_DT;
    float friction = mix(g_FrictionTable[tablePosI],
                        g_FrictionTable[tablePosI + 1],
                        tablePosFrac) * g_DT;

    if (g_MoveToTime) {
        // New position is old posiition + (total distance) * (interpolated time)
        g_PosPage = g_MoveToOldPos + (gTargetPos - g_MoveToOldPos) * getInterpolation((g_MoveToTotalTime - g_MoveToTime) / g_MoveToTotalTime);
        g_MoveToTime -= g_DT;
        if (g_MoveToTime <= 0) {
            g_MoveToTime = 0;
            g_PosPage = gTargetPos;
        }
        return;
    }

    // If our velocity is low OR acceleration is opposing it, apply it.
    if (fabs(g_PosVelocity) < 4.0f || (g_PosVelocity * accel) < 0) {
        g_PosVelocity += accel;
    }
    //RS_DEBUG(g_PosPage);
    //RS_DEBUG(g_PosVelocity);
    //RS_DEBUG(friction);
    //RS_DEBUG(accel);

    // Normal physics
    if (g_PosVelocity > 0) {
        g_PosVelocity -= friction;
        g_PosVelocity = max(g_PosVelocity, 0.f);
    } else {
        g_PosVelocity += friction;
        g_PosVelocity = min(g_PosVelocity, 0.f);
    }

    if ((friction > fabs(g_PosVelocity)) && (friction > fabs(accel))) {
        // Special get back to center and overcome friction physics.
        float t = tablePosNorm - 0.5f;
        if (fabs(t) < (friction * g_DT)) {
            // really close, just snap
            g_PosPage = round(g_PosPage);
            g_PosVelocity = 0;
        } else {
            if (t > 0) {
                g_PosVelocity = -friction;
            } else {
                g_PosVelocity = friction;
            }
        }
    }

    // Check for out of boundry conditions.
    if (g_PosPage < 0 && g_PosVelocity < 0) {
        float damp = 1.0f + (g_PosPage * 4);
        damp = clamp(damp, 0.f, 0.9f);
        g_PosVelocity *= damp;
    }
    if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
        float damp = 1.0f - ((g_PosPage - g_PosMax) * 4);
        damp = clamp(damp, 0.f, 0.9f);
        g_PosVelocity *= damp;
    }

    g_PosPage += g_PosVelocity * g_DT;
    g_PosPage = clamp(g_PosPage, -0.49f, g_PosMax + 0.49f);
}

static void
draw_home_button()
{
    color(1.0f, 1.0f, 1.0f, 1.0f);
    rsgBindTexture(gPFTexNearest, 0, gHomeButton);

    float w = rsgGetWidth();
    float h = rsgGetHeight();
    float tw = rsAllocationGetDimX(gHomeButton);
    float th = rsAllocationGetDimY(gHomeButton);

    float x;
    float y;
    if (w > h) {
        x = w - (tw * (1 - g_Animation)) + 20;
        y = (h - th) * 0.5f;
    } else {
        x = (w - tw) / 2;
        y = -g_Animation * th;
        y -= 30; // move the house to the edge of the screen as it doesn't fill the texture.
    }

    rsgDrawSpriteScreenspace(x, y, 0, tw, th);
}

static void drawFrontGrid(float rowOffset, float p)
{
    float h = rsgGetHeight();
    float w = rsgGetWidth();

    int intRowOffset = rowOffset;
    float rowFrac = rowOffset - intRowOffset;
    float colWidth = 120.f;//w / 4;
    float rowHeight = colWidth + 25.f;
    float yoff = 0.5f * h + 1.5f * rowHeight;

    int row, col;
    int colCount = 4;
    if (w > h) {
        colCount = 6;
        rowHeight -= 12.f;
        yoff = 0.47f * h + 1.0f * rowHeight;
    }

    int iconNum = (intRowOffset - 5) * colCount;

    rsgBindProgramVertex(gPVCurve);

    vpConstants->Position.z = p;

    color(1.0f, 1.0f, 1.0f, 1.0f);
    for (row = -5; row < 15; row++) {
        float y = yoff - ((-rowFrac + row) * rowHeight);

        for (col=0; col < colCount; col++) {
            if (iconNum >= gIconCount) {
                return;
            }

            if (iconNum >= 0) {
                float x = colWidth * col + (colWidth / 2);
                vpConstants->Position.x = x + 0.2f;

                if (gSelectedIconIndex == iconNum && !p && gSelectedIconTexture.p) {
                    rsgBindProgramFragment(gPFTexNearest);
                    rsgBindTexture(gPFTexNearest, 0, gSelectedIconTexture);
                    vpConstants->ImgSize.x = rsAllocationGetDimX(gSelectedIconTexture);
                    vpConstants->ImgSize.y = rsAllocationGetDimY(gSelectedIconTexture);
                    vpConstants->Position.y = y - (rsAllocationGetDimY(gSelectedIconTexture)
                                                - rsAllocationGetDimY(gIconIDs[iconNum])) * 0.5f;
                    rsgDrawMesh(gSMCell);
                }

                rsgBindProgramFragment(gPFTexMip);
                vpConstants->ImgSize.x = rsAllocationGetDimX(gIconIDs[iconNum]);
                vpConstants->ImgSize.y = rsAllocationGetDimY(gIconIDs[iconNum]);
                vpConstants->Position.y = y - 0.2f;
                rsgBindTexture(gPFTexMip, 0, gIconIDs[iconNum]);
                rsgDrawMesh(gSMCell);

                rsgBindProgramFragment(gPFTexMipAlpha);
                vpConstants->ImgSize.x = rsAllocationGetDimX(gLabelIDs[iconNum]);
                vpConstants->ImgSize.y = rsAllocationGetDimY(gLabelIDs[iconNum]);
                vpConstants->Position.y = y - 64.f - 0.2f;
                rsgBindTexture(gPFTexMipAlpha, 0, gLabelIDs[iconNum]);
                rsgDrawMesh(gSMCell);
            }
            iconNum++;
        }
    }
}


int root()
{
    // Compute dt in seconds.
    int64_t newTime = rsUptimeMillis();
    g_DT = (newTime - g_LastTime) * 0.001f;
    g_LastTime = newTime;

    // physics may break if DT is large.
    g_DT = min(g_DT, 0.1f);

    if (g_Zoom != gZoomTarget) {
        float dz = g_DT * 1.7f;
        if (gZoomTarget < 0.5f) {
            dz = -dz;
        }
        if (fabs(g_Zoom - gZoomTarget) < fabs(dz)) {
            g_Zoom = gZoomTarget;
        } else {
            g_Zoom += dz;
        }
        updateReadback();
    }
    g_Animation = pow(1.f - g_Zoom, 3.f);

    // Set clear value to dim the background based on the zoom position.
    if ((g_Zoom < 0.001f) && (gZoomTarget < 0.001f)) {
        rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        // When we're zoomed out and not tracking motion events, reset the pos to 0.
        if (!g_LastTouchDown) {
            g_PosPage = 0;
        }
        return 0;
    } else {
        rsgClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
    }

    rsgBindProgramStore(gPS);

    // icons & labels
    if (rsgGetWidth() > rsgGetHeight()) {
        g_Cols = COLUMNS_PER_PAGE_LANDSCAPE;
        g_Rows = ROWS_PER_PAGE_LANDSCAPE;
    } else {
        g_Cols = COLUMNS_PER_PAGE_PORTRAIT;
        g_Rows = ROWS_PER_PAGE_PORTRAIT;
    }

    g_PosMax = ((gIconCount + (g_Cols-1)) / g_Cols) - g_Rows;
    if (g_PosMax < 0) g_PosMax = 0;

    updatePos();
    updateReadback();

    // Draw the icons ========================================
    drawFrontGrid(g_PosPage, g_Animation);

    rsgBindProgramFragment(gPFTexNearest);
    draw_home_button();
    return (g_PosVelocity != 0) || rsFrac(g_PosPage) || g_Zoom != gZoomTarget || (g_MoveToTime != 0);
}