Refactor text drawing
This commit is contained in:
parent
13944ace05
commit
ffd84715d7
4 changed files with 34 additions and 75 deletions
|
@ -94,7 +94,7 @@ pub fn render(state: *AppState) !void {
|
||||||
ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5);
|
ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5);
|
||||||
},
|
},
|
||||||
rl.KEY_T => {
|
rl.KEY_T => {
|
||||||
ds.include_tram = !ds.include_tram;
|
ds.include_tram = !ds.include_tram;
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
@ -141,12 +141,7 @@ pub fn render(state: *AppState) !void {
|
||||||
const destination = try std.fmt.allocPrintZ(allocator, "{s}", .{first.get("direction").?.string});
|
const destination = try std.fmt.allocPrintZ(allocator, "{s}", .{first.get("direction").?.string});
|
||||||
defer allocator.free(destination);
|
defer allocator.free(destination);
|
||||||
var next_y = y;
|
var next_y = y;
|
||||||
if (state.db_font) |db_font| {
|
next_y += @intFromFloat(raylib.DrawAndMeasureTextEx(state.font, line.ptr, 16, @floatFromInt(y), 32, 1, rl.WHITE).y);
|
||||||
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 += 16;
|
next_y += 16;
|
||||||
if (ds.platform.items.len == 0) blk: {
|
if (ds.platform.items.len == 0) blk: {
|
||||||
if (first.get("platform")) |platform_raw| {
|
if (first.get("platform")) |platform_raw| {
|
||||||
|
@ -154,31 +149,23 @@ pub fn render(state: *AppState) !void {
|
||||||
.string => |p| {
|
.string => |p| {
|
||||||
const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk;
|
const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk;
|
||||||
defer allocator.free(platform);
|
defer allocator.free(platform);
|
||||||
if (state.db_font) |db_font| {
|
raylib.DrawRightAlignedTextEx(state.font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), 40, 1, rl.WHITE);
|
||||||
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);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y = next_y;
|
y = next_y;
|
||||||
if (state.db_font) |db_font| {
|
y += @intFromFloat(raylib.DrawAndMeasureTextEx(
|
||||||
y += @intFromFloat(raylib.DrawAndMeasureTextEx(
|
state.font,
|
||||||
db_font,
|
destination.ptr,
|
||||||
destination.ptr,
|
16,
|
||||||
16,
|
@floatFromInt(y),
|
||||||
@floatFromInt(y),
|
56,
|
||||||
56,
|
1,
|
||||||
1,
|
rl.WHITE,
|
||||||
rl.WHITE,
|
).y);
|
||||||
).y);
|
|
||||||
} else {
|
|
||||||
rl.DrawText(destination.ptr, 16, y, 56, rl.WHITE);
|
|
||||||
y += 56;
|
|
||||||
}
|
|
||||||
y += 16;
|
y += 16;
|
||||||
}
|
}
|
||||||
if (not_cancelled.items.len > 1) {
|
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;
|
var y = rl.GetScreenHeight() - (font_size + 8) * max_trains - 4;
|
||||||
rl.DrawRectangle(0, y, rl.GetScreenWidth(), rl.GetScreenHeight(), rl.WHITE);
|
rl.DrawRectangle(0, y, rl.GetScreenWidth(), rl.GetScreenHeight(), rl.WHITE);
|
||||||
y += 8;
|
y += 8;
|
||||||
const label_measurement_width = if (state.db_font) |db_font| @as(c_int, @intFromFloat(raylib.DrawAndMeasureTextEx(
|
const label_measurement_width = @as(c_int, @intFromFloat(raylib.DrawAndMeasureTextEx(
|
||||||
db_font,
|
state.font,
|
||||||
if (max_trains == 1) "Next train: " else "Next trains: ",
|
if (max_trains == 1) "Next train: " else "Next trains: ",
|
||||||
@floatFromInt(x),
|
@floatFromInt(x),
|
||||||
@floatFromInt(y),
|
@floatFromInt(y),
|
||||||
@floatFromInt(font_size),
|
@floatFromInt(font_size),
|
||||||
1,
|
1,
|
||||||
db_blue,
|
db_blue,
|
||||||
).x)) else raylib.DrawAndMeasureText(
|
).x));
|
||||||
if (max_trains == 1) "Next train: " else "Next trains: ",
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
font_size,
|
|
||||||
db_blue,
|
|
||||||
).width;
|
|
||||||
x += label_measurement_width;
|
x += label_measurement_width;
|
||||||
|
|
||||||
// Compute line name width
|
// Compute line name width
|
||||||
|
@ -220,14 +201,10 @@ pub fn render(state: *AppState) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
defer allocator.free(next_train_line);
|
defer allocator.free(next_train_line);
|
||||||
if (state.db_font) |db_font| {
|
line_name_width = @max(
|
||||||
line_name_width = @max(
|
line_name_width,
|
||||||
line_name_width,
|
@as(c_int, @intFromFloat(rl.MeasureTextEx(state.font, next_train_line.ptr, @floatFromInt(font_size), 1).x)),
|
||||||
@as(c_int, @intFromFloat(rl.MeasureTextEx(db_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;
|
const destionation_x = x + line_name_width;
|
||||||
|
|
||||||
|
@ -251,24 +228,15 @@ pub fn render(state: *AppState) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
defer allocator.free(next_train_direction);
|
defer allocator.free(next_train_direction);
|
||||||
if (state.db_font) |db_font| {
|
rl.DrawTextEx(state.font, next_train_line.ptr, .{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, font_size, 1, db_blue);
|
||||||
rl.DrawTextEx(db_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);
|
||||||
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);
|
|
||||||
}
|
|
||||||
if (ds.platform.items.len == 0) blk: {
|
if (ds.platform.items.len == 0) blk: {
|
||||||
if (second.get("platform")) |platform_raw| {
|
if (second.get("platform")) |platform_raw| {
|
||||||
switch (platform_raw) {
|
switch (platform_raw) {
|
||||||
.string => |p| {
|
.string => |p| {
|
||||||
const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk;
|
const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk;
|
||||||
defer allocator.free(platform);
|
defer allocator.free(platform);
|
||||||
if (state.db_font) |db_font| {
|
raylib.DrawRightAlignedTextEx(state.font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), @floatFromInt(font_size), 1, db_blue);
|
||||||
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);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
14
src/home.zig
14
src/home.zig
|
@ -136,13 +136,9 @@ pub fn render(state: *AppState) !void {
|
||||||
const body_size: c_int = 28;
|
const body_size: c_int = 28;
|
||||||
|
|
||||||
rl.ClearBackground(rl.BLACK);
|
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);
|
rl.DrawLine(x, y + title_size + 2, rl.GetScreenWidth() - 16, y + title_size + 2, rl.WHITE);
|
||||||
if (state.db_font) |db_font| {
|
rl.DrawTextEx(state.font, hs.station_name.items.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, title_size, 0.9, rl.WHITE);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
y += title_size + 2 + 16;
|
y += title_size + 2 + 16;
|
||||||
|
|
||||||
|
@ -175,11 +171,7 @@ pub fn render(state: *AppState) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.db_font) |db_font| {
|
rl.DrawTextEx(state.font, suggestion.name.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, body_size, 0.9, color);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
y += body_size + 2;
|
y += body_size + 2;
|
||||||
}
|
}
|
||||||
|
|
15
src/main.zig
15
src/main.zig
|
@ -15,13 +15,12 @@ pub fn main() !void {
|
||||||
defer rl.CloseWindow();
|
defer rl.CloseWindow();
|
||||||
|
|
||||||
const font = blk: {
|
const font = blk: {
|
||||||
var cp_cnt: c_int = 0;
|
var cp_cnt: c_int = 0;
|
||||||
const cp = rl.LoadCodepoints("aäbcdeèéfghijklmnoöpqrsßtuüvwxyzAÄBCDEÈÉFGHIJKLMNOÖPQRSẞTUÜVWXYZ0123456789-_,()/\\:+", &cp_cnt,);
|
const cp = rl.LoadCodepoints(
|
||||||
const maybeFont = rl.LoadFontEx("./db.ttf", 64, cp, cp_cnt);
|
"aäbcdeèéfghijklmnoöpqrsßtuüvwxyzAÄBCDEÈÉFGHIJKLMNOÖPQRSẞTUÜVWXYZ0123456789-_,()/\\:+",
|
||||||
if (std.meta.eql(maybeFont, rl.GetFontDefault())) {
|
&cp_cnt,
|
||||||
break :blk null;
|
);
|
||||||
}
|
break :blk rl.LoadFontEx("./db.ttf", 64, cp, cp_cnt);
|
||||||
break :blk maybeFont;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var station_name_buffer: [100]u8 = .{0} ** 100;
|
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 station_id_buffer: [10]u8 = .{0} ** 10;
|
||||||
var appState = AppState{
|
var appState = AppState{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.db_font = font,
|
.font = font,
|
||||||
.home_screen_state = .{
|
.home_screen_state = .{
|
||||||
.station_name = std.ArrayListUnmanaged(u8).initBuffer(&station_name_buffer),
|
.station_name = std.ArrayListUnmanaged(u8).initBuffer(&station_name_buffer),
|
||||||
},
|
},
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub const DepartureScreenState = struct {
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
close_app: bool = false,
|
close_app: bool = false,
|
||||||
db_font: ?rl.Font = null,
|
font: rl.Font,
|
||||||
screen: Screen = .home,
|
screen: Screen = .home,
|
||||||
home_screen_state: HomeScreenState,
|
home_screen_state: HomeScreenState,
|
||||||
departure_screen_state: DepartureScreenState,
|
departure_screen_state: DepartureScreenState,
|
||||||
|
|
Loading…
Add table
Reference in a new issue