view libmlk-example/mlk/example/trace-hud.c @ 507:d49a05e7a5b5

ui: separate delegate/style Now UI elements do have different styling properties: - _delegate: functions used to update, draw or perform specific actions on the UI element. - _style: basic properties that the delegate should support if possible.
author David Demelier <markand@malikania.fr>
date Thu, 02 Mar 2023 21:36:43 +0100
parents 6100c643dba0
children 970cad994a95
line wrap: on
line source

/*
 * trace_hud.c -- on screen hud
 *
 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include <mlk/core/font.h>
#include <mlk/core/trace.h>
#include <mlk/core/window.h>

#include <mlk/ui/label.h>

#include "trace-hud.h"

#define LINES_MAX       (4)
#define THEME(t)        ((t) ? (t) : &mlk_theme)

static struct {
	char lines[LINES_MAX + 1][TRACE_LINE_MAX];
	unsigned int elapsed;
} data;

struct mlk_trace_hud mlk_trace_hud = {
	.timeout = MLK_TRACE_HUD_TIMEOUT_DEFAULT
};

void
mlk_trace_hud_handler(const char *str)
{
	assert(str);

	/* 1.Try to find an empty line. */
	for (size_t i = 0; i < LINES_MAX; ++i) {
		if (data.lines[i][0] == '\0') {
			snprintf(data.lines[i], sizeof (data.lines[i]), "%s", str);
			return;
		}
	}

	/* 2. All lines are full, put in last one and move other. */
	memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX - 1);
	snprintf(data.lines[LINES_MAX - 1], sizeof (data.lines[0]), "%s", str);

	/* 3. Reset elapsed time now. */
	data.elapsed = 0;
}

void
mlk_trace_hud_update(unsigned int ticks)
{
	data.elapsed += ticks;

	/*
	 * We have an empty line in the data.lines at LINES_MAX, so we simply so
	 * to move the whole array.
	 *
	 * [0] = "abc"
	 * [1] = "def"
	 * [2] = "xyz"
	 * [3] = "zef"
	 * [n] = "ldkf"
	 * [LINES_MAX + 1] = "\0"
	 */
	if (data.elapsed >= mlk_trace_hud.timeout) {
		data.elapsed = 0;
		memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX);
	}
}

void
mlk_trace_hud_draw(void)
{
#if 0
	struct mlk_theme *th;
	int x, y;

	th = THEME(mlk_trace_hud.theme);
	x = th->padding;
	y = th->padding;

	for (int i = 0; i < LINES_MAX && data.lines[i][0]; ++i) {
		mlk_label_draw(&(struct mlk_label) {
			.x = x,
			.y = y,
			.text = data.lines[i],
			.flags = MLK_LABEL_FLAGS_SHADOW
		});

		y += mlk_font_height(th->fonts[MLK_THEME_FONT_INTERFACE]);
		y += th->padding;
	}
#endif
}

void
mlk_trace_hud_clear(void)
{
	memset(&data, 0, sizeof (data));
}