mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-04 03:37:59 -04:00
Allow ungrabbing the mouse by pressing escape and add a way to show the title bar of transparent windows.
This commit is contained in:
parent
9c20b463e3
commit
ef0f9ebf67
BIN
assets/cubyz/ui/window_expand.png
Normal file
BIN
assets/cubyz/ui/window_expand.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
@ -57,6 +57,7 @@ title: []const u8 = "",
|
|||||||
id: []const u8,
|
id: []const u8,
|
||||||
components: []GuiComponent = &.{},
|
components: []GuiComponent = &.{},
|
||||||
showTitleBar: bool = true,
|
showTitleBar: bool = true,
|
||||||
|
titleBarExpanded: bool = false,
|
||||||
hasBackground: bool = true,
|
hasBackground: bool = true,
|
||||||
isHud: bool = false,
|
isHud: bool = false,
|
||||||
|
|
||||||
@ -80,6 +81,7 @@ var titleTexture: Texture = undefined;
|
|||||||
var closeTexture: Texture = undefined;
|
var closeTexture: Texture = undefined;
|
||||||
var zoomInTexture: Texture = undefined;
|
var zoomInTexture: Texture = undefined;
|
||||||
var zoomOutTexture: Texture = undefined;
|
var zoomOutTexture: Texture = undefined;
|
||||||
|
var expandTitleBarTexture: Texture = undefined;
|
||||||
var shader: Shader = undefined;
|
var shader: Shader = undefined;
|
||||||
var windowUniforms: struct {
|
var windowUniforms: struct {
|
||||||
screen: c_int,
|
screen: c_int,
|
||||||
@ -102,6 +104,7 @@ pub fn __init() !void {
|
|||||||
closeTexture = try Texture.initFromFile("assets/cubyz/ui/window_close.png");
|
closeTexture = try Texture.initFromFile("assets/cubyz/ui/window_close.png");
|
||||||
zoomInTexture = try Texture.initFromFile("assets/cubyz/ui/window_zoom_in.png");
|
zoomInTexture = try Texture.initFromFile("assets/cubyz/ui/window_zoom_in.png");
|
||||||
zoomOutTexture = try Texture.initFromFile("assets/cubyz/ui/window_zoom_out.png");
|
zoomOutTexture = try Texture.initFromFile("assets/cubyz/ui/window_zoom_out.png");
|
||||||
|
expandTitleBarTexture = try Texture.initFromFile("assets/cubyz/ui/window_expand.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn __deinit() void {
|
pub fn __deinit() void {
|
||||||
@ -115,7 +118,7 @@ pub fn defaultErrorFunction() Allocator.Error!void {}
|
|||||||
|
|
||||||
pub fn mainButtonPressed(self: *const GuiWindow, mousePosition: Vec2f) void {
|
pub fn mainButtonPressed(self: *const GuiWindow, mousePosition: Vec2f) void {
|
||||||
const scaledMousePos = (mousePosition - self.pos)/@splat(2, self.scale);
|
const scaledMousePos = (mousePosition - self.pos)/@splat(2, self.scale);
|
||||||
if(scaledMousePos[1] < 16) {
|
if(scaledMousePos[1] < 16 and (self.showTitleBar or self.titleBarExpanded or (!main.Window.grabbed and scaledMousePos[0] < 16))) {
|
||||||
grabbedWindow = self;
|
grabbedWindow = self;
|
||||||
grabPosition = mousePosition;
|
grabPosition = mousePosition;
|
||||||
selfPositionWhenGrabbed = self.pos;
|
selfPositionWhenGrabbed = self.pos;
|
||||||
@ -133,10 +136,16 @@ pub fn mainButtonPressed(self: *const GuiWindow, mousePosition: Vec2f) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void {
|
pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void {
|
||||||
if(grabPosition != null and grabbedWindow == self) {
|
if(grabPosition != null and @reduce(.And, grabPosition.? == mousePosition) and grabbedWindow == self) {
|
||||||
if(mousePosition[0] - self.pos[0] > self.size[0] - 54*self.scale) {
|
if(!self.showTitleBar) {
|
||||||
if(mousePosition[0] - self.pos[0] > self.size[0] - 36*self.scale) {
|
if(mousePosition[0] - self.pos[0] < 16*self.scale) {
|
||||||
if(mousePosition[0] - self.pos[0] > self.size[0] - 18*self.scale) {
|
self.titleBarExpanded = !self.titleBarExpanded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(self.showTitleBar or self.titleBarExpanded) {
|
||||||
|
if(mousePosition[0] - self.pos[0] > self.size[0] - 48*self.scale) {
|
||||||
|
if(mousePosition[0] - self.pos[0] > self.size[0] - 32*self.scale) {
|
||||||
|
if(mousePosition[0] - self.pos[0] > self.size[0] - 16*self.scale) {
|
||||||
// Close
|
// Close
|
||||||
gui.closeWindow(self);
|
gui.closeWindow(self);
|
||||||
return;
|
return;
|
||||||
@ -161,6 +170,7 @@ pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
grabPosition = null;
|
grabPosition = null;
|
||||||
grabbedWindow = undefined;
|
grabbedWindow = undefined;
|
||||||
for(self.components) |*component| {
|
for(self.components) |*component| {
|
||||||
@ -422,6 +432,13 @@ fn drawOrientationLines(self: *const GuiWindow) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn drawIcons(self: *const GuiWindow) void {
|
||||||
|
draw.setColor(0xffffffff);
|
||||||
|
closeTexture.render(.{self.size[0]/self.scale - 16, 0}, .{16, 16});
|
||||||
|
zoomOutTexture.render(.{self.size[0]/self.scale - 32, 0}, .{16, 16});
|
||||||
|
zoomInTexture.render(.{self.size[0]/self.scale - 48, 0}, .{16, 16});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void {
|
pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void {
|
||||||
const oldTranslation = draw.setTranslation(self.pos);
|
const oldTranslation = draw.setTranslation(self.pos);
|
||||||
const oldScale = draw.setScale(self.scale);
|
const oldScale = draw.setScale(self.scale);
|
||||||
@ -440,16 +457,27 @@ pub fn render(self: *const GuiWindow, mousePosition: Vec2f) !void {
|
|||||||
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
|
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
|
||||||
shader.bind();
|
shader.bind();
|
||||||
titleTexture.bind();
|
titleTexture.bind();
|
||||||
if(self == gui.selectedWindow) {
|
|
||||||
draw.setColor(0xff000040);
|
|
||||||
} else {
|
|
||||||
draw.setColor(0xff000000);
|
draw.setColor(0xff000000);
|
||||||
}
|
|
||||||
draw.customShadedRect(windowUniforms, .{0, 0}, .{self.size[0]/self.scale, 16});
|
draw.customShadedRect(windowUniforms, .{0, 0}, .{self.size[0]/self.scale, 16});
|
||||||
|
self.drawIcons();
|
||||||
|
} else if(!main.Window.grabbed) {
|
||||||
|
if(self.titleBarExpanded) {
|
||||||
|
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
|
||||||
|
shader.bind();
|
||||||
|
titleTexture.bind();
|
||||||
|
draw.setColor(0xff000000);
|
||||||
|
draw.customShadedRect(windowUniforms, .{0, 0}, .{self.size[0]/self.scale, 16});
|
||||||
|
self.drawIcons();
|
||||||
|
expandTitleBarTexture.render(.{0, 0}, .{16, 16});
|
||||||
|
} else {
|
||||||
|
graphics.c.glActiveTexture(graphics.c.GL_TEXTURE0);
|
||||||
|
shader.bind();
|
||||||
|
titleTexture.bind();
|
||||||
|
draw.setColor(0xff000000);
|
||||||
|
draw.customShadedRect(windowUniforms, .{0, 0}, .{16, 16});
|
||||||
draw.setColor(0xffffffff);
|
draw.setColor(0xffffffff);
|
||||||
closeTexture.render(.{self.size[0]/self.scale - 18, 0}, .{16, 16});
|
expandTitleBarTexture.render(.{0, 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.restoreTranslation(oldTranslation);
|
||||||
draw.restoreScale(oldScale);
|
draw.restoreScale(oldScale);
|
||||||
|
@ -158,6 +158,9 @@ pub fn setNextKeypressListener(listener: ?*const fn(c_int, c_int, c_int) void) !
|
|||||||
if(nextKeypressListener != null) return error.AlreadyUsed;
|
if(nextKeypressListener != null) return error.AlreadyUsed;
|
||||||
nextKeypressListener = listener;
|
nextKeypressListener = listener;
|
||||||
}
|
}
|
||||||
|
fn ungrabMouse() void {
|
||||||
|
Window.setMouseGrabbed(false);
|
||||||
|
}
|
||||||
pub var keyboard: struct {
|
pub var keyboard: struct {
|
||||||
// Gameplay:
|
// Gameplay:
|
||||||
forward: Key = Key{.key = c.GLFW_KEY_W},
|
forward: Key = Key{.key = c.GLFW_KEY_W},
|
||||||
@ -170,6 +173,7 @@ pub var keyboard: struct {
|
|||||||
fullscreen: Key = Key{.key = c.GLFW_KEY_F11, .releaseAction = &Window.toggleFullscreen},
|
fullscreen: Key = Key{.key = c.GLFW_KEY_F11, .releaseAction = &Window.toggleFullscreen},
|
||||||
|
|
||||||
// Gui:
|
// Gui:
|
||||||
|
escape: Key = Key{.key = c.GLFW_KEY_ESCAPE, .releaseAction = &ungrabMouse},
|
||||||
mainGuiButton: Key = Key{.mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .pressAction = &gui.mainButtonPressed, .releaseAction = &gui.mainButtonReleased},
|
mainGuiButton: Key = Key{.mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .pressAction = &gui.mainButtonPressed, .releaseAction = &gui.mainButtonReleased},
|
||||||
rightMouseButton: Key = Key{.mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT},
|
rightMouseButton: Key = Key{.mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT},
|
||||||
middleMouseButton: Key = Key{.mouseButton = c.GLFW_MOUSE_BUTTON_MIDDLE},
|
middleMouseButton: Key = Key{.mouseButton = c.GLFW_MOUSE_BUTTON_MIDDLE},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user