Skip to content

Commit

Permalink
Adding defos.is_mouse_cursor_within_window(), some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
subsoap committed Nov 8, 2017
1 parent 5c47cdc commit 89eeb39
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 40 deletions.
7 changes: 7 additions & 0 deletions defos/src/defos.cpp
Expand Up @@ -71,6 +71,12 @@ static int is_maximized(lua_State* L) {
return 1;
}

static int is_mouse_cursor_within_window(lua_State* L) {
bool isWithin = defos_is_mouse_cursor_within_window();
lua_pushboolean(L, isWithin);
return 1;
}


static const luaL_reg Module_methods[] =
{
Expand All @@ -85,6 +91,7 @@ static const luaL_reg Module_methods[] =
{"is_fullscreen", is_fullscreen},
{"toggle_maximize", toggle_maximize},
{"is_maximized", is_maximized},
{"is_mouse_cursor_within_window", is_mouse_cursor_within_window},
{0, 0}
};

Expand Down
5 changes: 5 additions & 0 deletions defos/src/defos.mm
Expand Up @@ -8,6 +8,7 @@
NSWindow* window;

bool is_maximized = false;
bool is_mouse_cursor_within_window = false;
NSRect previous_state;

void init_window(){
Expand Down Expand Up @@ -74,6 +75,10 @@ bool defos_is_maximized() {
return is_maximized;
}

bool defos_is_mouse_cursor_within_window() {
return is_mouse_cursor_within_window;
}

void defos_set_window_size(int x, int y, int w, int h) {
init_window();
//correction for result like on Windows PC
Expand Down
2 changes: 2 additions & 0 deletions defos/src/defos_private.h
Expand Up @@ -10,6 +10,8 @@ extern void defos_enable_mouse_cursor();
extern void defos_toggle_fullscreen();
extern void defos_toggle_maximize();

extern bool defos_is_mouse_cursor_within_window();

extern void defos_set_window_size(int x, int y, int w, int h);
extern void defos_set_window_title(const char* title_lua);

Expand Down
30 changes: 30 additions & 0 deletions defos/src/defos_win.c
Expand Up @@ -22,9 +22,39 @@ bool defos_is_fullscreen() {
}

bool defos_is_maximized() {

return IsZoomed(dmGraphics::GetNativeWindowsHWND());
}

bool defos_is_mouse_cursor_within_window() {
HWND window = dmGraphics::GetNativeWindowsHWND();

POINT ptr;
ClientToScreen(window, &ptr);
int client_x = ptr.x;
int client_y = ptr.y;

POINT pos;
GetCursorPos(&pos);
int cursor_x = pos.x;
int cursor_y = pos.y;

RECT client_rect;
GetClientRect(window, &client_rect);
int client_width = client_rect.right;
int client_height = client_rect.bottom;

cursor_x = cursor_x - client_x;
cursor_y = cursor_y - client_y;

if ((cursor_x >= 0) && (cursor_y >=0) && (cursor_x <= client_width) && (cursor_y <= client_height)) {
return true;
}
else {
return false;
}
}

void defos_disable_maximize_button() {
set_window_style(get_window_style() & ~WS_MAXIMIZEBOX);
}
Expand Down
62 changes: 62 additions & 0 deletions example/example.gui
Expand Up @@ -1842,6 +1842,68 @@ nodes {
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 813.0
y: 29.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 0.5
y: 0.5
z: 0.5
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "is_mouse_cursor_within_window : false"
font: "larryfont"
id: "is_mouse_cursor_within_window"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_LEGACY
max_nodes: 512
15 changes: 14 additions & 1 deletion example/example.gui_script
Expand Up @@ -3,13 +3,25 @@ local dirtylarry = require "dirtylarry/dirtylarry"
function init(self)
self.is_cursor = true
msg.post(".", "acquire_input_focus")

self.mouse_inside_label = gui.get_node("is_mouse_cursor_within_window")
end

local function change_mouse_label(text)
local label = gui.get_node("disable_mouse_cursor/larrylabel")
gui.set_text(label,text)
end

function update(self, dt)
self.mouse_inside = defos.is_mouse_cursor_within_window()
if self.mouse_inside == true then
gui.set_text(self.mouse_inside_label, "MOUSE IS INSIDE")
else
gui.set_text(self.mouse_inside_label, "MOUSE IS OUTSIDE")
end

end

function on_input(self, action_id, action)
self.input_title = dirtylarry:input("window_title", action_id, action, gui.KEYBOARD_TYPE_DEFAULT, "Type text")

Expand Down Expand Up @@ -60,4 +72,5 @@ function on_input(self, action_id, action)

gui.set_text(gui.get_node("is_fullscreen"),"is_fullscreen "..tostring(defos.is_fullscreen()))
gui.set_text(gui.get_node("is_maximized"),"is_maximized "..tostring(defos.is_maximized()))
end
end

39 changes: 0 additions & 39 deletions example/example.script

This file was deleted.

0 comments on commit 89eeb39

Please sign in to comment.