Skip to content

Commit

Permalink
Merge pull request #10 from miss-architect/small-improvements
Browse files Browse the repository at this point in the history
Ready for version 1.2.2
  • Loading branch information
miss-architect authored Feb 5, 2017
2 parents 4914a63 + 590649c commit 98f6a35
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 39 deletions.
2 changes: 2 additions & 0 deletions resources/strings/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

<string id="menu_label_1">Item 1</string>
<string id="menu_label_2">Item 2</string>

<string id="confirm_exit">Quit Squash App?</string>
</strings>
15 changes: 14 additions & 1 deletion source/DataTracker.mc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class DataTracker {
hidden var session;
//! Sets tracker
hidden var setsTracker;
//! Contains who is serving
hidden var servingPlayer;

//! Constructor
function initialize() {
Expand All @@ -36,6 +38,7 @@ class DataTracker {
session.stop();
}
setsTracker = new SetsTracker();
servingPlayer = Player.NO_PLAYER;
}

//! Updates the calculated values taken from
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

}
10 changes: 10 additions & 0 deletions source/Player.mc
Original file line number Diff line number Diff line change
@@ -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
}

}
16 changes: 6 additions & 10 deletions source/SetsTracker.mc
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
Expand All @@ -48,21 +44,21 @@ 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));
}

//! 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
Expand All @@ -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];
Expand All @@ -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);
Expand Down
69 changes: 51 additions & 18 deletions source/SquashDelegate.mc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -69,52 +73,81 @@ 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();
}
}

//! 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();
}
}
}
28 changes: 18 additions & 10 deletions source/SquashView.mc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 98f6a35

Please sign in to comment.