summaryrefslogtreecommitdiff
path: root/src/ui/mediaui.c
blob: 24b29cb0e18bba55b169a599a3bfea02f86f058d (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
/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "mediaui.h"
#include "media.h"
#include "documentwidget.h"
#include "gmdocument.h"
#include "audio/player.h"
#include "paint.h"
#include "util.h"
#include "lang.h"

#include <the_Foundation/path.h>

static const char *volumeChar_(float volume) {
    if (volume <= 0) {
        return "\U0001f507";
    }
    if (volume < 0.4f) {
        return "\U0001f508";
    }
    if (volume < 0.8f) {
        return "\U0001f509";
    }
    return "\U0001f50a";
}

void init_PlayerUI(iPlayerUI *d, const iPlayer *player, iRect bounds) {
    d->player = player;
    d->bounds = bounds;
    const int height = height_Rect(bounds);
    d->playPauseRect = (iRect){ addX_I2(topLeft_Rect(bounds), gap_UI / 2), init_I2(3 * height / 2, height) };
    d->rewindRect    = (iRect){ topRight_Rect(d->playPauseRect), init1_I2(height) };
    d->menuRect      = (iRect){ addX_I2(topRight_Rect(bounds), -height - gap_UI / 2), init1_I2(height) };
    d->volumeRect    = (iRect){ addX_I2(topLeft_Rect(d->menuRect), -height), init1_I2(height) };
    d->volumeAdjustRect = d->volumeRect;
    adjustEdges_Rect(&d->volumeAdjustRect, 0, 0, 0, -35 * gap_UI);
    d->scrubberRect  = initCorners_Rect(topRight_Rect(d->rewindRect), bottomLeft_Rect(d->volumeRect));
    /* Volume slider. */ {
        d->volumeSlider = shrunk_Rect(d->volumeAdjustRect, init_I2(gap_UI / 2, gap_UI));
        adjustEdges_Rect(&d->volumeSlider, 0, -width_Rect(d->volumeRect) - 2 * gap_UI, 0, 5 * gap_UI);
    }
}

static void drawPlayerButton_(iPaint *p, iRect rect, const char *label, int font) {
    const iInt2 mouse     = mouseCoord_Window(get_Window());
    const iBool isHover   = contains_Rect(rect, mouse);
    const iBool isPressed = isHover && (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON_LEFT) != 0;
    const int frame = (isPressed ? uiTextCaution_ColorId : isHover ? uiHeading_ColorId : uiAnnotation_ColorId);
    iRect frameRect = shrunk_Rect(rect, init_I2(gap_UI / 2, gap_UI));
    drawRect_Paint(p, frameRect, frame);
    if (isPressed) {
        fillRect_Paint(
            p,
            adjusted_Rect(shrunk_Rect(frameRect, divi_I2(gap2_UI, 2)), zero_I2(), one_I2()),
            frame);
    }
    const int fg = isPressed ? (permanent_ColorId | uiBackground_ColorId) : uiHeading_ColorId;
    drawCentered_Text(font, frameRect, iTrue, fg, "%s", label);
}

static const uint32_t sevenSegmentDigit_ = 0x1fbf0;

static const char *sevenSegmentStr_ = "\U0001fbf0";

static int drawSevenSegmentTime_(iInt2 pos, int color, int align, int seconds) { /* returns width */
    const int hours = seconds / 3600;
    const int mins  = (seconds / 60) % 60;
    const int secs  = seconds % 60;
    const int font  = uiLabel_FontId;
    iString   num;
    init_String(&num);
    if (hours) {
        appendChar_String(&num, sevenSegmentDigit_ + (hours % 10));
        appendChar_String(&num, ':');
    }
    appendChar_String(&num, sevenSegmentDigit_ + (mins / 10) % 10);
    appendChar_String(&num, sevenSegmentDigit_ + (mins % 10));
    appendChar_String(&num, ':');
    appendChar_String(&num, sevenSegmentDigit_ + (secs / 10) % 10);
    appendChar_String(&num, sevenSegmentDigit_ + (secs % 10));
    iInt2 size = advanceRange_Text(font, range_String(&num));
    if (align == right_Alignment) {
        pos.x -= size.x;
    }
    drawRange_Text(font, pos, color, range_String(&num));
    deinit_String(&num);
    return size.x;
}

void draw_PlayerUI(iPlayerUI *d, iPaint *p) {
    const int   playerBackground_ColorId = uiBackground_ColorId;
    const int   playerFrame_ColorId      = uiSeparator_ColorId;
    const iBool isAdjusting = (flags_Player(d->player) & adjustingVolume_PlayerFlag) != 0;
    fillRect_Paint(p, d->bounds, playerBackground_ColorId);
    drawRect_Paint(p, d->bounds, playerFrame_ColorId);
    drawPlayerButton_(p,
                      d->playPauseRect,
                      isPaused_Player(d->player) ? "\U0001f782" : "\u23f8",
                      uiContent_FontId);
    drawPlayerButton_(p, d->rewindRect, "\u23ee", uiContent_FontId);
    drawPlayerButton_(p, d->menuRect, "\U0001d362", uiContent_FontId);
    if (!isAdjusting) {
        drawPlayerButton_(
            p, d->volumeRect, volumeChar_(volume_Player(d->player)), uiContentSymbols_FontId);
    }
    const int   hgt       = lineHeight_Text(uiLabel_FontId);
    const int   yMid      = mid_Rect(d->scrubberRect).y;
    const float playTime  = time_Player(d->player);
    const float totalTime = duration_Player(d->player);
    const int   bright    = uiHeading_ColorId;
    const int   dim       = uiAnnotation_ColorId;
    int leftWidth = drawSevenSegmentTime_(
        init_I2(left_Rect(d->scrubberRect) + 2 * gap_UI, yMid - hgt / 2),
        isPaused_Player(d->player) ? dim : bright,
        left_Alignment,
        iRound(playTime));
    int rightWidth = 0;
    if (totalTime > 0) {
        rightWidth =
            drawSevenSegmentTime_(init_I2(right_Rect(d->scrubberRect) - 2 * gap_UI, yMid - hgt / 2),
                                  dim,
                                  right_Alignment,
                                  iRound(totalTime));
    }
    /* Scrubber. */
    const int   s1       = left_Rect(d->scrubberRect) + leftWidth + 6 * gap_UI;
    const int   s2       = right_Rect(d->scrubberRect) - rightWidth - 6 * gap_UI;
    const float normPos  = totalTime > 0 ? playTime / totalTime : 0.0f;
    const int   part     = (s2 - s1) * normPos;
    const int   scrubMax = (s2 - s1) * streamProgress_Player(d->player);
    drawHLine_Paint(p, init_I2(s1, yMid), part, bright);
    drawHLine_Paint(p, init_I2(s1 + part, yMid), scrubMax - part, dim);
    const char *dot = "\u23fa";
    const int dotWidth = advance_Text(uiLabel_FontId, dot).x;
    draw_Text(uiLabel_FontId,
              init_I2(s1 * (1.0f - normPos) + s2 * normPos - dotWidth / 2, yMid - hgt / 2),
              isPaused_Player(d->player) ? dim : bright,
              dot);
    /* Volume adjustment. */
    if (isAdjusting) {
        const iInt2 mouse   = mouseCoord_Window(get_Window());
        const iBool isHover = contains_Rect(d->volumeRect, mouse) &&
                              ~flags_Player(d->player) & volumeGrabbed_PlayerFlag;
        const iBool isPressed = (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON_LEFT) != 0;
        iRect adjRect = shrunk_Rect(d->volumeAdjustRect, init_I2(gap_UI / 2, gap_UI));
        fillRect_Paint(p, adjRect, playerBackground_ColorId);
        drawRect_Paint(p, adjRect, bright);
        if (isHover) {
            fillRect_Paint(
                p,
                shrunk_Rect(d->volumeRect, init_I2(gap_UI / 2 + gap_UI / 2, 3 * gap_UI / 2)),
                isPressed ? uiTextCaution_ColorId : bright);
        }
        drawCentered_Text(uiContentSymbols_FontId,
                          d->volumeRect,
                          iTrue,
                          isHover ? playerBackground_ColorId : bright,
                          volumeChar_(volume_Player(d->player)));
        const int volColor =
            flags_Player(d->player) & volumeGrabbed_PlayerFlag ? uiTextCaution_ColorId : bright;
        const int volPart = volume_Player(d->player) * width_Rect(d->volumeSlider);
        const iInt2 volPos = init_I2(left_Rect(d->volumeSlider), mid_Rect(d->volumeSlider).y);
        drawHLine_Paint(p, volPos, volPart, volColor);
        drawHLine_Paint(p,
                        addX_I2(volPos, volPart),
                        width_Rect(d->volumeSlider) - volPart,
                        dim);
        draw_Text(uiLabel_FontId,
                  init_I2(left_Rect(d->volumeSlider) + volPart - dotWidth / 2, yMid - hgt / 2),
                  volColor,
                  dot);
    }
}

/*----------------------------------------------------------------------------------------------*/

static void drawSevenSegmentBytes_(iInt2 pos, int color, size_t numBytes) {
    iString digits;
    init_String(&digits);
    if (numBytes == 0) {
        appendChar_String(&digits, sevenSegmentDigit_);
    }
    else {
        int magnitude = 0;
        while (numBytes) {
            if (magnitude == 3) {
                prependCStr_String(&digits, "\u2024");
            }
            else if (magnitude == 6) {
                prependCStr_String(&digits, restore_ColorEscape "\u2024");
            }
            else if (magnitude == 9) {
                prependCStr_String(&digits, "\u2024");
            }
            prependChar_String(&digits, sevenSegmentDigit_ + (numBytes % 10));
            numBytes /= 10;
            magnitude++;
        }
        if (magnitude > 6) {
            prependCStr_String(&digits, uiTextStrong_ColorEscape);
        }
    }
    const int font = uiLabel_FontId;
    const iInt2 dims = advanceRange_Text(font, range_String(&digits));
    drawRange_Text(font, addX_I2(pos, -dims.x), color, range_String(&digits));
    deinit_String(&digits);
}

void init_DownloadUI(iDownloadUI *d, const iDocumentWidget *doc, uint16_t mediaId, iRect bounds) {
    d->doc     = doc;
    d->mediaId = mediaId;
    d->bounds  = bounds;
}

iBool processEvent_DownloadUI(iDownloadUI *d, const SDL_Event *ev) {
    return iFalse;
}

void draw_DownloadUI(const iDownloadUI *d, iPaint *p) {
    const iMedia *media = constMedia_GmDocument(document_DocumentWidget(d->doc));
    iGmMediaInfo info;
    float bytesPerSecond;
    const iString *path;
    iBool isFinished;
    downloadInfo_Media(media, d->mediaId, &info);
    downloadStats_Media(media, d->mediaId, &path, &bytesPerSecond, &isFinished);
    fillRect_Paint(p, d->bounds, uiBackground_ColorId);
    drawRect_Paint(p, d->bounds, uiSeparator_ColorId);
    iRect rect = d->bounds;
    shrink_Rect(&rect, init_I2(3 * gap_UI, 0));
    const int fonts[2] = { uiContentBold_FontId, uiLabel_FontId };
    const int contentHeight = lineHeight_Text(fonts[0]) + lineHeight_Text(fonts[1]);
    const int x = left_Rect(rect);
    const int y1 = mid_Rect(rect).y - contentHeight / 2;
    const int y2 = y1 + lineHeight_Text(fonts[1]);
    if (path) {
        drawRange_Text(fonts[0], init_I2(x, y1), uiHeading_ColorId, baseName_Path(path));
    }
    draw_Text(uiLabel_FontId,
              init_I2(x, y2),
              isFinished ? uiTextAction_ColorId : uiTextDim_ColorId,
              cstr_Lang(isFinished ? "media.download.complete" : "media.download.warnclose"));
    const int x2 = right_Rect(rect);
    drawSevenSegmentBytes_(init_I2(x2, y1), uiTextDim_ColorId, info.numBytes);
    const iInt2 pos = init_I2(x2, y2);
    if (bytesPerSecond > 0) {
        drawAlign_Text(uiLabel_FontId, pos, uiTextDim_ColorId, right_Alignment,
                       translateCStr_Lang("%.3f ${mb.per.sec}"),
                       bytesPerSecond / 1.0e6);
    }
    else {
        drawAlign_Text(uiLabel_FontId, pos, uiTextDim_ColorId, right_Alignment,
                       translateCStr_Lang("\u2014 ${mb.per.sec}"));
    }
}