aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
blob: a7ff973f3b3a1d0e21a5c9208c93862c6753987f (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
/*
 * render.c
 *
 * Created by buzzert <buzzert@buzzert.net> 2019-01-18
 */

#include "render.h"
#include "resources.h"

#include <assert.h>
#include <gio/gio.h>

static const double kLogoBackgroundWidth = 500.0;

GBytes* get_data_for_resource(const char *resource_path)
{
    GBytes *result = NULL;
    GError *error = NULL;

    GResource *resource = as_get_resource();
    result = g_resource_lookup_data(
        resource,
        resource_path,
        G_RESOURCE_LOOKUP_FLAGS_NONE,
        &error
    );

    if (error != NULL) {
        fprintf(stderr, "Error loading resource %s\n", resource_path);
    }

    return result;
}

static void update_single_animation(saver_state_t *state, animation_t *anim)
{
    // Cursor animation
    if (anim->anim.type == ACursorAnimation) {
        CursorAnimation *ca = &anim->anim.cursor_anim;

        if (ca->cursor_animating) {
            const double cursor_fade_speed = 0.01;
            if (ca->cursor_fade_direction > 0) {
                state->cursor_opacity += cursor_fade_speed;
                if (state->cursor_opacity > 1.0) {
                    ca->cursor_fade_direction *= -1;
                }
            } else {
                state->cursor_opacity -= cursor_fade_speed;
                if (state->cursor_opacity <= 0.0) {
                    ca->cursor_fade_direction *= -1;
                }
            }
        } else {
            state->cursor_opacity = 1.0;
        }
    }

    // Logo animation
    else if (anim->anim.type == ALogoAnimation) {
        const double logo_duration = 0.6;

        anim_time_interval_t now = anim_now();
        double progress = (now - anim->start_time) / logo_duration;

        state->logo_fill_progress = anim_qubic_ease_out(progress);
        if (anim->anim.logo_anim.direction) {
            state->logo_fill_progress = 1.0 - anim_qubic_ease_out(progress);
        }

        bool completed = (state->logo_fill_progress >= 1.0);
        if (anim->anim.logo_anim.direction) {
            completed = (state->logo_fill_progress <= 0.0);
        }

        anim->completed = completed;
    }
}

static unsigned next_anim_index(saver_state_t *state, unsigned cur_idx)
{
    unsigned idx = cur_idx + 1;
    for (; idx < kMaxAnimations; idx++) {
        animation_t anim = state->animations[idx];
        if (anim.anim.type != _EmptyAnimationType) break;
    }

    return idx;
}

void schedule_animation(saver_state_t *state, animation_t anim)
{
    anim.start_time = anim_now();

    // Find next empty element
    for (unsigned idx = 0; idx < kMaxAnimations; idx++) {
        animation_t check_anim = state->animations[idx];
        if (check_anim.anim.type == _EmptyAnimationType) {
            state->animations[idx] = anim;
            state->num_animations++;
            break;
        }
    }
}

void update_animations(saver_state_t *state)
{
    unsigned idx = 0;
    unsigned processed_animations = 0;
    unsigned completed_animations = 0;
    while (processed_animations < state->num_animations) {
        animation_t *anim = &state->animations[idx];

        update_single_animation(state, anim);
        if (anim->completed) {
            state->animations[idx].anim.type = _EmptyAnimationType;
            if (anim->completion_func != NULL) {
                anim->completion_func((struct animation_t *)anim, anim->completion_func_context);
            }

            completed_animations++;
        }

        processed_animations++;
        idx = next_anim_index(state, idx);
        if (idx == kMaxAnimations) break;
    }

    state->num_animations -= completed_animations;
}

void draw_logo(saver_state_t *state)
{
    if (state->logo_svg_handle == NULL) {
        GError *error = NULL;
        GBytes *bytes = get_data_for_resource("/resources/logo.svg");

        gsize size = 0;
        gconstpointer data = g_bytes_get_data(bytes, &size);
        state->logo_svg_handle = rsvg_handle_new_from_data(data, size, &error);
        g_bytes_unref(bytes);
        if (error != NULL) {
            fprintf(stderr, "Error loading logo SVG\n");
            return;
        }
    }


    cairo_t *cr = state->ctx;

    cairo_save(cr);
    cairo_set_source_rgb(cr, (208.0 / 255.0), (69.0 / 255.0), (255.0 / 255.0));
    double fill_height = (state->canvas_height * state->logo_fill_progress);
    cairo_rectangle(cr, 0, 0, kLogoBackgroundWidth, fill_height);
    cairo_fill(cr);

    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    
    // Scale and draw logo
    RsvgDimensionData dimensions;
    rsvg_handle_get_dimensions(state->logo_svg_handle, &dimensions);

    const double padding = 100.0;
    double scale_factor = ((kLogoBackgroundWidth - (padding * 2.0)) / dimensions.width);
    double scaled_height = (dimensions.height * scale_factor);
    double y_position = (state->canvas_height - scaled_height) / 2.0;
    cairo_translate(cr, padding, y_position);
    cairo_scale(cr, scale_factor, scale_factor);
    rsvg_handle_render_cairo(state->logo_svg_handle, cr);

    cairo_restore(cr);
}

void draw_password_field(saver_state_t *state)
{
    const double cursor_height = 40.0;
    const double cursor_width  = 30.0;
    const double field_x = kLogoBackgroundWidth + 50.0;
    const double field_y = (state->canvas_height - cursor_height) / 2.0;
    const double field_padding = 10.0;
    
    cairo_t *cr = state->ctx;

    // Draw status text
    const char *prompt = (state->password_prompt ?: "???");
    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
    pango_layout_set_font_description(state->pango_layout, state->status_font);
    pango_layout_set_text(state->pango_layout, prompt, -1);

    int t_width, t_height;
    pango_layout_get_size(state->pango_layout, &t_width, &t_height);
    double line_height = t_height / PANGO_SCALE;

    cairo_move_to(cr, field_x, field_y - line_height - field_padding);
    pango_cairo_show_layout(cr, state->pango_layout);

    // Draw password asterisks
    if (state->asterisk_svg_handle == NULL) {
        GError *error = NULL;
        GBytes *bytes = get_data_for_resource("/resources/asterisk.svg");

        gsize size = 0;
        gconstpointer data = g_bytes_get_data(bytes, &size);
        state->asterisk_svg_handle = rsvg_handle_new_from_data(data, size, &error);
        g_bytes_unref(bytes);
        if (error != NULL) {
            fprintf(stderr, "Error loading asterisk SVG\n");
            return;
        }
    }

    const double cursor_padding_x = 10.0;
    double cursor_offset_x = 0.0;

    RsvgDimensionData dimensions;
    rsvg_handle_get_dimensions(state->asterisk_svg_handle, &dimensions);
    
    double asterisk_height = cursor_height - 20.0;
    double scale_factor = (asterisk_height / dimensions.height);
    double scaled_width = (dimensions.width * scale_factor);

    for (unsigned i = 0; i < strlen(state->password_buffer); i++) {
        cairo_save(cr);
        cairo_translate(cr, field_x + cursor_offset_x, field_y + ((cursor_height - asterisk_height) / 2.0));
        cairo_scale(cr, scale_factor, scale_factor);
        rsvg_handle_render_cairo(state->asterisk_svg_handle, cr);
        cairo_restore(cr);

        cursor_offset_x += scaled_width + cursor_padding_x;
    }

    
    // Draw cursor
    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, state->cursor_opacity);
    if (!state->is_processing) {
        cairo_rectangle(cr, field_x + cursor_offset_x, field_y, cursor_width, cursor_height);
    } else {
        // Fill asterisks
        cairo_rectangle(cr, field_x, field_y, cursor_offset_x, cursor_height);
    }
    cairo_fill(cr);

}