From d30a376c1a93cdcfe3151325757fff6a9bde6c78 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Wed, 21 Aug 2019 23:54:32 -0400 Subject: [PATCH] Renamed the menu. Added some dialog functions in preparation for moving to dialog. --- game_functions.sh | 95 +++++++++++++++++++++++++++++++++++++++++ game-menu.sh => menu.sh | 0 2 files changed, 95 insertions(+) create mode 100755 game_functions.sh rename game-menu.sh => menu.sh (100%) diff --git a/game_functions.sh b/game_functions.sh new file mode 100755 index 0000000..135f1f5 --- /dev/null +++ b/game_functions.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +# Get the coluns and lines of the "screen" +cols=$(tput cols) +lines=$(tput lines) +# Settings to improve accessibility of dialog. +export DIALOGOPTS='--insecure --no-lines --visit-items' + +inputbox() { + # Returns: text entered by the user + # Args 1, Instructions for box. + # args: 2 initial text (optional) + dialog --backtitle "$(gettext "Enter text and press enter.")" \ + --clear \ + --inputbox "$1" 0 0 "$2" --stdout +} + +msgbox() { +# Returns: None +# Shows the provided message on the screen with an ok button. +dialog --clear --msgbox "$*" 10 72 +} + +infobox() { + # Returns: None + # Shows the provided message on the screen with no buttons. + local timeout=3 + dialog --infobox "$*" 0 0 + read -n1 -t $timeout continue + # Clear any keypresses from the buffer + clear_buffer +} + +yesno() { + # Returns: Yes or No + # Args: Question to user. + # Called in if $(yesno) == "Yes" + # Or variable=$(yesno) + if dialog --clear --backtitle "$(gettext "Press 'Enter' for \"yes\" or 'Escape' for \"no\".")" --yesno "$*" 0 0 ; then + echo "Yes" + else + echo "No" + fi +} + +get_keypress() { + # Returnes the pressed key. + # There arre two ways to use this function. + # first way, get_keypress variableName + # Second way variableName="$(get_keypress)" + # This variable name is long to absolutely minimize possibility of collision. + local getKeypressFunctionReturnVariable=$1 + local returnedKeypress + # Unset IFS to capture any key that is pressed. + local ifs="$IFS" + unset IFS + read -sn1 returnedKeypress + # Restore IFS + IFS="$ifs" + if [[ $getKeypressFunctionReturnVariable ]]; then + eval $getKeypressFunctionReturnVariable="'$returnedKeypress'" + else + echo "$returnedKeypress" + fi +} + +menulist() { + # Args: menu options + # returns: selected option + # set gameMenu to control the message. + declare -a menuList + for i in $@ ; do + menuList+=("$i" "$i") + done + dialog --backtitle "${menuMessage:-Game menu...}" \ + --clear \ + --no-tags \ + --menu "Please make your selection" 0 0 0 "${menuList[@]}" --stdout +} + +numpicker() { + # Args: max number, Min numberr optional. + # returns: selected number + # set gameMenu to control the message. + declare -a menuList + local max=$1 + local min=${2:-10} + for i in $(seq $min $max) ; do + menuList+=("$i" "$i") + done + dialog --backtitle "${menuMessage:-Numeric menu...}" \ + --clear \ + --no-tags \ + --menu "Please select a number between $min and $max." 0 0 0 "${menuList[@]}" --stdout +} diff --git a/game-menu.sh b/menu.sh similarity index 100% rename from game-menu.sh rename to menu.sh