LoadFile("DATA:GUI/ScreenUtil.lua")

-------------------------------------------------------
-- Constants
-------------------------------------------------------

if GetVersion() == "IOS" then
	CONTROL_PANEL_OFFSET = 10
	SCROLL_BAR_OFFSET = 15
	LIST_BOX_ACTIVATE_ON_UP = true
else
	CONTROL_PANEL_OFFSET = 0
	SCROLL_BAR_OFFSET = 0
	LIST_BOX_ACTIVATE_ON_UP = false
end

SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768

CONTROL_PANEL_HEIGHT = 210 - CONTROL_PANEL_OFFSET

MENU_HEIGHT = 1076/4
EDITOR_BUTTONS_HEIGHT = 50

miscellaneous =
{
  -- define the size of the screen.  (this was the old worldbounds stuff..)
  screen_bounds = {1024, 620}
}

WHITE = { r = 255, g = 255, b = 255, alpha = 255 }
BLACK = { r = 0, g = 0, b = 0, alpha = 255 }

-------------------------------------------------------
--
-- This file contains convenient widget templates that
-- other screens can base off of.
--
-- Each template creates a local table that is the
-- template, which is parameterized by a function call
-- that returns a copy of that table.
--
-- Client files would call the functions to create
-- their widget.
--
-------------------------------------------------------

-------------------------------------------------------
-- Sprite
-------------------------------------------------------

local Sprite =
{
	name = "",
	enabled = true,
	visible = true,
	hit_top_left = {0,0},
	hit_bottom_right = {0,0},
	sprite_ext =
		{
		},
}

function CreateSprite( n, alias, p)
	req( n, "name", "string" )
	req( alias, "alias", "string")
	opt( p, "position", "table")
	
	local sprite_table = inherit(Sprite)
	{
		name = n,
		position = p,
		sprite_ext =
		{
			sprite = alias,
		},
	}
	return sprite_table
	
end

-------------------------------------------------------
-- Image
------------------------------------------------------

local Image =
{
	name = "",
	enabled = true,
	visible = true,
	hit_top_left = {0,0},
	hit_bottom_right = {0,0},
}

function CreateImage( n, obj_or_anim, p, f )

	req( n, "name", "string" )
	req( obj_or_anim, "obj_or_anim", "string" )
	opt( p, "position", "table" )
	opt( f, "facing", "table" )

	local image = inherit( Image )
	{
		name = n,
		position = p,
	}
  
	--ask if it's an object or an animation. if it ends with .anim, it's an animation
	local is_anim = string.find( obj_or_anim, ".anim" ) --or string.find( obj_or_anim, ".png" )

	if is_anim then
		image.anim_ext = 
		{
			animation = obj_or_anim,
			facing = f
		}
	else
		image.object_ext =
		{
			hit_region = false,
			object = obj_or_anim,
			mouse_events = false
		}
	end

	return image

end

-------------------------------------------------------
-- A group widget
------------------------------------------------------

local Group =
{
  name = "",
  enabled = true,
  visible = true,
  
  parent_ext =
  {
    children = {},
  },
}

function CreateGroup( n, c, w, h, p )

  req( n, "name", "string" )
  req( c, "children", "table" )
  req( w, "width", "number" )
  req( h, "height", "number" )
  req( p, "position", "table" )

  local group = inherit( Group )
  {
    name = n,
    position = p,
    width = w,
    height = h,
    parent_ext =
    {
      children = c
    },
  }
  
  return group

end

------------------------------------------------------
-- Standard one-click push button.
-------------------------------------------------------

local PushButton =
{
  name = "",
  enabled = true,
  visible = true,

  button_ext =
  {
    num_modes = 1,
    cursor = "",
	autofocusing = false,
	over_growth = 1.0,
  },
  sound_ext =
  {
    enter = { patch = "eets2/GUI/mouseOver" },
    click = { patch = "eets2/GUI/click" },
  }
}

function CreatePushButton( n, obj_or_anim, p, og )

	req( n, "name", "string" )
	req( obj_or_anim, "object_name", "string" )
	opt( p, "position", "table" )
	opt( og, "overgrowth", "number" )
	
	local button = inherit( PushButton )
	{
		name = n,
		position = p,
	}
  
	--
	if( obj_or_anim ) then
		
		--ask if it's an object or an animation. if it ends with .anim, it's an animation
		local is_anim = string.find( obj_or_anim, ".anim" )

		if is_anim then
			button.anim_ext = 
			{
				animation = obj_or_anim,
				do_not_animate = true
			}
		else
			button.object_ext =
			{
				hit_region = true,
				object = obj_or_anim
			}
		end
	end
	
	if og then
		button.button_ext.over_growth = og
	end
	
	return button

end

function CreateRadioButton(n, anim_enabled, anim_disabled, p)

	req( n, "name", "string" )
	req( anim_enabled, "anim_enabled", "string" )
	req( anim_disabled, "anim_disabled", "string" )
	opt( p, "position", "table" )
	
	local button = inherit( PushButton )
	{
		name = n,
		position = p,
	}
	
	--local is_anim = string.find( anim_enabled, ".anim" )
	--is_anim = is_anim and string.find( anim_disabled, ".anim" )
	--assert(is_anim)
	
	button.anim_ext = 
	{
		animation = anim_enabled,
		animation_disabled = anim_disabled,
	}
	
	return button
	
end

function CreateCheckBox( n, obj, p )

  local button = CreatePushButton( n, obj, p )
  button.button_ext.num_modes = 2
    
  return button
    
end
  
function SetHitRegion( widget, v1, v2 )

  widget.hit_top_left = v1
  widget.hit_bottom_right = v2
  if( widget.object_ext ) then
    widget.object_ext.hit_region = false
  end
  
end

------------------------------------------------------
-- A draggable scroller button
-------------------------------------------------------

local ScrollerButton =
{
  name = "",
  enabled = true,
  visible = true,
  
  button_ext =
  {
    num_modes = 1,
    cursor = "",
	autofocusing = false,
	over_growth = 1.0,
	under_growth = 1.0,
  },
  drag_ext =
  {
  },
  sound_ext =
  {
    enter = { patch = "eets2/GUI/mouseOver" },
    click = { patch = "eets2/GUI/pickup" },
  }
}

function CreateScrollerButton( n, obj_or_anim, og )

	req( n, "name", "string" )
	req( obj_or_anim, "obj_or_anim", "string" )
	opt( og, "overgrowth", "number" )
	
	local button = inherit( ScrollerButton )
	{
		name = n,
	}
	
	--ask if it's an object or an animation. if it ends with .anim, it's an animation
	local is_anim = string.find( obj_or_anim, ".anim" ) --or string.find( obj_or_anim, ".png" )

	if is_anim then
		button.anim_ext = 
		{
			animation = obj_or_anim,
			disable_collision_mask = false,
			do_not_animate = true,
		}
	else
		button.object_ext =
		{
			hit_region = true,
			object = obj_or_anim,
			mouse_events = true,
		}
	end
	
	if og then
		button.button_ext.over_growth = og
	end
	
	return button

end

-----------------------------------------------------
-- Radio group
------------------------------------------------------

local RadioGroup =
{
  name = "",
  enabled = true,
  visible = true,
  
  parent_ext =
  {
    children =
    {
    },
  },
  radio_ext =
  {
    items =
    {
    },
  },
}

function CreateRadioGroup( n, children, p )

  req( n, "name", "string" )
  req( children, "children", "table" )
  opt( p, "position", "table" )

  local radioGroup = inherit(RadioGroup)
  {
    name = n,
    position = p
  }
  
  tconcat( radioGroup.parent_ext.children, children )
  for i,v in ipairs( children ) do
    table.insert( radioGroup.radio_ext.items, v.name )
  end

  return radioGroup
  
end

------------------------------------------------------
-- Clickable Static Text
-------------------------------------------------------

local ClickableText =
{
  name = "",
  enabled = true,
  visible = true,
  
  text_ext =
  {
    can_edit = false,
    autowrap = false,
	hover_alpha = 200,
	font = "default",
  },
  button_ext =
  {
    num_modes = 1,
    cursor = "",
	autofocusing = false,
  },
  sound_ext =
  {
    enter = { patch = "eets2/GUI/mouseOver" },
    click = { patch = "eets2/GUI/click" },
  }
}

function CreateClickableText( n, t, p, fontstyle )

  req( n, "name", "string" )
  req( t, "text", "string" )
  opt( p, "position", "table" )
  opt( fontstyle, "fontstyle", "string" )

  local label = inherit( ClickableText )
  {
    name = n,
    position = p,
    text_ext =
    {
      text = t,
      style = fontstyle,
    },
  }
  
  return label

end

------------------------------------------------------
-- Static edit box
-------------------------------------------------------

local StaticText =
{
  name = "",
  enabled = true,
  visible = true,
  
  text_ext =
  {
    can_edit = false,
    autowrap = false,
	font = "default",
  },
}

function CreateStaticText( n, t, p, fontname, fontstyle, f )

  req( n, "name", "string" )
  req( t, "text", "string" )
  opt( p, "position", "table" )
  opt( fontname, "fontname", "string" )
  opt( fontstyle, "fontstyle", "string" )

  local label = inherit( StaticText )
  {
    name = n,
    position = p,
    text_ext =
    {
      text = t,
      style = fontstyle,
	  font = fontname,
	  facing = f
    },
  }
  
  return label

end

------------------------------------------------------
-- Editable edit box.
-------------------------------------------------------

local EditText =
{
  name = "",
  enabled = true,
  visible = true,
  
  text_ext =
  {
    can_edit = true,
    autowrap = false,
	max_length = 0,
	font = "unicode",
  },
}

function CreateEditText( n, t, p, ml)

  req( n, "name", "string" )
  req( t, "text", "string" )
  opt( p, "position", "table" )
  opt( ml, "max_length", "number" )

  local editLabel = inherit( EditText )
  {
    name = n,
    position = p,
    text_ext =
    {
      text = t,
	  max_length = ml,
    },
  }
  
  return editLabel

end

-------------------------------------------------------
-- Scroll bar template
-------------------------------------------------------

local ScrollBar =
{
  name = "",
  enabled = true,
  visible = true,
  
  scrollbar_ext =
  {
    range_start = 0,
    range_end = 100,
    -- up_button = "ScrollBarUpButton",
    -- down_button = "ScrollBarDownButton",
    -- scroller = "ScrollBarScroller",
  },
  parent_ext =
  {
    children =
    {
    },
  },
}

function CreateScrollBar( n, upButton, downButton, scroller, p )

  req( n, "name", "string" )
  opt( upButton, "upButtonName", "table" )
  opt( downButton, "downButtonName", "table" )
  opt( scroller, "scrollerName", "table" )
  opt( p, "position", "table" )

  local scrollBar = inherit( ScrollBar )
  {
    name = n,
    position = p,
  }
  
  if upButton then
    scrollBar.scrollbar_ext.up_button = upButton.name
    table.insert( scrollBar.parent_ext.children, upButton )
  end
  if downButton then
    scrollBar.scrollbar_ext.down_button = downButton.name
    table.insert( scrollBar.parent_ext.children, downButton )
  end
  if scroller then
    scrollBar.scrollbar_ext.scroller = scroller.name
    table.insert( scrollBar.parent_ext.children, scroller )
  end

  return scrollBar
end

-------------------------------------------------------
-- ListBox template
-------------------------------------------------------

local ListBox =
{
  name = "",
  enabled = true,
  visible = true,
  width = 100,
  height = 100,
  
  listbox_ext =
  {
    items =
    {
    }
  },
  parent_ext =
  {
    children =
    {
    },
  },
}

function CreateListBox( n, w, h, bar, p )

  req( n, "name", "string" )
  req( w, "width", "number" )
  opt( h, "height", "number" )
  opt( bar, "scrollbar", "table" )
  opt( p, "position", "table" )

  local listBox = inherit( ListBox )
  {
    name = n,
    width = w,
    height = h,
    position = p,
    listbox_ext =
    {
      scrollbar = bar.name,
    }
  }
  
  if( bar ) then
    table.insert( listBox.parent_ext.children, bar )
  end
  
  return listBox

end

function AddListBoxMask( listBox, mask )

	req( listBox, "listBox", "table" )
	req( mask, "mask", "table" )
	
	table.insert( listBox.parent_ext.children, mask )
	if listBox.listbox_ext.masks then
		table.insert( listBox.listbox_ext.masks, mask.name )
	else
		listBox.listbox_ext.masks = { mask.name }
	end

end

-------------------------------------------------------
-- Radial menu widget template
-------------------------------------------------------

local Radial =
{
  name = "",
  enabled = true,
  visible = false,
  
  parent_ext =
  {
    children =
    {
    },
  },
  radial_ext =
  {
    radius = 70,
    items =
    {
    },
  },
}

function CreateRadial( n, children, obj, p )

  req( n, "name", "string" )
  req( children, "children", "table" )
  opt( obj, "object", "string" )
  opt( p, "position", "table" )

  local radial = inherit(Radial)
  {
    name = n,
    position = p
  }
  
  tconcat( radial.parent_ext.children, children )
  for i,v in ipairs( children ) do
    table.insert( radial.radial_ext.items, v.name )
  end
  --
  if( obj ) then
    --
    radial.object_ext =
    {
      object = obj,
    }
  end

  return radial
  
end

-------------------------------------------------------
-- Modal dialog template
-------------------------------------------------------

local ModalDialog =
{
	name = "",
	enabled = true,
	visible = false,      -- dialogs invisible until called upon
	position = { 0, 0 },
	modal_ext =
	{
		start_sound = "Popup",
		fade_colour = { r = 0, g = 0, b = 0, alpha = 0 },
	},
	parent_ext =
	{
		children =
		{
		},
	}, 
}

function CreateModalDialog( n, children, obj_or_anim, p )

	req( n, "name", "string" )
	req( children, "children", "table" )
	opt( obj_or_anim, "object_or_anim", "string" )
	opt( p, "position", "table" )
	
	local dialog = inherit(ModalDialog)
	{
		name = n,
		position = p
	}
  
	tconcat( dialog.parent_ext.children, children )
	--
	if( obj_or_anim ) then
		
		--ask if it's an object or an animation. if it ends with .anim, it's an animation
		local is_anim = string.find( obj_or_anim, ".anim" )

		if is_anim then
			dialog.anim_ext = 
			{
				animation = obj_or_anim
			}
		else
			dialog.object_ext =
			{
				object = obj_or_anim
			}
		end
	end
	
	return dialog
  
end

function CreateAutoModalDialog( n, children, obj, p )

	local dialog = CreateModalDialog( n, children, obj, p )
	
	dialog.button_ext =
	{
		num_modes = 1,
		cursor = "",
		autofocusing = false,
		over_growth = 1.0
	}
	if dialog.object_ext then
		dialog.object_ext.hit_region = false
	end
	
	return dialog
	
end


---------------------------------------------------------
-- iOS style badge counter
----------------------------------------------------------

local Badge =
{
	name = "",
	enabled = false,
	visible = true,
	position = { 0, 0 },
	text_ext =
	{
		offset = { 0, 1.5 },
		colour = WHITE,
		can_edit = false,
		autowrap = false,
		text = "",
		style = "centre centy",
		font = "small_digit",
	},
}

function CreateBadge(n, obj_or_anim, p, f)

	req(n, "name", "string")
	req(obj_or_anim, "obj_or_anim", "string")
	opt(p, "position", "table")
	opt(f, "facing", "table")
	
	local badge = inherit(Badge)
	{
		name = n,
		position = p,
		text_ext =
		{
			facing = f,
		},
	}
	
	--ask if it's an object or an animation. if it ends with .anim, it's an animation
	local is_anim = string.find(obj_or_anim, ".anim") --or string.find(obj_or_anim, ".png")

	if is_anim then
		badge.anim_ext = 
		{
			animation = obj_or_anim,
			facing = f,
			do_not_animate = true,
		}
	else
		image.object_ext =
		{
			hit_region = false,
			object = obj_or_anim,
			mouse_events = false,
		}
	end
	
	return badge
	
end

function SetTooltip(widget, tooltipWidget, offs, del)
	req(widget, "widget", "table")
	req(tooltipWidget, "tooltipWidget", "table")
	opt(offs, "offset", "table")
	opt(del, "delay", "number")
	
	if del == nil then
		del = 0.3
	end
	
	widget.tooltip_ext =
	{
		widget = tooltipWidget,
		offset = offs,
		delay = del,
		padding = { 10, 10 },
		colour = { r = 51, g = 0, b = 102, alpha = 160 },
	}
end

function SetButtonText(button, buttonText)
	button.text_ext =
	{
		text = buttonText,
		font = "button",
		style = "centre centy",
		colour = { r = 230, g = 230, b = 230, alpha = 255 },
		hover_colour = WHITE,
		dropshadow_distance = 2,
		offset = { 0, 0 },
	}
end
