-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0d16f5
commit e30593b
Showing
5 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
|
||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[GNU Lesser General Public License (LGPL)](http://www.gnu.org/licenses/lgpl-3.0.txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
coronasdk-slider | ||
================ | ||
# coronasdk-slider | ||
|
||
Turn a display object into a sliding panel | ||
|
||
## Usage | ||
|
||
### 1. Install | ||
Setup [lua-loader](https://github.com/wscherphof/lua-loader) and then just `npm install coronasdk-slider` | ||
|
||
### 2. Require | ||
```lua | ||
local Slider = require("coronasdk-slider") | ||
``` | ||
|
||
### 3. Have fun | ||
```lua | ||
local panel = display.newRect(...) | ||
local slidingpanel = Slider:new({}, panel) | ||
slidingpanel.on("slide") = function (position) | ||
if "left" == position then print("panel closed") end | ||
if "right" == position then print("panel open") end | ||
end | ||
``` | ||
Happen to have a TableView widget that you want to slide? Do this: | ||
```lua | ||
local tableview = widget.newTableView(...) | ||
local view = tableview[2] -- yes, it's dirty | ||
local slidingpanel = Slider:new({}, view, {moveobject = tableview}) | ||
``` | ||
|
||
## License | ||
[GNU Lesser General Public License (LGPL)](http://www.gnu.org/licenses/lgpl-3.0.txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
local EventEmitter = require("lua-events").EventEmitter | ||
|
||
local slider = {} | ||
|
||
function slider:new (base, touchobject, options) | ||
local base = base or {} | ||
local options = options or {} | ||
local limits = { | ||
left = options.left or 0, | ||
right = options.right or display.viewableContentWidth * .8 | ||
} | ||
local position = options.position or "left" | ||
local startthreshold = options.startthreshold or 10 | ||
local swipethreshold = options.swipethreshold or 75 | ||
local moveobject = options.moveobject or touchobject | ||
|
||
local instance = EventEmitter:new(base) | ||
function instance:slide (leftorright) | ||
self:emit("slide", leftorright) | ||
if moveobject.x == limits[leftorright] then return end | ||
position = leftorright | ||
transition.to(moveobject, { | ||
time = 400, | ||
transition = easing.outExpo, | ||
x = limits[leftorright] | ||
}) | ||
end | ||
|
||
local defaulttouch = touchobject.touch | ||
local sliding, scrolling, prev | ||
function touchobject:touch (event) | ||
|
||
-- desired behaviour: | ||
-- * start scrolling or sliding only when moved more than a certain threshold | ||
-- * no sliding while scrolling; no scrolling while sliding | ||
-- * only scrolling if in left position | ||
-- * when sliding, snap back to current position if not slided further than a certain threshold | ||
-- * when in right position, sliding to the left will do the nice sliding; any other movement, | ||
-- including tapping, will snap it back to the left position | ||
|
||
local function direction () | ||
if event.x > event.xStart then return "right" | ||
else return "left" end | ||
end | ||
|
||
local distance = {} | ||
function distance._d (a, b) return math.abs(a - b) end | ||
function distance:x () return self._d(event.x, event.xStart) end | ||
function distance:y () return self._d(event.y, event.yStart) end | ||
|
||
if "began" == event.phase then | ||
sliding, scrolling = false, false | ||
defaulttouch(touchobject, event) | ||
|
||
elseif "moved" == event.phase then | ||
if not sliding and not scrolling | ||
and "left" == position | ||
and distance:y() > startthreshold then | ||
scrolling = true | ||
end | ||
if not scrolling and not sliding | ||
and direction() ~= position | ||
and distance:x() > startthreshold then | ||
sliding = true | ||
end | ||
|
||
if scrolling then | ||
defaulttouch(touchobject, event) | ||
elseif sliding | ||
and moveobject.x >= limits.left and moveobject.x <= limits.right then | ||
moveobject.x = moveobject.x + (event.x - prev) | ||
end | ||
|
||
elseif "ended" == event.phase or "canceled" == event.phase then | ||
if sliding then | ||
if distance:x() > swipethreshold then | ||
instance:slide(direction()) | ||
else | ||
instance:slide(position) | ||
end | ||
elseif "right" == position then | ||
instance:slide("left") | ||
end | ||
sliding, scrolling = false, false | ||
defaulttouch(touchobject, event) | ||
end | ||
|
||
prev = event.x | ||
return true | ||
end | ||
|
||
return instance | ||
end | ||
|
||
return slider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "coronasdk-slider", | ||
"version": "0.0.1", | ||
"description": "Turn a display object into a sliding panel", | ||
"main": "init.lua", | ||
"dependencies": { | ||
"lua-loader": "~0.0.3", | ||
"lua-events": "~0.0.2" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wscherphof/coronasdk-slider.git" | ||
}, | ||
"keywords": [ | ||
"lua", | ||
"lua-loader", | ||
"corona", | ||
"sdk", | ||
"coronasdk", | ||
"slide", | ||
"slider", | ||
"menu", | ||
"eventemitter" | ||
], | ||
"author": "Wouter Scherphof", | ||
"license": "LGPL", | ||
"bugs": { | ||
"url": "https://github.com/wscherphof/coronasdk-slider/issues" | ||
} | ||
} |