Focus event

This commit is contained in:
2026-01-26 20:42:30 +01:00
parent 11fc1b5b4b
commit 6d35cfe3a2
2 changed files with 98 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import "core:fmt"
Window :: struct { Window :: struct {
title: string, title: string,
width, height: u16, width, height: u16,
focused: bool,
backend: Backend_Info, backend: Backend_Info,
@@ -16,12 +17,17 @@ Event_Queue :: queue.Queue(Event)
Event :: union { Event :: union {
Event_Quit, Event_Quit,
Event_Focus,
Event_Input, Event_Input,
Event_Resize, Event_Resize,
} }
Event_Quit :: struct {} Event_Quit :: struct {}
Event_Focus :: struct {
is_focused: bool,
}
Event_Input_State :: enum { Event_Input_State :: enum {
Up, Up,
Down, Down,
@@ -82,6 +88,7 @@ create :: proc(width, height: u16) -> (^Window, bool) {
} }
set_title(w, "Window") set_title(w, "Window")
// show_cursor(w, true)
return w, true return w, true
} }
@@ -99,6 +106,33 @@ set_title :: proc(window: ^Window, title: string) {
} }
} }
show_cursor :: proc(window: ^Window, show: bool) {
when ODIN_OS == .Windows {
show_cursor_window(window, show)
}
else {
#assert(false, "Missing implementation for 'show_cursor'.")
}
}
cursor_visible :: proc(window: ^Window) -> bool {
when ODIN_OS == .Windows {
return cursor_visible_windows(window)
}
else {
#assert(false, "Missing implementation for 'cursor_visible'.")
}
return false
}
set_cursor_position :: proc(window: ^Window, pos: [2]u32) {
// fmt.printfln("Set cursor pos: %v:%v", pos.x, pos.y)
when ODIN_OS == .Windows {
set_cursor_position_windows(window, pos)
}
}
get_pointer_to_surface :: proc(window: ^Window) -> rawptr { get_pointer_to_surface :: proc(window: ^Window) -> rawptr {
when ODIN_OS == .Windows { when ODIN_OS == .Windows {
return window.backend.hwnd return window.backend.hwnd
@@ -116,7 +150,7 @@ update :: proc(window: ^Window, out_event: ^Event) -> bool {
update_windows(window) update_windows(window)
} }
else { else {
#assert(false, "Missing implementation for 'set_title'.") #assert(false, "Missing implementation for 'update'.")
} }
out_event^ = nil out_event^ = nil
@@ -126,6 +160,18 @@ update :: proc(window: ^Window, out_event: ^Event) -> bool {
return false return false
} }
switch e in event {
case Event_Quit: {}
case Event_Focus: {
window.focused = e.is_focused
}
case Event_Resize: {
window.width = e.new_width
window.height = e.new_height
}
case Event_Input: {}
}
assert(event != nil) assert(event != nil)
out_event^ = event out_event^ = event

View File

@@ -1,3 +1,4 @@
#+private
package window package window
import "core:container/queue" import "core:container/queue"
@@ -36,6 +37,13 @@ Backend_Info :: struct {
event_queue := get_event_queue(hwnd) event_queue := get_event_queue(hwnd)
switch(msg) { switch(msg) {
case win.WM_CREATE: return wm_create(hwnd, lparam) case win.WM_CREATE: return wm_create(hwnd, lparam)
case win.WM_SETFOCUS, win.WM_KILLFOCUS: {
if event_queue != nil {
queue.push_back(event_queue, Event_Focus {
is_focused = msg == win.WM_SETFOCUS
})
}
}
case win.WM_KEYDOWN, win.WM_SYSKEYDOWN: { case win.WM_KEYDOWN, win.WM_SYSKEYDOWN: {
input_event: Event_Input = Event_Keyboard { input_event: Event_Input = Event_Keyboard {
virtual_key = get_virtual_key_windows(i32(wparam)), virtual_key = get_virtual_key_windows(i32(wparam)),
@@ -192,6 +200,48 @@ set_title_windows :: proc(window: ^Window) {
win.SetWindowTextW(window.backend.hwnd, win.utf8_to_wstring(window.title)) win.SetWindowTextW(window.backend.hwnd, win.utf8_to_wstring(window.title))
} }
show_cursor_window :: proc(window: ^Window, show: bool) {
assert(window != nil)
if show == cursor_visible_windows(window) {
if show {
fmt.printfln("Tried to show cursor but it's already visible.")
}
else {
fmt.printfln("Tried to hide cursor but it's already not visible.")
}
return
}
for { // Win32 :)))
counter := win.ShowCursor(show ? win.TRUE : win.FALSE)
if (show && counter >= 0) || (!show && counter < 0) {
break
}
}
win.ShowCursor(show ? win.TRUE : win.FALSE)
}
cursor_visible_windows :: proc(window: ^Window) -> bool {
old_count := win.ShowCursor(win.TRUE)
win.ShowCursor(win.FALSE)
return old_count >= 0
}
set_cursor_position_windows :: proc(window: ^Window, pos: [2]u32) {
pt := win.POINT { x = i32(pos.x), y = i32(pos.y) }
rect: win.RECT
win.GetClientRect(window.backend.hwnd, &rect)
ok := win.ClientToScreen(window.backend.hwnd, &pt)
if !ok {
return
}
win.SetCursorPos(pt.x, pt.y)
}
update_windows :: proc(window: ^Window) { update_windows :: proc(window: ^Window) {
assert(window != nil) assert(window != nil)