Skip to content

Commit b5e09bb

Browse files
committed
small refactoring
1 parent 5c3ebed commit b5e09bb

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

src/engine.zig

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ pub const Element = struct {
971971
});
972972
if (std.mem.eql(u8, new_translated, old_translated) and !forced) {
973973
// Don't update if nothing changed
974+
// TODO: This should not be needed
974975
return;
975976
}
976977

@@ -986,7 +987,7 @@ pub const Element = struct {
986987
try self.type.text_input.text.appendSlice(allocator, new_text);
987988
self.text_data_to_runes(allocator);
988989
if (display.generate_text_texture(self.type.text_input.text.items, self.type.text_input.font)) |texture| {
989-
self.type.text_input.cursor_pixels = text_size(display, texture, .normal).width;
990+
self.type.text_input.cursor_pixels = TextSize.normal.pixel_size(display, texture).width;
990991
self.type.text_input.texture = texture;
991992
self.type.text_input.cursor_character = self.type.text_input.runes.items.len;
992993
} else {
@@ -1113,6 +1114,7 @@ pub const Element = struct {
11131114
return child;
11141115
}
11151116

1117+
/// Swap the ordering of two child elements belonging to this panel.
11161118
pub inline fn swap(self: *Element, from: usize, to: usize) void {
11171119
std.debug.assert(self.type == .panel);
11181120
std.debug.assert(from < self.type.panel.children.items.len);
@@ -1270,7 +1272,7 @@ pub const Element = struct {
12701272
}
12711273

12721274
if (self.type.button.text_texture) |t| {
1273-
const size = text_size(display, t, .normal);
1275+
const size = TextSize.normal.pixel_size(display, t);
12741276
width += size.width;
12751277
}
12761278
return @max(self.minimum.width, width);
@@ -1482,6 +1484,7 @@ pub const Element = struct {
14821484
}
14831485
}
14841486

1487+
/// Draw the contents of a panel.
14851488
inline fn draw_panel(
14861489
element: *Element,
14871490
display: *Display,
@@ -1509,6 +1512,7 @@ pub const Element = struct {
15091512
}
15101513
}
15111514

1515+
/// Draw a basic rectangle.
15121516
inline fn draw_rectangle_element(
15131517
element: *Element,
15141518
display: *Display,
@@ -1528,6 +1532,8 @@ pub const Element = struct {
15281532
_ = sdl.SDL_RenderFillRect(display.renderer, @ptrCast(&dest));
15291533
}
15301534

1535+
/// Draw a text input box along with any text or cursor that
1536+
/// may appear inside the text input box.
15311537
inline fn draw_text_input(
15321538
element: *Element,
15331539
display: *Display,
@@ -1562,7 +1568,6 @@ pub const Element = struct {
15621568
}
15631569

15641570
if (element.texture) |texture| {
1565-
//const size = text_size(display, texture.texture, .normal);
15661571
const icon_size = element.rect.height - element.pad.top - element.pad.bottom;
15671572
// Draw the text
15681573
var dest: Rect = .{
@@ -1591,7 +1596,7 @@ pub const Element = struct {
15911596

15921597
if (element.type.text_input.text.items.len > 0) {
15931598
if (element.type.text_input.texture) |texture| {
1594-
const size = text_size(display, texture, .normal);
1599+
const size = .normal.pixel_size(display, texture);
15951600
// Draw the text
15961601
var dest: Rect = .{
15971602
.x = @round(x),
@@ -1615,7 +1620,7 @@ pub const Element = struct {
16151620
}
16161621
} else {
16171622
if (element.type.text_input.placeholder_texture) |texture| {
1618-
const size = text_size(display, texture, .normal);
1623+
const size = .normal.pixel_size(display, texture);
16191624
// Draw the placeholder text
16201625
var dest: Rect = .{
16211626
.x = @round(x),
@@ -1753,6 +1758,7 @@ pub const Element = struct {
17531758
}
17541759
}
17551760

1761+
/// Draw a radio box combined with a text label.
17561762
inline fn draw_checkbox(element: *Element, display: *Display, _: Vector, _: ?Clip, scroll_offset: Vector) void {
17571763
const checkbox = display.checkbox();
17581764
// Output checkbox text.
@@ -1776,6 +1782,7 @@ pub const Element = struct {
17761782
}
17771783
}
17781784

1785+
/// Draw a progress bar.
17791786
inline fn draw_progress_bar(element: *Element, display: *Display, _: Vector, _: ?Clip) void {
17801787
// Draw the background matching the current button state
17811788
if (element.texture) |texture| {
@@ -1836,6 +1843,9 @@ pub const Element = struct {
18361843
}
18371844
}
18381845

1846+
/// Draw a button with its text and/or icon. Mouse hover, mouse click
1847+
/// and the disabled status may change the picture or icon
1848+
/// displayed in the button.
18391849
inline fn draw_button(element: *Element, display: *Display, _: Vector, _: ?Clip, scroll_offset: Vector) void {
18401850
// Draw the background matching the current button state
18411851
if (element.current_background()) |background_image| {
@@ -1876,7 +1886,7 @@ pub const Element = struct {
18761886
// The inner content can contain a button and/or text texture.
18771887
var content_width = element.type.button.icon_size.width;
18781888
if (element.type.button.text_texture) |texture| {
1879-
const size = text_size(display, texture, .normal);
1889+
const size = .normal.pixel_size(display, texture);
18801890

18811891
// Do we need space between text and icon?
18821892
if (content_width > 0)
@@ -1932,7 +1942,7 @@ pub const Element = struct {
19321942
}
19331943
}
19341944
if (element.type.button.text_texture) |texture| {
1935-
const size = text_size(display, texture, .normal);
1945+
const size = .normal.pixel_size(display, texture);
19361946
var dest: Rect = .{
19371947
.x = element.rect.x + element.type.button.icon_size.width + element.pad.left + content_offset,
19381948
.y = element.rect.y + (element.rect.height / 2.0) - (size.height / 2),
@@ -2001,7 +2011,7 @@ pub const Element = struct {
20012011
if (display.generate_text_texture(self.type.text_input.text.items, self.type.text_input.font)) |texture| {
20022012
self.type.text_input.texture = texture;
20032013
// For now, the cursor position is simply the end of the text.
2004-
self.type.text_input.cursor_pixels = text_size(display, texture, .normal).width;
2014+
self.type.text_input.cursor_pixels = .normal.pixel_size(display, texture).width;
20052015
}
20062016
} else {
20072017
self.type.text_input.cursor_pixels = 0;
@@ -2121,7 +2131,7 @@ pub const Element = struct {
21212131
display.selected = null;
21222132
}
21232133

2124-
pub fn text_runes_to_data(self: *Element, allocator: Allocator) void {
2134+
fn text_runes_to_data(self: *Element, allocator: Allocator) void {
21252135
std.debug.assert(self.type == .text_input);
21262136
self.type.text_input.text.clearRetainingCapacity();
21272137
for (self.type.text_input.runes.items) |rune| {
@@ -2135,7 +2145,7 @@ pub const Element = struct {
21352145
}
21362146
}
21372147

2138-
pub fn text_data_to_runes(self: *Element, allocator: Allocator) void {
2148+
fn text_data_to_runes(self: *Element, allocator: Allocator) void {
21392149
std.debug.assert(self.type == .text_input);
21402150
self.type.text_input.runes.clearRetainingCapacity();
21412151
var v = std.unicode.Utf8View.init(self.type.text_input.text.items) catch {
@@ -2160,13 +2170,6 @@ pub const Element = struct {
21602170
self.type.text_input.cursor_character = self.type.text_input.runes.items.len;
21612171
cursor_slice = self.type.text_input.text.items.len;
21622172
}
2163-
2164-
if (dev_build) {
2165-
//debug(
2166-
// "text to runes cursor {d} {d}",
2167-
// .{ self.type.text_input.cursor_character, cursor_slice },
2168-
//);
2169-
}
21702173
}
21712174

21722175
inline fn do_word_alignment(
@@ -2285,14 +2288,6 @@ inline fn draw_rectangle(
22852288
}
22862289
}
22872290

2288-
inline fn text_size(display: *Display, texture: *sdl.SDL_Texture, size: TextSize) Size {
2289-
const height = display.text_height * display.scale * size.height();
2290-
return .{
2291-
.height = height,
2292-
.width = height * @as(f32, @floatFromInt(texture.*.w)) / @as(f32, @floatFromInt(texture.*.h)),
2293-
};
2294-
}
2295-
22962291
/// Calculate the layout of all elements, and optionally render every element.
22972292
///
22982293
/// Normally text is converted to an image and rendered left to right, starting
@@ -2342,7 +2337,7 @@ inline fn draw_label(
23422337

23432338
for (children, 0..) |*item, i| {
23442339
const is_cr = item.text != null and item.text.?.len == 1 and item.text.?[0] == '\n';
2345-
const size = text_size(display, item.texture, text_height);
2340+
const size = text_height.pixel_size(display, item.texture);
23462341
// Would drawing this word overflow?
23472342
if ((x + size.width > x_ending and line_word_count > 0) or is_cr) {
23482343
element.do_word_alignment(
@@ -2478,7 +2473,7 @@ inline fn text_elements_size(
24782473
var line_word_count: usize = 0;
24792474

24802475
for (children, 0..) |item, i| {
2481-
const size = text_size(display, item.texture, text_height);
2476+
const size = text_height.pixel_size(display, item.texture);
24822477

24832478
mm.min_width = @max(size.width, mm.min_width);
24842479
mm.min_height = @max(size.height, mm.min_height);
@@ -2538,6 +2533,14 @@ pub const TextSize = enum {
25382533
.footnote => 0.75,
25392534
};
25402535
}
2536+
2537+
pub fn pixel_size(size: TextSize, display: *const Display, texture: *const sdl.SDL_Texture) Size {
2538+
const height_adjusted = display.text_height * display.scale * size.height();
2539+
return .{
2540+
.height = height_adjusted,
2541+
.width = height_adjusted * @as(f32, @floatFromInt(texture.*.w)) / @as(f32, @floatFromInt(texture.*.h)),
2542+
};
2543+
}
25412544
};
25422545

25432546
/// Display describes how to draw all visual elements onto the main

0 commit comments

Comments
 (0)