test3.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # --- Test 3. Window events handling. --- #
  2. import nodesnim
  3. Window("newwindow")
  4. var
  5. main = Scene("Main")
  6. node = Node("My node")
  7. main.addChild(node)
  8. # Bind actions:
  9. Input.addKeyAction("forward", "w")
  10. Input.addKeyAction("backward", "s")
  11. Input.addKeyAction("left", "a")
  12. Input.addKeyAction("right", "d")
  13. Input.addButtonAction("click", BUTTON_LEFT)
  14. Input.addButtonAction("release", BUTTON_RIGHT)
  15. node.on_process =
  16. proc(self: NodeRef) = # This called every frame.
  17. if Input.isActionPressed("forward"): # returns true, when user press "w"
  18. echo "forward"
  19. if Input.isActionPressed("backward"): # returns true, when user press "s"
  20. echo "backward"
  21. if Input.isActionPressed("left"): # returns true, when user press "a"
  22. echo "left"
  23. if Input.isActionPressed("right"): # returns true, when user press "d"
  24. echo "right"
  25. if Input.isActionJustPressed("click"): # returns true, when the user clicks the left button one time.
  26. echo "clicked!"
  27. if Input.isActionReleased("release"): # returns true, when the user no more press on the right button.
  28. echo "release!"
  29. node.on_input =
  30. proc(self: NodeRef, event: InputEvent) = # This called only on user input.
  31. if event.isInputEventMouseButton() and event.pressed:
  32. echo "hi"
  33. addScene(main)
  34. setMainScene("Main")
  35. windowLaunch()