Files
garminApp/source/menuPlayerDelegate.mc
2026-10-05 20:55:29 +01:00

141 lines
4.4 KiB
Mason

import Toybox.WatchUi;
import Toybox.Lang;
import Toybox.Graphics;
class menuPlayerDelegate extends WatchUi.PickerDelegate {
private var _action as enAction?;
private var _team as enTeam?;
private var _player as Number?;
function initialize(action as enAction, team as enTeam) {
_team = team;
_action =action;
PickerDelegate.initialize();
}
function onAccept(values) as Boolean {
var tens = values[0] as Number;
var units = values[1] as Number;
_player = (tens * 10) + units;
System.println(Lang.format("Action: $1$ - Team: $2$ - Player: $3$", [_action, _team, _player]));
WatchUi.popView(WatchUi.SLIDE_DOWN);
switch (_action){
case GOAL:
WatchUi.pushView($.gMenuGoals, new menuGoalsDelegate(_team, _player), WatchUi.SLIDE_UP);
break;
case CARD:
WatchUi.pushView($.gMenuCards, new menuCardsDelegate(_team, _player), WatchUi.SLIDE_UP);
break;
}
return true;
}
function onCancel() as Boolean {
WatchUi.popView(WatchUi.SLIDE_DOWN);
return true;
}
}
class playerPicker extends WatchUi.Picker {
function initialize() {
var title = new WatchUi.Text({
:text => "Player",
:color => Graphics.COLOR_ORANGE,
:font => Graphics.FONT_TINY,
:locX =>WatchUi.LAYOUT_HALIGN_CENTER,
:locY =>WatchUi.LAYOUT_VALIGN_TOP
// :justification => Graphics.TEXT_JUSTIFY_CENTER,
// :locY => 30 // Shifted down slightly to comfortably clear round screen curvature
});
var pattern = [
new NumberFactory(0, 9, 1,{:format=>"%d"}), // Index 0: Tens
new NumberFactory(0, 9, 1,{:format=>"%d"}) // Index 1: Units
];
Picker.initialize({
:title => title,
:pattern => pattern,
:defaults => [0, 0]
});
}
}
class NumberFactory extends WatchUi.PickerFactory {
private var _start as Number;
private var _stop as Number;
private var _increment as Number;
private var _formatString as String;
private var _font as FontDefinition;
//! Constructor
//! @param start Number to start with
//! @param stop Number to end with
//! @param increment How far apart the numbers should be
//! @param options Dictionary of options
//! @option options :font The font to use
//! @option options :format The number format to display
public function initialize(start as Number, stop as Number, increment as Number, options as {
:font as FontDefinition,
:format as String
}) {
PickerFactory.initialize();
_start = start;
_stop = stop;
_increment = increment;
var format = options.get(:format);
if (format != null) {
_formatString = format;
} else {
_formatString = "%d";
}
var font = options.get(:font);
if (font != null) {
_font = font;
} else {
_font = Graphics.FONT_NUMBER_HOT;
}
}
//! Get the index of a number item
//! @param value The number to get the index of
//! @return The index of the number
public function getIndex(value as Number) as Number {
return (value / _increment) - _start;
}
//! Generate a Drawable instance for an item
//! @param index The item index
//! @param selected true if the current item is selected, false otherwise
//! @return Drawable for the item
public function getDrawable(index as Number, selected as Boolean) as Drawable? {
var value = getValue(index);
var text = "No item";
if (value instanceof Number) {
text = value.format(_formatString);
}
return new WatchUi.Text({:text=>text, :color=>Graphics.COLOR_WHITE, :font=>_font,
:locX=>WatchUi.LAYOUT_HALIGN_CENTER, :locY=>WatchUi.LAYOUT_VALIGN_CENTER});
}
//! Get the value of the item at the given index
//! @param index Index of the item to get the value of
//! @return Value of the item
public function getValue(index as Number) as Object? {
return _start + (index * _increment);
}
//! Get the number of picker items
//! @return Number of items
public function getSize() as Number {
return (_stop - _start) / _increment + 1;
}
}