gui.qml 4.51 KB
import QtQuick 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.4

ApplicationWindow {
  id: root

  signal initNewHumanGame()
  signal initNewGameVsOrangeMonkeyAI()
  signal orangeMonkeyAIVsOrangeMonkeyAI()
  signal endGameAndQuit()
  signal boardClicked(int board_pos)

  signal displayFinalScores()

  onDisplayFinalScores: {
    final_score_window.visible = true

    console.debug("Player scores: " + gamemodel.playerOneScore + " vs. " + gamemodel.playerTwoScore)
  }

  visible: true

  width: 700
  height: 600

  menuBar: MenuBar {
        Menu {
          title: "Game"
          MenuItem {
            text: "New Game: Human Endeavour"
            onTriggered: initNewHumanGame()
          }
          MenuItem {
            text: "New Game: Human vs Orange Monkey"
            onTriggered: initNewGameVsOrangeMonkeyAI()
          }
          MenuItem {
            text: "New Game: Orange Monkey vs Orange Monkey"
            onTriggered: orangeMonkeyAIVsOrangeMonkeyAI()
          }
          MenuItem {
            text: "Quit"
            onTriggered: endGameAndQuit()
          }
        }
    }


  GridLayout {
    //anchors.fill: parent
    width: 600
    height: 600
    columns: 8
    rows: 8
    columnSpacing: 0.2
    rowSpacing: 0.2


    Repeater {
      model: VisualDataModel {
        model: gamemodel
        delegate: Rectangle {

            id: piece_rec
            property bool contains_mouse: false
            property string player_one_color: "orange"
            property string player_two_color: "blue"
            property string player_one_hover_color: "yellow"
            property string player_two_hover_color: "steelblue"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Rectangle {
               anchors.centerIn: parent
               width: Math.min(parent.width, parent.height) * 0.7
               height: width
               radius: width * 0.5

               color: {
                 if(occupied)
                   return playernr === 0 ? player_one_color : player_two_color
                 else if(occupied && contains_mouse)
                   return gamemodel.currentPlayer === 0 ? player_one_hover_color : player_two_color
                 else if(!occupied && contains_mouse)
                   return gamemodel.currentPlayer === 0 ? player_one_hover_color : player_two_color
                 else return piece_rec.color
               }
             }

//            color: {
//              if (occupied) {
//                if(playernr == 0) return player_one_color
//                else if(playernr == 1) return player_two_color
//                else return "black"
//              }
//              else if(contains_mouse) {
//                if(gamemodel.currentPlayer === 0) return player_one_hover_color
//                else if(gamemodel.currentPlayer  === 1) return player_two_hover_color
//                else return "black"
//              }
//              else return "white"
//            }

            border.color: "black"
            border.width: 1
            MouseArea {
              anchors.fill: parent
              onClicked: {
                console.debug("Piece nr: " + piecenr)
                boardClicked(piecenr)
              }

              hoverEnabled: true
              onContainsMouseChanged: piece_rec.contains_mouse = containsMouse

            }

              //...

        }
      }
    }
  }

  Rectangle {
    id: in_game_scores

    width: 100
    x: 600

    ColumnLayout {
      anchors.fill: parent
      anchors.margins: 10

      Item{ height: 20}
      Text{ text: "Current Score!" }
      Item{ height: 20}
      Text{ text: "Player one: " + gamemodel.playerOneScore}
      Text{ text: "Player two: " + gamemodel.playerTwoScore}
      //Item{ height: 20}
      //Text{ text: + gamemodel.winner "Player one: " + gamemodel.playerOneScore}
    }
  }

  Rectangle {
    id: final_score_window
    visible: false

    anchors.fill: parent
    anchors.margins: 30

    color: "red"
    opacity: 0.8

    ColumnLayout {
      anchors.fill: parent
      anchors.margins: 20

      Text{ Layout.preferredWidth: parent.width; height: 20;
            text: "Game Over!" }
      Item{ height: 20}
      Text{ Layout.preferredWidth: parent.width; height: 20;
            text: "Player one: " + gamemodel.playerOneScore}
      Text{ Layout.preferredWidth: parent.width; height: 20;
            text: "Player two: " + gamemodel.playerTwoScore}
      Item{ Layout.fillHeight: true}
      Button{ text: "Ok"; onClicked: final_score_window.visible = false }
    }

  }
}