Skip to content

Commit df4e4b4

Browse files
committed
draw() is now fully implemented in individual Element files
1 parent db3e4bf commit df4e4b4

6 files changed

Lines changed: 79 additions & 75 deletions

File tree

src/button.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn Button(comptime T: type) type {
3434
element: *Element(T),
3535
display: *Display(T),
3636
_: Vector,
37-
_: ?Clip,
37+
_: ?Clip, // parent_clip
3838
scroll_offset: Vector,
3939
) void {
4040
// Draw the background matching the current button state

src/element.zig

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -997,15 +997,13 @@ pub fn Element(comptime T: type) type {
997997
}
998998

999999
switch (element.type) {
1000-
.button => |b| b.draw(element, display, parent_scroll_offset, parent_clip, scroll_offset),
1001-
.checkbox => |c| c.draw(element, display, parent_scroll_offset, parent_clip, scroll_offset),
1002-
.expander => |e| e.draw(),
1003-
.label => |l| l.draw(element, display, parent_scroll_offset, parent_clip, scroll_offset),
1004-
.panel => element.draw_panel(display, parent_scroll_offset, parent_clip, scroll_offset),
1005-
.progress_bar => |d| d.draw(element, display, parent_scroll_offset, parent_clip, scroll_offset),
1006-
.rectangle => element.draw_rectangle_element(display, parent_scroll_offset, parent_clip, scroll_offset),
1007-
.sprite => |s| s.draw(element, display, parent_scroll_offset, parent_clip, scroll_offset),
1008-
.text_input => |ti| ti.draw(element, display, parent_scroll_offset, parent_clip, scroll_offset),
1000+
inline else => |o| o.draw(
1001+
element,
1002+
display,
1003+
parent_scroll_offset,
1004+
parent_clip,
1005+
scroll_offset,
1006+
),
10091007
}
10101008

10111009
// Draw a border around an element if a border is specified, or
@@ -1107,54 +1105,6 @@ pub fn Element(comptime T: type) type {
11071105
}
11081106
}
11091107

1110-
/// Draw the contents of a panel.
1111-
inline fn draw_panel(
1112-
element: *Self,
1113-
display: *Display(T),
1114-
_: Vector,
1115-
parent_clip: ?Clip,
1116-
scroll_offset: Vector,
1117-
) void {
1118-
if (parent_clip) |clip| {
1119-
for (element.type.panel.children.items) |child| {
1120-
child.draw(display, scroll_offset, clip);
1121-
}
1122-
} else if (element.type.panel.scrollable.scroll.x or element.type.panel.scrollable.scroll.y) {
1123-
for (element.type.panel.children.items) |child| {
1124-
child.draw(display, scroll_offset, Clip{
1125-
.top = element.rect.y,
1126-
.left = element.rect.x,
1127-
.bottom = element.rect.y + element.rect.height,
1128-
.right = element.rect.x + element.rect.width,
1129-
});
1130-
}
1131-
} else {
1132-
for (element.type.panel.children.items) |child| {
1133-
child.draw(display, scroll_offset, null);
1134-
}
1135-
}
1136-
}
1137-
1138-
/// Draw a basic rectangle.
1139-
inline fn draw_rectangle_element(
1140-
element: *Self,
1141-
display: *Display(T),
1142-
_: Vector,
1143-
_: ?Clip,
1144-
scroll_offset: Vector,
1145-
) void {
1146-
const colour = element.style.panel(display.theme, element.background.colour);
1147-
_ = sdl.SDL_SetRenderDrawColor(
1148-
display.renderer,
1149-
colour.r,
1150-
colour.g,
1151-
colour.b,
1152-
colour.a,
1153-
);
1154-
var dest = element.rect.move(&scroll_offset);
1155-
_ = sdl.SDL_RenderFillRect(display.renderer, @ptrCast(&dest));
1156-
}
1157-
11581108
/// Calculate the layout of all elements, and optionally render every element.
11591109
///
11601110
/// Normally text is converted to an image and rendered left to right, starting

src/expander.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ pub fn Expander(comptime T: type) type {
33
pub const Self = @This();
44
weight: f32 = 0,
55

6-
pub fn draw(_: *const Self) void {
6+
pub inline fn draw(
7+
_: *const Self,
8+
_: *Element(T),
9+
_: *Display(T),
10+
_: Vector,
11+
_: ?Clip, //parent_clip
12+
_: Vector, // scroll_offset
13+
) void {
714
if (T.normal.pixel_height(1) == 0) {
815
//
916
}
@@ -22,6 +29,7 @@ const warn = engine.warn;
2229
const info = engine.info;
2330
const debug = engine.debug;
2431
const trace = engine.trace;
32+
const Clip = engine.Clip;
2533
const Display = engine.Display;
2634
const Element = engine.Element;
2735
const Scroller = engine.Scroller;

src/panel.zig

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ pub fn Panel(comptime T: type) type {
1111
.size = .{ .width = 0, .height = 0 },
1212
},
1313
overflow: Vector = .{ .x = 0, .y = 0 },
14+
15+
/// Draw the contents of a panel.
16+
pub inline fn draw(
17+
self: *const Self,
18+
element: *Element(T),
19+
display: *Display(T),
20+
_: Vector, // parent_scroll_offset
21+
parent_clip: ?Clip,
22+
scroll_offset: Vector,
23+
) void {
24+
if (parent_clip) |clip| {
25+
for (self.children.items) |child| {
26+
child.draw(display, scroll_offset, clip);
27+
}
28+
} else if (self.scrollable.scroll.x or self.scrollable.scroll.y) {
29+
for (self.children.items) |child| {
30+
child.draw(display, scroll_offset, Clip{
31+
.top = element.rect.y,
32+
.left = element.rect.x,
33+
.bottom = element.rect.y + element.rect.height,
34+
.right = element.rect.x + element.rect.width,
35+
});
36+
}
37+
} else {
38+
for (self.children.items) |child| {
39+
child.draw(display, scroll_offset, null);
40+
}
41+
}
42+
}
1443
};
1544
}
1645

@@ -23,14 +52,15 @@ const warn = engine.warn;
2352
const info = engine.info;
2453
const debug = engine.debug;
2554
const trace = engine.trace;
55+
const Clip = engine.Clip;
2656
const Display = engine.Display;
2757
const Element = engine.Element;
58+
const Error = engine.Error;
59+
const Font = engine.Font;
60+
const LayoutDirection = engine.LayoutDirection;
2861
const Scroller = engine.Scroller;
2962
const Size = engine.Size;
3063
const Vector = engine.Vector;
3164
const BoolCallback = engine.BoolCallback;
3265
const Callback = engine.Callback;
3366
const UpdateCallback = engine.UpdateCallback;
34-
const LayoutDirection = engine.LayoutDirection;
35-
const Error = engine.Error;
36-
const Font = engine.Font;

src/rectangle.zig

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@ pub fn Rectangle(comptime T: type) type {
22
return struct {
33
pub const Self = @This();
44

5-
pub fn draw() void {
6-
if (T.normal.pixel_height(1) == 0) {
7-
//
8-
}
5+
/// Draw a basic rectangle.
6+
pub inline fn draw(
7+
_: *const Self,
8+
element: *Element(T),
9+
display: *Display(T),
10+
_: Vector,
11+
_: ?Clip,
12+
scroll_offset: Vector,
13+
) void {
14+
const colour = element.style.panel(display.theme, element.background.colour);
15+
_ = sdl.SDL_SetRenderDrawColor(
16+
display.renderer,
17+
colour.r,
18+
colour.g,
19+
colour.b,
20+
colour.a,
21+
);
22+
var dest = element.rect.move(&scroll_offset);
23+
_ = sdl.SDL_RenderFillRect(display.renderer, @ptrCast(&dest));
924
}
1025
};
1126
}
@@ -21,17 +36,18 @@ const warn = engine.warn;
2136
const info = engine.info;
2237
const debug = engine.debug;
2338
const trace = engine.trace;
39+
const Clip = engine.Clip;
2440
const Display = engine.Display;
2541
const Element = engine.Element;
26-
const Scroller = engine.Scroller;
27-
const Vector = engine.Vector;
28-
const Callback = engine.Callback;
29-
const BoolCallback = engine.BoolCallback;
30-
const UpdateCallback = engine.UpdateCallback;
31-
const LayoutDirection = engine.LayoutDirection;
3242
const Error = engine.Error;
33-
const Font = engine.Font;
3443
const Fit = engine.Fit;
44+
const Font = engine.Font;
45+
const LayoutDirection = engine.LayoutDirection;
3546
const Size = engine.Size;
47+
const Scroller = engine.Scroller;
3648
const Texture = engine.Texture;
3749
const ToggleState = engine.ToggleState;
50+
const Vector = engine.Vector;
51+
const Callback = engine.Callback;
52+
const BoolCallback = engine.BoolCallback;
53+
const UpdateCallback = engine.UpdateCallback;

src/text_input.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub fn TextInput(comptime T: type) type {
2424
element: *Element(T),
2525
display: *Display(T),
2626
_: Vector,
27-
_: ?Clip,
28-
_: Vector,
27+
_: ?Clip, // parent_clip
28+
_: Vector, // scroll offset
2929
) void {
3030
var x = element.rect.x + element.pad.left;
3131
const y = element.rect.y + element.pad.top;

0 commit comments

Comments
 (0)