From 703418a419a57776dc804c58397cb74a24f0816a Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Sun, 4 Oct 2015 01:07:26 +0200 Subject: [PATCH] Added Controls layer, Fixed something --- Controls.lua | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ uCompat.lua | 6 ++++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 Controls.lua diff --git a/Controls.lua b/Controls.lua new file mode 100644 index 0000000..acd6dc4 --- /dev/null +++ b/Controls.lua @@ -0,0 +1,61 @@ +--[[ + Controls related µLua compatibility layer/lib for ctrµLua + + Warning: this HID library will give you headaches. Really. +]] + +-- Local + +local ctr = require("ctr") +local hid = require("ctr.hid") + +local dsKeys = {["A"] = "a", ["B"] = "b", ["X"] = "x", ["Y"] = "y", + ["L"] = "l", ["R"] = "r", ["Start"] = "start", ["Select"] = "select", + ["Up"] = "up", ["Down"] = "down", ["Left"] = "left", ["Right"] = "right"} + +dblcFreq = 30 -- Stylus double click timer, in frames +stylusX = 0 -- \ Both for Stylus.deltaX +stylusY = 0 -- / +stylusTime = 0 + +-- Module + +Keys = {} +Keys.held = {} +Keys.released = {} +Keys.newPress = {} +Stylus = {} + +Controls = {} + +function Controls.read() + hid.read() + local k = hid.keys() + + for n,v in pairs(dsKeys) do + Keys.held[n] = k.held[v] + Keys.released[n] = k.up[v] + Keys.newPress[n] = k.down[v] + end + + Stylus.X, Stylus.Y = hid.touch() + Stylus.deltaX = (stylusX-Stylus.X) + Stylus.deltaY = (stylusY-Stylus.Y) + stylusX, stylusY = Stylus.X, Stylus.Y + + Stylus.held = k.held.touch + Stylus.released = k.up.touch + Stylus.newPress = k.down.touch + + if k.down.touch and (ctr.time()-stylusTime)*(1000/(NB_FPS or 30)) > (dblcFreq/30) then + Stylus.doubleClick = true + stylusTime = ctr.time() + elseif k.down.touch then + stylusTime = ctr.time() + end + +end + +function Controls.setStylusDblcFreq(freq) + dblcFreq = freq +end diff --git a/uCompat.lua b/uCompat.lua index 1f71efb..99330f4 100644 --- a/uCompat.lua +++ b/uCompat.lua @@ -1,5 +1,5 @@ --[[ - Main µLua compatibility layer/lib for ctrµLua + Main µLua compatibility layer/lib for ctrµLua The goal is not to provide a full compatibility, but something close. ]] @@ -13,4 +13,6 @@ ULUA_BOOT_FILE = "main.lua" ULUA_BOOT_FULLPATH = (ULUA_DIR..ULUA_BOOT_FILE) -- Other libs -screen = require("uCompat.screen") +require("uCompat.screen") +require("uCompat.Color") +require("uCompat.Controls")