#method menu_back(m_background!, m_background$)
{
 #m_back_style! = m_background!
 #kill(m_background!)
}

#method menu_back_rgb(m_back_r!, m_back_g!, m_back_b!)
{
 #m_back_style! = 2
}

#method menu_bar(m_bar$, m_bar_width!)
{
 #m_bar_style! = 1
}

#method menu_bar_rgb(m_bar_r!, m_bar_g!, m_bar_b!)
{
 #m_bar_style! = 2
}

#method menu_cursor(m_cursor$, m_cursor_width!, m_cursor_height!, m_cursor_r!, m_cursor_g!, m_cursor_b!)
{
 #m_cursor_style! = 1
}

#method menu_cursor_rgb(m_cursor_r!, m_cursor_g!, m_cursor_b!)
{
 #m_cursor_style! = 2
}

#method menu_keys(m_up$, m_down$, m_select$, m_cancel$)
{
 #m_keys_set! = 1
}

#method menu_create(m_x!, m_y!, m_width!, m_height!, m_id!)
{
 #if(m_keys_set! == 0)
 {
  #m_up$ = "UP"
  #m_down$ = "DOWN"
  #m_select$ = "ENTER"
  #m_cancel$ = " "
  #m_keys_set! = 1
 }
 #if(m_bar_style! == 0)
 {
  #m_bar_style! = 2
  #m_bar_r! = 0
  #m_bar_g! = 0
  #m_bar_b! = 255
 }
 #if(m_cursor_style! == 0)
 {
  #m_cursor_style! = 2
  #m_cursor_r! = 255
  #m_cursor_g! = 255
  #m_cursor_b! = 0
 }
 #if(m_menus! == 0)
 {
  #savescreen()
 }
 #m_menus! = m_menus! + 1
 #getfontsize(m_font[m_id!]!)
 #m_x[m_id!]! = m_x!
 #m_y[m_id!]! = m_y!
 #m_x2[m_id!]! = m_x! + m_width!
 #m_y2[m_id!]! = m_y! + m_height!
 #if(m_bar_style! == 1)
 {
  #m_x3[m_id!]! = m_x! + m_width! - m_bar_width!
 }
 #if(m_bar_style! == 2)
 {
  #m_x3[m_id!]! = m_x! + m_width! - 10
 }
 #if(m_cursor_style! == 1)
 {
  #m_x4[m_id!]! = m_x! - m_cursor_width! - 2
 }
 #if(m_cursor_style! == 2)
 {
  #m_cursor_radius[m_id!]! = m_font[m_id!]! / 4
  #m_x4[m_id!]! = m_x! - m_font[m_id!]! + m_cursor_radius[m_id!]!
 }
 #m_fx[m_id!]! = m_x! + 5 / m_font[m_id!]! + 1
 #m_fy[m_id!]! = m_y! + 5 / m_font[m_id!]! + 1
 #m_max_options[m_id!]! = m_height! - 10 / m_font[m_id!]!
 #castint(m_max_options[m_id!]!, m_max_options[m_id!]!)
 #m_options[m_id!]! = 0
 #m_selected[m_id!]! = 0
 #m_first[m_id!]! = 0
 #m_active[m_id!]! = 0
 #if(m_back_style! == 1)
 {
  #setimage(m_background$, m_x!, m_y!, m_width!, m_height!)
 }
 #if(m_back_style! == 2)
 {
  #getcolor(temp_r!, temp_g!, temp_b!)
  #colorrgb(m_back_r!, m_back_g!, m_back_b!)
  #fillrect(m_x!, m_y!, m_x2[m_id!]!, m_y2[m_id!]!)
  #colorrgb(temp_r!, temp_g!, temp_b!)
  #kill(temp_r!)
  #kill(temp_g!)
  #kill(temp_b!)
 }
 #kill(m_x!)
 #kill(m_y!)
 #kill(m_width!)
 #kill(m_height!)
}

#method menu_destroy(m_id!)
{
 #m_menus! = m_menus! - 1
 #temp_x! = m_x2[m_id!]! + 1
 #temp_y! = m_y2[m_id!]! + 1
 #restorescreen(m_x4[m_id!]!, m_y[m_id!]!, temp_x!, temp_y!, m_x4[m_id!]!, m_y[m_id!]!)
 #for(temp! = 0; temp! < m_options[m_id!]!; temp! = temp! + 1)
 {
  #castlit(temp!, temp$)
  #command$ = "#kill(m_option" + temp$ + "[m_id!]$)"
  #rpgcode(command$)
  #command$ = "#kill(m_command" + temp$ + "[m_id!]$)"
  #rpgcode(command$)
 }
 #kill(temp!)
 #kill(temp$)
 #kill(command$)
 #kill(m_options[m_id!]!)
 #kill(m_max_options[m_id!]!)
 #kill(m_first[m_id!]!)
 #kill(m_selected[m_id!]!)
 #kill(m_active[m_id!]!)
 #kill(m_x[m_id!]!)
 #kill(m_y[m_id!]!)
 #kill(m_x2[m_id!]!)
 #kill(m_y2[m_id!]!)
 #kill(m_x3[m_id!]!)
 #kill(m_x4[m_id!]!)
 #kill(m_font[m_id!]!)
 #kill(m_fx[m_id!]!)
 #kill(m_fy[m_id!]!)
 #kill(m_cursor_radius[m_id!]!)
}

#method menu_add(m_option$, m_command$, m_id!)
{
 #castlit(m_options[m_id!]!, temp$)
 #command$ = "#m_option" + temp$ + "[m_id!]$ = m_option$"
 #rpgcode(command$)
 #command$ = "#m_command" + temp$ + "[m_id!]$ = m_command$"
 #m_options[m_id!]! = m_options[m_id!]! + 1
 #rpgcode(command$)
 #kill(temp$)
 #menu_draw(m_id!)
 #kill(m_option$)
 #kill(m_command$)
}

#method menu_remove(m_id!)
{
 #m_options[m_id!]! = m_options[m_id!]! - 1
 #castlit(m_options[m_id!]!, temp$)
 #command$ = "#kill(m_option" + temp$ + "[m_id!]$)"
 #rpgcode(command$)
 #command$ = "#kill(m_command" + temp$ + "[m_id!]$)"
 #rpgcode(command$)
 #m_first[m_id!]! = 0
 #m_selected[m_id!]! = 0
}

#method menu_activate(m_id!)
{
 #menu_draw(m_id!)
 #m_active[m_id!]! = 1
 #while(m_active[m_id!]! == 1)
 {
  #c_y! = m_selected[m_id!]! - m_first[m_id!]! * m_font[m_id!]! + m_y[m_id!]! + 5
  #if(m_cursor_style! == 1)
  {
   #setimagetransparent(m_cursor$, m_x4[m_id!]!, c_y!, m_cursor_width!, m_cursor_height!, m_cursor_r!, m_cursor_g!, m_cursor_b!)
  }
  #if(m_cursor_style! == 2)
  {
   #c_y! = m_cursor_radius[m_id!]! * 2 + c_y!
   #getcolor(temp_r!, temp_g!, temp_b!)
   #colorrgb(m_cursor_r!, m_cursor_g!, m_cursor_b!)
   #fillcircle(m_x4[m_id!]!, c_y!, m_cursor_radius[m_id!]!
   #colorrgb(temp_r!, temp_g!, temp_b!)
   #kill(temp_r!)
   #kill(temp_g!)
   #kill(temp_b!)
  }
  #wait(m_a[m_id!]$)
  #c_y! = m_selected[m_id!]! - m_first[m_id!]! * m_font[m_id!]! + m_y[m_id!]! + 5
  #if(m_cursor_style! == 1)
  {
   #c_y2! = c_y! + m_cursor_height! + 1
   #restorescreen(m_x4[m_id!]!, c_y!, m_x[m_id!]!, c_y2!, m_x4[m_id!]!, c_y!)
  }
  #if(m_cursor_style! == 2)
  {
   #c_y2! = c_y! + m_font[m_id!]! + 1
   #c_x! = m_x[m_id!]! - m_font[m_id!]!
   #restorescreen(c_x!, c_y!, m_x[m_id!]!, c_y2!, c_x!, c_y!)
  }
  #if(m_a[m_id!]$ == m_up$)
  {
   #if(m_selected[m_id!]! > 0)
   {
    #m_selected[m_id!]! = m_selected[m_id!]! - 1
    #if(m_selected[m_id!]! < m_first[m_id!]!)
    {
     #m_first[m_id!]! = m_selected[m_id!]!
     #menu_draw(m_id!)
    }
   }
  }
  #if(m_a[m_id!]$ == m_down$)
  {
   #temp! = m_options[m_id!]! - 1
   #if(m_selected[m_id!]! < temp!)
   {
    #m_selected[m_id!]! = m_selected[m_id!]! + 1
    #m_last! = m_first[m_id!]! + m_max_options[m_id!]!
    #if(m_selected[m_id!]! => m_last!)
    {
     #m_first[m_id!]! = m_selected[m_id!]! - m_max_options[m_id!]! + 1
     #menu_draw(m_id!)
    }
   }
  }
  #if(m_a[m_id!]$ == m_select$)
  {
   #temp_menu! = temp_menu! + 1
   #m_temp_id[temp_menu!]! = m_id!
   #castlit(m_selected[m_id!]!, temp$)
   #command$ = "#rpgcode(m_command" + temp$ + "[m_id!]$)"
   #rpgcode(command$)
   #m_id! = m_temp_id[temp_menu!]!
   #temp_menu! = temp_menu! - 1
  }
  #if(m_cancel$ ~= "")
  {
   #if(m_a[m_id!]$ == m_cancel$)
   {
    #m_active[m_id!]! = 0
   }
  }
 }
 #kill(c_y!)
 #kill(c_y2!)
 #kill(c_x!)
}

#method menu_deactivate(m_id!)
{
 #m_active[m_id!]! = 0
}

#method menu_draw(m_id!)
{
 #if(m_back_style! == 0)
 {
  #temp_x! = m_x2[m_id!]! + 1
  #temp_y! = m_y2[m_id!]! + 1
  #restorescreen(m_x[m_id!]!, m_y[m_id!]!, temp_x!, temp_y!, m_x[m_id!]!, m_y[m_id!]!)
 }
 #if(m_back_style! == 1)
 {
  #m_width! = m_x2[m_id!]! - m_x[m_id!]!
  #m_height! = m_y2[m_id!]! - m_y[m_id!]!
  #setimage(m_background$, m_x[m_id!]!, m_y[m_id!]!, m_width!, m_height!)
  #kill(m_width!)
  #kill(m_height!)
 }
 #if(m_back_style! == 2)
 {
  #getcolor(temp_r!, temp_g!, temp_b!)
  #colorrgb(m_back_r!, m_back_g!, m_back_b!)
  #fillrect(m_x[m_id!]!, m_y[m_id!]!, m_x2[m_id!]!, m_y2[m_id!]!)
  #colorrgb(temp_r!, temp_g!, temp_b!)
  #kill(temp_r!)
  #kill(temp_g!)
  #kill(temp_b!)
 }
 #getfontsize(temp_font!)
 #fontsize(m_font[m_id!]!)
 #if(m_options[m_id!]! <= m_max_options[m_id!]!)
 {
  #for(temp! = 0; temp! < m_options[m_id!]!; temp! = temp! + 1)
  {
   #castlit(temp!, temp$)
   #m_fy! = m_fy[m_id!]! + temp!
   #command$ = "#text(m_fx[m_id!]!, m_fy!, m_option" + temp$ + "[m_id!]$)"
   #rpgcode(command$)
  }
 }
 #if(m_options[m_id!]! > m_max_options[m_id!]!)
 {
  #m_last! = m_first[m_id!]! + m_max_options[m_id!]!
  #temp2! = 0
  #for(temp! = m_first[m_id!]!; temp! < m_last!; temp! = temp! + 1)
  {
   #castlit(temp!, temp$)
   #m_fy! = m_fy[m_id!]! + temp2!
   #command$ = "#text(m_fx[m_id!]!, m_fy!, m_option" + temp$ + "[m_id!]$)"
   #rpgcode(command$)
   #temp2! = temp2! + 1
  }
  #bar_y! = m_y2[m_id!]! - m_y[m_id!]! / m_options[m_id!]! * m_first[m_id!]! + m_y[m_id!]!
  #bar_h! = m_y2[m_id!]! - m_y[m_id!]! / m_options[m_id!]! * m_max_options[m_id!]!
  #if(m_bar_style! == 1)
  {
   #setimage(m_bar$, m_x3[m_id!]!, bar_y!, m_bar_width!, bar_h!)
  }
  #if(m_bar_style! == 2)
  {
   #bar_y2! = bar_y! + bar_h!
   #getcolor(temp_r!, temp_g!, temp_b!)
   #colorrgb(m_bar_r!, m_bar_g!, m_bar_b!)
   #fillrect(m_x3[m_id!]!, bar_y!, m_x2[m_id!]!, bar_y2!)
   #colorrgb(temp_r!, temp_g!, temp_b!)
   #kill(temp_r!)
   #kill(temp_g!)
   #kill(temp_b!)
  }
 }
 #fontsize(temp_font!)
 #kill(temp_font!)
}




