Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[clientside][dev 8] Simple menu framework
#1
Hello, i want to release my old menu framework so here it is.

Squirrel Script
  1. class Menu
  2. {
  3. constructor(x, y, title, lineHeight)
  4. {
  5. m_Position = { x = x, y = y };
  6. m_TitleId = createDraw(title, "Font_Old_20_White_Hi.TGA", x, y, 123, 118, 94);
  7. m_TextureId = createTexture(x - 100, y - 100, 3000, 1500, "ramka_gui.tga");
  8. m_Options = [];
  9. m_CopyOptions = [];
  10. m_LineHeight = lineHeight
  11. m_Cur = 0;
  12. m_CurTitle = "";
  13. m_Page = 0;
  14. m_Showed = false;
  15.  
  16. local m_This = this;
  17. addEvent("onKey", function(key, letter) {m_This.onKey(key, letter);});
  18. }
  19.  
  20. function addOption(title)
  21. {
  22. if(m_Options.len() == 0)
  23. {
  24. m_Position.y += 300;
  25.  
  26. m_Options.append(createDraw(title, "Font_Old_10_White_Hi.TGA", m_Position.x + 50, m_Position.y, 255, 255, 255));
  27. m_CopyOptions.append(title);
  28. m_CurTitle = title;
  29. }
  30. else
  31. {
  32. m_Position.y += 150;
  33.  
  34. m_Options.append(createDraw(title, "Font_Old_10_White_Hi.TGA", m_Position.x + 50, m_Position.y, 111, 106, 81));
  35. m_CopyOptions.append(title);
  36. }
  37. }
  38.  
  39. function show()
  40. {
  41. m_Showed = true;
  42. setDrawVisible(m_TitleId, true);
  43. setTextureVisible(m_TextureId, true);
  44. for(local i = 0; i < m_LineHeight; i++)
  45. {
  46. setDrawVisible(m_Options[i], true);
  47. }
  48. }
  49.  
  50. function hide()
  51. {
  52. m_Showed = false;
  53. setDrawVisible(m_TitleId, false);
  54. setTextureVisible(m_TextureId, false);
  55. for(local i = 0; i < m_LineHeight; i++)
  56. {
  57. setDrawVisible(m_Options[i], false);
  58. }
  59. }
  60.  
  61. function moveUp()
  62. {
  63. if(m_Cur % m_LineHeight == 0 && m_Page > 0)
  64. m_Page -= m_LineHeight;
  65.  
  66. if(m_Cur > 0)
  67. m_Cur--;
  68.  
  69. refresh();
  70. }
  71.  
  72. function moveDown()
  73. {
  74. if(m_Cur == (m_Options.len() - 1))
  75. return false;
  76.  
  77. m_Cur++;
  78.  
  79. if(m_Cur % m_LineHeight == 0)
  80. m_Page += m_LineHeight;
  81.  
  82. refresh();
  83. }
  84.  
  85. function refresh()
  86. {
  87. for(local i = 0; i < m_LineHeight; i++)
  88. {
  89. try {
  90. setDrawText(m_Options[i], m_CopyOptions[i + m_Page]);
  91. setDrawColor(m_Options[i], 111, 106, 81);
  92. } catch(error) {
  93. setDrawText(m_Options[i], "");
  94. setDrawColor(m_Options[i], 111, 106, 81);
  95. }
  96. }
  97.  
  98. if(m_Page > 0)
  99. setDrawColor(m_Options[m_Cur - m_Page], 255, 255, 255);
  100. else
  101. setDrawColor(m_Options[m_Cur], 255, 255, 255);
  102.  
  103. m_CurTitle = m_CopyOptions[m_Cur];
  104. }
  105.  
  106. function onKey(key, letter)
  107. {
  108. switch(key)
  109. {
  110. case KEY_UP:
  111. moveUp();
  112. break;
  113. case KEY_DOWN:
  114. moveDown();
  115. break;
  116. }
  117. }
  118.  
  119. m_Position = null;
  120. m_TitleId = null;
  121. m_TextureId = null;
  122. m_Options = null;
  123. m_CopyOptions = null;
  124. m_LineHeight = null;
  125. m_Cur = null;
  126. m_CurTitle = null;
  127. m_Page = null;
  128. m_Showed = null;
  129. }



Example of usage. (It uses my NPC class but you should know how to use this by your own way.)
Squirrel Script
  1. local Npc = Npcs("Trener");
  2.  
  3. local menu = Menu(3000, 6500, "Trener", 5);
  4. menu.addOption("Siła (+1)");
  5. menu.addOption("Siła (+5)");
  6. menu.addOption("Zręczność (+1)");
  7. menu.addOption("Zręczność (+5)");
  8. menu.addOption("(Powrót)");
  9.  
  10. addEvent("onKey", function(key, letter)
  11. {
  12. Npc.onCtrl(key, letter);
  13. });
  14.  
  15. addEvent("onTakeFocus", function(id, name)
  16. {
  17. Npc.setFocus(name, true);
  18. });
  19.  
  20. addEvent("onLostFocus", function(id, name)
  21. {
  22. Npc.setFocus(name, false);
  23.  
  24. if(menu.m_Showed)
  25. {
  26. menu.hide();
  27. setFreeze(false);
  28. }
  29. });
  30.  
  31. Npc.onCtrl = function(key, letter)
  32. {
  33. if(menu.m_Showed && key == KEY_RETURN)
  34. {
  35. if(menu.m_CurTitle == "(Powrót)")
  36. {
  37. menu.hide();
  38. setFreeze(false);
  39. }
  40. }
  41. if(Npc.isFocused() && key == KEY_LCONTROL)
  42. {
  43. if(!menu.m_Showed)
  44. {
  45. callServerFunc(RELIABLE, "botAngleToPos", FOCUS_ID, getID());
  46. menu.show();
  47. setFreeze(true);
  48. }
  49. else
  50. {
  51. menu.hide();
  52. setFreeze(false);
  53. }
  54. }
  55. };


Reply
#2
Now you can use in the (version >=G2O- 0.0.6)
Code:
class Menu
{
    constructor(x, y, title, lineHeight)
    {
        m_Position = { x = x, y = y };
        m_TitleId = Draw(x, y, title);
        m_TitleId.font="Font_Old_10_White_Hi.TGA";
        m_TitleId.setColor(123, 118, 94);
        m_TextureId = Texture(x-100, y-100, 3000, 1500, "MENU_INGAME.TGA")
        m_Options = [];
        m_CopyOptions = [];
        m_LineHeight = lineHeight
        m_Cur = 0;
        m_Page = 0;
        m_Showed = false;

        local m_This = this;
        addEventHandler("onKey", function(key) {m_This.menuKeyHandler(key);});
        
    }
function drawLajkDev(txt, fnt, x, y, r, g, b)
{
local draw=Draw(x, y, txt);
draw=Draw(x, y, txt);
draw.font=fnt;
draw.setColor(r, g, b);
return draw;
}
function addOption(title)
    {
        if(m_Options.len() == 0)
        {
            m_Position.y += 300;
            m_Options.append(drawLajkDev(title, "Font_Old_10_White_Hi.TGA", m_Position.x + 50, m_Position.y, 255, 255, 255));
            m_CopyOptions.append(title);
        }
        else
        {
            m_Position.y += 150;
            m_Options.append(drawLajkDev(title, "Font_Old_10_White_Hi.TGA", m_Position.x + 50, m_Position.y, 111, 106, 81));
            m_CopyOptions.append(title);
        }
    }

function show()
    {
        m_Showed = true;
        m_TitleId.visible=true;
        m_TextureId.visible=true;
            
        for(local i = 0; i < m_LineHeight; i++)
        {
            m_Options[i].visible=true;
        }
    }

function hide()
    {
        m_Showed = false;
        m_TitleId.visible=false;
        m_TextureId.visible=false;
        for(local i = 0; i < m_LineHeight; i++)
        {
            m_Options[i].visible=false;
        }
    }
    
function moveDown()
{
    if(m_Cur<m_Options.len()-1){m_Cur++;}else{m_Cur=0;}
    refresh();
}
function moveUp()
{
    if(m_Cur>0){m_Cur--;}else{m_Cur=m_Options.len()-1;}
    refresh();
}

function refresh()
{
for(local i = 0; i < m_LineHeight; i++)
        {
            try {
                m_Options[i].text=m_CopyOptions[i + m_Page];
                m_Options[i].setColor(111, 106, 81);
            } catch(error) {
                m_Options[i].text="";
                m_Options[i].setColor(111, 106, 81);
            }
        }

        if(m_Page > 0)
            m_Options[m_Cur - m_Page].setColor(255, 255, 255);
        else
            m_Options[m_Cur].setColor(255, 255, 255);

}

function menuKeyHandler(key)//Handler is in constructor
    {
        if(m_Showed)
        {    
            if(key==KEY_UP)        {moveUp();}
            if(key==KEY_DOWN)    {moveDown();}
        }
    }    

    

    m_Position = null;
    m_TitleId = null;
    m_TextureId = null;
    m_Options = null;
    m_CopyOptions = null;
    m_LineHeight = null;
    m_Cur = null;
    m_Page = null;
    m_Showed = null;
    }
EXAMPLE:
Code:
local menu = Menu(3000, 6500, "Trener", 5);
menu.addOption("Siła (+1)");//0
menu.addOption("Siła (+5)");//1
menu.addOption("Zręczność (+1)");//2
menu.addOption("Zręczność (+5)");//3
menu.addOption("(Powrót)");//4

function onKeyMenuExample(key)
    {
        if(!menu.m_Showed && key==KEY_P)        {menu.show();setFreeze(true);}
        if(menu.m_Showed && key == KEY_RETURN)
        {
            switch(menu.m_Cur)
            {
            case 0: print("Siła (+1)");break;
            case 1: print("Siła (+5)");break;
            case 2: print("Zręczność (+1)");break;
            case 3: print("Zręczność (+5)");break;
            case 4:    menu.hide();setFreeze(false);break;            
            }
            
        }
            
    }    
    addEventHandler("onKey", onKeyMenuExample)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [New G2O][Client] GUI.Framework Patrix 0 3,151 07.02.2019, 01:39
Last Post: Patrix
  GUI Framework 2.0 Osmith 7 9,625 27.01.2018, 15:01
Last Post: Osmith
  [Clientside] Simple menu framework Sative 1 4,189 09.05.2016, 06:46
Last Post: Sative

Forum Jump:


Users browsing this thread: 1 Guest(s)