Code Monkey home page Code Monkey logo

defos's Introduction

DefOS

Extra native OS functions for games written using the Defold game engine

Currently supports macOS, Windows, Linux and HTML5. Contribute!

Installation

You can use DefOS in your own project by adding this project as a Defold library dependency.

Add the latest dependency URL from the releases page to your dependencies field in game.project.

game.project

(From Defold 1.2.188)

You can change the initial view size and position of your game's window by editing the game.project file of your project in a plain text editor and adding the following lines:

[defos]
view_width = 640
view_height = 480
view_x = 20
view_y = 40

⚠️ It may show you ERROR:ENGINE: Could not find '@render' socket. error on start. But that means only that window change event can't send to the render_script because it's not created yet. More info is available here: #130

view_width and view_height can be used without view_x and view_y but not vice versa.

These initial values will be used at the launch of your project without needing to call any extension functions. Use these values to decrease the initial size of your game’s window view size if your game.project's [display] width / height values are large.

Methods

Customize title bar accessories and title.

defos.disable_maximize_button() -- Not supported on HTML5
defos.disable_minimize_button() -- Not supported on HTML5
defos.disable_window_resize() -- Not supported on HTML5
defos.set_window_title("I set this title using Defos")

Toggle window maximize status.

defos.set_maximized(bool_value)
defos.toggle_maximized()
bool_value = defos.is_maximized()

Toggle full screen. On HTML5, this only works from defos.on_click() or defos.on_interaction().

defos.set_fullscreen(bool_value)
defos.toggle_fullscreen()
bool_value = defos.is_fullscreen()

Keep window on top. Not supported on HTML5.

defos.set_always_on_top(bool_value)
defos.toggle_always_on_top()
bool_value = defos.is_always_on_top()

Minimize window. Not supported on HTML5.

defos.minimize()

Activate (focus) window. Not supported on HTML5.

defos.activate()

Get/set the window's size and position in screen coordinates. The window area includes the title bar, so the actual contained game view area might be smaller than the given metrics.

Passing nil for x and y will center the window in the middle of the display.

Screen coordinates start at (0, 0) in the top-left corner of the main display. X axis goes right. Y axis goes down.

x, y, w, h = defos.get_window_size()
defos.set_window_size(x, y, w, h)

Get/set the game view size and position in screen coordinates. This adjusts the window so that its containing game view is at the desired size and position. The window will be larger than the given metrics because it includes the title bar.

Passing nil for x and y will center the window in the middle of the display.

x, y, w, h = defos.get_view_size()
defos.set_view_size(x, y, w, h)

Query displays.

defos.get_displays() returns a table which can be indexed with either number indices (like an array), either with display ids.

Not supported on HTML5.

displays = defos.get_displays()
pprint(displays[1]) -- Print info about the main display
current_display_id = defos.get_current_display_id() -- Get the ID of the game's current display
pprint(displays[current_display_id]) -- Print info about the game's current display

A display info table has the following format:

{
  id = <userdata>,
  bounds = { -- This is the position and size in screen coordinates of the
    x = 0,   -- display (relative to the top-left corner of the main display)
    y = 0,
    width = 1440,
    height = 900,
  }
  mode = {   -- The current resolution mode of the display
    width = 2880,
    height = 1800,
    scaling_factor = 2,
    refresh_rate = 60,
    bits_per_pixel = 32,
    orientation = 0,
    reflect_x = false,
    reflect_y = false,
  },
  name = "Built-in Retina Display",
}

Query resolution modes for a display.

Returns a table with all the resolution modes a display supports.

Not supported on HTML5.

display_id = defos.get_current_display_id()
modes = defos.get_display_modes(display_id)
pprint(modes[1]) -- Print information about the first available resolution mode

A resolution mode has the following format:

{
  width = 2880, -- Full width/height in pixels (not points)
  height = 1800,
  scaling_factor = 2, -- Hi-DPI scaling factor
  refresh_rate = 60,
  bits_per_pixel = 32,
  orientation = 0, -- One of 0, 90, 180, 270 (degrees measured clockwise)
  reflect_x = false, -- Linux supports reflecting either of the axes,
  reflect_y = false, -- effectively flipping the image like a mirror
}

Show/hide the mouse cursor.

defos.set_cursor_visible(bool_value)
bool_value = defos.is_cursor_visible()

Respond to the mouse entering and leaving the game view area.

bool_value = defos.is_mouse_in_view()
defos.on_mouse_enter(function ()
  print("Mouse entered view")
end)
defos.on_mouse_leave(function ()
  print("Mouse left view")
end)

Get the cursor position.

x, y = defos.get_cursor_pos() -- In screen coordinates
x, y = defos.get_cursor_pos_view() -- In game view coordinates

Move the cursor programatically.

Not supported on HTML5.

defos.set_cursor_pos(x, y) -- In screen coordinates
defos.set_cursor_pos_view(x, y) -- In game view coordinates

Clip cursor to current game view area.

Not supported on Linux and HTML5.

defos.set_cursor_clipped(bool_value)
bool_value = defos.is_cursor_clipped()

Lock cursor movement.

On HTML5 this only works from defos.on_click() or defos.on_interaction(). Not supported on Linux yet.

defos.set_cursor_locked(bool_value)
bool_value = defos.is_cursor_locked()
defos.on_cursor_lock_disabled(function ()
  print("Called on HTML5 when the user presses ESC and the browser disables locking");
end)

Load custom hardware cursors. cursor_data must be:

  • On HTML5, an URL to an image (data URLs work as well).
  • On Windows, a path to an .ani or .cur file on the file system.
  • On Linux, a path to an X11 cursor file on the file system.
  • On macOS, a table of the form:
{
  image = resource.load("cursor.tiff"),
  hot_spot_x = 18,
  hot_spot_y = 2,
}

On macOS, custom cursors can be any image file supported by NSImage, but it's highly recommended to create a TIFF with two images, one at 72DPI (for low density displays) and another at 144DPI (for Retina displays).

The hotspot is an anchor point within the image that will overlap with the functional position of the mouse pointer (eg. the tip of the arrow).

local cursor = defos.load_cursor(cursor_data)

Set custom hardware cursors. cursor can be one of the following:

  • nil: Resets the cursor to default. Equivalent to defos.reset_cursor().
  • defos.CURSOR_ARROW
  • defos.CURSOR_HAND
  • defos.CURSOR_CROSSHAIR
  • defos.CURSOR_IBEAM
  • A cursor value obtained with defos.load_cursor().
  • A cursor_data value that will be used to create a single-use cursor. See defos.load_cursor() above for supported values.
defos.set_cursor(cursor)
defos.reset_cursor()

Show/hide the console window on Windows. Only works when not running from the Editor.

defos.set_console_visible(bool_value)
bool_value = defos.is_console_visible()

On HTML5 only, get a synchronous event on user interaction with the canvas. User interaction is either a mouse click, touch or key press. This is necessary because some HTML5 functions only work when called synchronously from an event handler.

This is currently needed for:

  • defos.toggle_fullscreen()
  • defos.set_cursor_locked(true)
defos.on_interaction(function ()
  print("The user has interacted with the canvas. I have the chance to respond synchronously")
end)

defos.on_click(function ()
  print("The user has clicked. I have the chance to respond synchronously")
end)

Get the absolute path to the game's containing directory. On macOS this will be the path to the .app bundle. On HTML5 this will be the page URL up until the last /.

path = defos.get_bundle_root()

The system path separator. "\\" on Windows, "/" everywhere else.

defos.PATH_SEP

Change the game window's icon at runtime. On Windows, this function accepts .ico files. On macOS this accepts any image file supported by NSImage. On Linux this function is not supported yet.

defos.set_window_icon(path_to_icon_file)

Returns a table of command line arguments used to run the app. On HTML5, returns a table with a single string: the query string part of the URL (eg. { "?param1=foo&param2=bar" }).

arguments = defos.get_arguments()

If you'd like to see any other features, open an issue.

Example

An example is made using DirtyLarry

Defos example screenshot

defos's People

Contributors

agulev avatar britzl avatar chaosddp avatar chaosyu101 avatar dapetcu21 avatar e1e5en-gd avatar greenxenith avatar jcash avatar joshuagrams avatar subsoap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

defos's Issues

dx / dy issue

I'm working on a First Person Shooter style camera setup, and am using action.dx and action.dy - only problem is that the cursor lock / set functions of defos interferes with the build in dx/dy so that the dx/dy movement gets reversed after a mouse cursor moves and then is returned to where it is.

Then do we need to be able to get mouse x/y position and calculate dx/dy ourselves or should I be going about this another way?

Toggling fullscreen in update() of gui script creates error

Hi! This is asvegren from the Defold forums.

First of all - massive thanks for this amazing extension.

I have encountered an issue using defos.set_fullscreen(bool_value) and defos.toggle_fullscreen(). I have a gui component that deals with various game settings, including full screen. I noticed that toggling full screen interrups the completion of the rest of the update() cycle in my gui script.

When I click the fullscreen button in my script (which immediately triggers defos.toggle_fullscreen()), the next gui node accessed using gui.get_node() is not found, yielding an error like this:
ERROR:SCRIPT: /HUD/settings/settings.gui_script:19: No such node: scaling_bg

scaling_bg in this example isn't actually deleted however.

I've realised that I don't have any issue if I just move my fullscreen toggle check to the end of the update() cycle (thereby not accessing any more gui nodes), however I thought I would raise this just in case it's important for anyone else.

Maximize issue on Windows

Reported by forum user Ross Grams:

I still have a weird issue with maximizing. I think I’ve narrowed it down—if, before toggling maximized or fullscreen from DefOS, I use the windows command (windows-button + up) to maximize the window, then toggle maximize from DefOS, the window vanishes completely. It doesn’t show up in the task bar or the task manager, but it’s still running and sending stuff to the editor console. The only way to stop it is to kill the dmengine.exe process from the task manager (or build from the editor again).

If I do Win+Up to maximize, then toggle fullscreen from DefOS, then toggle maximized or fullscreen back again from DefOS, it crashes.

Get number of displays

It would be great if DefOS had a function to get the number of available displays. Also, if possible, to get the name or some kind of identifier for each display.

Feature wishlist

Posting here as a reminder

  • set_video_mode - change current video mode to be windowed / fullscreen / window borderless etc.
  • restore_video_mode - change back to previously saved video mode
  • get_monitors - get list of monitors to be able to set fullscreen on primary or secondary monitors
    * using custom hardware cursors
    * changing window icon
    * setting min / max window sizes? or let user define that?
    * force aspect ratio when window is scaled in window mode? no, let user do this
  • enable/disable window decoration
    * add enable versions of existing functions
  • gamma ramps?
  • get raw key input? possibly problematic because might interfere with engine
    * Change window title
    * lock mouse to window
    * set mouse cursor position
  • change resolution of fullscreen window
  • get list of supported resolutions of monitor
    * disable locking of mouse cursor at center of screen (what defold does in fullscreen mode atm + it auto disables mouse cursor) possible, but just don't use Defold's built in fullscreen option

use of undeclared identifier 'init_window' on MacOs

Just check out my project and there is an error. It was working before(3-4 days ago). Looks like there is a problem with latest update/merge.

  • Defold Editor 2, MacOs High Sierra.

Looks like commenting out line 85 works fine.

screen shot 2018-01-01 at 16 49 51

Alt+f4 and Alt+enter on windows

I am working with publisher and we make Win version of the game.
He told that Alt+f4 and Alt+enter - "must have" functions on windows.

I tried to make this function in lua, but in some popups I block message passing

function on_input() 
...
return true
end

What do you think, should I add this functions to defos (Win only)?
Or it should be an optional for a developer? But how this options should work then? Do you have any ideas?

Proposal: move_mouse_to

defos.move_mouse_to(x,y)

Move mouse to x,y coordinates of the game window client area.

win/mac/linux only

2.0 release

Anything which should be done before publishing a 2.0 release?

Lock aspect ratio

I need to lock minimum aspect ration as of the window (on windows) 4:3 and maximus as 16:9.
Do somebody have some ideas how I can do this using DefOs?

Xubuntu 16.04 Window / Fullscreen issue

From a tester

First off, it was set to fullscreen but in a windowed mode about 1/4 my laptop's resolution. I unchecked and checked "Fullscreen", and the game changed to fullscreen. After restarting the game, it was marked as fullscreen again, but still in a much smaller window, same as before.

This is all on Xubuntu 16.04, so I'll give a try on Xubuntu 18.04 when I get the chance after I finish work later tonight.

Get native resolution for display

I would like to be able to get a list of all the resolutions that a display supports. Hopefully this would support multiple monitors, you would pass in an index for which display you want to check.

New release or depend on master?

There's quite a few commits since the last release. Is it possible to do a release of the new features and fixes or are they considered experimental? Or should I simply depend on master and expect that new commits will ensure backwards compatibility with the API?

Using existing GLFW 2.7 functions

Cool thing!!

http://www.glfw.org/GLFWReference27.pdf
http://www.glfw.org/GLFWUsersGuide27.pdf

glfw.h.zip

2017-11-10 00_12_42-defold editor 2 0 - defos

If we add this header file to the project then we can call the GLFW 2.7 functions directly. A bunch of them are useful and are already cross platform.

If we can include the built in glfw.h I think that should be the way to do it though so I'm asking what the path to it is in the forums. It also might matter if Defold team has made changes to the GLFW 2.7 version I don't remember if they said they did or not, but we can still find with experimentation if features don't work as expected.

Window position on different platforms

On macos window position sets from left down corner.
for example

 	defos.set_window_size(-1,-1,800,600)

2017-05-04_09-38-25

As I know on windows position starts from top-left corner? Am I right?

I think we should resolve this issue.
We can identify what exactly screen point point we should consider like a 0,0?
And then implement the same behavior for all platforms.

Linux support

Native extensions now supports Linux so support for it needs to be added.

Make a 1.0 release

I'd guess Linux native extensions will come next release, can make sure it has feature parity without issues, and then publish first versioned release. Then encourage people to use versioned releases in important projects. Though we probably won't make major changes that would break existing projects it's better to give people sense of security that using the lib won't break their project if we publish updates.

Too difficult logic for set_cursor

As I think would be better to have one simple method for every platform set_cursor(nil or CONSTANT or path) where user can set path to the image .

I think those constructions are difficult:

  • On HTML5, an URL to an image (data URLs work as well)
  • On Windows, a path to an .ani or .cur file on the file system.
  • On macOS, a table of the form

`

if system_name == "Windows" then
	for i, v in ipairs({ "cursor_01.ani", "cursor_02.ani" }) do
		-- load source file and write them to save folder, so that we can access them with fullpath
		local cursor_resource = resource.load("/resources/"..v)
		local raw_bytes = buffer.get_bytes(cursor_resource, hash("data"))

		local cursor_path = sys.get_save_file(appname, v)
		local f = io.open(cursor_path, "wb")
		f:write(raw_bytes)
		f:flush()
		f:close()

		print(cursor_path)
		table.insert(self.cursors, cursor_path)
	end
end

if system_name == "Darwin" then
	table.insert(self.cursors, {
		image = resource.load("/resources/cursor_mac.tiff"),
		hot_spot_x = 18,
		hot_spot_y = 2,
	})
end

if system_name == "HTML5" then
	table.insert(self.cursors, cursor_url)
end`

Developer can include cursor file "as is" into the bundle using hidden parameter: bundle_resources (I made it with icons). https://www.defold.com/manuals/project-settings/#_project

Thanks to @dapecthu I made:
defos.get_bundle_root() - method that returns path to bundle,
defos.PATH_SEP - path separator depends of platform.
Those methods can help with this remaking.

I can make it after merging my current set_icon pull-request. What do you think about it?

Locking cursor on macOS stops dx dy

After calling defos.set_cursor_locked(true) on macOS, Defold doesn’t receive dx/dy events anymore. I suspect it’s because Defold’s input code diffs mouse positions instead of using NSEvent.deltaX and NSEvent.deltaY.

I tried locking the cursor both by calling CGAssociateMouseAndMouseCursorPosition(false) and by repeatedly setting the mouse position to the same point each frame with CGWarpMouseCursorPosition() and I got the same results.

API change proposal

I propose we change the following:

defos.disable_mouse_cursor()
defos.enable_mouse_cursor()
defos.toggle_maximize()
defos.is_maximized()
defos.toggle_fullscreen()
defos.is_fullscreen()

To the following:

defos.set_mouse_cursor_hidden(true)
defos.set_mouse_cursor_hidden(false)
defos.is_mouse_cursor_hidden()
defos.set_maximized(true)
defos.set_maximized(false)
defos.is_maximized()
defos.set_fullscreen(true)
defos.set_fullscreen(false)
defos.is_fullscreen()

This has the benefit of not having to know what state fullscreen is in before you toggle. You can just say set_fullscreen(true) to make sure you're in full screen, and if you were already full screen, then nothing happens. Toggle could be implemented by the user as defos.set_fullscreen(not defos.is_fullscreen()).

What do you think?

This is obviously a breaking change, but better make it now before 1.0.

defos.set_window_size doesn't adjust based on client area size

2018-01-09-22_23_39-untitled-1- -33

defos.set_window_size is based on window size and not client area size. We either need another function or to modify this so it's based on resizing the client area. This is important for pixel art games especially so people can easily set 2x, 4x window sizes of default res for even pixel scaling.

Proposal defos.get_cursor_pos()

defos.get_cursor_pos() -- screen
defos.get_cursor_pos_view() -- view

Would return the cursor position x,y relative to the game's view. Then we can get/set cursor position between frames while calculating dx/dy ourselves.

Toggle fullscreen not work on Linux launch

Getting lots of reports from Linux users that toggling fullscreen like we do on main.script init does not work, only when they go to options screen can they toggle fullscreen. Not sure of a solution other than to toggle fullscreen after a moment on Linux. Maybe an error is happening when toggling as soon as a project inits on Linux?

Work on Mac/Win though.

Keep window always on top

I use defos on a day to day basis to change resolution on the fly and test against the most common mobile ones and it is great!
It would be also great to have an option to always keep the window on top of the others. I use hot reload a lot and that would help not switch windows constantly.

Multiple windows open

Is there any way to spawn multiple windows at the same time?

If it is not, that would be an amazing feature :)

defos fail with Defold 1.2.123 update

clang++ -c -arch x86_64 -target x86_64-apple-darwin12 -isysroot /opt/MacOSX10.13.sdk/ -m64 -O2 -g -mmacosx-version-min=10.7 -DDM_PLATFORM_OSX -DLUA_BYTECODE_ENABLE -Iupload/defos/include

-I/var/extender/sdk/46d8b7e63d7e0f0f4acd545c46d25ca2b227a806/defoldsdk//include -I/var/extender/sdk/46d8b7e63d7e0f0f4acd545c46d25ca2b227a806/defoldsdk//sdk/include upload/defos/src/defos_mac.mm -obuild/defos_mac.mm_3.o

In file included from upload/defos/src/defos_mac.mm:7:

In file included from /opt/MacOSX10.13.sdk//System/Library/Frameworks/AppKit.framework/Headers/AppKit.h:10:
In file included from /opt/MacOSX10.13.sdk//System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:128:
/opt/MacOSX10.13.sdk//System/Library/Frameworks/Foundation.framework/Headers/NSUUID.h:26:49: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'uuid_t' (aka 'unsigned char [16]')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.