Successfully upgraded to Vanilla Forums v2.6. Please report any issue you may find.

2.5-The Events of the Window: Part 2

edited May 2016 in English
This tutorial is virtually identical to the previous one but shows a programming technique that can be useful in many circumstances. Those who program with LUA know very well that this method is called "anonymous functions", that means functions without a name.

In practice, it is good to define the callback function directly where necessary, ie. where they should be assigned to the event to be detected. This technique can be applied anywhere in Hollywood and not only in this particular case.

;Include the HGui library in our application
@INCLUDE "yourpath/+Includes.hws"
@INCLUDE #INC_HGUI

; For now we disable debugging commenting on the following lines
; DBG.Console.Enable(True, False)
; DBG.Console.AddChannel(HGui.DebugChannel)

Local myWin0 = HGui.Window:new(
  { title = "My first window",
    name = "win0",
    position = { x = 20, y = 20 },
    size = { w = 400, h = 100 },
    ; Here's where we use the anonymous functions
    events = 
      { OnMove = Function(msg, win)
                   ; This function will be executed each time the window will be moved
                   DebugPrint("::: CALLBACK FUNCTION :::")
                   DebugPrint(GetTime(True), "OnMove event for window " .. win.Name)
                   DebugPrint("--- message contents ---")
                   ForEach(msg, DebugPrint)
                   DebugPrint("")
                 EndFunction,
        OnSize = Function(msg, win)
                   ; This function will be executed each time the window will be resized
                   DebugPrint("::: CALLBACK FUNCTION :::")
                   DebugPrint(GetTime(True), "OnSize event for window " .. win.Name)
                   DebugPrint("--- message contents ---")
                   ForEach(msg, DebugPrint)
                   DebugPrint("")
                 EndFunction }
  })

Repeat
  WaitEvent()
Forever
Although this technique is not closely related to Gui I think it is really useful to know to use it when necessary.

Try to create a new script can detect other events using anonymous functions.
Sign In or Register to comment.