eval_js_file ("file:///usr/local/javascript/wing/surfer/vt_local6.js"); 

var rgs_icon_obj = new Array ();
var floor;
var frame;
var cursor;
var focus_first_item;
var s = new stage ({});

var frame_rgs = "/usr/local/anim/frame.rgs";
var cursor_rgs = "/usr/local/anim/cursor6.rgs";

var scenario1 = new gscenario;
scenario1.init ();

name_text = new gtext ({
  "text" : "menu 1",
  "font_size" : 50,
  "width" : 500,
  "height" : 100,
  "align" : CENTER,
  "color" : [255,255,255,255],
  "translate" : [0, -485, 0],
});

name_base = new gbox ({
  "round_height" : 10,
  "round_width" : 10,
  "round_enable" : true,
  "height" : 65,
  "color" : [150, 150, 50, 0],
  "translate" : [0, -485, 0],
});

for (var i=0; i<menu_num; i++)
{
  rgs_icon_obj[i] = new ghop ({
    "src": icon_rgs[i],
    "work_size": 4,
    "color_enable": true,
    "color": [255, 255, 255, 255],
  });
  rgs_icon_obj[i].set_texture (icon_pics_nml[i]);
}

floor = new gbox ({
  "translate":[0, -1500, -400],
  "rotation": [-60.0, 1, 0, 0],
  "width": 7680,
  "height": 2160,
  "color_enable": true,
  "color": [0, 0, 0, 130],
});

frame = new ghop ({
  "src": frame_rgs,
  "work_size": 4,
  "color_enable": true,
  "color": [255, 194, 0, 255],
});

cursor = new ghop ({
  "src": cursor_rgs,
  "work_size": 4,
  "color_enable": true,
  "color": [255, 194, 0, 255],
});

var appear_menutext = function (pos) {
  name_base.translate [0] = name_x[pos];
  name_text.translate [0] = name_x[pos];
  str_width = get_text_width (name_text, gettext (menu_str[cur_pos]));

  if ((name_base.translate [0] - (str_width / 2)) < left_limit) {
    name_base.translate [0] = (str_width / 2) + left_limit;
    name_text.translate [0] = (str_width / 2) + left_limit;
  }

  if ((name_base.translate [0] + (str_width / 2)) > right_limit) {
    name_base.translate [0] = right_limit - (str_width / 2);
    name_text.translate [0] = right_limit - (str_width / 2);
  }

  name_base.width = str_width + 100;
  name_text.width = str_width + 100;
  setf_text (name_text, gettext (menu_str[cur_pos]));

  s.components [21] = name_base;
  s.components [22] = name_text;
}

var disappear_menutext = function (){
  s.components [21] = 0;
  s.components [22] = 0;
}


var anim_cursor_move = function (dir) {
  var start_fr;
  var start2_fr;
  var prev_pos = cur_pos;

  disappear_menutext ();
  focus_first_item = function (){};

  if (dir == 1) {
    cur_pos --;
    cur_pos = (cur_pos + menu_num) % menu_num;
  }
  if (dir == 2) {
    cur_pos ++;
    cur_pos %= menu_num;
  }

  rgs_icon_obj[prev_pos].set_texture (icon_pics_nml[prev_pos]);
  rgs_icon_obj[cur_pos].set_texture (icon_pics_fcs[cur_pos]);
  appear_menutext (cur_pos);

  start_fr = zoom_in[cur_pos];
  start2_fr = zoom_out[prev_pos];

  s.components[cur_pos+2] = 0;
  s.components[menu_num+2] = rgs_icon_obj[cur_pos];
  s.components[prev_pos+2] = rgs_icon_obj[prev_pos];

  scenario1.finish ();
  scenario1.set_anim_frame (rgs_icon_obj[cur_pos], start_fr, start_fr+9, 0, 10);
  scenario1.set_anim_color (rgs_icon_obj[cur_pos], [0, 0, 255, 255], [255, 194, 0, 255], 0, 10);
  scenario1.set_anim_frame (rgs_icon_obj[prev_pos], start2_fr, start2_fr+9, 0, 10);
  scenario1.set_anim_color (rgs_icon_obj[prev_pos], [255, 194, 0, 255], [0, 0, 255, 255], 0, 10);
  scenario1.set_anim_frame (cursor, start_fr, start_fr+9, 0, 10);
  scenario1.run (function (){});
};

s.symbol = "home-screen";
s.key_hook = function (up_down, key) {
  if (key==TXK_RIGHT && up_down==KEY_PRESS) {
    anim_cursor_move (2);
    write_back_bytes ([1, cur_pos]);
  }
  else if (key==TXK_LEFT && up_down==KEY_PRESS) {
    anim_cursor_move (1);
    write_back_bytes ([1, cur_pos]);
  }
  else if (key==TXK_ENTER && up_down==KEY_PRESS) {
    if ((cur_pos == 0) || (cur_pos == 1)) {
      launch_apl (key, cur_pos);
    }
    else {
      goto_appli (key, cur_pos);
    }
  }
  else if (up_down==KEY_PRESS) {
    exit_vtool (key);
  }
};
s.in = [
  {
    "from": ["default"],
  }
];

function focus_first_item (start_fr) {
  scenario1.set_anim_color (rgs_icon_obj[cur_pos], [255, 255, 255, 255], [255, 194, 0, 255], 0, 10);
  scenario1.set_anim_frame (rgs_icon_obj[cur_pos], start_fr, start_fr+9, 0, 10);
  scenario1.set_anim_frame (cursor, start_fr, start_fr+9, 0, 10);
  appear_menutext (cur_pos);
  rgs_icon_obj[cur_pos].set_texture (icon_pics_fcs[cur_pos]);
  scenario1.run ();
}

function appear_obj () {

  if (cur_pos >= menu_num)
    cur_pos = 0;

  s.components[0] = floor;
  s.components[1] = frame;
  for (var i=0; i<menu_num; i++)
  {
    s.components[i+2] = rgs_icon_obj[i];
  }

  s.components[cur_pos+2] = 0;
  s.components[menu_num+2] = rgs_icon_obj[cur_pos];
  s.components[menu_num+3] = cursor;
 
  var start_fr = 344 - cur_pos * 30;

  scenario1.finish ();
  scenario1.set_anim_translate (floor, [0, -1500, -400], [0, -1310, -400], 0, 10);
  for (i=0; i<menu_num; i++)
  {
    scenario1.set_anim_frame (rgs_icon_obj[i], 0, 40, 10, 30);
    scenario1.set_anim_color (rgs_icon_obj[i], [255, 255, 255, 255], [5, 50, 255, 255], 0, 1);
  }
  scenario1.run (function (){focus_first_item (start_fr);});
} 

function delete_png (){
  delete_all_pngbuf ();
}
