This simplified example creates 4 buttons that each play a different sound, accessible with mouse, keyboard and gamepad. It's called BooBoo. You can find it on Codeberg.
function draw_window x y w h focussed data
{
filled_rectangle 0 0 255 255 0 0 255 255 0 0 255 255 0 0 255 255 x y w h
}
function draw_button x y w h focussed data
{
number r g b
if (== focussed TRUE) yellow white
= r 255
= g 255
= b 0
:yellow
= r 255
= g 255
= b 255
:white
number tw th
font_width font tw [data "text"]
font_height font th
number xx yy
= xx (+ x (/ w 2))
- xx (/ tw 2)
= yy (+ y (/ h 2))
- yy (/ th 2)
filled_rectangle 0 0 255 255 0 0 255 255 0 255 255 255 0 255 255 255 x y w h
rectangle r g b 255 x y w h 2
font_draw font r g b 255 [data "text"] xx yy
:draw_button
}
function null_event type a b c d x y w h focussed data
{
}
function button_event type a b c d x y w h focussed data
{
if (&& (== type EVENT_MOUSE_DOWN) (== a 1)) play
number on_button
call_result on_button owned x y w h c d
if (== on_button TRUE) really_play
mml_play [data "sfx"] 1 0
:really_play
:play
if (&& (== TRUE focussed) (|| (&& (== type EVENT_KEY_DOWN) (== KEY_RETURN a)) (&& (== type EVENT_JOY_DOWN) (== a JOY_A)))) play_it
mml_play [data "sfx"] 1 0
:play_it
}
function mkbutton text sfx
{
map m
map_set m "type" "button"
map_set m "text" text
map_set m "sfx" sfx
map_set m "draw" draw_button
map_set m "event" button_event
return m
}
function owned wx wy ww wh x y
{
if (|| (< x wx) (< y wy) (>= x (+ wx ww)) (>= y (+ wy wh))) nope
return FALSE
:nope
return TRUE
}
function gui_event id type a b c d x y w h focussed data
{
call [data "event"] type a b c d x y w h focussed data
}
function gui_draw id x y w h focussed data
{
call [data "draw"] x y w h focussed data
}
number sfx1 sfx2 sfx3 sfx4 mml_load sfx1 "sfx/b1.mml" mml_load sfx2 "sfx/b2.mml" mml_load sfx3 "sfx/b3.mml" mml_load sfx4 "sfx/b4.mml"
map c b1 b2 b3 b4 map_set c "type" "window" map_set c "draw" draw_window map_set c "event" null_event call_result b1 mkbutton "Elixir" sfx1 call_result b2 mkbutton "Holy Water" sfx2 call_result b3 mkbutton "Potion" sfx3 call_result b4 mkbutton "Potion Omega" sfx4
number font font_load font "vga.ttf" 12 1
number container w1 w2 w3 w4
widget_create container 200 200 c widget_set_accepts_focus container FALSE widget_create w1 0.5 0.5 b1 widget_set_parent w1 container widget_create w2 0.5 0.5 b2 widget_set_parent w2 container widget_create w3 0.5 0.5 b3 widget_set_break_line w3 TRUE widget_set_parent w3 container widget_create w4 0.5 0.5 b4 widget_set_parent w4 container
widget_set_padding w1 5 widget_set_padding w2 5 widget_set_padding w3 5 widget_set_padding w4 5
gui_start container
gui_set_focus w1
function draw_window x y w h focussed data { filled_rectangle 0 0 255 255 0 0 255 255 0 0 255 255 0 0 255 255 x y w h }
function draw_button x y w h focussed data { number r g b if (== focussed TRUE) yellow white = r 255 = g 255 = b 0 :yellow = r 255 = g 255 = b 255 :white number tw th font_width font tw [data "text"] font_height font th
number xx yy = xx (+ x (/ w 2)) - xx (/ tw 2) = yy (+ y (/ h 2)) - yy (/ th 2)
filled_rectangle 0 0 255 255 0 0 255 255 0 255 255 255 0 255 255 255 x y w h rectangle r g b 255 x y w h 2 font_draw font r g b 255 [data "text"] xx yy :draw_button }
function null_event type a b c d x y w h focussed data { }
function button_event type a b c d x y w h focussed data { if (&& (== type EVENT_MOUSE_DOWN) (== a 1)) play number on_button call_result on_button owned x y w h c d if (== on_button TRUE) really_play mml_play [data "sfx"] 1 0 :really_play :play
if (&& (== TRUE focussed) (|| (&& (== type EVENT_KEY_DOWN) (== KEY_RETURN a)) (&& (== type EVENT_JOY_DOWN) (== a JOY_A)))) play_it mml_play [data "sfx"] 1 0 :play_it }
function mkbutton text sfx { map m map_set m "type" "button" map_set m "text" text map_set m "sfx" sfx map_set m "draw" draw_button map_set m "event" button_event return m }
function owned wx wy ww wh x y { if (|| (< x wx) (< y wy) (>= x (+ wx ww)) (>= y (+ wy wh))) nope return FALSE :nope return TRUE }
function gui_event id type a b c d x y w h focussed data { call [data "event"] type a b c d x y w h focussed data }
function gui_draw id x y w h focussed data { call [data "draw"] x y w h focussed data }