// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Zone Button Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.propertyValue : object.zoneID

private alias object.value1 : object.zoneDetailsWidth
private alias object.value2 : object.disabled
private alias object.value3 : object.zoneIconFrame
private alias object.value4 : object.detailsFrameL
private alias object.value5 : object.detailsFrameM
private alias object.value6 : object.detailsFrameR
private alias object.value7 : object.zoneTextFrame
private alias object.value8 : object.buttonID

private alias 0 : ZONEBUTTON_SETUP
private alias 1 : ZONEBUTTON_ACTIVE
private alias 2 : ZONEBUTTON_NONE

// StageMenu aliases
private alias object.value3 : stageMenu.selectedIcon
private alias object.value4 : stageMenu.zoneTimer

private alias 4 : STAGEMENU_EXITZONES


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case ZONEBUTTON_SETUP
		object.zoneIconFrame = object.zoneID
		object.zoneIconFrame += 7
		object.zoneTextFrame = object.zoneID
		object.zoneTextFrame += 14
		object.inkEffect = INK_ALPHA
		object.zoneDetailsWidth = 64
		object.buttonID = object.entityPos
		object.buttonID -= 9
		object.alpha = 256
		object.state = ZONEBUTTON_ACTIVE
		break

	case ZONEBUTTON_ACTIVE
		if options.physicalControls == false
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult > -1
				temp0 = screen.xcenter
				temp0 += object.xpos
				temp0 -= 32
				temp1 = temp0
				temp1 += 64
				temp2 = object.ypos
				temp2 += 148
				temp2 -= 28
				temp3 = temp2
				temp3 += 56
				CheckTouchRect(temp0, temp2, temp1, temp3)
				if checkResult > -1
					stageMenu[1].selectedIcon = object.buttonID
				end if
			end if
		end if
		break

	case ZONEBUTTON_NONE
		break
		
	end switch
end event


event ObjectDraw
	if stageMenu[1].selectedIcon == object.buttonID
		temp0 = stageMenu[1].zoneTimer
		temp0 >>= 1
		temp0 &= 1
		if object[1].state < STAGEMENU_EXITZONES
			temp0 = true
		end if
	else
		temp0 = false
	end if

	if temp0 == true
		// Highlighted
		object.detailsFrameL = 3
		object.detailsFrameM = 4
		object.detailsFrameR = 5
		temp3 = 159
	else
		// Unhighlighted
		object.detailsFrameL = 0
		object.detailsFrameM = 1
		object.detailsFrameR = 2
		temp3 = 110
	end if

	temp0 = object.zoneDetailsWidth
	temp0 >>= 1
	temp0 -= 4
	FlipSign(temp0)
	temp0 += object.xpos
	temp0 += screen.xcenter
	temp1 = object.ypos
	temp1 += 148
	DrawSpriteScreenFX(object.detailsFrameL, FX_INK, temp0, temp1)

	temp2 = object.zoneDetailsWidth
	temp2 -= 8
	EditFrame(object.detailsFrameM, 0, -24, temp2, 48, 5, temp3)
	DrawSpriteScreenFX(object.detailsFrameM, FX_INK, temp0, temp1)

	temp0 += 7
	if object.disabled == false // In S1, stages are never disabled - the functionality is just leftover from copying from S2's version where HPZ and 2PVS stages would need to be disabled sometimes
		DrawSpriteScreenFX(6, FX_INK, temp0, temp1)
		DrawSpriteScreenFX(object.zoneIconFrame, FX_INK, temp0, temp1)
	else
		// If disabled, draw a red X
		// (Or at least, that's what it's meant to be)
		DrawSpriteScreenFX(7, FX_INK, temp0, temp1)
	end if

	temp0--
	temp0 += object.zoneDetailsWidth
	temp0 -= 14
	DrawSpriteScreenFX(object.detailsFrameR, FX_INK, temp0, temp1)
	if object.zoneDetailsWidth == 224
		temp0 -= object.zoneDetailsWidth
		temp0 += 60
		DrawSpriteScreenFX(object.zoneTextFrame, FX_INK, temp0, temp1)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	
	SpriteFrame(-4, -24, 4, 48, 1, 110)		// Details Border L Unhighlighted - #0
	SpriteFrame(0, -24, 40, 48, 5, 110)		// Details Border M Unhighlighted - #1
	SpriteFrame(0, -24, 4, 48, 221, 110)	// Details Border R Unhighlighted - #2
	
	SpriteFrame(-4, -24, 4, 48, 1, 159)		// Details Border L Highlighted - #3
	SpriteFrame(0, -24, 40, 48, 5, 159)		// Details Border M Highlighted - #4
	SpriteFrame(0, -24, 4, 48, 221, 159)	// Details Border R Highlighted - #5
	
	SpriteFrame(0, -17, 42, 34, 180, 1)		// Filler Blank Icon - #6
	SpriteFrame(0, -17, 42, 34, 180, 1)		// Filler Blank Icon (Was Red X in S2) - #7
	SpriteFrame(4, -13, 32, 24, 1, 85)		// GHZ Icon - #8
	SpriteFrame(4, -13, 32, 24, 34, 85)		// MZ Icon - #9
	SpriteFrame(4, -13, 32, 24, 67, 85)		// SYZ Icon - #10
	SpriteFrame(4, -13, 32, 24, 100, 85)	// LZ Icon - #11
	SpriteFrame(4, -13, 32, 24, 133, 85)	// SLZ Icon - #12
	SpriteFrame(4, -13, 32, 24, 166, 85)	// SBZ Icon - #13
	SpriteFrame(4, -13, 32, 24, 199, 85)	// FZ Icon - #14
	SpriteFrame(4, -3, 120, 7, 1, 223)  	// GHZ Text - #15
	SpriteFrame(4, -3, 88, 7, 122, 223) 	// MZ Text - #16
	SpriteFrame(4, -3, 128, 7, 1, 231) 		// SYZ Text - #17
	SpriteFrame(4, -3, 112, 7, 130, 231) 	// LZ Text - #18
	SpriteFrame(4, -3, 120, 7, 1, 239) 		// SLZ Text - #19
	SpriteFrame(4, -3, 128, 7, 122, 239) 	// SBZ Text - #20
	SpriteFrame(4, -3, 80, 7, 1, 247) 		// FZ Text - #21
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(8)
end event


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(4, -13, 32, 24, 1, 85) // GHZ Icon
	
	// Although used by the object, it's not to be set from the editor
	// Instead, it's set when this object is spawned by a Stage Menu object
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
