|
@@ -5,7 +5,7 @@ require 'rx-love'
|
|
local object = rx.BehaviorSubject.create(400, 300)
|
|
local object = rx.BehaviorSubject.create(400, 300)
|
|
|
|
|
|
local radius = 30
|
|
local radius = 30
|
|
-local isLeft = function(x, y, button) return button == 'l' end
|
|
|
|
|
|
+local isLeft = function(x, y, button) return button == 1 end
|
|
|
|
|
|
-- Set up observables for clicking and releasing the left mouse button.
|
|
-- Set up observables for clicking and releasing the left mouse button.
|
|
local leftPressed = love.mousepressed:filter(isLeft)
|
|
local leftPressed = love.mousepressed:filter(isLeft)
|
|
@@ -13,21 +13,17 @@ local leftReleased = love.mousereleased:filter(isLeft)
|
|
|
|
|
|
-- Run some operations on the mousepressed and mousereleased observables:
|
|
-- Run some operations on the mousepressed and mousereleased observables:
|
|
-- * filter out any mouse presses that aren't within the radius of the circle.
|
|
-- * filter out any mouse presses that aren't within the radius of the circle.
|
|
--- * map over the mouse presses and return an observable from the love.mousemoved event, which
|
|
|
|
--- produces the coordinates of the mouse as it moves.
|
|
|
|
--- * takeUntil to stop moving the circle when the mouse is released.
|
|
|
|
--- * flatten it to convert the observable of observables into an observable that just contains the
|
|
|
|
--- mouse coordinates.
|
|
|
|
--- * subscribe to them and set the circle's position.
|
|
|
|
|
|
+-- * flatMap the mousepresses to start producing values from the love.mousemoved event. takeUntil
|
|
|
|
+-- ensures that the dragging stops when the mouse button is released.
|
|
|
|
+-- * subscribe to the mouse positions and set the circle's position accordingly.
|
|
local drag = leftPressed
|
|
local drag = leftPressed
|
|
:filter(function(x, y)
|
|
:filter(function(x, y)
|
|
local ox, oy = object:getValue()
|
|
local ox, oy = object:getValue()
|
|
return (x - ox) ^ 2 + (y - oy) ^ 2 < radius ^ 2
|
|
return (x - ox) ^ 2 + (y - oy) ^ 2 < radius ^ 2
|
|
end)
|
|
end)
|
|
- :map(function()
|
|
|
|
|
|
+ :flatMap(function()
|
|
return love.mousemoved:takeUntil(leftReleased)
|
|
return love.mousemoved:takeUntil(leftReleased)
|
|
end)
|
|
end)
|
|
- :flatten()
|
|
|
|
:subscribe(function(x, y)
|
|
:subscribe(function(x, y)
|
|
object:onNext(x, y)
|
|
object:onNext(x, y)
|
|
end)
|
|
end)
|