Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature main menu views #2

Merged
merged 17 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# mdbook compiled book
book

project/project/
.bloop/
project/metals.sbt
.vscode/
.metals/
.DS_Store
.idea/*
scaladoc/
Expand Down
Binary file added res/img/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/img/backgroundBlurred.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions scatan.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Scatan</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="root"></div>
Expand Down
31 changes: 21 additions & 10 deletions src/main/scala/scatan/Main.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
package scatan
import com.raquo.laminar.api.L.{*, given}
import scatan.example.controller.{AboutController, AboutControllerImpl, HomeController, HomeControllerImpl}
import scatan.example.model.CounterAppState
import scatan.example.view.{AboutView, HomeView, ScalaJSAboutView, ScalaJsHomeView}
import scatan.controllers.home.{HomeController, HomeControllerImpl}
import scatan.controllers.game.{SetUpController, SetUpControllerImpl}
import scatan.views.game.{SetUpView, ScalaJsSetUpView}
import scatan.views.home.{HomeView, ScalaJsHomeView}
import scatan.model.ApplicationState
import scatan.views.home.{AboutView, ScalaJSAboutView}
import scatan.controllers.home.{AboutController, AboutControllerImpl}
import scatan.mvc.lib.application.NavigableApplication
import scatan.mvc.lib.page.PageFactory
import scatan.mvc.lib.{Controller, Model, NavigableApplicationManager, ScalaJSView}

import scala.util.Random

// Route
enum Pages(val pageFactory: PageFactory[?, ?, CounterAppState]):
enum Pages(val pageFactory: PageFactory[?, ?, ApplicationState]):
case Home
extends Pages(
PageFactory[HomeController, HomeView, CounterAppState](
PageFactory[HomeController, HomeView, ApplicationState](
viewFactory = new ScalaJsHomeView(_, "root"),
controllerFactory = new HomeControllerImpl(_)
)
)
case Setup
extends Pages(
PageFactory[SetUpController, SetUpView, ApplicationState](
viewFactory = new ScalaJsSetUpView(_, "root"),
controllerFactory = new SetUpControllerImpl(_)
)
)
case About
extends Pages(
PageFactory[AboutController, AboutView, CounterAppState](
PageFactory[AboutController, AboutView, ApplicationState](
viewFactory = new ScalaJSAboutView(_, "root"),
controllerFactory = new AboutControllerImpl(_)
)
)

// Application
val CounterApplication: NavigableApplication[CounterAppState, Pages] = NavigableApplication[CounterAppState, Pages](
initialState = CounterAppState(0),
// App
val Application: NavigableApplication[ApplicationState, Pages] = NavigableApplication[ApplicationState, Pages](
initialState = ApplicationState(),
pagesFactories = Pages.values.map(p => p -> p.pageFactory).toMap
)

@main def main(): Unit =
NavigableApplicationManager.startApplication(CounterApplication, Pages.Home)
NavigableApplicationManager.startApplication(Application, Pages.Home)
27 changes: 27 additions & 0 deletions src/main/scala/scatan/controllers/game/SetUpController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scatan.controllers.game

import scatan.mvc.lib.Controller
import scatan.views.game.SetUpView
import scatan.mvc.lib.NavigableApplicationManager
import scatan.Pages
import scatan.model.ApplicationState

/** This is the controller for the setup page.
*/
trait SetUpController extends Controller:
/** This method is called when the user clicks on the back button.
*/
def goToHome(): Unit

/** This method is called when the user clicks on the start button.
*/
def goToPlay(): Unit

/** This is the implementation of the controller for the setup page.
* @param dependencies,
* the dependencies for the controller.
*/
class SetUpControllerImpl(dependencies: Controller.Requirements[SetUpView, ApplicationState]) extends SetUpController:
override def goToHome(): Unit = NavigableApplicationManager.navigateTo(Pages.Home)
// TODO: implement goToPlay
override def goToPlay(): Unit = NavigableApplicationManager.navigateTo(Pages.Home)
15 changes: 15 additions & 0 deletions src/main/scala/scatan/controllers/home/AboutController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scatan.controllers.home
import scatan.mvc.lib.Controller
import scatan.model.ApplicationState
import scatan.views.home.AboutView
import scatan.mvc.lib.NavigableApplicationManager
import scatan.Pages

trait AboutController extends Controller:
def goToHome(): Unit

class AboutControllerImpl(requirements: Controller.Requirements[AboutView, ApplicationState])
extends AboutController
with Controller.Dependencies(requirements):
override def goToHome(): Unit =
NavigableApplicationManager.navigateTo(Pages.Home)
30 changes: 30 additions & 0 deletions src/main/scala/scatan/controllers/home/HomeController.scala
luigi-borriello00 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scatan.controllers.home

import scatan.mvc.lib.Controller
import scatan.views.home.HomeView
import scatan.Pages
import scatan.mvc.lib.application.NavigableApplication
import scatan.mvc.lib.NavigableApplicationManager
import scatan.model.ApplicationState

/** This is the controller for the home page.
*/
trait HomeController extends Controller:
/** This method is called when the user clicks on the settings button.
*/
def goToSetup(): Unit

/** This method is called when the user clicks on the about button.
*/
def goToAbout(): Unit

/** This is the implementation of the controller for the home page.
* @param requirements,
* the requirements for the controller.
*/
class HomeControllerImpl(requirements: Controller.Requirements[HomeView, ApplicationState]) extends HomeController:
override def goToSetup(): Unit =
NavigableApplicationManager.navigateTo[Pages](Pages.Setup)

override def goToAbout(): Unit =
NavigableApplicationManager.navigateTo[Pages](Pages.About)
5 changes: 5 additions & 0 deletions src/main/scala/scatan/model/ApplicationModel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package scatan.model

import scatan.mvc.lib.Model

final case class ApplicationState() extends Model.State
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider having consistent names between files and their contents (i.e ApplicationModel.scala and ApplicationState)

Suggested change
final case class ApplicationState() extends Model.State
final case class ApplicationModel() extends Model.State

62 changes: 62 additions & 0 deletions src/main/scala/scatan/views/game/SetUpView.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package scatan.views.game

import scatan.controllers.game.SetUpController

import scatan.Pages
import scatan.mvc.lib.View
import scatan.controllers.game.SetUpController
import com.raquo.laminar.api.L.*
import scatan.mvc.lib.{ScalaJSView, View}
import scatan.Pages

/** This is the view for the setup page.
*/
trait SetUpView extends View

/** This is the view for the setup page.
*
* @param requirements,
* the requirements for the view
* @param container,
* the container for the view
*/
class ScalaJsSetUpView(requirements: View.Requirements[SetUpController], container: String)
extends SetUpView
with View.Dependencies(requirements)
with ScalaJSView(container):
val numberOfUsers: Int = 3

override def element: Element =
div(
cls := "setup-view",
// Title
div(
cls := "setup-title",
"Setup your game"
),
div(
cls := "setup-menu",
for i <- 1 to numberOfUsers
yield div(
cls := "setup-menu-textbox-container",
input(
cls := "setup-menu-textbox",
placeholder := "Player " + i
),
label(
cls := "setup-menu-label",
"Player" + i
)
),
button(
cls := "setup-menu-button",
onClick --> (_ => controller.goToHome()),
"Back"
),
button(
cls := "setup-menu-button",
onClick --> (_ => controller.goToPlay()),
"Start"
)
)
)
21 changes: 21 additions & 0 deletions src/main/scala/scatan/views/home/AboutView.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package scatan.views.home

import com.raquo.laminar.api.L.*
import scatan.mvc.lib.{ScalaJSView, View}
import scatan.controllers.home.AboutController

trait AboutView extends View

class ScalaJSAboutView(requirements: View.Requirements[AboutController], container: String)
extends AboutView
with View.Dependencies[AboutController](requirements)
with ScalaJSView(container):

override def element: Element = div(
h1("About"),
p("This is a ScalaJS view"),
button(
"Back",
onClick --> (_ => controller.goToHome())
)
)
47 changes: 47 additions & 0 deletions src/main/scala/scatan/views/home/HomeView.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package scatan.views.home

import scatan.mvc.lib.View
import scatan.controllers.home.HomeController
import com.raquo.laminar.api.L.*
import scatan.Pages

import scatan.mvc.lib.{ScalaJSView, View}

/** This is the view for the home page.
*/
trait HomeView extends View

/** This is the view for the home page.
*
* @param requirements,
* the requirements for the view
* @param container,
* the container for the view
*/
class ScalaJsHomeView(requirements: View.Requirements[HomeController], container: String)
extends HomeView
with View.Dependencies(requirements)
with ScalaJSView(container):

override def element: Element =
div(
cls := "home-view",
// Title
div(
cls := "home-title"
),
// Menu view with 3 buttons, play, settings and about, dispose them vertically
div(
cls := "home-menu",
button(
cls := "home-menu-button",
onClick --> (_ => controller.goToSetup()),
"Play"
),
button(
cls := "home-menu-button",
onClick --> (_ => controller.goToAbout()),
"About"
)
)
)
48 changes: 48 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
body {
/* Set the background image URL */
background-image: url('res/img/backgroundBlurred.jpg');

/* Set the background size to cover the entire body */
background-size: cover;

}
.home-view{
margin-top: 1em;
margin-bottom: 1em;
margin-left: auto;
margin-right: auto;
width: 80%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

.home-title{
background-position-x: center;
background-image: url("res/img/logo.png");
background-size: contain;
background-repeat: no-repeat;
width: 100%;
height: 20em;
}
/* Home
display all menu item vertically
*/
.home-menu{
margin-top: 1em;
margin-bottom: 1em;
display: flex;
width: 70%;
height: auto;
flex-direction: column;
gap: 1em;
}

.home-menu-button{
flex-grow: 1;
font-size: large;
font-weight: bold;
height: 3em;
}