mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
Allow closing windows and zooming in/out.
This commit is contained in:
parent
5b3aaa02c8
commit
fbb28869f6
@ -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);
|
||||
|
||||
|
BIN
assets/cubyz/ui/window_close.png
Normal file
BIN
assets/cubyz/ui/window_close.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
BIN
assets/cubyz/ui/window_zoom_in.png
Normal file
BIN
assets/cubyz/ui/window_zoom_in.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
BIN
assets/cubyz/ui/window_zoom_out.png
Normal file
BIN
assets/cubyz/ui/window_zoom_out.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
@ -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 {
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user