dofile(LockOn_Options.common_script_path.."elements_defs.lua")



gettext = require("i_18n")

_ = gettext.translate


materials = {}
materials["RED"]		= {255, 0, 0, 255}
materials["YELLOW"]		= {255, 255, 0, 255}
materials["GREEN"]		= {0, 255, 0, 255}
materials["BLUE"]		= {0, 0, 255, 255}
materials["PURPLE"]		= {255, 0, 255, 255}
materials["GREY"]       = {160, 160, 160, 255}
materials["RED_TRANSPARENT"]    = {255, 0, 0, 100}
materials["GREEN_TRANSPARENT"]  = {0, 255, 0, 100}
materials["YELLOW_TRANSPARENT"] = {255, 255, 0, 100}
textures = {}
textures["ARCADE"]                      = MakeMaterial("arcade.tga",materials["RED"])
textures["ARCADE_YELLOW"]               = MakeMaterial("arcade.tga",materials["YELLOW"])
textures["ARCADE_PURPLE"]               = MakeMaterial("arcade.tga",materials["PURPLE"])
textures["ARCADE_GREY"]                 = MakeMaterial("arcade.tga",materials["GREY"])
textures["ARCADE_RED"]                  = MakeMaterial("arcade.tga",materials["RED"])
textures["ARCADE_GREEN"]                = MakeMaterial("arcade.tga",materials["GREEN"])
textures["ARCADE_BLUE"]                 = MakeMaterial("arcade.tga",materials["BLUE"])

textures["ARCADE_2"]                    = MakeMaterial("arcade.tga",materials["RED"])
textures["ARCADE_2_YELLOW"]             = MakeMaterial("arcade.tga",materials["YELLOW"])
textures["ARCADE_2_PURPLE"]             = MakeMaterial("arcade.tga",materials["PURPLE"])
textures["ARCADE_2_GREY"]               = MakeMaterial("arcade.tga",materials["GREY"])
textures["ARCADE_3_RED"]                = MakeMaterial("arcade.tga",materials["RED"])
textures["ARCADE_TRANSP_RED"]           = MakeMaterial("arcade.tga",materials["RED_TRANSPARENT"])
textures["ARCADE_TRANSP_GREEN"]         = MakeMaterial("arcade.tga",materials["GREEN_TRANSPARENT"])
textures["ARCADE_TRANSP_YELLOW"]	    = MakeMaterial("arcade.tga",materials["YELLOW_TRANSPARENT"])

SetCustomScale(1.0)

function AddElement(object)
	object.screenspace = ScreenType.SCREENSPACE_TRUE
    object.use_mipfilter = true
    Add(object)
end

hsd_size        = 0.3
hsd_size_border = (hsd_size)*( 1 + 35 /(256 - 35))
hud_symbol_size = 0.03

hsd_symbol_size = 0.01 * 1.5

hsd_coeff       = hsd_symbol_size / hud_symbol_size


curr_target_size_y_px = 0.5 * (79 - 24) / 256
curr_target_size_x_px = 0.5 * (111 -70) / 256

curr_target_size_y    = 1.5 * hud_symbol_size
curr_target_size_x    = (curr_target_size_x_px / curr_target_size_y_px) * curr_target_size_y
hud_symbol_size_px    = curr_target_size_y_px / 1.5 

trg_size_x_px = 0.5 * 30/256
trg_size_y_px = 0.5 * 37.5/256

trg_size_x = ( trg_size_x_px / hud_symbol_size_px) * hud_symbol_size
trg_size_y = ( trg_size_y_px / hud_symbol_size_px) * hud_symbol_size



tex_scale       = curr_target_size_y_px / curr_target_size_y

aspect   = LockOn_Options.screen.aspect

local   sight_size              = ((0.5*(79 - 51)/256) / hud_symbol_size_px) * hud_symbol_size
local   sight_vertices     	    = {{-sight_size,-sight_size}, 
                                   {-sight_size, sight_size},
                                   { sight_size, sight_size},
                                   { sight_size,-sight_size}}
local mask_material = MakeMaterial(nil,{123,123,123,123})

function set_clear_level(level,controllers)
local  clear_level					= CreateElement "ceMeshPoly"
       clear_level.name				= create_guid_string()
       clear_level.primitivetype    = "triangles"
       clear_level.vertices			= {{-aspect,-1}, 
                                       {-aspect, 1},
                                       { aspect, 1},
                                       { aspect,-1}}
       clear_level.indices			= default_box_indices
       clear_level.material			= mask_material
       clear_level.h_clip_relation  = h_clip_relations.REWRITE_LEVEL
       clear_level.level		  	= DEFAULT_LEVEL + level
       if controllers then
       clear_level.controllers      = controllers
       end
       clear_level.isvisible        = false
       AddElement(clear_level)
       return                       clear_level
end

function add_hud()
local  clear_level = set_clear_level(1)
       add_current_sight_hud(clear_level)


local     target_hud             = CreateElement "ceSimple"
          target_hud.name        = create_guid_string()
          if LockOn_Options.flight.radio_assist then  
          target_hud.controllers      = {{"reset_buffer",0},{"draw_targets",0},{"draw_mlws_contacts",0},{"apply_buffer",0}}
          else
          target_hud.controllers      = {{"reset_buffer",0},{"draw_targets",0},{"apply_buffer",0}}
          end
          AddElement(target_hud) 

local   selector_size              = ((0.5*(257 - 223)/256) / hud_symbol_size_px) * hud_symbol_size
local   selector_vertices       = {{-selector_size,-selector_size}, 
                                   {-selector_size, selector_size},
                                   { selector_size, selector_size},
                                   { selector_size,-selector_size}}

local   selector                    = CreateElement "ceTexPoly"
        selector.name				= create_guid_string()
        selector.vertices			= selector_vertices
        selector.indices			= default_box_indices
        selector.material			= textures["ARCADE_RED"]
        selector.tex_params		    = {101/512,257/512,tex_scale,tex_scale}
        selector.additive_alpha     = true
        selector.parent_element     = target_hud.name
        selector.controllers        = {{"draw_selector_point"}}
        AddElement(selector)
		
local   TVV_size              = ((0.5*(39)/256) / hud_symbol_size_px) * hud_symbol_size
local   TVV_vertices          =   {{-TVV_size * 1.5,-TVV_size}, 
                                   {-TVV_size * 1.5, TVV_size},
                                   { TVV_size * 1.5, TVV_size},
                                   { TVV_size * 1.5,-TVV_size}}
local   TVV                    	= CreateElement "ceTexPoly"
        TVV.name				= create_guid_string()
        TVV.vertices			= TVV_vertices
        TVV.indices				= default_box_indices
        TVV.material			= textures["ARCADE_RED"]
        TVV.tex_params		    = {256/512,458.5/512,tex_scale,tex_scale}
        TVV.additive_alpha      = true
        TVV.parent_element      = target_hud.name
        TVV.controllers         = {{"external_view",1},{"draw_velocity_vector"}}
        AddElement(TVV)
       
       
return target_hud
end


function add_circle_scale(parent,material,intitial_rot,size,controllers,additive)

local   mask                 = CreateElement "ceMeshPoly"
        mask.name            = create_guid_string()
        mask.primitivetype   = "triangles"
        mask.material        = mask_material
        if parent then 
        mask.parent_element  = parent.name
        end
        mask.isvisible       = false
        set_circle(mask,hsd_size_border,0.95*hsd_size_border,size)
        mask.h_clip_relation = h_clip_relations.INCREASE_IF_LEVEL
        mask.level           = DEFAULT_LEVEL + 2
        AddElement(mask)
        
local   scale                 = CreateElement "ceTexPoly" --"ceMeshPoly"
        scale.name            = create_guid_string()
        scale.primitivetype   = "triangles"
        scale.material        = material
        scale.parent_element  = mask.name
        set_circle(scale,hsd_size_border,0.95*hsd_size_border,size)
        if additive == nil then
        scale.additive_alpha  = false
        else 
        scale.additive_alpha  = additive
        end
        scale.init_rot        = {intitial_rot,0,0}
        scale.controllers     = controllers
        scale.h_clip_relation = h_clip_relations.COMPARE
        scale.level           = DEFAULT_LEVEL + 3
        
        local   scale_tex     = (255 / 512)/(0.99*hsd_size_border)
        
        scale.tex_params      = {0.5,0.5,scale_tex,scale_tex}
        AddElement(scale)
        
local   mask_return                 = Copy(mask)
        mask_return.h_clip_relation = h_clip_relations.DECREASE_IF_LEVEL
        mask_return.level           = DEFAULT_LEVEL + 3
        mask_return.parent_element  = mask.name
        mask_return.isvisible       = false
        AddElement(mask_return)
        
        return mask
end


function add_baro_altitude_block(parent)

local   base             = CreateElement "ceSimple"
        base.name        = create_guid_string()
        base.parent_element = parent.name
        base.controllers = {{"switch_altitude_scale_radar",0,900}}
        AddElement(base)

       
      add_circle_scale(base,textures["ARCADE_YELLOW"],135,-135,{{"baro_altitude_rotate" ,-math.rad(135)/6000}}) 
      add_circle_scale(base,textures["ARCADE"],135,-135,{{"surface_height_rotate",-math.rad(135)/6000}},false)
end

function add_radar_altitude_block(parent)
local   radar             =  add_circle_scale(parent,textures["ARCADE_YELLOW"],135,-135,{{"radar_altitude_rotate",-math.rad(135)/900}})
        radar.controllers = {{"switch_altitude_scale_radar",1,900}}
end

function add_current_sight_hud(parent)

local   sight_size              = ((0.5*(79 - 51)/256) / hud_symbol_size_px) * hud_symbol_size


local   sight_clear				   = CreateElement "ceTexPoly"
        sight_clear.name		   = create_guid_string()
        sight_clear.primitivetype  = "triangles"
        sight_clear.vertices	   = {{-sight_size,-sight_size}, 
                                      {-sight_size, sight_size},
                                      { sight_size, sight_size},
                                      { sight_size,-sight_size}}
        sight_clear.indices		   = default_box_indices
        sight_clear.material	   = mask_material
        sight_clear.h_clip_relation= h_clip_relations.INCREASE_LEVEL
        sight_clear.isvisible      = false
        sight_clear.controllers    =  {{"external_view",1},{"draw_sight_hud"}}
        if parent then
        sight_clear.parent_element = parent.name
        end
        AddElement(sight_clear)

local   sight					= CreateElement "ceTexPoly"
        sight.name				= create_guid_string()
        sight.vertices			= sight_clear.vertices
        sight.indices			= sight_clear.indices
        sight.material			= textures["ARCADE_RED"]
        sight.tex_params		= {0.5, 0.5, tex_scale, tex_scale}
        sight.parent_element    = sight_clear.name
        sight.additive_alpha    = true
        AddElement(sight)
       
local   forestall_size              = ((0.5*(79 - 63)/256) / hud_symbol_size_px) * hud_symbol_size

local   forestall					 = CreateElement "ceTexPoly"
        forestall.name				 = create_guid_string()
        forestall.vertices			 = {{-forestall_size,-forestall_size}, 
                                        {-forestall_size, forestall_size},
                                        { forestall_size, forestall_size},
                                        { forestall_size,-forestall_size}}
        forestall.indices			 = default_box_indices
        forestall.tex_params	     = {394/512,0.5, tex_scale, tex_scale}  
        forestall.material			 = textures["ARCADE_RED"]
        forestall.controllers        = {{"external_view",1},
										{"draw_forestall_hud"},
										{"draw_iff_color"}}
		if parent then
        forestall.parent_element = parent.name
        end
        forestall.additive_alpha     = true
        AddElement(forestall)
end


