diff --git a/assets/cubyz/shaders/graphics/Image.vs b/assets/cubyz/shaders/graphics/Image.vs index 72baf42a8..007571d8c 100644 --- a/assets/cubyz/shaders/graphics/Image.vs +++ b/assets/cubyz/shaders/graphics/Image.vs @@ -15,7 +15,7 @@ uniform int color; void main() { // Convert to opengl coordinates: - vec2 position_percentage = (start + vertex_pos*size)/screen; + vec2 position_percentage = (start + vec2(vertex_pos.x*size.x, size.y - vertex_pos.y*size.y))/screen; vec2 position = vec2(position_percentage.x, -position_percentage.y)*2+vec2(-1, 1); diff --git a/assets/cubyz/ui/window_close.png b/assets/cubyz/ui/window_close.png new file mode 100644 index 000000000..40f51c94b Binary files /dev/null and b/assets/cubyz/ui/window_close.png differ diff --git a/assets/cubyz/ui/window_zoom_in.png b/assets/cubyz/ui/window_zoom_in.png new file mode 100644 index 000000000..d12fdc044 Binary files /dev/null and b/assets/cubyz/ui/window_zoom_in.png differ diff --git a/assets/cubyz/ui/window_zoom_out.png b/assets/cubyz/ui/window_zoom_out.png new file mode 100644 index 000000000..b5c1001f0 Binary files /dev/null and b/assets/cubyz/ui/window_zoom_out.png differ diff --git a/src/graphics.zig b/src/graphics.zig index d9fae37a3..feb7e5c9b 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -322,7 +322,7 @@ pub const draw = struct { var imageShader: Shader = undefined; fn initImage() void { - imageShader = Shader.initAndGetUniforms("assets/cubyz/shaders/graphics/Circle.vs", "assets/cubyz/shaders/graphics/Circle.fs", &imageUniforms) catch Shader{.id = 0}; + imageShader = Shader.initAndGetUniforms("assets/cubyz/shaders/graphics/Image.vs", "assets/cubyz/shaders/graphics/Image.fs", &imageUniforms) catch Shader{.id = 0}; } fn deinitImage() void { @@ -958,12 +958,11 @@ pub const Shader = struct { return err; }; defer main.threadAllocator.free(source); - const ref_buffer = [_] [*c]u8 {@ptrCast([*c]u8, source.ptr)}; const shader = c.glCreateShader(shader_stage); defer c.glDeleteShader(shader); - var sourceLen: c_int = @intCast(c_int, source.len); - c.glShaderSource(shader, 1, @ptrCast([*c]const [*c]const u8, &ref_buffer[0]), &sourceLen); + const sourceLen = @intCast(c_int, source.len); + c.glShaderSource(shader, 1, &source.ptr, &sourceLen); c.glCompileShader(shader); @@ -1395,6 +1394,12 @@ pub const Texture = struct { c.glTexParameteri(c.GL_TEXTURE_2D, c.GL_TEXTURE_WRAP_S, c.GL_REPEAT); c.glTexParameteri(c.GL_TEXTURE_2D, c.GL_TEXTURE_WRAP_T, c.GL_REPEAT); } + + pub fn render(self: Texture, pos: Vec2f, dim: Vec2f) void { + c.glActiveTexture(c.GL_TEXTURE0); + self.bind(); + draw.boundImage(pos, dim); + } }; pub const Color = extern struct { diff --git a/src/gui/GuiWindow.zig b/src/gui/GuiWindow.zig index d2f67a043..f79c9eac0 100644 --- a/src/gui/GuiWindow.zig +++ b/src/gui/GuiWindow.zig @@ -74,6 +74,9 @@ var selfPositionWhenGrabbed: Vec2f = undefined; var backgroundTexture: Texture = undefined; var titleTexture: Texture = undefined; +var closeTexture: Texture = undefined; +var zoomInTexture: Texture = undefined; +var zoomOutTexture: Texture = undefined; var shader: Shader = undefined; var windowUniforms: struct { screen: c_int, @@ -93,6 +96,9 @@ pub fn __init() !void { backgroundTexture = try Texture.initFromFile("assets/cubyz/ui/window_background.png"); titleTexture = try Texture.initFromFile("assets/cubyz/ui/window_title.png"); + closeTexture = try Texture.initFromFile("assets/cubyz/ui/window_close.png"); + zoomInTexture = try Texture.initFromFile("assets/cubyz/ui/window_zoom_in.png"); + zoomOutTexture = try Texture.initFromFile("assets/cubyz/ui/window_zoom_out.png"); } pub fn __deinit() void { @@ -122,7 +128,35 @@ pub fn mainButtonPressed(self: *const GuiWindow, mousePosition: Vec2f) void { } } -pub fn mainButtonReleased(self: *const GuiWindow, mousePosition: Vec2f) void { +pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void { + if(grabPosition != null) { + if(mousePosition[0] - self.pos[0] > self.size[0] - 54*self.scale) { + if(mousePosition[0] - self.pos[0] > self.size[0] - 36*self.scale) { + if(mousePosition[0] - self.pos[0] > self.size[0] - 18*self.scale) { + // Close + gui.closeWindow(self); + return; + } else { + // Zoom out + if(self.scale > 1) { + self.scale -= 0.5; + } else { + self.scale -= 0.25; + } + self.scale = @max(self.scale, 0.25); + gui.updateWindowPositions(); + } + } else { + // Zoom in + if(self.scale >= 1) { + self.scale += 0.5; + } else { + self.scale += 0.25; + } + gui.updateWindowPositions(); + } + } + } grabPosition = null; for(self.components) |*component| { component.mainButtonReleased((mousePosition - self.pos)/@splat(2, self.scale)); @@ -407,6 +441,10 @@ pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void { draw.setColor(0xff000000); } draw.customShadedRect(windowUniforms, .{0, 0}, .{self.size[0]/self.scale, 16}); + draw.setColor(0xffffffff); + closeTexture.render(.{self.size[0]/self.scale - 18, 0}, .{16, 16}); + zoomOutTexture.render(.{self.size[0]/self.scale - 36, 0}, .{16, 16}); + zoomInTexture.render(.{self.size[0]/self.scale - 54, 0}, .{16, 16}); } draw.restoreTranslation(oldTranslation); draw.restoreScale(oldScale);