Browse Source

Refactor text drawing

master
Kenneth Bruen 9 months ago
parent
commit
ffd84715d7
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 54
      src/departure.zig
  2. 14
      src/home.zig
  3. 13
      src/main.zig
  4. 2
      src/state.zig

54
src/departure.zig

@ -141,12 +141,7 @@ pub fn render(state: *AppState) !void {
const destination = try std.fmt.allocPrintZ(allocator, "{s}", .{first.get("direction").?.string});
defer allocator.free(destination);
var next_y = y;
if (state.db_font) |db_font| {
next_y += @intFromFloat(raylib.DrawAndMeasureTextEx(db_font, line.ptr, 16, @floatFromInt(y), 32, 1, rl.WHITE).y);
} else {
rl.DrawText(line.ptr, 16, y, 32, rl.WHITE);
next_y += 32;
}
next_y += @intFromFloat(raylib.DrawAndMeasureTextEx(state.font, line.ptr, 16, @floatFromInt(y), 32, 1, rl.WHITE).y);
next_y += 16;
if (ds.platform.items.len == 0) blk: {
if (first.get("platform")) |platform_raw| {
@ -154,20 +149,15 @@ pub fn render(state: *AppState) !void {
.string => |p| {
const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk;
defer allocator.free(platform);
if (state.db_font) |db_font| {
raylib.DrawRightAlignedTextEx(db_font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), 40, 1, rl.WHITE);
} else {
raylib.DrawRightAlignedText(platform.ptr, rl.GetScreenWidth() - 16, y, 40, rl.WHITE);
}
raylib.DrawRightAlignedTextEx(state.font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), 40, 1, rl.WHITE);
},
else => {},
}
}
}
y = next_y;
if (state.db_font) |db_font| {
y += @intFromFloat(raylib.DrawAndMeasureTextEx(
db_font,
state.font,
destination.ptr,
16,
@floatFromInt(y),
@ -175,10 +165,7 @@ pub fn render(state: *AppState) !void {
1,
rl.WHITE,
).y);
} else {
rl.DrawText(destination.ptr, 16, y, 56, rl.WHITE);
y += 56;
}
y += 16;
}
if (not_cancelled.items.len > 1) {
@ -189,21 +176,15 @@ pub fn render(state: *AppState) !void {
var y = rl.GetScreenHeight() - (font_size + 8) * max_trains - 4;
rl.DrawRectangle(0, y, rl.GetScreenWidth(), rl.GetScreenHeight(), rl.WHITE);
y += 8;
const label_measurement_width = if (state.db_font) |db_font| @as(c_int, @intFromFloat(raylib.DrawAndMeasureTextEx(
db_font,
const label_measurement_width = @as(c_int, @intFromFloat(raylib.DrawAndMeasureTextEx(
state.font,
if (max_trains == 1) "Next train: " else "Next trains: ",
@floatFromInt(x),
@floatFromInt(y),
@floatFromInt(font_size),
1,
db_blue,
).x)) else raylib.DrawAndMeasureText(
if (max_trains == 1) "Next train: " else "Next trains: ",
x,
y,
font_size,
db_blue,
).width;
).x));
x += label_measurement_width;
// Compute line name width
@ -220,14 +201,10 @@ pub fn render(state: *AppState) !void {
},
);
defer allocator.free(next_train_line);
if (state.db_font) |db_font| {
line_name_width = @max(
line_name_width,
@as(c_int, @intFromFloat(rl.MeasureTextEx(db_font, next_train_line.ptr, @floatFromInt(font_size), 1).x)),
@as(c_int, @intFromFloat(rl.MeasureTextEx(state.font, next_train_line.ptr, @floatFromInt(font_size), 1).x)),
);
} else {
line_name_width = @max(line_name_width, rl.MeasureText(next_train_line.ptr, font_size));
}
}
const destionation_x = x + line_name_width;
@ -251,24 +228,15 @@ pub fn render(state: *AppState) !void {
},
);
defer allocator.free(next_train_direction);
if (state.db_font) |db_font| {
rl.DrawTextEx(db_font, next_train_line.ptr, .{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, font_size, 1, db_blue);
rl.DrawTextEx(db_font, next_train_direction.ptr, .{ .x = @floatFromInt(destionation_x), .y = @floatFromInt(y) }, font_size, 1, db_blue);
} else {
rl.DrawText(next_train_line.ptr, x, y, font_size, db_blue);
rl.DrawText(next_train_direction.ptr, destionation_x, y, font_size, db_blue);
}
rl.DrawTextEx(state.font, next_train_line.ptr, .{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, font_size, 1, db_blue);
rl.DrawTextEx(state.font, next_train_direction.ptr, .{ .x = @floatFromInt(destionation_x), .y = @floatFromInt(y) }, font_size, 1, db_blue);
if (ds.platform.items.len == 0) blk: {
if (second.get("platform")) |platform_raw| {
switch (platform_raw) {
.string => |p| {
const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk;
defer allocator.free(platform);
if (state.db_font) |db_font| {
raylib.DrawRightAlignedTextEx(db_font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), @floatFromInt(font_size), 1, db_blue);
} else {
raylib.DrawRightAlignedText(platform.ptr, rl.GetScreenWidth() - 16, y, font_size, db_blue);
}
raylib.DrawRightAlignedTextEx(state.font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), @floatFromInt(font_size), 1, db_blue);
},
else => {},
}

14
src/home.zig

@ -136,13 +136,9 @@ pub fn render(state: *AppState) !void {
const body_size: c_int = 28;
rl.ClearBackground(rl.BLACK);
x += raylib.DrawAndMeasureText("Station: ", x, y, title_size, rl.WHITE).width + 8;
x += @intFromFloat(raylib.DrawAndMeasureTextEx(state.font, "Station: ", @floatFromInt(x), @floatFromInt(y), @floatFromInt(title_size), 1, rl.WHITE).x + 8);
rl.DrawLine(x, y + title_size + 2, rl.GetScreenWidth() - 16, y + title_size + 2, rl.WHITE);
if (state.db_font) |db_font| {
rl.DrawTextEx(db_font, hs.station_name.items.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, title_size, 0.9, rl.WHITE);
} else {
rl.DrawText(hs.station_name.items.ptr, x, y, title_size, rl.WHITE);
}
rl.DrawTextEx(state.font, hs.station_name.items.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, title_size, 0.9, rl.WHITE);
y += title_size + 2 + 16;
@ -175,11 +171,7 @@ pub fn render(state: *AppState) !void {
}
}
if (state.db_font) |db_font| {
rl.DrawTextEx(db_font, suggestion.name.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, body_size, 0.9, color);
} else {
rl.DrawText(suggestion.name.ptr, x, y, body_size, color);
}
rl.DrawTextEx(state.font, suggestion.name.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, body_size, 0.9, color);
y += body_size + 2;
}

13
src/main.zig

@ -16,12 +16,11 @@ pub fn main() !void {
const font = blk: {
var cp_cnt: c_int = 0;
const cp = rl.LoadCodepoints("aäbcdeèéfghijklmnoöpqrsßtuüvwxyzAÄBCDEÈÉFGHIJKLMNOÖPQRSẞTUÜVWXYZ0123456789-_,()/\\:+", &cp_cnt,);
const maybeFont = rl.LoadFontEx("./db.ttf", 64, cp, cp_cnt);
if (std.meta.eql(maybeFont, rl.GetFontDefault())) {
break :blk null;
}
break :blk maybeFont;
const cp = rl.LoadCodepoints(
"aäbcdeèéfghijklmnoöpqrsßtuüvwxyzAÄBCDEÈÉFGHIJKLMNOÖPQRSẞTUÜVWXYZ0123456789-_,()/\\:+",
&cp_cnt,
);
break :blk rl.LoadFontEx("./db.ttf", 64, cp, cp_cnt);
};
var station_name_buffer: [100]u8 = .{0} ** 100;
@ -29,7 +28,7 @@ pub fn main() !void {
var station_id_buffer: [10]u8 = .{0} ** 10;
var appState = AppState{
.allocator = allocator,
.db_font = font,
.font = font,
.home_screen_state = .{
.station_name = std.ArrayListUnmanaged(u8).initBuffer(&station_name_buffer),
},

2
src/state.zig

@ -33,7 +33,7 @@ pub const DepartureScreenState = struct {
allocator: std.mem.Allocator,
close_app: bool = false,
db_font: ?rl.Font = null,
font: rl.Font,
screen: Screen = .home,
home_screen_state: HomeScreenState,
departure_screen_state: DepartureScreenState,

Loading…
Cancel
Save