Add the crosshair.

This commit is contained in:
IntegratedQuantum 2023-03-18 22:54:05 +01:00
parent 0467e43163
commit ba8856bd35
6 changed files with 85 additions and 13 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 B

View File

@ -53,10 +53,11 @@ contentSize: Vec2f,
scale: f32 = 1,
spacing: f32 = 0,
relativePosition: [2]RelativePosition = .{.{.ratio = 0.5}, .{.ratio = 0.5}},
showTitleBar: bool = true,
title: []const u8 = "",
id: []const u8,
components: []GuiComponent = &.{},
showTitleBar: bool = true,
hasBackground: bool = true,
isHud: bool = false,
/// Called every frame.
@ -422,15 +423,15 @@ fn drawOrientationLines(self: *const GuiWindow) void {
}
pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void {
draw.setColor(0xff808080);
draw.rect(self.pos, self.size);
const oldTranslation = draw.setTranslation(self.pos);
const oldScale = draw.setScale(self.scale);
draw.setColor(0xff000000);
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
shader.bind();
backgroundTexture.bind();
draw.customShadedRect(windowUniforms, .{0, 0}, self.size/@splat(2, self.scale));
if(self.hasBackground) {
draw.setColor(0xff000000);
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
shader.bind();
backgroundTexture.bind();
draw.customShadedRect(windowUniforms, .{0, 0}, self.size/@splat(2, self.scale));
}
try self.renderFn();
for(self.components) |*component| {
try component.render((mousePosition - self.pos)/@splat(2, self.scale));

View File

@ -35,7 +35,13 @@ pub fn init(_allocator: Allocator) !void {
hudWindows = std.ArrayList(*GuiWindow).init(allocator);
openWindows = std.ArrayList(*GuiWindow).init(allocator);
inline for(@typeInfo(windowlist).Struct.decls) |decl| {
try addWindow(&@field(windowlist, decl.name).window);
const windowStruct = @field(windowlist, decl.name);
try addWindow(&windowStruct.window);
inline for(@typeInfo(windowStruct).Struct.decls) |_decl| {
if(comptime std.mem.eql(u8, _decl.name, "init")) {
try windowStruct.init();
}
}
}
try GuiWindow.__init();
try Button.__init();
@ -62,6 +68,14 @@ pub fn deinit() void {
ScrollBar.__deinit();
Slider.__deinit();
TextInput.__deinit();
inline for(@typeInfo(windowlist).Struct.decls) |decl| {
const windowStruct = @field(windowlist, decl.name);
inline for(@typeInfo(windowStruct).Struct.decls) |_decl| {
if(comptime std.mem.eql(u8, _decl.name, "deinit")) {
windowStruct.deinit();
}
}
}
}
fn save() !void {
@ -183,17 +197,14 @@ fn addWindow(window: *GuiWindow) !void {
}
if(window.isHud) {
try hudWindows.append(window);
window.showTitleBar = false;
}
try windowList.append(window);
}
pub fn openWindow(id: []const u8) Allocator.Error!void {
defer updateWindowPositions();
var wasFound: bool = false;
for(windowList.items) |window| {
if(std.mem.eql(u8, window.id, id)) {
wasFound = true;
for(openWindows.items, 0..) |_openWindow, i| {
if(_openWindow == window) {
_ = openWindows.swapRemove(i);
@ -202,7 +213,6 @@ pub fn openWindow(id: []const u8) Allocator.Error!void {
return;
}
}
window.showTitleBar = true;
try openWindows.append(window);
try window.onOpenFn();
selectedWindow = null;
@ -212,6 +222,23 @@ pub fn openWindow(id: []const u8) Allocator.Error!void {
std.log.warn("Could not find window with id {s}.", .{id});
}
pub fn openHud() Allocator.Error!void {
for(windowList.items) |window| {
if(window.isHud) {
for(openWindows.items, 0..) |_openWindow, i| {
if(_openWindow == window) {
_ = openWindows.swapRemove(i);
openWindows.appendAssumeCapacity(window);
selectedWindow = null;
return;
}
}
try openWindows.append(window);
try window.onOpenFn();
}
}
}
pub fn openWindowFunction(comptime id: []const u8) *const fn() void {
const function = struct {
fn function() void {

View File

@ -1,6 +1,7 @@
pub const change_name = @import("change_name.zig");
pub const controls = @import("controls.zig");
pub const crosshair = @import("crosshair.zig");
pub const graphics = @import("graphics.zig");
pub const healthbar = @import("healthbar.zig");
pub const hotbar = @import("hotbar.zig");

View File

@ -0,0 +1,40 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const main = @import("root");
const graphics = main.graphics;
const Shader = graphics.Shader;
const Texture = graphics.Texture;
const Vec2f = main.vec.Vec2f;
const gui = @import("../gui.zig");
const GuiWindow = gui.GuiWindow;
const GuiComponent = gui.GuiComponent;
const size: f32 = 64;
pub var window = GuiWindow {
.contentSize = Vec2f{size, size},
.title = "Crosshair",
.id = "cubyz:crosshair",
.renderFn = &render,
.showTitleBar = false,
.hasBackground = false,
.isHud = true,
};
var texture: Texture = undefined;
pub fn init() !void {
texture = try Texture.initFromFile("assets/cubyz/ui/crosshair.png");
}
pub fn deinit() void {
texture.deinit();
}
pub fn render() Allocator.Error!void {
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
texture.bind();
graphics.draw.setColor(0xffffffff);
graphics.draw.boundImage(.{0, 0}, .{size, size});
}

View File

@ -77,6 +77,9 @@ fn join() void {
for(gui.openWindows.items) |openWindow| {
gui.closeWindow(openWindow);
}
gui.openHud() catch |err| {
std.log.err("Encountered error while opening world: {s}", .{@errorName(err)});
};
}
fn copyIp() void {