From 0d064fc9dade6f1ec7f4ea022ff6e7d70cc8b46f Mon Sep 17 00:00:00 2001 From: IntegratedQuantum Date: Sun, 12 Mar 2023 17:11:00 +0100 Subject: [PATCH] Dynamic gui scale and some refactoring and fixes. --- src/gui/GuiWindow.zig | 132 +++++++++++++------------------ src/gui/components/Button.zig | 2 +- src/gui/components/CheckBox.zig | 2 +- src/gui/components/ScrollBar.zig | 2 - src/gui/components/TextInput.zig | 16 +++- src/gui/gui.zig | 42 ++++++---- src/json.zig | 7 ++ src/main.zig | 4 +- src/settings.zig | 2 +- 9 files changed, 110 insertions(+), 99 deletions(-) diff --git a/src/gui/GuiWindow.zig b/src/gui/GuiWindow.zig index 35f894ae..058a44bb 100644 --- a/src/gui/GuiWindow.zig +++ b/src/gui/GuiWindow.zig @@ -46,6 +46,8 @@ const RelativePosition = union(enum) { }, }; +const snapDistance = 3; + pos: Vec2f = undefined, size: Vec2f = undefined, contentSize: Vec2f, @@ -111,35 +113,28 @@ pub fn __deinit() void { pub fn defaultFunction() void {} pub fn defaultErrorFunction() Allocator.Error!void {} -pub fn mainButtonPressed(self: *const GuiWindow) void { - const scale = @floor(settings.guiScale*self.scale); // TODO - var mousePosition = main.Window.getMousePosition(); - mousePosition -= self.pos; - mousePosition /= @splat(2, scale); - if(mousePosition[1] < 16) { - grabPosition = main.Window.getMousePosition(); +pub fn mainButtonPressed(self: *const GuiWindow, mousePosition: Vec2f) void { + const scaledMousePos = (mousePosition - self.pos)/@splat(2, self.scale); + if(scaledMousePos[1] < 16) { + grabPosition = mousePosition; selfPositionWhenGrabbed = self.pos; } else { var selectedComponent: ?*GuiComponent = null; for(self.components) |*component| { - if(GuiComponent.contains(component.pos, component.size, mousePosition)) { + if(GuiComponent.contains(component.pos, component.size, scaledMousePos)) { selectedComponent = component; } } if(selectedComponent) |component| { - component.mainButtonPressed(mousePosition); + component.mainButtonPressed(scaledMousePos); } } } -pub fn mainButtonReleased(self: *const GuiWindow) void { +pub fn mainButtonReleased(self: *const GuiWindow, mousePosition: Vec2f) void { grabPosition = null; - const scale = @floor(settings.guiScale*self.scale); // TODO - var mousePosition = main.Window.getMousePosition(); - mousePosition -= self.pos; - mousePosition /= @splat(2, scale); for(self.components) |*component| { - component.mainButtonReleased(mousePosition); + component.mainButtonReleased((mousePosition - self.pos)/@splat(2, self.scale)); } } @@ -168,27 +163,26 @@ fn detectCycles(self: *GuiWindow, other: *GuiWindow) bool { } fn snapToOtherWindow(self: *GuiWindow) void { - const scale = @floor(settings.guiScale*self.scale); // TODO for(&self.relativePosition, 0..) |*relPos, i| { - var minDist: f32 = settings.guiScale*2; + var minDist: f32 = snapDistance; var minWindow: ?*GuiWindow = null; var selfAttachment: AttachmentPoint = undefined; var otherAttachment: AttachmentPoint = undefined; for(gui.openWindows.items) |other| { // Check if they touch: const start = @max(self.pos[i^1], other.pos[i^1]); - const end = @min(self.pos[i^1] + self.size[i^1]*scale, other.pos[i^1] + other.size[i^1]*@floor(settings.guiScale*other.scale)); + const end = @min(self.pos[i^1] + self.size[i^1], other.pos[i^1] + other.size[i^1]); if(start >= end) continue; if(detectCycles(self, other)) continue; - const dist1 = @fabs(self.pos[i] - other.pos[i] - other.size[i]*@floor(settings.guiScale*other.scale)); // TODO: scale + const dist1 = @fabs(self.pos[i] - other.pos[i] - other.size[i]); if(dist1 < minDist) { minDist = dist1; minWindow = other; selfAttachment = .lower; otherAttachment = .upper; } - const dist2 = @fabs(self.pos[i] + self.size[i]*scale - other.pos[i]); + const dist2 = @fabs(self.pos[i] + self.size[i] - other.pos[i]); if(dist2 < minDist) { minDist = dist2; minWindow = other; @@ -203,30 +197,29 @@ fn snapToOtherWindow(self: *GuiWindow) void { } fn positionRelativeToFrame(self: *GuiWindow) void { - const scale = @floor(settings.guiScale*self.scale); // TODO - const windowSize = main.Window.getWindowSize(); + const windowSize = main.Window.getWindowSize()/@splat(2, gui.scale); for(&self.relativePosition, 0..) |*relPos, i| { // Snap to the center: - if(@fabs(self.pos[i] + self.size[i]*scale - windowSize[i]/2) <= settings.guiScale*2) { + if(@fabs(self.pos[i] + self.size[i] - windowSize[i]/2) <= snapDistance) { relPos.* = .{.attachedToFrame = .{ .selfAttachmentPoint = .upper, .otherAttachmentPoint = .middle, }}; - } else if(@fabs(self.pos[i] + self.size[i]*scale/2 - windowSize[i]/2) <= settings.guiScale*2) { + } else if(@fabs(self.pos[i] + self.size[i]/2 - windowSize[i]/2) <= snapDistance) { relPos.* = .{.attachedToFrame = .{ .selfAttachmentPoint = .middle, .otherAttachmentPoint = .middle, }}; - } else if(@fabs(self.pos[i] - windowSize[i]/2) <= settings.guiScale*2) { + } else if(@fabs(self.pos[i] - windowSize[i]/2) <= snapDistance) { relPos.* = .{.attachedToFrame = .{ .selfAttachmentPoint = .lower, .otherAttachmentPoint = .middle, }}; } else { - var ratio: f32 = (self.pos[i] + self.size[i]*scale/2)/windowSize[i]; + var ratio: f32 = (self.pos[i] + self.size[i]/2)/windowSize[i]; if(self.pos[i] <= 0) { ratio = 0; - } else if(self.pos[i] + self.size[i]*scale >= windowSize[i]) { + } else if(self.pos[i] + self.size[i] >= windowSize[i]) { ratio = 1; } relPos.* = .{.ratio = ratio}; @@ -235,36 +228,35 @@ fn positionRelativeToFrame(self: *GuiWindow) void { } fn positionRelativeToConnectedWindow(self: *GuiWindow, other: *GuiWindow, i: usize) void { - const scale = @floor(settings.guiScale*self.scale); // TODO - const otherSize = other.size*@splat(2, @floor(settings.guiScale*other.scale)); // TODO: scale + const otherSize = other.size; const relPos = &self.relativePosition[i]; // Snap to the center: - if(@fabs(self.pos[i] + self.size[i]*scale - (other.pos[i] + otherSize[i]/2)) <= settings.guiScale*2) { + if(@fabs(self.pos[i] + self.size[i] - (other.pos[i] + otherSize[i]/2)) <= snapDistance) { relPos.* = .{.attachedToWindow = .{ .reference = other, .selfAttachmentPoint = .upper, .otherAttachmentPoint = .middle, }}; - } else if(@fabs(self.pos[i] + self.size[i]*scale/2 - (other.pos[i] + otherSize[i]/2)) <= settings.guiScale*2) { + } else if(@fabs(self.pos[i] + self.size[i]/2 - (other.pos[i] + otherSize[i]/2)) <= snapDistance) { relPos.* = .{.attachedToWindow = .{ .reference = other, .selfAttachmentPoint = .middle, .otherAttachmentPoint = .middle, }}; - } else if(@fabs(self.pos[i] - (other.pos[i] + otherSize[i]/2)) <= settings.guiScale*2) { + } else if(@fabs(self.pos[i] - (other.pos[i] + otherSize[i]/2)) <= snapDistance) { relPos.* = .{.attachedToWindow = .{ .reference = other, .selfAttachmentPoint = .lower, .otherAttachmentPoint = .middle, }}; // Snap to the edges: - } else if(@fabs(self.pos[i] - other.pos[i]) <= settings.guiScale*2) { + } else if(@fabs(self.pos[i] - other.pos[i]) <= snapDistance) { relPos.* = .{.attachedToWindow = .{ .reference = other, .selfAttachmentPoint = .lower, .otherAttachmentPoint = .lower, }}; - } else if(@fabs(self.pos[i] + self.size[i]*scale - (other.pos[i] + otherSize[i])) <= settings.guiScale*2) { + } else if(@fabs(self.pos[i] + self.size[i] - (other.pos[i] + otherSize[i])) <= snapDistance) { relPos.* = .{.attachedToWindow = .{ .reference = other, .selfAttachmentPoint = .upper, @@ -273,16 +265,14 @@ fn positionRelativeToConnectedWindow(self: *GuiWindow, other: *GuiWindow, i: usi } else { self.relativePosition[i] = .{.relativeToWindow = .{ .reference = other, - .ratio = (self.pos[i] + self.size[i]*scale/2 - other.pos[i])/otherSize[i] + .ratio = (self.pos[i] + self.size[i]/2 - other.pos[i])/otherSize[i] }}; } } -pub fn updateSelected(self: *GuiWindow) !void { +pub fn updateSelected(self: *GuiWindow, mousePosition: Vec2f) !void { try self.updateSelectedFn(); - const scale = @floor(settings.guiScale*self.scale); // TODO - const mousePosition = main.Window.getMousePosition(); - const windowSize = main.Window.getWindowSize(); + const windowSize = main.Window.getWindowSize()/@splat(2, gui.scale); if(grabPosition) |_grabPosition| { self.relativePosition[0] = .{.ratio = undefined}; self.relativePosition[1] = .{.ratio = undefined}; @@ -296,7 +286,7 @@ pub fn updateSelected(self: *GuiWindow) !void { self.positionRelativeToConnectedWindow(self.relativePosition[0].attachedToWindow.reference, 1); } self.pos = @max(self.pos, Vec2f{0, 0}); - self.pos = @min(self.pos, windowSize - self.size*@splat(2, scale)); + self.pos = @min(self.pos, windowSize - self.size); gui.updateWindowPositions(); } for(self.components) |*component| { @@ -304,33 +294,28 @@ pub fn updateSelected(self: *GuiWindow) !void { } } -pub fn updateHovered(self: *GuiWindow) !void { +pub fn updateHovered(self: *GuiWindow, mousePosition: Vec2f) !void { try self.updateHoveredFn(); - const scale = @floor(settings.guiScale*self.scale); // TODO - var mousePosition = main.Window.getMousePosition(); - mousePosition -= self.pos; - mousePosition /= @splat(2, scale); var i: usize = self.components.len; while(i != 0) { i -= 1; const component = &self.components[i]; - if(GuiComponent.contains(component.pos, component.size, mousePosition)) { - component.updateHovered(mousePosition); + if(GuiComponent.contains(component.pos, component.size, (mousePosition - self.pos)/@splat(2, self.scale))) { + component.updateHovered((mousePosition - self.pos)/@splat(2, self.scale)); break; } } } pub fn updateWindowPosition(self: *GuiWindow) void { - self.size = self.contentSize; // TODO - const scale = @floor(settings.guiScale*self.scale); // TODO - const windowSize = main.Window.getWindowSize(); + self.size = self.contentSize*@splat(2, self.scale); + const windowSize = main.Window.getWindowSize()/@splat(2, gui.scale); for(self.relativePosition, 0..) |relPos, i| { switch(relPos) { .ratio => |ratio| { - self.pos[i] = windowSize[i]*ratio - self.size[i]*scale/2; + self.pos[i] = windowSize[i]*ratio - self.size[i]/2; self.pos[i] = @max(self.pos[i], 0); - self.pos[i] = @min(self.pos[i], windowSize[i] - self.size[i]*scale); + self.pos[i] = @min(self.pos[i], windowSize[i] - self.size[i]); }, .attachedToFrame => |attachedToFrame| { const otherPos = switch(attachedToFrame.otherAttachmentPoint) { @@ -340,37 +325,36 @@ pub fn updateWindowPosition(self: *GuiWindow) void { }; self.pos[i] = switch(attachedToFrame.selfAttachmentPoint) { .lower => otherPos, - .middle => otherPos - 0.5*self.size[i]*scale, - .upper => otherPos - self.size[i]*scale, + .middle => otherPos - 0.5*self.size[i], + .upper => otherPos - self.size[i], }; }, .attachedToWindow => |attachedToWindow| { const other = attachedToWindow.reference; const otherPos = switch(attachedToWindow.otherAttachmentPoint) { .lower => other.pos[i], - .middle => other.pos[i] + 0.5*other.size[i]*@floor(settings.guiScale*other.scale), // TODO: scale - .upper => other.pos[i] + other.size[i]*@floor(settings.guiScale*other.scale), // TODO: scale + .middle => other.pos[i] + 0.5*other.size[i], + .upper => other.pos[i] + other.size[i], }; self.pos[i] = switch(attachedToWindow.selfAttachmentPoint) { .lower => otherPos, - .middle => otherPos - 0.5*self.size[i]*scale, - .upper => otherPos - self.size[i]*scale, + .middle => otherPos - 0.5*self.size[i], + .upper => otherPos - self.size[i], }; }, .relativeToWindow => |relativeToWindow| { const other = relativeToWindow.reference; - const otherSize = other.size[i]*@floor(settings.guiScale*other.scale); // TODO: scale + const otherSize = other.size[i]; const otherPos = other.pos[i]; - self.pos[i] = otherPos + relativeToWindow.ratio*otherSize - self.size[i]*scale/2; + self.pos[i] = otherPos + relativeToWindow.ratio*otherSize - self.size[i]/2; }, } } } fn drawOrientationLines(self: *const GuiWindow) void { - const scale = @floor(settings.guiScale*self.scale); // TODO draw.setColor(0x80000000); - const windowSize = main.Window.getWindowSize(); + const windowSize = main.Window.getWindowSize()/@splat(2, gui.scale); for(self.relativePosition, 0..) |relPos, i| { switch(relPos) { .ratio, .relativeToWindow => { @@ -390,14 +374,14 @@ fn drawOrientationLines(self: *const GuiWindow) void { }, .attachedToWindow => |attachedToWindow| { const other = attachedToWindow.reference; - const otherSize = other.size*@splat(2, @floor(settings.guiScale*other.scale)); // TODO: scale + const otherSize = other.size; const pos = switch(attachedToWindow.otherAttachmentPoint) { .lower => other.pos[i], .middle => other.pos[i] + 0.5*otherSize[i], .upper => other.pos[i] + otherSize[i], }; const start = @min(self.pos[i^1], other.pos[i^1]); - const end = @max(self.pos[i^1] + self.size[i^1]*scale, other.pos[i^1] + otherSize[i^1]); + const end = @max(self.pos[i^1] + self.size[i^1], other.pos[i^1] + otherSize[i^1]); if(i == 0) { draw.line(.{pos, start}, .{pos, end}); } else { @@ -408,23 +392,19 @@ fn drawOrientationLines(self: *const GuiWindow) void { } } -pub fn render(self: *const GuiWindow) !void { - const scale = @floor(settings.guiScale*self.scale); // TODO +pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void { draw.setColor(0xff808080); - draw.rect(self.pos, self.size*@splat(2, scale)); - var mousePosition = main.Window.getMousePosition(); - mousePosition -= self.pos; - mousePosition /= @splat(2, scale); + draw.rect(self.pos, self.size); const oldTranslation = draw.setTranslation(self.pos); - const oldScale = draw.setScale(scale); + 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); + draw.customShadedRect(windowUniforms, .{0, 0}, self.size/@splat(2, self.scale)); try self.renderFn(); for(self.components) |*component| { - try component.render(mousePosition); + try component.render((mousePosition - self.pos)/@splat(2, self.scale)); } if(self.showTitleBar) { graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0); @@ -435,15 +415,15 @@ pub fn render(self: *const GuiWindow) !void { } else { draw.setColor(0xff000000); } - draw.customShadedRect(windowUniforms, .{0, 0}, .{self.size[0], 16}); + draw.customShadedRect(windowUniforms, .{0, 0}, .{self.size[0]/self.scale, 16}); } draw.restoreTranslation(oldTranslation); draw.restoreScale(oldScale); if(self.showTitleBar) { var text = try graphics.TextBuffer.init(gui.allocator, self.title, .{}, false, .center); defer text.deinit(); - const titleDimension = try text.calculateLineBreaks(16*scale, self.size[0]*scale); - try text.render(self.pos[0] + self.size[0]*scale/2 - titleDimension[0]/2, self.pos[1], 16*scale); + const titleDimension = try text.calculateLineBreaks(16*self.scale, self.size[0]); + try text.render(self.pos[0] + self.size[0]/2 - titleDimension[0]/2, self.pos[1], 16*self.scale); } if(self == gui.selectedWindow and grabPosition != null) { self.drawOrientationLines(); diff --git a/src/gui/components/Button.zig b/src/gui/components/Button.zig index d074df48..4fae2dff 100644 --- a/src/gui/components/Button.zig +++ b/src/gui/components/Button.zig @@ -104,8 +104,8 @@ pub fn render(self: *Button, pos: Vec2f, size: Vec2f, mousePosition: Vec2f) !voi draw.setColor(0xff000040); } else { draw.setColor(0xff000000); - self.hovered = false; } + self.hovered = false; draw.customShadedRect(buttonUniforms, pos, size); graphics.c.glUniform1i(buttonUniforms.pressed, 0); const textPos = pos + size/@splat(2, @as(f32, 2.0)) - self.textSize/@splat(2, @as(f32, 2.0)); diff --git a/src/gui/components/CheckBox.zig b/src/gui/components/CheckBox.zig index 444f5eef..8e0f0ceb 100644 --- a/src/gui/components/CheckBox.zig +++ b/src/gui/components/CheckBox.zig @@ -102,9 +102,9 @@ pub fn render(self: *CheckBox, pos: Vec2f, size: Vec2f, mousePosition: Vec2f) !v } else if(GuiComponent.contains(pos, size, mousePosition) and self.hovered) { draw.setColor(0xff000040); } else { - self.hovered = false; draw.setColor(0xff000000); } + self.hovered = false; draw.customShadedRect(Button.buttonUniforms, pos + Vec2f{0, size[1]/2 - boxSize/2}, @splat(2, boxSize)); graphics.c.glUniform1i(Button.buttonUniforms.pressed, 0); const textPos = pos + Vec2f{boxSize/2, 0} + size/@splat(2, @as(f32, 2.0)) - self.textSize/@splat(2, @as(f32, 2.0)); diff --git a/src/gui/components/ScrollBar.zig b/src/gui/components/ScrollBar.zig index c87c7dde..fe45436f 100644 --- a/src/gui/components/ScrollBar.zig +++ b/src/gui/components/ScrollBar.zig @@ -23,8 +23,6 @@ const fontSize: f32 = 16; var texture: Texture = undefined; -// TODO: Scroll wheel support. - currentState: f32, button: Button, buttonSize: Vec2f, diff --git a/src/gui/components/TextInput.zig b/src/gui/components/TextInput.zig index ed4cbb59..2ae0d501 100644 --- a/src/gui/components/TextInput.zig +++ b/src/gui/components/TextInput.zig @@ -92,13 +92,23 @@ pub fn mainButtonPressed(self: *TextInput, pos: Vec2f, size: Vec2f, mousePositio } } self.cursor = null; - self.selectionStart = self.textBuffer.mousePosToIndex(mousePosition - pos, self.currentString.items.len); + var textPos = Vec2f{border, border}; + if(self.textSize[1] > self.maxHeight - 2*border) { + const diff = self.textSize[1] - (self.maxHeight - 2*border); + textPos[1] -= diff*self.scrollBar.currentState; + } + self.selectionStart = self.textBuffer.mousePosToIndex(mousePosition - textPos - pos, self.currentString.items.len); self.pressed = true; } pub fn mainButtonReleased(self: *TextInput, pos: Vec2f, size: Vec2f, mousePosition: Vec2f) void { if(self.pressed) { - self.cursor = self.textBuffer.mousePosToIndex(mousePosition - pos, self.currentString.items.len); + var textPos = Vec2f{border, border}; + if(self.textSize[1] > self.maxHeight - 2*border) { + const diff = self.textSize[1] - (self.maxHeight - 2*border); + textPos[1] -= diff*self.scrollBar.currentState; + } + self.cursor = self.textBuffer.mousePosToIndex(mousePosition - textPos - pos, self.currentString.items.len); if(self.cursor == self.selectionStart) { self.selectionStart = null; } @@ -448,7 +458,7 @@ pub fn render(self: *TextInput, pos: Vec2f, size: Vec2f, mousePosition: Vec2f) ! } try self.textBuffer.render(textPos[0], textPos[1], fontSize); if(self.pressed) { - self.cursor = self.textBuffer.mousePosToIndex(mousePosition - pos, self.currentString.items.len); + self.cursor = self.textBuffer.mousePosToIndex(mousePosition - textPos - pos, self.currentString.items.len); } if(self.cursor) |cursor| { var cursorPos = textPos + self.textBuffer.indexToCursorPos(cursor); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 2732f8eb..9de34977 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -26,6 +26,8 @@ pub var selectedTextInput: ?*TextInput = null; pub var allocator: Allocator = undefined; +pub var scale: f32 = undefined; + pub fn init(_allocator: Allocator) !void { allocator = _allocator; windowList = std.ArrayList(*GuiWindow).init(allocator); @@ -57,6 +59,19 @@ pub fn deinit() void { TextInput.__deinit(); } +pub fn updateGuiScale() void { + if(settings.guiScale) |guiScale| { + scale = guiScale; + } else { + const windowSize = main.Window.getWindowSize(); + const screenWidth = @min(windowSize[0], windowSize[1]*16/9); + scale = @floor(screenWidth/640.0 + 0.2); + if(scale < 1) { + scale = 0.5; + } + } +} + pub fn addWindow(window: *GuiWindow, isHudWindow: bool) !void { for(windowList.items) |other| { if(std.mem.eql(u8, window.id, other.id)) { @@ -87,8 +102,6 @@ pub fn openWindow(id: []const u8) Allocator.Error!void { } window.showTitleBar = true; try openWindows.append(window); - window.pos = .{0, 0}; - window.size = window.contentSize; try window.onOpenFn(); selectedWindow = null; return; @@ -198,16 +211,16 @@ pub fn mainButtonPressed() void { selectedTextInput = null; var selectedI: usize = 0; for(openWindows.items, 0..) |window, i| { - var mousePosition = main.Window.getMousePosition(); + var mousePosition = main.Window.getMousePosition()/@splat(2, scale); mousePosition -= window.pos; - mousePosition /= @splat(2, window.scale*settings.guiScale); if(@reduce(.And, mousePosition >= Vec2f{0, 0}) and @reduce(.And, mousePosition < window.size)) { selectedWindow = window; selectedI = i; } } if(selectedWindow) |_selectedWindow| { - _selectedWindow.mainButtonPressed(); + const mousePosition = main.Window.getMousePosition()/@splat(2, scale); + _selectedWindow.mainButtonPressed(mousePosition); _ = openWindows.orderedRemove(selectedI); openWindows.appendAssumeCapacity(_selectedWindow); } @@ -217,9 +230,8 @@ pub fn mainButtonReleased() void { var oldWindow = selectedWindow; selectedWindow = null; for(openWindows.items) |window| { - var mousePosition = main.Window.getMousePosition(); + var mousePosition = main.Window.getMousePosition()/@splat(2, scale); mousePosition -= window.pos; - mousePosition /= @splat(2, window.scale*settings.guiScale); if(@reduce(.And, mousePosition >= Vec2f{0, 0}) and @reduce(.And, mousePosition < window.size)) { selectedWindow = window; } @@ -228,7 +240,8 @@ pub fn mainButtonReleased() void { selectedWindow = null; } if(oldWindow) |_oldWindow| { - _oldWindow.mainButtonReleased(); + const mousePosition = main.Window.getMousePosition()/@splat(2, scale); + _oldWindow.mainButtonReleased(mousePosition); } } @@ -246,21 +259,22 @@ pub fn updateWindowPositions() void { } pub fn updateAndRenderGui() !void { + const mousePos = main.Window.getMousePosition()/@splat(2, scale); if(selectedWindow) |selected| { - try selected.updateSelected(); + try selected.updateSelected(mousePos); } - const mousePos = main.Window.getMousePosition(); var i: usize = openWindows.items.len; while(i != 0) { i -= 1; const window: *GuiWindow = openWindows.items[i]; - const scale = @floor(settings.guiScale*window.scale); // TODO - if(GuiComponent.contains(window.pos, window.size*@splat(2, scale), mousePos)) { - try window.updateHovered(); + if(GuiComponent.contains(window.pos, window.size, mousePos)) { + try window.updateHovered(mousePos); break; } } for(openWindows.items) |window| { - try window.render(); + const oldScale = draw.setScale(scale); + defer draw.restoreScale(oldScale); + try window.render(mousePos); } } \ No newline at end of file diff --git a/src/json.zig b/src/json.zig index fa24d34a..9401d7fe 100644 --- a/src/json.zig +++ b/src/json.zig @@ -151,6 +151,13 @@ pub const JsonElement = union(JsonType) { @compileError("Unknown value type."); } }, + .Optional => { + if(value) |val| { + return createElementFromRandomType(val); + } else { + return JsonElement{.JsonNull={}}; + } + }, else => { @compileError("Unknown value type."); }, diff --git a/src/main.zig b/src/main.zig index 6862452e..e55e824a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -254,6 +254,7 @@ pub const Window = struct { width = @intCast(u31, newWidth); height = @intCast(u31, newHeight); renderer.updateViewport(width, height, settings.fov); + gui.updateGuiScale(); gui.updateWindowPositions(); } // Mouse deltas are averaged over multiple frames using a circular buffer: @@ -472,6 +473,7 @@ pub fn main() !void { c.glCullFace(c.GL_BACK); c.glEnable(c.GL_BLEND); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); + Window.GLFWCallbacks.framebufferSize(undefined, Window.width, Window.height); while(c.glfwWindowShouldClose(Window.window) == 0) { { // Check opengl errors: @@ -538,7 +540,7 @@ pub fn main() !void { c.glCullFace(c.GL_BACK); c.glEnable(c.GL_BLEND); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); - Window.GLFWCallbacks.framebufferSize(null, Window.width, Window.height); + Window.GLFWCallbacks.framebufferSize(undefined, Window.width, Window.height); var lastTime = std.time.milliTimestamp(); var buffer = try graphics.TextBuffer.init(threadAllocator, "Time to wrap some lines! a⃗ a⃗⃗ _a#ff0000⃗#ffff00⃗#00ff00⃗#00ffff⃗_#0000ff⃗#ff00ff⃗#000000 ⌬ __*italic*__ _**bold**_ ___***everything***___ #ff0000red#00ff00green#0000ffblue", .{}, true); defer buffer.deinit(); diff --git a/src/settings.zig b/src/settings.zig index 96b1347d..5f3511d5 100644 --- a/src/settings.zig +++ b/src/settings.zig @@ -33,7 +33,7 @@ pub var playerName: []const u8 = "quanturmdoelvloper"; pub var lastUsedIPAddress: []const u8 = "127.0.0.1"; -pub var guiScale: f32 = 2; +pub var guiScale: ?f32 = null; pub fn init() !void {