Skip to content

Commit

Permalink
feat(setup-view): make it reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-borriello00 committed Sep 20, 2023
1 parent 2a0b863 commit 5dd3f52
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/main/scala/scatan/controllers/game/SetUpController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import scatan.lib.mvc.Controller

/** This is the controller for the setup page.
*/
trait SetUpController extends Controller
trait SetUpController extends Controller:
def startGame(usernames: String*): Unit

object SetUpController:
def apply(requirements: Controller.Requirements[SetUpView, ApplicationState]): SetUpController =
Expand All @@ -21,4 +22,8 @@ object SetUpController:
*/
private class SetUpControllerImpl(requirements: Controller.Requirements[SetUpView, ApplicationState])
extends BaseController(requirements)
with SetUpController
with SetUpController:

override def startGame(usernames: String*): Unit =
this.model.update(_.createGame(usernames*))
print("start game")
69 changes: 47 additions & 22 deletions src/main/scala/scatan/views/game/SetUpView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@ private class ScalaJsSetUpView(container: String, requirements: View.Requirement
extends BaseScalaJSView(container, requirements)
with SetUpView:

val numberOfUsers: Int = 3
val numberOfUsers: Var[Int] = Var(3)
val reactiveNumberOfUsers: Signal[Int] = numberOfUsers.signal

private def validateNames(usernames: String*) =
usernames.forall(_.matches(".*\\S.*"))

@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
override def switchToGame(): Unit =
val usernames =
for i <- 1 to numberOfUsers
for i <- 1 to numberOfUsers.now()
yield document
.getElementsByClassName("setup-menu-textbox")
.item(i - 1)
.asInstanceOf[org.scalajs.dom.raw.HTMLInputElement]
.value
this.navigateTo(Pages.Game)
if validateNames(usernames*) then
println(usernames)
// this.controller.startGame(usernames*)
print(this.controller)
this.navigateTo(Pages.Game)

override def switchToHome(): Unit =
this.navigateTo(Pages.Home)
Expand All @@ -58,27 +66,44 @@ private class ScalaJsSetUpView(container: String, requirements: View.Requirement
),
div(
cls := "setup-menu",
for i <- 1 to numberOfUsers
yield div(
cls := "setup-menu-textbox-container",
input(
cls := "setup-menu-textbox",
placeholder := "Player " + i
// combobox for choose 3 or 4 players
select(
cls := "setup-menu-combobox",
onChange.mapToValue.map(_.toInt) --> numberOfUsers,
option(
cls := "setup-menu-combobox-option",
value := "3",
"3 Players"
// on select, change the number of users
),
label(
cls := "setup-menu-label",
"Player" + i
option(
cls := "setup-menu-combobox-option",
value := "4",
"4 Players"
)
)
),
div(
cls := "setup-menu",
children <-- reactiveNumberOfUsers.map(element =>
for i <- 1 to element
yield div(
cls := "setup-menu-textbox-container",
input(
cls := "setup-menu-textbox",
placeholder := s"Player $i"
)
)
),
button(
cls := "setup-menu-button",
onClick --> (_ => this.switchToHome()),
"Back"
),
button(
cls := "setup-menu-button",
onClick --> (_ => this.switchToGame()),
"Start"
)
),
button(
cls := "setup-menu-button",
onClick --> (_ => this.switchToHome()),
"Back"
),
button(
cls := "setup-menu-button",
onClick --> (_ => this.switchToGame()),
"Start"
)
)

0 comments on commit 5dd3f52

Please sign in to comment.