-
Notifications
You must be signed in to change notification settings - Fork 2
/
tutorial_07_fx.ml
124 lines (107 loc) · 3.69 KB
/
tutorial_07_fx.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(* Adaptation of the FX tutorial from Orx *)
(* This example is a direct adaptation of the 07_FX.c tutorial from Orx *)
module State = struct
type t = {
soldier : Orx.Object.t;
mutable soldier_fx_lock : bool;
box : Orx.Object.t;
mutable selected_fx : string;
}
let state : t option ref = ref None
let get () = Option.get !state
end
let input_event_handler
(_event : Orx.Event.t)
(_input : Orx.Input_event.t)
(_payload : Orx.Input_event.payload) =
(* Do nothing for now... *)
Ok ()
let fx_event_handler
(event : Orx.Event.t)
(fx_event : Orx.Fx_event.t)
(payload : Orx.Fx_event.payload) =
let state = State.get () in
let recipient = Orx.Event.get_recipient_object event |> Option.get in
( match fx_event with
| Start ->
Orx.Log.log "FX <%s>@<%s> has started!"
(Orx.Fx_event.get_name payload)
(Orx.Object.get_name recipient);
if Orx.Object.equal recipient state.soldier then
state.soldier_fx_lock <- true
| Stop ->
Orx.Log.log "FX <%s>@<%s> has stopped!"
(Orx.Fx_event.get_name payload)
(Orx.Object.get_name recipient);
if Orx.Object.equal recipient state.soldier then
state.soldier_fx_lock <- false
| Add | Remove | Loop -> ()
);
Ok ()
let update (_clock_info : Orx.Clock.Info.t) =
let state = State.get () in
let possible_fx_inputs =
[
("SelectMultiFX", "MultiFX");
("SelectWobble", "WobbleFX");
("SelectCircle", "CircleFX");
("SelectFade", "FadeFX");
("SelectFlash", "FlashFX");
("SelectMove", "MoveFX");
("SelectFlip", "FlipFX");
]
in
let found =
List.find_opt
(fun (input, _fx) -> Orx.Input.is_active input)
possible_fx_inputs
in
( match found with
| None -> (* No relevant input so there's nothing to do... *) ()
| Some (_input, fx_name) -> state.selected_fx <- fx_name
);
if not state.soldier_fx_lock then
if Orx.Input.has_been_activated "ApplyFX" then
Orx.Object.add_unique_fx_exn state.soldier state.selected_fx
let init () =
(* Print out a hint to the user about what's to come *)
let get_name (binding : string) : string =
let (type_, id, mode) = Orx.Input.get_binding binding 0 |> Result.get_ok in
Orx.Input.get_binding_name type_ id mode
in
Orx.Log.log
("@.- To select the FX to apply:@."
^^ " . '%s' => Wobble@."
^^ " . '%s' => Circle@."
^^ " . '%s' => Fade@."
^^ " . '%s' => Flash@."
^^ " . '%s' => Move@."
^^ " . '%s' => Flip@."
^^ " . '%s' => MultiFX that contains the slots of 4 of the above FXs@."
^^ "- '%s' will apply the current selected FX on soldier@."
^^ "* Only once FX will be applied at a time in this tutorial@."
^^ "* However an object can support up to 8 FXs at the same time@."
^^ "* Box has a looping rotating FX applied directly from config, \
requiring no code"
)
(get_name "SelectWobble") (get_name "SelectCircle") (get_name "SelectFade")
(get_name "SelectFlash") (get_name "SelectMove") (get_name "SelectFlip")
(get_name "SelectMultiFX") (get_name "ApplyFX");
let (_viewport : Orx.Viewport.t) =
Orx.Viewport.create_from_config_exn "Viewport"
in
let soldier = Orx.Object.create_from_config_exn "Soldier" in
let box = Orx.Object.create_from_config_exn "Box" in
State.state :=
Some { soldier; box; soldier_fx_lock = false; selected_fx = "WobbleFX" };
let clock = Orx.Clock.get_core () in
Orx.Clock.register clock update;
Orx.Event.add_handler Fx fx_event_handler;
Orx.Event.add_handler Input input_event_handler;
Ok ()
let run () =
if Orx.Input.is_active "Quit" then
Orx.Status.error
else
Orx.Status.ok
let () = Orx.Main.start ~config_dir:"examples/tutorial/data" ~init ~run "07_FX"