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

(class_function world_menu void create (group)
	(group

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

	# logo
	(this.logo.load (asset "assets/logo_rue.asset" DD_PLY))
	(this.logo.loadTexture (asset "assets/logo_rue_texture.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 3)
	(this.button[0].mesh.load (asset "assets/button_disabled.asset" DD_PLY))
	(this.button[0].setSize 1 1)
	(this.button[0].setProportionalPosition 0.25 0.4)
	(this.button[0].setProportionalWidth 0.2)

	(this.button[1].mesh.load (asset "assets/button.asset" DD_PLY))
	(this.button[1].setSize 1 1)
	(this.button[1].setProportionalPosition 0.5 0.4)
	(this.button[1].setProportionalWidth 0.2)

	(this.button[2].mesh.load (asset "assets/button.asset" DD_PLY))
	(this.button[2].setSize 1 1)
	(this.button[2].setProportionalPosition 0.75 0.4)
	(this.button[2].setProportionalWidth 0.2)

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

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

	(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"
		)
	)

	(= 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_menu void onload (group)
	(group
	)
)

(class_function world_menu void resize (group)
	(group
	(for (def int i 0) (< i this.buttonTotal) (= i (+ i 1))
		(this.button[i].resize)
	)
	(this.textButton.resize)
	)
)

(class_function world_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
	#(= this.selection -1)
	(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_menu void draw (group)
	(group

	# draw background
	(dd_matrix_push)
	(dd_translatef 0 -3 -3)
	(dd_rotatef (+ 40 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 logo
	(dd_matrix_push)
	(dd_translatef 0 1.7 -8)
	(dd_rotatef 10 1 0 0)
	(this.logo.draw)
	(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.1)
		(dd_scalef 0.2 0.2 0.2)

		(if (== i 0)
			(this.font.draw "challenges")
		(== i 1)
			(this.font.draw "custom game")
			(this.font.draw "exit")
		)

		(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 "not available in alpha version" limit)
		(== this.selection 1)
			(this.font.drawLimit "create and play a custom match" limit)
			(this.font.drawLimit "exit game" limit)
		)

		(dd_matrix_pop)
		)
	)

	# draw version number
	(dd_matrix_push)
	(this.font.setAlign DD_STRING3D_ALIGN_RIGHT)
	(dd_translatef
		(/ (dd_screen_width_get 5) 2)
		(+ (/ (dd_screen_height_get 5) -2) 0.15)
		-5
	)
	(dd_scalef 0.15 0.15 0.15)
	(this.font.draw "alpha version")
	(this.font.setAlign DD_STRING3D_ALIGN_CENTER)
	(dd_matrix_pop)

	)
)

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

(class_function world_menu void clean (group)
	(group
	)
)

(class_function world_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

			(if (== this.selectionClick 0)
				(group
				)
			(== this.selectionClick 1)
				(group
				(dd_world_prepare world_custom_game_menu 1.0)
				(dd_world_ready)
				(= this.isMovingToNewWorld 1)
				)
			(== this.selectionClick 2)
				(group
				(= dd_flag_exit 1) # exit game
				(= this.isMovingToNewWorld 1)
				)
			)

			)
		)

		(= this.selectionClick -1)

		)
	) # mouse button released

	)
) # mouse input
