EN | RU
< Back | Index | Next > |
---|---|---|
01. Background color | Tutorials | 03. Spheres |
In this tutorial we are going to set background color on mouse button press.
Estimated completion time: 5 minutes.
Let's detect mouse buttons' presses and releases:
local mouse = main.application.mouse
mouse.pressedButtonsChanged:addCallback(
function()
print("Mouse buttons have been pressed or released")
print("Pressed buttons:", #mouse.pressedButtons)
end
)
Let's see into API we introduced:
main.application.mouse
instance lets you get current mouse (finger) properties like position, pressed mouse buttons and subscribe to changes of the propertiesmain.application.mouse.pressedButtons
is an array of pressed mouse buttonsmain.application.mouse.pressedButtonsChanged
is a notification instance (core.Reporter
) that reports notifications when mouse buttons are pressed or released- to subscribe to all notifications, we use
core.Reporter
'saddCallback()
method:main.application.mouse.pressedButtonsChanged:addCallback( function() print("Mouse buttons have been pressed or released") end )
- to subscribe to all notifications, we use
core
namespace hosts functionality used throughout whole applicationcore.Reporter
class implements Publish-subscribe pattern to simplify event circulation among independent application partscore.Reporter:addCallback()
method allows one to subscribe to all notifications of a specificcore.Reporter
instance
Notes:
pressedButtonsChanged
is a notification instance we subscribe toaddCallback()
is a method ofpressedButtonsChanged
instancepressedButtonsChanged
andaddCallback
are separated by:
because it's a method callfunction() ... end
block represents a callback function (closure) to be executed each time notification is reported#
operator returns number of items of an array
Have a look at the debug console. You should see output similar to this when you press mouse button(s):
Mouse buttons have been pressed or released
Pressed buttons: 1
Mouse buttons have been pressed or released
Pressed buttons: 0
Mouse buttons have been pressed or released
Pressed buttons: 1
Mouse buttons have been pressed or released
Pressed buttons: 2
Mouse buttons have been pressed or released
Pressed buttons: 1
Mouse buttons have been pressed or released
Pressed buttons: 0
Let's set background color to red when at least one mouse button is pressed,
and revert background color back to default 0.2, 0.2, 0.4
when
none of mouse buttons is pressed:
local DEFAULT_COLOR = {0.2, 0.2, 0.4}
local PRESSED_COLOR = {1.0, 0.0, 0.0}
local mouse = main.application.mouse
local camera = main.application.camera
mouse.pressedButtonsChanged:addCallback(
function()
if (#mouse.pressedButtons > 0)
then
camera.clearColor = PRESSED_COLOR
else
camera.clearColor = DEFAULT_COLOR
end
end
)
Now background color is red when you hold mouse button(s).
You have successfully detected mouse buttons' presses/releases and toggled background color.
< Back | Index | Next > |
---|---|---|
01. Background color | Tutorials | 03. Spheres |