Skip to content

Latest commit

 

History

History
135 lines (104 loc) · 4.77 KB

README.md

File metadata and controls

135 lines (104 loc) · 4.77 KB

EN | RU

< Back Index Next >
01. Background color Tutorials 03. Spheres

02. Mouse

In this tutorial we are going to set background color on mouse button press.

Estimated completion time: 5 minutes.

Table of contents

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
)

Run in ogse

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 properties
  • main.application.mouse.pressedButtons is an array of pressed mouse buttons
  • main.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's addCallback() method:
      main.application.mouse.pressedButtonsChanged:addCallback(
          function()
              print("Mouse buttons have been pressed or released")
          end
      )
  • core namespace hosts functionality used throughout whole application
  • core.Reporter class implements Publish-subscribe pattern to simplify event circulation among independent application parts
  • core.Reporter:addCallback() method allows one to subscribe to all notifications of a specific core.Reporter instance

Notes:

  • pressedButtonsChanged is a notification instance we subscribe to
  • addCallback() is a method of pressedButtonsChanged instance
  • pressedButtonsChanged and addCallback are separated by : because it's a method call
  • function() ... 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
)

Run in ogse

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