Просмотр исходного кода

Draw input hints to a separate string

This makes it possible to actually run things after deleting
characters...
Luna Stadler лет назад: 4
Родитель
Сommit
42f9763ae9
1 измененных файлов с 33 добавлено и 10 удалено
  1. 33 10
      zig/sdl/hello_sdl.zig

+ 33 - 10
zig/sdl/hello_sdl.zig

402
    const renderer = c.SDL_CreateRenderer(screen, -1, c.SDL_RENDERER_ACCELERATED);
402
    const renderer = c.SDL_CreateRenderer(screen, -1, c.SDL_RENDERER_ACCELERATED);
403
403
404
    var msg = "                                                                                                    ".*;
404
    var msg = "                                                                                                    ".*;
405
    var msg_overlay = "                                                                                                    ".*;
405
    var pos: usize = 0;
406
    var pos: usize = 0;
406
    var max_chars = std.math.min(@divTrunc(@intCast(usize, window_width), @intCast(usize, glyph_width)), msg.len);
407
    var max_chars = std.math.min(@divTrunc(@intCast(usize, window_width), @intCast(usize, glyph_width)), msg.len);
407
408
451
                    if (ctrlPressed) {
452
                    if (ctrlPressed) {
452
                        switch (event.key.keysym.sym) {
453
                        switch (event.key.keysym.sym) {
453
                            c.SDLK_a => {
454
                            c.SDLK_a => {
455
                                if (msg_overlay[pos] == '_') {
456
                                    msg_overlay[pos] = ' ';
457
                                }
454
                                pos = 0;
458
                                pos = 0;
455
                                msg[pos] = '_';
459
                                msg_overlay[pos] = '_';
456
                            },
460
                            },
457
                            c.SDLK_k => {
461
                            c.SDLK_k => {
458
                                var i: usize = 0;
462
                                var i: usize = 0;
493
                                quit = true;
497
                                quit = true;
494
                            },
498
                            },
495
                            c.SDLK_BACKSPACE => {
499
                            c.SDLK_BACKSPACE => {
500
                                if (msg_overlay[pos] == '_') {
501
                                    msg_overlay[pos] = ' ';
502
                                }
503
                                if (pos > 0) {
504
                                    msg[pos - 1] = ' ';
505
                                }
496
                                pos = if (pos == 0) max_chars - 1 else (pos - 1) % (max_chars - 1);
506
                                pos = if (pos == 0) max_chars - 1 else (pos - 1) % (max_chars - 1);
497
                                msg[pos] = '_';
507
                                msg_overlay[pos] = '_';
498
                                changed = true;
508
                                changed = true;
499
                            },
509
                            },
500
                            c.SDLK_RETURN => {
510
                            c.SDLK_RETURN => {
536
                    if (!ctrlPressed and event.text.text.len > 0) {
546
                    if (!ctrlPressed and event.text.text.len > 0) {
537
                        c.SDL_Log("input: '%s' at %d", event.text.text, pos);
547
                        c.SDL_Log("input: '%s' at %d", event.text.text, pos);
538
                        msg[pos] = event.text.text[0];
548
                        msg[pos] = event.text.text[0];
549
                        msg_overlay[pos] = ' ';
539
                        pos = (pos + 1) % (max_chars - 1);
550
                        pos = (pos + 1) % (max_chars - 1);
540
551
541
                        changed = true;
552
                        changed = true;
561
        _ = c.SDL_RenderClear(renderer);
572
        _ = c.SDL_RenderClear(renderer);
562
573
563
        // thanks to https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2 for some actually useful code here
574
        // thanks to https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2 for some actually useful code here
564
        const white: c.SDL_Color = c.SDL_Color{ .r = 255, .g = 255, .b = 255, .a = 0 };
575
        const white: c.SDL_Color = c.SDL_Color{ .r = 255, .g = 255, .b = 255, .a = 255 };
565
        const gray: c.SDL_Color = c.SDL_Color{ .r = 150, .g = 150, .b = 150, .a = 255 };
576
        const gray: c.SDL_Color = c.SDL_Color{ .r = 150, .g = 150, .b = 150, .a = 255 };
566
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 100 };
567
        // Shaded vs Solid gives a nicer output, with solid the output
568
        // was squiggly and not aligned with a baseline.
569
        const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
570
        const texture = c.SDL_CreateTextureFromSurface(renderer, text);
571
        c.SDL_FreeSurface(text);
572
        _ = c.SDL_RenderCopy(renderer, texture, null, &c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(c_int, msg.len) * glyph_width, .h = glyph_height });
577
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
578
579
        {
580
            // Shaded vs Solid gives a nicer output, with solid the output
581
            // was squiggly and not aligned with a baseline.
582
            const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
583
            const texture = c.SDL_CreateTextureFromSurface(renderer, text);
584
            c.SDL_FreeSurface(text);
585
            _ = c.SDL_RenderCopy(renderer, texture, null, &c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(c_int, msg.len) * glyph_width, .h = glyph_height });
586
        }
587
        {
588
            const text = c.TTF_RenderUTF8_Shaded(font, &msg_overlay, white, c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 0 });
589
            const texture = c.SDL_CreateTextureFromSurface(renderer, text);
590
            if (c.SDL_SetTextureBlendMode(texture, c.SDL_BLENDMODE_ADD) != 0) {
591
                c.SDL_Log("Unable set texture blend mode: %s", c.SDL_GetError());
592
            }
593
            c.SDL_FreeSurface(text);
594
            _ = c.SDL_RenderCopy(renderer, texture, null, &c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(c_int, msg.len) * glyph_width, .h = glyph_height });
595
        }
573
596
574
        var i: c_int = 1;
597
        var i: c_int = 1;
575
        for (commands) |*command| {
598
        for (commands) |*command| {