Skip to content

Commit

Permalink
Merge branch 'Set-number-of-holes'
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Tellevik committed Oct 19, 2020
2 parents eb8b627 + 3045b97 commit f02c358
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 29 deletions.
1 change: 1 addition & 0 deletions resources/Menus/MainMenu.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<menu id="MainMenu" title="@Strings.MainMenuTitle">
<menu-item id="selectHole" label="@Strings.MenuSelectHole"></menu-item>
<menu-item id="changePar" label="@Strings.MenuPar"></menu-item>
<menu-item id="numberOfHoles" label="@Strings.NumberOfHoles"></menu-item>
<menu-item id="newGame" label="@Strings.MenuNewGame"></menu-item>
<menu-item id="scorecard" label="@Strings.MenuScorecard"></menu-item>
</menu>
4 changes: 4 additions & 0 deletions resources/strings/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<string id="MenuPar">Edit Par</string>
<string id="MenuNewGame">New game</string>
<string id="MenuScorecard">Scorecard</string>
<string id="NumberOfHoles">Number of holes</string>

<!-- Titles -->
<string id="numberOfHolesPickerTitle">Number of holes</string>
</strings>
5 changes: 4 additions & 1 deletion source/Delegate/MainMenuDelegate.mc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class MainMenuDelegate extends Ui.MenuInputDelegate{
}else if(item == :scorecard){
var view = new ScoreCardView(mController);
Ui.switchToView(view, new ScoreCardDelegate(view, mController), Ui.SLIDE_UP);
}else if(item == :numberOfHoles){
var picker = new SelectNumberOfHolesView(mController);
Ui.pushView(picker, new SelectNumberOfHolesDelegate(picker, mController), Ui.SLIDE_UP);
}

if(item == :changePar){
mController.editPar = true;
}
Expand Down
6 changes: 3 additions & 3 deletions source/Delegate/ScoreCardDelegate.mc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ class ScoreCardDelegate extends Ui.BehaviorDelegate{

switch(key){
case Ui.KEY_UP:
relatedView.shiftScoreCard();
relatedView.previousPage();
break;
case Ui.KEY_DOWN:
relatedView.shiftScoreCard();
relatedView.nextPage();
break;
}
relatedView.requestUpdate();
return false;
}

function onTap(clickEvent) {
relatedView.shiftScoreCard();
relatedView.nextPage();
relatedView.requestUpdate();
return false;
}
Expand Down
70 changes: 70 additions & 0 deletions source/Delegate/SelectNumberOfHolesDelegate.mc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Toybox.WatchUi as Ui;

class SelectNumberOfHolesDelegate extends Ui.BehaviorDelegate{

hidden var mController;
hidden var relatedView;

function initialize(view, controller) {
BehaviorDelegate.initialize();
relatedView = view;
mController = controller;
}

function onCancel() {
WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
}

function onKey(keyEvent) {
var key = keyEvent.getKey();

switch(key){
case Ui.KEY_UP:
relatedView.addHole();
break;
case Ui.KEY_DOWN:
relatedView.removeHole();
break;
case Ui.KEY_ENTER:
relatedView.setHole();
Ui.popView(Ui.SLIDE_IMMEDIATE);
return false;
break;
}
relatedView.requestUpdate();
return false;
}

function onTap(keyEvent){

if(tapUp(keyEvent)){
relatedView.addHole();
}
if(tapDown(keyEvent)){
relatedView.removeHole();
}
relatedView.requestUpdate();

return false;
}

hidden function tapDown(keyEvent) {
var coordinates = keyEvent.getCoordinates();
var gui = relatedView.gui;
var tapDown = gui.tapBoxDown;

return coordinates[0] > tapDown[0] && coordinates[0] < tapDown[0] + gui.TAP_BOX_WIDTH
&& coordinates[1] > tapDown[1] && coordinates[1] < tapDown[1] + gui.TAP_BOX_HEIGHT;

}

hidden function tapUp(keyEvent) {
var coordinates = keyEvent.getCoordinates();
var gui = relatedView.gui;
var tapUp = gui.tapBoxUp;

return coordinates[0] > tapUp[0] && coordinates[0] < tapUp[0] + gui.TAP_BOX_WIDTH
&& coordinates[1] > tapUp[1] && coordinates[1] < tapUp[1] + gui.TAP_BOX_HEIGHT;
}

}
11 changes: 9 additions & 2 deletions source/Gui/Gui.mc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ module Gui {
/** TEXT JUSTIFYCATION **/
const CENTER_TEXT = Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER;





/** Arrows **/
const UP_ARROW = [[0,20], [30,20], [15,0]];
const DOWN_ARROW = [[0,0], [30,0], [15,20]];


const TAP_BOX_HEIGHT = 50;
const TAP_BOX_WIDTH = 50;

/** COLORS **/
const BLACK = Gfx.COLOR_BLACK;
Expand Down
9 changes: 3 additions & 6 deletions source/Gui/MainGui.mc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ module Gui{

class MainGui extends CommonGui{

const UP_ARROW = [[0,20], [30,20], [15,0]];
const DOWN_ARROW = [[0,0], [30,0], [15,20]];


const TAP_BOX_HEIGHT = 50;
const TAP_BOX_WIDTH = 50;

var tapBoxUp;
var tapBoxDown;
Expand Down Expand Up @@ -119,8 +116,8 @@ module Gui{
editParValue(MEDIUM_FONT);
}else if(version == Vivoactive){
editParValue(SMALL_FONT);
}else if(version == Forerunner645){
editParValue(TINY_FONT|| version == Big);
}else if(version == Forerunner645 || version == Big){
editParValue(TINY_FONT);
}

}else{
Expand Down
34 changes: 28 additions & 6 deletions source/Gui/ScoreCardGui.mc
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,39 @@ module Gui{
}


function update(front) {
function update(page) {
setColor();
drawTable();
if(front){
drawScoreFront9();
}else{
drawScoreBack9();
}
drawScore(page);

}

hidden function drawScore(page){
var startHole = 9*page - 8;
var lastHole = 9*page;
var holes = mController.getGame().getCourse().getHoles();
if(holes.size() < lastHole){
lastHole = holes.size();
}
var title = "Hole " + startHole + "-" + lastHole;
titleText(title);

var x = left + (columnWidth/2);



for(var i = startHole; i <= lastHole; i++){
drawTopLine(i, x);
x += columnWidth;
}

x = left + (columnWidth/2);
for(var i = startHole-1; i < lastHole; i++){
color(holes[i], x);
drawBottomLine(throws(holes[i]), x);
x += columnWidth;
}
}

hidden function drawTable() {

Expand Down
84 changes: 84 additions & 0 deletions source/Gui/SelectNumberOfHoles.mc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

module Gui{
class SelectNumberOfHoles extends CommonGui{

var tapBoxUp;
var tapBoxDown;

hidden var numberPos;
hidden var arrowPos;
hidden var numberOfHoles;

function initialize(dc, controller){
CommonGui.initialize(dc, controller);
numberPos = [ width/2, height/2 ];

tapBoxUp = [numberPos[0] - 25, numberPos[1] - 75];
tapBoxDown = [numberPos[0] - 25, numberPos[1] + 25];

numberOfHoles = controller.getGame().getCourse().getNumberOfHoles();
}


function setNumberOfHoles(n){
numberOfHoles = n;
}

function getNumberOfHoles(){
return numberOfHoles;
}

function update(){
setColor();
updateNumber();
}

hidden function updateNumber(){
drawArrows();
drawNumber();
}

hidden function drawNumber(){
drawText(numberOfHoles,numberPos, LARGE_FONT, CENTER_TEXT);
}

hidden function drawArrows() {
var up = [UP_ARROW[0], UP_ARROW[1], UP_ARROW[2]];
var down = [DOWN_ARROW[0], DOWN_ARROW[1], DOWN_ARROW[2]];

var pos = [numberPos[0] - 15, numberPos[1] - 10];

setArrowPos(up, down, pos);

mDc.fillPolygon(up);
mDc.fillPolygon(down);

resetArrowPos(up, down, pos);
}

hidden function setArrowPos(up, down, pos) {
for(var i = 0; i < up.size(); i++){
up[i][0] += pos[0];
up[i][1] += pos[1] - 50;
}

for(var i = 0; i < down.size(); i++){
down[i][0] += pos[0];
down[i][1] += pos[1] + 50;
}
}

hidden function resetArrowPos(up, down, pos) {
for(var i = 0; i < up.size(); i++){
up[i][0] -= pos[0];
up[i][1] -= pos[1] - 50;
}

for(var i = 0; i < down.size(); i++){
down[i][0] -= pos[0];
down[i][1] -= pos[1] + 50;
}
}

}
}
10 changes: 9 additions & 1 deletion source/Model/Controller.mc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class Controller{
}

}


function addHole(){

}

function parValue() {
return mGame.getActiveHole().getPar().toString();
}
Expand Down Expand Up @@ -140,4 +144,8 @@ class Controller{
}
mGame.selectHole(0);
}

function createNewCourse(numberOfHoles){
mGame.newCourse(numberOfHoles);
}
}
41 changes: 35 additions & 6 deletions source/Model/Course.mc
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
class Course{

hidden var mHoles;
hidden var numberOfHoles;

function initialize(state, par){
if(state == null){
createNewCourse(par);
setUpCourse(par);
}else{
lastCourse(state, par);
}

}


function getNumberOfHoles(){
return numberOfHoles;
}

function lastCourse(state, par) {
mHoles = new [state.size()];
numberOfHoles = mHoles.size();
for(var i = 0; i < mHoles.size(); i++){
var hole = new Hole(i);
if(state[i] != 0){
Expand All @@ -29,12 +35,35 @@ class Course{
}
}

function createNewCourse(par) {
mHoles = new [18];
hidden function setUpCourse(par) {
if(par == null){
mHoles = new [18];
numberOfHoles = mHoles.size();
for(var i = 0; i < mHoles.size(); i++){
mHoles[i] = new Hole(i);
}
}else{
numberOfHoles = par.size();
mHoles = new [numberOfHoles];

for(var i = 0; i < numberOfHoles; i++){
mHoles[i] = new Hole(i);
mHoles[i].setPar(par[i]);
}
}


}

function createCourse(holes){
var keepScore = mHoles;
numberOfHoles = holes;
mHoles = new [numberOfHoles];
for(var i = 0; i < mHoles.size(); i++){
mHoles[i] = new Hole(i);
if(par != null){
mHoles[i].setPar(par[i]);
if(keepScore != null && keepScore.size() > i && keepScore[i].isRegistered()){
mHoles[i].setThrows(keepScore[i].getThrows());
mHoles[i].setPar(keepScore[i].getPar());
}
}
}
Expand Down
Loading

0 comments on commit f02c358

Please sign in to comment.