Appkit: /util.lua code sample - Stingray Lua API Reference
--[[
An assortment of useful utility functions.
]]--
Appkit.Util = Appkit.Util or {}
local Util = Appkit.Util
local Application = stingray.Application
local Pad1 = stingray.Pad1
local Vector3 = stingray.Vector3
local simulate_touch = false
function Util.is_pc()
return Application.platform() == Application.WIN32
or Application.platform() == Application.MACOSX
or Application.platform() == Application.WEB
or Application.platform() == Application.LINUX
end
function Util.is_console()
return Application.platform() == Application.PS4
or Application.platform() == Application.XB1
end
function Util.is_android()
return Application.platform() == Application.ANDROID
end
function Util.is_ios()
return Application.platform() == Application.IOS
end
function Util.is_mobile()
return Util.is_android() or Util.is_ios()
end
function Util.set_simulated_touch_enabled(is_enabled)
stingray.SimulatedTouchPanel.set_enabled(is_enabled)
simulate_touch = is_enabled
end
function Util.use_touch()
local p = Application.platform()
return p == Application.ANDROID or p == Application.IOS or (simulate_touch and p == Application.WIN32)
end
function Util.touch_interface()
if Util.use_touch() and Application.platform() == Application.WIN32 then
return stingray.SimulatedTouchPanel
else
return stingray.TouchPanel1
end
end
--Returns whatever argument is appropriate for the current platform.
function Util.plat(pc, xb1, touch, ps4)
if Util.use_touch() then return touch end
local p = Application.platform()
if p == Application.XB1 then return xb1 end
if p == Application.PS4 then return ps4 end
if p == Application.WIN32 and pc == nil and Pad1 and Pad1.active() then
if Pad1.type() == "windows_ps4_controller" then return ps4 end
if Pad1.type() == "xbox_controller" then return xb1 end
end
return pc
end
function Util.location(touch, id)
-- Scale the touch input to the back buffer size.
local back_buffer_w, back_buffer_h = Application.back_buffer_size()
local touch_resolution = touch.resolution()
local scale = Vector3(back_buffer_w / touch_resolution.x, back_buffer_h / touch_resolution.y, 1.0)
local scaled_coordinates = Vector3.multiply_elements(touch.location(id), scale)
return scaled_coordinates
end
return Util