From 1536d0192f9bb0490d64d58b4da22d12076a25af Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Wed, 4 Jan 2023 15:22:28 -0700 Subject: Move to a new state when menu option selected --- lib/chessh/ssh/screens/menu.ex | 72 ++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 23 deletions(-) (limited to 'lib/chessh/ssh/screens/menu.ex') diff --git a/lib/chessh/ssh/screens/menu.ex b/lib/chessh/ssh/screens/menu.ex index 22aba85..f89670f 100644 --- a/lib/chessh/ssh/screens/menu.ex +++ b/lib/chessh/ssh/screens/menu.ex @@ -7,13 +7,13 @@ defmodule Chessh.SSH.Client.Menu do defmodule State do defstruct y: 0, - x: 0 + x: 0, + selected: 0 end use Chessh.SSH.Client.Screen @logo " Simponic's - dP MP\"\"\"\"\"\"`MM MP\"\"\"\"\"\"`MM M\"\"MMMMM\"\"MM 88 M mmmmm..M M mmmmm..M M MMMMM MM .d8888b. 88d888b. .d8888b. M. `YM M. `YM M `M @@ -22,54 +22,80 @@ defmodule Chessh.SSH.Client.Menu do `88888P' dP dP `88888P' Mb. .dM Mb. .dM M MMMMM MM MMMMMMMMMMM MMMMMMMMMMM MMMMMMMMMMMM" + @options [ + {"Option 1", {Chessh.SSH.Client.Board, %{}}}, + {"Option 2", {Chessh.SSH.Client.Board, %{}}}, + {"Option 3", {Chessh.SSH.Client.Board, %{}}} + ] + def render(%Client.State{ width: width, height: height, - state_stack: [{_this_module, %State{y: y, x: x}} | _tail] + state_stack: [{_this_module, %State{selected: selected, y: dy, x: dx}} | _tail] }) do + text = String.split(@logo, "\n") {logo_width, logo_height} = Utils.text_dim(@logo) - - split = String.split(@logo, "\n") + {y, x} = center_rect({logo_width, logo_height + length(text)}, {width, height}) Enum.flat_map( - Enum.zip(0..(length(split) - 1), split), + Enum.zip(0..(length(text) - 1), text), fn {i, line} -> [ - ANSI.cursor(div(height - logo_height, 2) + i + y, div(width - logo_width, 2) + x), - "#{line}\n" + ANSI.cursor(y + i + dy, x + dx), + line ] end - ) + ) ++ + Enum.flat_map( + Enum.zip(0..(length(@options) - 1), @options), + fn {i, {option, _}} -> + [ + ANSI.cursor(y + length(text) + i + dy, x + dx), + if(i == selected, do: ANSI.format([:light_cyan, "* #{option}"]), else: option) + ] + end + ) ++ [ANSI.home()] + end + + def wrap_around(index, delta, length) do + calc = index + delta + if(calc < 0, do: length, else: 0) + rem(calc, length) end def handle_input( data, - %Client.State{state_stack: [{this_module, %State{y: y, x: x} = screen_state} | tail]} = - state + %Client.State{ + state_stack: + [{this_module, %State{selected: selected} = screen_state} | tail] = state_stack + } = state ) do - case data do - :left -> + case(data) do + :up -> %Client.State{ state - | state_stack: [{this_module, %State{screen_state | x: x - 1}} | tail] + | state_stack: [ + {this_module, + %State{screen_state | selected: wrap_around(selected, -1, length(@options))}} + | tail + ] } - :right -> + :down -> %Client.State{ state - | state_stack: [{this_module, %State{screen_state | x: x + 1}} | tail] + | state_stack: [ + {this_module, + %State{screen_state | selected: wrap_around(selected, 1, length(@options))}} + | tail + ] } - :up -> - %Client.State{ - state - | state_stack: [{this_module, %State{screen_state | y: y - 1}} | tail] - } + :return -> + {_, new_state} = Enum.at(@options, selected) - :down -> %Client.State{ state - | state_stack: [{this_module, %State{screen_state | y: y + 1}} | tail] + | state_stack: [new_state] ++ state_stack } _ -> -- cgit v1.2.3-70-g09d2