From 2cc25a054222ad7b3a0d3038c60725ded70b5a4c Mon Sep 17 00:00:00 2001 From: miss-architect Date: Sun, 4 Dec 2016 14:55:08 +0100 Subject: [PATCH 1/2] Added simple logic to highlight which player is serving. --- source/DataTracker.mc | 15 +++++++++++++- source/Player.mc | 10 +++++++++ source/SetsTracker.mc | 16 ++++++--------- source/SquashDelegate.mc | 44 ++++++++++++++++++++++++---------------- source/SquashView.mc | 28 ++++++++++++++++--------- 5 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 source/Player.mc diff --git a/source/DataTracker.mc b/source/DataTracker.mc index 2b42b0f..8178c0b 100644 --- a/source/DataTracker.mc +++ b/source/DataTracker.mc @@ -15,6 +15,8 @@ class DataTracker { hidden var session; //! Sets tracker hidden var setsTracker; + //! Contains who is serving + hidden var servingPlayer; //! Constructor function initialize() { @@ -36,6 +38,7 @@ class DataTracker { session.stop(); } setsTracker = new SetsTracker(); + servingPlayer = Player.NO_PLAYER; } //! Updates the calculated values taken from @@ -44,7 +47,6 @@ class DataTracker { var activityInfo = Act.getInfo(); numberOfSteps = activityInfo.steps - initialSteps; numberOfCalories = activityInfo.calories - initialCalories; - } //! Returns the number of steps done during the current activity @@ -110,4 +112,15 @@ class DataTracker { return session; } + //! Sets the plater that is currently serving + //! @param newServingPlayer Should be Player.PLAYER_1, Player.PLAYER_2 or Player.NO_PLAYER + function changeServingPlayer(newServingPlayer){ + servingPlayer = newServingPlayer; + } + + //! Returns the current serving player (Player.PLAYER_1, Player.PLAYER_2 or Player.NO_PLAYER) + function getServingPlayer() { + return servingPlayer; + } + } \ No newline at end of file diff --git a/source/Player.mc b/source/Player.mc new file mode 100644 index 0000000..c07fe79 --- /dev/null +++ b/source/Player.mc @@ -0,0 +1,10 @@ +//! Module containing things related to Players +module Player { + //! Enum used to identify players as PlayerId. + enum { + NO_PLAYER = -1, + PLAYER_1, + PLAYER_2 + } + +} \ No newline at end of file diff --git a/source/SetsTracker.mc b/source/SetsTracker.mc index ce81ec1..abb7367 100644 --- a/source/SetsTracker.mc +++ b/source/SetsTracker.mc @@ -1,10 +1,6 @@ //! Class used to track the score of previous //! sets of the game class SetsTracker { - //! Representation of Player 1 - const PLAYER_1 = 0; - //! Representation of Player 2 - const PLAYER_2 = 1; //! Score of all sets of a game hidden var sets; //! Total game score (number of sets each player won) @@ -23,7 +19,7 @@ class SetsTracker { } //! Increases the score for a player - //! @param player PLAYER_1 or PLAYER_2 + //! @param player Player.PLAYER_1 or Player.PLAYER_2 function increaseScore(player) { // Assert that player is 0 or 1. var won = false; @@ -48,13 +44,13 @@ class SetsTracker { //! Private method that increments the total score //! for the given player and starts a new set if //! the game is not over - //! @param player PLAYER_1 or PLAYER_2 + //! @param player Player.PLAYER_1 or Player.PLAYER_2 hidden function updateGameScores(player){ gameScore[player]++; } //! Returns true is the given player won the current set - //! @param player PLAYER_1 or PLAYER_2 + //! @param player Player.PLAYER_1 or Player.PLAYER_2 function didPlayerWin(player) { return ((sets[currentSet][player] >= gameConfiguration.getMaxScore()) && (sets[currentSet][player] - sets[currentSet][1 - player] > 1)); @@ -62,7 +58,7 @@ class SetsTracker { //! Returns if anyPlayerWon the current set hidden function anyPlayerWon() { - return (didPlayerWin(PLAYER_1) || didPlayerWin(PLAYER_2)); + return (didPlayerWin(Player.PLAYER_1) || didPlayerWin(Player.PLAYER_2)); } //! Get all sets @@ -72,7 +68,7 @@ class SetsTracker { } //! Get the current set score of the given player - //! @param player PLAYER_1 or PLAYER_2 + //! @param player Player.PLAYER_1 or Player.PLAYER_2 //! @return player score function getPlayerScore(player){ return sets[currentSet][player]; @@ -93,7 +89,7 @@ class SetsTracker { //! Returns true if the player losing the match cannot //! win with the remaining sets hidden function theLoserCannotWin() { - var setDifference = (gameScore[PLAYER_1] - gameScore[PLAYER_2]).abs(); + var setDifference = (gameScore[Player.PLAYER_1] - gameScore[Player.PLAYER_2]).abs(); // Someone is winning and the remaining sets cannot make the loser win return ((setDifference > 0) && ((gameConfiguration.getTotalSets() - 1) - currentSet) < setDifference); diff --git a/source/SquashDelegate.mc b/source/SquashDelegate.mc index 2d39f3e..e010c82 100644 --- a/source/SquashDelegate.mc +++ b/source/SquashDelegate.mc @@ -10,7 +10,7 @@ class SquashDelegate extends Ui.BehaviorDelegate { hidden var dataTracker; //! Constructor - //! @param dataTracker Shared objtect that contains + //! @param dataTracker Shared objtect that contains //! the data that will be displayed on screen function initialize(dataTracker) { BehaviorDelegate.initialize(); @@ -55,6 +55,10 @@ class SquashDelegate extends Ui.BehaviorDelegate { :dataTracker=>dataTracker}), new WinGameDelegate(dataTracker), Ui.SLIDE_IMMEDIATE); } + dataTracker.changeServingPlayer(Player.NO_PLAYER); + } + else { + dataTracker.changeServingPlayer(Player.PLAYER_1); } } @@ -69,52 +73,56 @@ class SquashDelegate extends Ui.BehaviorDelegate { new Ui.BehaviorDelegate(), Ui.SLIDE_IMMEDIATE); } else { - Ui.pushView(new WinGameView({:player=>GameConfiguration.getInstance().getPlayer2Name(), + Ui.pushView(new WinGameView({:player=>GameConfiguration.getInstance().getPlayer2Name(), :dataTracker=>dataTracker}), new WinGameDelegate(dataTracker), Ui.SLIDE_IMMEDIATE); } + dataTracker.changeServingPlayer(Player.NO_PLAYER); + } + else { + dataTracker.changeServingPlayer(Player.PLAYER_2); } } - + //! Function called when user taps a touch screen. //! Replacement of Button feature that does not exist //! in sdk v1.3.1 function onTap(evt) { - var x = evt.getCoordinates()[0]; - var y = evt.getCoordinates()[1]; + var x = evt.getCoordinates()[0]; + var y = evt.getCoordinates()[1]; if (isHitting(x,y, player1LocX)) { - onPlayer1(); + onPlayer1(); } else if (isHitting(x,y, player2LocX)) { - onPlayer2(); + onPlayer2(); } return true; } - + //! Check if the user is tapping a score button. //! This function uses global variables defined //! in SquashView. This is ugly, and has to be //! improved! But if Garmin starts offering the //! button feature in older devices, this won't be necessary. function isHitting(x, y, xStartPosition) { - return (((x > xStartPosition) && (x < (xStartPosition + widthButton))) && - (y < heightButton)); + return (((x > xStartPosition) && (x < (xStartPosition + widthButton))) && + (y < heightButton)); } - + //! Event used in non-touchscreen devices to //! increment player 2 score function onNextPage() { - if (!System.getDeviceSettings().isTouchScreen) { - onPlayer2(); - } + if (!System.getDeviceSettings().isTouchScreen) { + onPlayer2(); + } } - + //! Event used in non-soutchscreen devices to //! increment player 1 score function onPreviousPage() { - if (!System.getDeviceSettings().isTouchScreen) { - onPlayer1(); - } + if (!System.getDeviceSettings().isTouchScreen) { + onPlayer1(); + } } } \ No newline at end of file diff --git a/source/SquashView.mc b/source/SquashView.mc index 4e650bd..639302b 100644 --- a/source/SquashView.mc +++ b/source/SquashView.mc @@ -13,7 +13,7 @@ using Toybox.System as System; var player1LocX = 0; var player2LocX = 0; var heightButton = 0; -var widthButton = 0; +var widthButton = 0; //! Class that represents the main Squash App @@ -40,7 +40,7 @@ class SquashView extends Ui.View { hidden var initialY; //! Constructor - //! @param dataTracker Shared objtect that contains + //! @param dataTracker Shared objtect that contains //! the data that will be displayed on screen function initialize(dataTracker) { View.initialize(); @@ -57,11 +57,11 @@ class SquashView extends Ui.View { if (System.getDeviceSettings().screenShape == System.SCREEN_SHAPE_ROUND) { initialY += 10; } - - heightButton = dc.getFontHeight(Gfx.FONT_TINY) + dc.getFontHeight(Gfx.FONT_NUMBER_MILD) + VERTICAL_SPACING; - widthButton = (dc.getWidth() / 2) - HORIZONTAL_SPACING; - player1LocX = 0; - player2LocX = (dc.getWidth() / 2) + HORIZONTAL_SPACING; + + heightButton = dc.getFontHeight(Gfx.FONT_TINY) + dc.getFontHeight(Gfx.FONT_NUMBER_MILD) + VERTICAL_SPACING; + widthButton = (dc.getWidth() / 2) - HORIZONTAL_SPACING; + player1LocX = 0; + player2LocX = (dc.getWidth() / 2) + HORIZONTAL_SPACING; } //! Called when this View is brought to the foreground. Restore @@ -85,8 +85,16 @@ class SquashView extends Ui.View { var y = initialY; var gameConfiguration = GameConfiguration.getInstance(); + if (dataTracker.getServingPlayer() == Player.PLAYER_1) { + dc.setColor(Gfx.COLOR_RED, Gfx.COLOR_BLACK); + } drawPlayerButton(dc, x, y, gameConfiguration.getPlayer1Name(), dataTracker.getPlayer1Score(), Gfx.TEXT_JUSTIFY_RIGHT); x = dc.getWidth() / 2 + HORIZONTAL_SPACING; + + if (dataTracker.getServingPlayer() == Player.PLAYER_2) { + dc.setColor(Gfx.COLOR_RED, Gfx.COLOR_BLACK); + } + drawPlayerButton(dc, x, y, gameConfiguration.getPlayer2Name(), dataTracker.getPlayer2Score(), Gfx.TEXT_JUSTIFY_LEFT); x = dc.getWidth() / 2 - HORIZONTAL_SPACING; // Size of label field @@ -138,9 +146,9 @@ class SquashView extends Ui.View { //! @param score Score to draw in the button //! @param justify Text justification (e.g. left, right) hidden function drawPlayerButton(dc, x, y, label, score, justify){ - dc.drawText(x, y, Gfx.FONT_TINY, label, justify); - y = y + dc.getFontHeight(Gfx.FONT_TINY) + VERTICAL_SPACING; - dc.drawText(x, y, Gfx.FONT_NUMBER_MILD, score, justify); + dc.drawText(x, y, Gfx.FONT_TINY, label, justify); + y = y + dc.getFontHeight(Gfx.FONT_TINY) + VERTICAL_SPACING; + dc.drawText(x, y, Gfx.FONT_NUMBER_MILD, score, justify); dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK); } From 590649ccc25aaf0231d124dba26e0e4ee75a6831 Mon Sep 17 00:00:00 2001 From: miss-architect Date: Sun, 5 Feb 2017 11:24:20 +0100 Subject: [PATCH 2/2] Added a confirmation dialog before quitting the App --- resources/strings/strings.xml | 2 ++ source/SquashDelegate.mc | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/resources/strings/strings.xml b/resources/strings/strings.xml index 09962d1..44401b7 100644 --- a/resources/strings/strings.xml +++ b/resources/strings/strings.xml @@ -5,4 +5,6 @@ Item 1 Item 2 + + Quit Squash App? diff --git a/source/SquashDelegate.mc b/source/SquashDelegate.mc index e010c82..43727f2 100644 --- a/source/SquashDelegate.mc +++ b/source/SquashDelegate.mc @@ -125,4 +125,29 @@ class SquashDelegate extends Ui.BehaviorDelegate { onPlayer1(); } } + + //! Event used when back button is pressed. + //! It shows a confirmation dialig before quitting the App + function onBack() { + Ui.pushView(new Confirmation(Ui.loadResource(Rez.Strings.confirm_exit)), + new ExitConfirmationDelegate(), Ui.SLIDE_IMMEDIATE); + return true; + } +} + +//! Delegate that handles the event from the Confirmation dialog +//! that appears before quitting the App. +class ExitConfirmationDelegate extends Ui.ConfirmationDelegate { + + function initialize() { + ConfirmationDelegate.initialize(); + } + + //! Event that happens on response of the user. + //! When the user replies YES, then the App exits. + function onResponse(response) { + if (response == CONFIRM_YES) { + System.exit(); + } + } } \ No newline at end of file