(include "custom_game_menu.ddh")
(include "game.ddh")
(include "menu.ddh")

(def int numberOfPlayers)
(def int startingPlayer)
(def int guide)

(class_function world_custom_game_menu void create (group)
	(group

	(= this.isMovingToNewWorld 0)

	# background
	(= this.bg_rotation 0)
	(this.bg.load (asset "assets/stage_2.asset" DD_PLY))

	# font
	(this.font.setAlign DD_STRING3D_ALIGN_CENTER)
	(this.font.setColorFront 0.8 0.8 0.8)
	(this.font.setColorBack 0.3 0.3 0.3)

	# buttons
	(= this.buttonTotal 7)
	(this.button[0].mesh.load (asset "assets/button_minus.asset" DD_PLY))
	(this.button[0].setSize 1 1)
	(this.button[0].setProportionalPosition 0.50 0.8)
	(this.button[0].setProportionalWidth 0.2)
	(this.button[0].setProportionalSize 0.7)

	(this.button[1].mesh.load (asset "assets/button_plus.asset" DD_PLY))
	(this.button[1].setSize 1 1)
	(this.button[1].setProportionalPosition 0.8 0.8)
	(this.button[1].setProportionalWidth 0.2)
	(this.button[1].setProportionalSize 0.7)

	(this.button[2].mesh.load (asset "assets/button_minus.asset" DD_PLY))
	(this.button[2].setSize 1 1)
	(this.button[2].setProportionalPosition 0.5 0.575)
	(this.button[2].setProportionalWidth 0.2)
	(this.button[2].setProportionalSize 0.7)

	(this.button[3].mesh.load (asset "assets/button_plus.asset" DD_PLY))
	(this.button[3].setSize 1 1)
	(this.button[3].setProportionalPosition 0.8 0.575)
	(this.button[3].setProportionalWidth 0.2)
	(this.button[3].setProportionalSize 0.7)

	(this.button[4].mesh.load (asset "assets/button_toggle.asset" DD_PLY))
	(this.button[4].setSize 1 1)
	(this.button[4].setProportionalPosition 0.5 0.35)
	(this.button[4].setProportionalWidth 0.2)
	(this.button[4].setProportionalSize 0.7)

	(this.button[5].mesh.load (asset "assets/button.asset" DD_PLY))
	(this.button[5].setSize 1 1)
	(this.button[5].setProportionalPosition 0.12 0.15)
	(this.button[5].setProportionalWidth 0.2)

	(this.button[6].mesh.load (asset "assets/button.asset" DD_PLY))
	(this.button[6].setSize 1 1)
	(this.button[6].setProportionalPosition 0.88 0.15)
	(this.button[6].setProportionalWidth 0.2)

	# number of players
	(this.buttonPlayerNumber.setSize 1 1)
	(this.buttonPlayerNumber.setProportionalPosition 0.25 0.8)
	(this.buttonPlayerNumber.setProportionalWidth 0.2)
	(this.buttonPlayerNumber.setProportionalSize 1.0)

	(this.buttonPlayerNumberValue.setSize 1 1)
	(this.buttonPlayerNumberValue.setProportionalPosition 0.65 0.8)
	(this.buttonPlayerNumberValue.setProportionalWidth 0.2)
	(this.buttonPlayerNumberValue.setProportionalSize 1.0)

	# starting player
	(this.buttonStartingPlayer.setSize 1 1)
	(this.buttonStartingPlayer.setProportionalPosition 0.25 0.575)
	(this.buttonStartingPlayer.setProportionalWidth 0.2)
	(this.buttonStartingPlayer.setProportionalSize 1.0)

	(this.buttonStartingPlayerValue.setSize 1 1)
	(this.buttonStartingPlayerValue.setProportionalPosition 0.65 0.575)
	(this.buttonStartingPlayerValue.setProportionalWidth 0.2)
	(this.buttonStartingPlayerValue.setProportionalSize 1.0)

	# text display
	(this.textButton.setSize 1 1)
	(this.textButton.setProportionalPosition 0.5 0.15)
	(this.textButton.setProportionalWidth 0.15)

	(= this.selection 0)
	(= this.selectionClick -1)
	(= this.isMovingToNewWorld 0)

	(= numberOfPlayers 3)
	(= startingPlayer -1)
	(= guide 0)

	# shader for buttons
	(this.buttonProgram.setVertexShader
		(multistring
			"AVDL_IN vec4 position;\n"
			"AVDL_IN vec3 colour;\n"

			"uniform mat4 matrix;\n"
			"uniform vec3 tint;\n"

			"AVDL_OUT vec4 outColour;\n"

			"void main() {\n"
			"	vec4 pos = matrix *position;\n"
			"       gl_Position = pos;\n"
			"       outColour = vec4(colour.rgb *tint.rgb, 1.0);\n"
			"}\n"
		)
	)
	(this.buttonProgram.setFragmentShader
		(multistring
			"AVDL_IN vec4 outColour;\n"

			"void main() {\n"
			"       avdl_frag_color = outColour;\n"
			"}\n"
		)
	)

	# button colours
	(= this.buttonClickCol[0] 0.389)
	(= this.buttonClickCol[1] 0.055)
	(= this.buttonClickCol[2] 0.682)

	(= this.buttonActiveCol[0] 0.916)
	(= this.buttonActiveCol[1] 0.516)
	(= this.buttonActiveCol[2] 0.074)

	(= this.buttonInactiveCol[0] 0.694)
	(= this.buttonInactiveCol[1] 0.137)
	(= this.buttonInactiveCol[2] 0.137)

	)
)

(class_function world_custom_game_menu void onload (group)
	(group
	)
)

(class_function world_custom_game_menu void resize (group)
	(group
	(for (def int i 0) (< i this.buttonTotal) (= i (+ i 1))
		(this.button[i].resize)
	)
	(this.buttonPlayerNumber.resize)
	(this.buttonPlayerNumberValue.resize)
	(this.buttonStartingPlayer.resize)
	(this.buttonStartingPlayerValue.resize)
	(this.textButton.resize)
	)
)

(class_function world_custom_game_menu void update (group)
	(group

	# constant background rotation
	(= this.bg_rotation (+ this.bg_rotation 0.01))
	(if (>= this.bg_rotation 360)
		(= this.bg_rotation (- this.bg_rotation 360))
	)

	# check if mouse is hovering over a button
	(for (def int i 0) (< i this.buttonTotal) (= i (+ i 1))
		(group

		(if (this.button[i].hasMouseCollided)
			(group
			(= this.selection i)
			)
		)

		(this.button[i].update (== this.selection i) (== this.selectionClick i))

		)
	)

	)
) # update

(class_function world_custom_game_menu void draw (group)
	(group

	# draw background
	(dd_matrix_push)
	(dd_translatef 0 -2 -3)
	(dd_rotatef (+ -80 this.bg_rotation) 0 1 0)
	(dd_rotatef 25 -1 0 0)
	(this.bg.draw)
	(dd_matrix_pop)

	# clear depth
	(glClear GL_DEPTH_BUFFER_BIT)

	# draw number of players
	(dd_matrix_push)
	(this.buttonPlayerNumber.applyTransform)
	(this.buttonPlayerNumber.applyMatrixTransform)
	(dd_translatef 0 0 0.1)
	(dd_scalef 0.2 0.2 0.2)
	(this.font.draw "number of players")
	(dd_matrix_pop)

	(dd_matrix_push)
	(this.buttonPlayerNumberValue.applyTransform)
	(this.buttonPlayerNumberValue.applyMatrixTransform)
	(dd_translatef 0 0 0.1)
	(dd_scalef 0.2 0.2 0.2)
	(this.font.drawInt numberOfPlayers)
	(dd_matrix_pop)

	# draw starting player
	(dd_matrix_push)
	(this.buttonStartingPlayer.applyTransform)
	(this.buttonStartingPlayer.applyMatrixTransform)
	(dd_translatef 0 0 0.1)
	(dd_scalef 0.2 0.2 0.2)
	(this.font.draw "starting player")
	(dd_matrix_pop)

	(dd_matrix_push)
	(this.buttonStartingPlayerValue.applyTransform)
	(this.buttonStartingPlayerValue.applyMatrixTransform)
	(dd_translatef 0 0 0.1)
	(dd_scalef 0.2 0.2 0.2)
	(if (== startingPlayer -1)
		(this.font.draw "random")
	(== startingPlayer 0)
		(this.font.draw "you")
		(group
		(dd_translatef -0.7 0 0)
		(this.font.draw "AI")
		(dd_translatef 1.4 0 0)
		(this.font.drawInt startingPlayer)
		)
	)
	(dd_matrix_pop)

	# draw buttons
	(this.buttonProgram.useProgram)
	(def int colLoc)
	(= colLoc (avdl_getUniformLocation this.buttonProgram "tint"))
	(for (def int i 0) (< i this.buttonTotal) (= i (+ i 1))
		(group
		(dd_matrix_push)

		# change the shader colour when clicked
		(if (> this.button[i].idleCol 1.0)
			(avdl_setUniform3f colLoc
				(dd_math_ease_linear
					(- this.button[i].idleCol 1.0)
					this.buttonActiveCol[0] this.buttonClickCol[0]
				)
				(dd_math_ease_linear
					(- this.button[i].idleCol 1.0)
					this.buttonActiveCol[1] this.buttonClickCol[1]
				)
				(dd_math_ease_linear
					(- this.button[i].idleCol 1.0)
					this.buttonActiveCol[2] this.buttonClickCol[2]
				)
			)
		# change colour when selected
			(avdl_setUniform3f colLoc
				(dd_math_ease_linear this.button[i].idleCol this.buttonInactiveCol[0] this.buttonActiveCol[0])
				(dd_math_ease_linear this.button[i].idleCol this.buttonInactiveCol[1] this.buttonActiveCol[1])
				(dd_math_ease_linear this.button[i].idleCol this.buttonInactiveCol[2] this.buttonActiveCol[2])
			)
		)

		(this.button[i].applyTransform)
		(this.button[i].applyMatrixTransform)
		(this.button[i].drawRaw)

		(dd_translatef 0 0 0.15)
		(dd_scalef 0.2 0.2 0.2)

		(if (== i 0)
			(group)
		(== i 1)
			(group)
		(== i 2)
			(group)
		(== i 3)
			(group)
		(== i 4)
			(if (== guide 0)
				(this.font.draw "include guide")
				(this.font.draw "no guide")
			)
		(== i 5)
			(this.font.draw "back")
		(== i 6)
			(this.font.draw "start")
		)

		(dd_matrix_pop)
		)
	)
	(avdl_useProgram 0)

	# draw button description
	(if (>= this.selection 0)
		(group
		(dd_matrix_push)
		(this.textButton.applyTransform)
		(this.textButton.applyMatrixTransform)
		(dd_translatef 0 0 0.1)
		(dd_scalef 0.2 0.2 0.2)
		(def float limit (* (dd_screen_width_get this.textButton.z) 1.8))
		(if this.isMovingToNewWorld
			(this.font.drawLimit "loading" limit)
		(== this.selection 0)
			(this.font.drawLimit "decrease number of players" limit)
		(== this.selection 1)
			(this.font.drawLimit "increase number of players" limit)
		(|| (== this.selection 2) (== this.selection 3))
			(this.font.drawLimit "change starting player" limit)
		(== this.selection 4)
			(this.font.drawLimit "toggle guide" limit)
		(== this.selection 5)
			(this.font.drawLimit "back to main menu" limit)
		(== this.selection 6)
			(this.font.drawLimit "start the game" limit)
		)

		(dd_matrix_pop)
		)
	)

	)
)

(class_function world_custom_game_menu void key_input (group char key)
	(group
	(if (== key 27)
		(= dd_flag_exit 1) # exit game
	) # key input
	)
)

(class_function world_custom_game_menu void clean (group)
	(group
	)
)

(class_function world_custom_game_menu void mouse_input (group int button int type)
	(group

	# re-check selection, this allows the user to directly click on a button
	# in a touch screen device
	(for (def int i 0) (< i this.buttonTotal) (= i (+ i 1))
		(group

		(if (this.button[i].hasMouseCollided)
			(group
			(= this.selection i)
			)
		)

		)
	)

	# control the camera look at point when click and dragging
	(if (== type DD_INPUT_MOUSE_TYPE_PRESSED)
		(group
		(if (&& (>= this.selection 0) (this.button[this.selection].hasMouseCollided))
			(= this.selectionClick this.selection)
		)
		)
	(== type DD_INPUT_MOUSE_TYPE_RELEASED)
		(group

		# clicked on a button
		(if (&& (== this.selection this.selectionClick) (== this.isMovingToNewWorld 0)
			(>= this.selection 0) (this.button[this.selection].hasMouseCollided))
			(group

			# minus num of players
			(if (== this.selectionClick 0)
				(group
				(= numberOfPlayers (dd_math_max (- numberOfPlayers 1) 3))
				(= startingPlayer (dd_math_min startingPlayer (- numberOfPlayers 1)))
				)
			# plus num of players
			(== this.selectionClick 1)
				(group
				(= numberOfPlayers (dd_math_min (+ numberOfPlayers 1) 5))
				)
			# minus starting player
			(== this.selectionClick 2)
				(group
				(= startingPlayer (dd_math_max (- startingPlayer 1) -1))
				)
			# plus starting player
			(== this.selectionClick 3)
				(group
				(= startingPlayer (dd_math_min (+ startingPlayer 1) (- numberOfPlayers 1)))
				)
			# toggle guide
			(== this.selectionClick 4)
				(group
				(if (== guide 0)
					(= guide -1)
					(= guide 0)
				)
				)
			# back to main menu
			(== this.selectionClick 5)
				(group
				(dd_world_prepare world_menu 1.0)
				(dd_world_ready)
				(= this.isMovingToNewWorld 1)
				)
			# start game !
			(== this.selectionClick 6)
				(group
				(dd_world_prepare world_game 1.0)
				(dd_world_ready)
				(= this.isMovingToNewWorld 1)
				)
			)

			)
		)

		(= this.selectionClick -1)

		)
	) # mouse button released

	)
) # mouse input
