Skip to content

Instantly share code, notes, and snippets.

@harpwood
Last active February 22, 2022 10:04
Show Gist options
  • Save harpwood/c9fa7f39008644cc5208be243647133b to your computer and use it in GitHub Desktop.
Save harpwood/c9fa7f39008644cc5208be243647133b to your computer and use it in GitHub Desktop.
draw_debug_mouse v0.1.1
///====================================
/// How to use with constructor
///====================================
/// Create event
/// d_mouse = new draw_debug_mouse();
///-----------------------------------
/// Draw event to get room x,y
/// d_mouse.room_xy();
///-----------------------------------
/// Draw GUI event to get GUI x,y
/// d_mouse.gui_xy();
///----------------------------------
/// The settings are not optimal yet, but you can customize via the configuration c struct (find it at line 34)
/// ===============================================================
/// How to use without constructor (and without any customization)
///===============================================================
/// Draw event to get room x,y
/// draw_mouse_xy();
///-------------------------------
/// Draw GUI event to get GUI x,y
/// draw_mouse_xy_gui();
/// ====================================================================
///
/// @Author Written by George Kritikos (Harpwood studio)
///
/// Licensed under MIT license
/// ===================================================================
function draw_debug_mouse() constructor {
pins = [];
pins_gui = [];
//configuration
c = {
//keybinding
enable_key : "E", // enable_key to enable/disable the draw_debug_mouse
pin_key : vk_alt, // pin_key to pin current xy
unpin_key : vk_control,
snap_key : vk_shift, // snap_key to snap to grid
//enable
enable : true,
//grid
grid_width : 16, //grid width
grid_height : 16, //grid height
//lines
horizontal_line_color1 : c_grey, // horizontal line color
horizontal_line_color2 : c_grey, // horizontal line color
vertical_line_color1 : c_grey, // vertical line color
vertical_line_color2 : c_grey, // vertical line color
horizontal_line_thickness : 1, // lines thickness
vertical_line_thickness : 1, // lines thickness
horizontal_line_alpha : 1,
vertical_line_alpha : 1,
// cursor text
cursor_text_color : c_white, // cursor text color
cursor_text_color_outline : c_black, // cursor text color outline
cursor_text_color_outline_thickness : 2, // cursor text color outline thickness
//pinned text
pinned_text_color : c_red, // pinned text color
pinned_text_color_outline : c_black, // pinned text color outline
pinned_text_color_outline_thickness : 2, // pinned text color outline thickness
//cross
horizontal_cross_line_lenght : 20, // horizontal cross line lenght
vertical_cross_line_lenght : 20, // vertical cross line lenght
horizontal_cross_line_color1 : c_red, // horizontal cross line color
horizontal_cross_line_color2 : c_red, // horizontal cross line color
vertical_cross_line_color1 : c_red, // vertical cross line color
vertical_cross_line_color2 : c_red, // vertical cross line color
horizontal_cross_line_thickness : 2, // horizontal lines cross thickness
vertical_cross_line_thickness : 2, // verticallines cross thickness
horizontal_cross_line_alpha : 1,
vertical_cross_line_alpha : 1
}
__dm_init();
press_to_enable =_keybind_event_check_pressed(c.enable_key);
press_to_pin = _keybind_event_check_pressed(c.pin_key);
press_to_unpin = _keybind_event_check_pressed(c.unpin_key);
press_to_snap = _keybind_event_check(c.snap_key);
function room_xy(x_offset = 10, y_offset = 10, scale = 1) {
if press_to_enable(c.enable_key)
c.enable = !c.enable;
if c.enable
{
var w = view_wport[view_current];
var h = view_hport[view_current];
var _mouse_x = mouse_x;
var _mouse_y = mouse_y;
if press_to_snap(c.snap_key)
{
_mouse_x = ceil(_mouse_x / c.grid_width) * c.grid_width;
_mouse_y = ceil(_mouse_y / c.grid_height) * c.grid_height;
//window_mouse_set(_mouse_x, _mouse_y);
}
var _a = draw_get_alpha();
draw_set_alpha(c.horizontal_line_alpha);
draw_line_width_color(0, _mouse_y, w, _mouse_y, c.horizontal_line_thickness, c.horizontal_line_color1, c.horizontal_line_color2);
draw_set_alpha(c.vertical_line_alpha);
draw_line_width_color(_mouse_x, 0, _mouse_x, h, c.vertical_line_thickness, c.vertical_line_color1, c.vertical_line_color2);
draw_set_alpha(_a);
var _str = string(_mouse_x) + "," + string(_mouse_y);
if (device_mouse_x_to_gui(0) + x_offset + string_width(_str) * scale) > display_get_gui_width()
x_offset = -x_offset - string_width(_str) / 2 * scale;
else x_offset = x_offset + string_width(_str) / 2 * scale;
if (device_mouse_y_to_gui(0) + y_offset + string_height(_str) * scale) > display_get_gui_height()
y_offset = -y_offset - string_height(_str) / 2 *scale;
//DDebug("x_offset", 1000, x_offset);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_text_ext_transformed_color_outlined(_mouse_x + x_offset, _mouse_y + y_offset, _str, 10, 10, scale, scale, 0, c.cursor_text_color, c.cursor_text_color_outline, c.cursor_text_color_outline_thickness );
if press_to_pin(c.pin_key) then array_push(pins, [_mouse_x, _mouse_y]);
var r1 = _mouse_x + x_offset - string_width(_str) / 2 * scale;
var r2 = _mouse_y + y_offset;
var r3 = _mouse_x + x_offset + string_width(_str) / 2 * scale;
var r4 = _mouse_y + y_offset + string_height(_str) / 2 * scale;
//draw_rectangle(r1, r2, r3, r4, true);
if array_length(pins) > 0
{
for (var i = 0; i < array_length(pins); i++)
{
var _x = pins[i][0];
var _y = pins[i][1];
draw_debug_cross(_x, _y, c.horizontal_cross_line_lenght, c.vertical_cross_line_lenght, c.horizontal_cross_line_thickness, c.vertical_cross_line_thickness, c.horizontal_cross_line_color1, c.horizontal_cross_line_color2, c.vertical_cross_line_color1, c.vertical_cross_line_color2, c.horizontal_cross_line_alpha, c.vertical_cross_line_alpha);
var _s = "[" + string(_x) + "," + string(_y) + "]";
var x_off = string_width(_s) / 2 * scale;
var y_off = -string_height(_s) / 2 * scale;
if (_x + x_off + string_width(_s) * scale) > view_wport[view_current]
x_off = -x_off;
if (_y + y_off - string_height(_s) * scale) < view_yport[view_current]
y_off = -y_off;
if abs(_mouse_x - _x) < 5 and abs(_mouse_y - _y) < 5
{
var pr1 = _x + x_off - string_width(_s) / 2 * scale;
var pr2 = _y + y_off;
var pr3 = _x + x_off + string_width(_s) / 2 * scale;
var pr4 = _y + y_off + string_height(_s) / 2 * scale;
if rectangle_in_rectangle(r1, r2, r3, r4, pr1, pr2, pr3, pr4)
{
//draw_set_color(c_red);
if (_y - y_off - string_height(_s) * scale) < view_yport[view_current]
y_off = y_off + abs(_y - r4);
if (_y + y_off + string_height(_s) * 2 * scale) > view_hport[view_current]
y_off = y_off - abs(_y - r2) * 1.5;
//DDebug("(_y + y_off + string_height(_s) * scale)", 1000, (_y + y_off + string_height(_s) * scale));
//DDebug("view_hport[view_current]", 1000, view_hport[view_current]);
//DDebug("_y + y_off", 1000, _y + y_off);
}
//draw_rectangle(pr1, pr2, pr3, pr4, true);
//draw_set_color(c_white);
draw_text_ext_transformed_color_outlined(_x + x_off, _y + y_off, _s, 10, 10, scale, scale, 0, c.pinned_text_color, c.pinned_text_color_outline, c.pinned_text_color_outline_thickness);
if press_to_unpin(c.unpin_key)
{
array_delete(pins, i, 1);
break;
}
}
}
}
}
}
function gui_xy(x_offset = 10, y_offset = 10, scale = 1) {
if press_to_enable(c.enable_key)
c.enable = !c.enable;
if c.enable
{
var w = display_get_gui_width();
var h = display_get_gui_height();
var _mouse_x = device_mouse_x_to_gui(0);
var _mouse_y = device_mouse_y_to_gui(0);
if press_to_snap(c.snap_key)
{
_mouse_x = ceil(_mouse_x / c.grid_width) * c.grid_width;
_mouse_y = ceil(_mouse_y / c.grid_height) * c.grid_height;
}
var _a = draw_get_alpha();
draw_set_alpha(c.horizontal_line_alpha);
draw_line_width_color(0, _mouse_y, w, _mouse_y, c.horizontal_line_thickness, c.horizontal_line_color1, c.horizontal_line_color2);
draw_set_alpha(c.vertical_line_alpha);
draw_line_width_color(_mouse_x, 0, _mouse_x, h, c.vertical_line_thickness, c.vertical_line_color1, c.vertical_line_color2);
draw_set_alpha(_a);
var _str = string(_mouse_x) + "," + string(_mouse_y);
if (device_mouse_x_to_gui(0) + x_offset + string_width(_str) * scale) > display_get_gui_width()
x_offset = -x_offset - string_width(_str) / 2 * scale;
else x_offset = x_offset + string_width(_str) / 2 * scale;
if (device_mouse_y_to_gui(0) + y_offset + string_height(_str) * scale) > display_get_gui_height()
y_offset = -y_offset - string_height(_str) / 2 *scale;
//DDebug("x_offset", 1000, x_offset);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_text_ext_transformed_color_outlined(_mouse_x + x_offset, _mouse_y + y_offset, _str, 10, 10, scale, scale, 0, c.cursor_text_color, c.cursor_text_color_outline, c.cursor_text_color_outline_thickness );
if press_to_pin(c.pin_key) then array_push(pins_gui, [_mouse_x, _mouse_y]);
var r1 = _mouse_x + x_offset - string_width(_str) / 2 * scale;
var r2 = _mouse_y + y_offset;
var r3 = _mouse_x + x_offset + string_width(_str) / 2 * scale;
var r4 = _mouse_y + y_offset + string_height(_str) / 2 * scale;
//draw_rectangle(r1, r2, r3, r4, true);
if array_length(pins_gui) > 0
{
for (var i = 0; i < array_length(pins_gui); i++)
{
var _x = pins_gui[i][0];
var _y = pins_gui[i][1];
draw_debug_cross(_x, _y, c.horizontal_cross_line_lenght, c.vertical_cross_line_lenght, c.horizontal_cross_line_thickness, c.vertical_cross_line_thickness, c.horizontal_cross_line_color1, c.horizontal_cross_line_color2, c.vertical_cross_line_color1, c.vertical_cross_line_color2, c.horizontal_cross_line_alpha, c.vertical_cross_line_alpha);
var _s = "[" + string(_x) + "," + string(_y) + "]";
var x_off = string_width(_s) / 2 * scale;
var y_off = -string_height(_s) / 2 * scale;
if (_x + x_off + string_width(_s) * scale) > w //view_wport[view_current]
x_off = -x_off;
if (_y + y_off - string_height(_s) * scale) < 0 //view_yport[view_current]
y_off = -y_off;
if abs(_mouse_x - _x) < 5 and abs(_mouse_y - _y) < 5
{
var pr1 = _x + x_off - string_width(_s) / 2 * scale;
var pr2 = _y + y_off;
var pr3 = _x + x_off + string_width(_s) / 2 * scale;
var pr4 = _y + y_off + string_height(_s) / 2 * scale;
if rectangle_in_rectangle(r1, r2, r3, r4, pr1, pr2, pr3, pr4)
{
//draw_set_color(c_red);
if (_y - y_off - string_height(_s) * scale) < 0 //view_yport[view_current]
y_off = y_off + abs(_y - r4);
if (_y + y_off + string_height(_s) * 2 * scale) > h// view_hport[view_current]
y_off = y_off - abs(_y - r2) * 1.5;
DDebug("(_y + y_off + string_height(_s) * scale)", 1000, (_y + y_off + string_height(_s) * scale));
DDebug("h", 1000, h);
DDebug("_y + y_off", 1000, _y + y_off);
}
//draw_rectangle(pr1, pr2, pr3, pr4, true);
//draw_set_color(c_white);
draw_text_ext_transformed_color_outlined(_x + x_off, _y + y_off, _s, 10, 10, scale, scale, 0, c.pinned_text_color, c.pinned_text_color_outline, c.pinned_text_color_outline_thickness);
if press_to_unpin(c.unpin_key)
{
array_delete(pins_gui, i, 1);
break;
}
}
}
}
}
}
}
function __dm_init(){
#region keybind functions
function __dm_is_mouse_key(_key) {
return _key == mb_left or
_key == mb_middle or
_key == mb_right or
_key == mb_any or
_key == mb_side1 or
_key == mb_side2;
}
function __dm_is_key(_key) {
return _key == vk_anykey or
_key == vk_left or
_key == vk_right or
_key == vk_up or
_key == vk_down or
_key == vk_enter or
_key == vk_escape or
_key == vk_space or
_key == vk_shift or
_key == vk_control or
_key == vk_alt or
_key == vk_backspace or
_key == vk_tab or
_key == vk_home or
_key == vk_end or
_key == vk_delete or
_key == vk_insert or
_key == vk_pageup or
_key == vk_pagedown or
_key == vk_printscreen or
_key == vk_f1 or
_key == vk_f2 or
_key == vk_f3 or
_key == vk_f4 or
_key == vk_f5 or
_key == vk_f6 or
_key == vk_f7 or
_key == vk_f8 or
_key == vk_f9 or
_key == vk_f10 or
_key == vk_f11 or
_key == vk_f12 or
_key == vk_numpad0 or
_key == vk_numpad1 or
_key == vk_numpad2 or
_key == vk_numpad3 or
_key == vk_numpad4 or
_key == vk_numpad5 or
_key == vk_numpad6 or
_key == vk_numpad7 or
_key == vk_numpad8 or
_key == vk_numpad9 or
_key == vk_multiply or
_key == vk_divide or
_key == vk_add or
_key == vk_subtract or
_key == vk_decimal or
_key == vk_anykey;
}
function _keybind_event_check_pressed(_key) {
var _event = undefined;
if is_string(_key)
{
if _key == ""
throw ("keybinding cannot be an empty string");
_key = string_upper(_key);
if string_length(_key) > 1
_key = string_char_at(_key, 1);
_event = function _orb_pressed(_k) {
return keyboard_check_pressed(ord(_k))
}
}
else if __dm_is_mouse_key(_key)
{
_event = function _mouse_pressed(_k) {
return device_mouse_check_button_pressed(0, _k);
}
}
else if __dm_is_key(_key)
{
_event = function _key_pressed(_k) {
return keyboard_check_pressed(_k);
}
}
else throw ("invalid keybinding");
return _event;
}
function _keybind_event_check(_key) {
var _event = undefined;
if is_string(_key)
{
if _key == ""
throw ("keybinding cannot be an empty string");
_key = string_upper(_key);
if string_length(_key) > 1
_key = string_char_at(_key, 1);
_event = function _orb_pressed(_k) {
return keyboard_check(ord(_k))
}
}
else if __dm_is_mouse_key(_key)
{
_event = function _mouse_pressed(_k) {
return device_mouse_check_button(0, _k);
}
}
else if __dm_is_key(_key)
{
_event = function _key_pressed(_k) {
return keyboard_check(_k);
}
}
else throw ("invalid keybinding");
return _event;
}
#endregion
}
function draw_debug_cross(_x, _y, _lenght_h = 8, _lenght_v = 8, _thick_h = 1, _thick_v = 1, _color_h1 = c_white, _color_h2 = c_white, _color_v1 = c_white, _color_v2 = c_white, _alpha_h = 1, _alpha_v = 1) {
var _a = draw_get_alpha();
var _lh = _lenght_h * .5;
var _lv = _lenght_v * .5;
draw_set_alpha = _alpha_h;
draw_line_width_colour(_x - _lh, _y, _x + _lh, _y, _thick_h, _color_h1, _color_h2);
draw_set_alpha = _alpha_v;
draw_line_width_colour(_x, _y - _lv, _x, _y + _lv, _thick_v, _color_v1, _color_v2);
draw_set_alpha = _a;
}
function draw_text_ext_transformed_color_outlined(_x, _y, _text, _sep, _w, _xscale, _yscale, _angle, _text_color, _outline_color, _thickness) {
var _c = draw_get_color();
//Outline
draw_set_color(_outline_color);
draw_text_ext_transformed(_x + _thickness, _y + _thickness, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x - _thickness, _y - _thickness, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x, _y + _thickness, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x + _thickness, _y, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x, _y - _thickness, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x - _thickness, _y, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x - _thickness, _y + _thickness, _text, _sep, _w, _xscale, _yscale, _angle);
draw_text_ext_transformed(_x + _thickness, _y - _thickness, _text, _sep, _w, _xscale, _yscale, _angle);
//Text
draw_set_color(_text_color);
draw_text_ext_transformed(_x, _y, _text, _sep, _w, _xscale, _yscale, _angle);
draw_set_color(_c);
}
// Simplified lite version
function draw_mouse_xy(x_offset = 10, y_offset = 10, scale = 1) {
var w = view_wport[view_current];
var h = view_hport[view_current];
draw_line(0, mouse_y, w, mouse_y);
draw_line(mouse_x, 0, mouse_x, h);
var _str = string(mouse_x) + "," + string(mouse_y);
if (device_mouse_x_to_gui(0) + x_offset + string_width(_str) * scale) > display_get_gui_width()
x_offset = -x_offset - string_width(_str) / 2 * scale;
else x_offset = x_offset + string_width(_str) / 2 * scale;
if (device_mouse_y_to_gui(0) + y_offset + string_height(_str) * scale) > display_get_gui_height()
y_offset = -y_offset - string_height(_str) / 2 *scale;
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_text_ext_transformed(mouse_x + x_offset, mouse_y + y_offset, _str, 10, 10, scale, scale, 0);
}
function draw_mouse_xy_gui(x_offset = 10, y_offset = 10, scale = 1) {
var w = display_get_gui_width();
var h = display_get_gui_height();
draw_line(0, device_mouse_y_to_gui(0), w, device_mouse_y_to_gui(0));
draw_line(device_mouse_x_to_gui(0), 0, device_mouse_x_to_gui(0), h);
var _str = string(device_mouse_x_to_gui(0)) + "," + string(device_mouse_y_to_gui(0));
if (device_mouse_x_to_gui(0) + x_offset + string_width(_str) * scale) > display_get_gui_width()
x_offset = -x_offset - string_width(_str) / 2 * scale;
else x_offset = x_offset + string_width(_str) / 2 * scale;
if (device_mouse_y_to_gui(0) + y_offset + string_height(_str) * scale) > display_get_gui_height()
y_offset = -y_offset - string_height(_str) / 2 *scale;
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_text_ext_transformed(device_mouse_x_to_gui(0) + x_offset, device_mouse_y_to_gui(0) + y_offset, _str, 10, 10, scale, scale, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment