![]() | ![]() | ![]() | Menus |
If your mini-app has many options, it's most convenient to allow the user to access them through a menu. The method createMenu creates such a menu. It can only be used inside the constructor of your Main class. Its argument is a list of pairs, where each pair consists of a string to be displayed in the menu, and a function object that is called when the menu entry is selected.
Here is a demonstration mini-app (menus.kt):
//
// A menu for the mini-app
//
import org.otfried.cs109.Context
import org.otfried.cs109.MiniApp
import org.otfried.cs109.Canvas
import org.otfried.cs109.Color
import org.otfried.cs109.DrawStyle
import org.otfried.cs109.TextAlign
class Main(val ctx: Context) : MiniApp {
init {
ctx.setTitle("Menu demo")
ctx.createMenu(listOf(Pair("Toast", { ctx.toast("Well done!") } ),
Pair("Input", { askForName() } )))
}
fun askForName() {
ctx.inputString("Tell me your name!") { ctx.setTitle("Hello $it") }
}
override fun onDraw(canvas: Canvas) {
val x = canvas.width / 2.0
canvas.clear(Color(255, 255, 192))
canvas.setColor(Color.BLUE)
canvas.setFont(32.0)
canvas.drawText("Open the menu", x, 50.0, TextAlign.CENTER)
}
}
The two new entries "Toast" and "Input" appear at the end of the app menu (after the standard entries for loading and reloading the mini-app).
![]() | ![]() | ![]() | Menus |