Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Getting Started

Guad edited this page Jul 25, 2015 · 5 revisions

Requirements

How to install Native UI

  1. Install ScriptHookV and ScriptHookVDotNet
  2. Download the latest stable release of NativeUI and place it in your scripts/ folder.
  3. Add it to your references in your Visual Studio project.

Creating a basic menu

You can create a menu by using the UIMenu class. This class provides 6 events: OnIndexChange, OnListChange, OnCheckboxChange, OnItemSelect, OnMenuClose and OnMenuChange.

UIMenu has two basic parameters: the banner title and the subtitle. It also has an overload with a third Point parameter which determines the offset, and an overload for a custom sprites.

var myMenu = new UIMenu("Banner Title", "~b~SUBTITLE");

Now you can start adding elements to your menu by using the AddItem(UIMenuItem item) method.

myMenu.AddItem(new UIMenuItem("Simple Button"));
myMenu.AddItem(new UIMenuCheckboxItem("Simple Checkbox", false));
myMenu.AddItem(new UIMenuListItem("Simple List", new List<dynamic> {"Item 1", "Item 2", "Item 3"}, 0));
myMenu.AddItem(new UIMenuItem("Another Button", "Items can have descriptions too!"));
myMenu.RefreshIndex();

After you have done adding items, you will have to set the index at 0 by using the RefreshIndex() method.

By using events, you can easily check when something has been changed.

myMenu.OnItemSelect += ItemSelectHandler;
 
public void ItemSelectHandler(UIMenu sender, UIMenuItem selectedItem, int index)
{
    UI.Notify("You have selected: ~b~" + selectedItem.Text);
}

Finally, you will have to implement an open/close trigger for your menu by using the Visible property, and some other final touches like the Draw method in your Tick event handler, and ProcessKey method in your KeyDown handler.

You can also use the MenuPool helper class. You'll have to initialise it and add your menu to it. Then you can call myMenuPool.ProcessMenus() and it will do all the work for you.

private MenuPool _myMenuPool = new MenuPool();

_myMenuPool.Add(myMenu);

public void OnTick(object o, EventArgs e)
{
    _myMenuPool.ProcessMenus();
}
public void OnKeyDown(object o, KeyEventArgs e)
{
    //myMenu.ProcessKey(e.KeyCode); // We are using controls instead of keys, so we comment it out.
    if (e.KeyCode == Keys.F5) // Our menu on/off switch
    {
        myMenu.Visible = !myMenu.Visible;
    }
}

Example Script

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GTA;
using GTA.Native;
using NativeUI;

public class MenuExample : Script
{
    private UIMenu mainMenu;
    private UIMenu newMenu;
    private UIMenuCheckboxItem ketchupCheckbox;
    private UIMenuListItem dishesListItem;
    private UIMenuItem cookItem;

    private MenuPool _menuPool;

    public MenuExample()
    {
        Tick += OnTick;
        KeyDown += OnKeyDown;
        _menuPool = new MenuPool();

        mainMenu = new UIMenu("Native UI", "~b~NATIVEUI SHOWCASE");
        _menuPool.Add(mainMenu);

        mainMenu.AddItem(ketchupCheckbox = new UIMenuCheckboxItem("Add ketchup?", false, "Do you wish to add ketchup?"));
        var foods = new List<dynamic>
        {
            "Banana",
            "Apple",
            "Pizza",
            "Quartilicious",
            0xF00D, // Dynamic!
        };
        mainMenu.AddItem(dishesListItem = new UIMenuListItem("Food", foods, 0));
        mainMenu.AddItem(cookItem = new UIMenuItem("Cook!", "Cook the dish with the appropiate ingredients and ketchup."));

        var menuItem = new UIMenuItem("Go to another menu.");
        mainMenu.AddItem(menuItem);
        cookItem.SetLeftBadge(UIMenuItem.BadgeStyle.Star);
        cookItem.SetRightBadge(UIMenuItem.BadgeStyle.Tick);
        mainMenu.RefreshIndex();

        mainMenu.OnItemSelect += OnItemSelect;
        mainMenu.OnListChange += OnListChange;
        mainMenu.OnCheckboxChange += OnCheckboxChange;
        mainMenu.OnIndexChange += OnItemChange;

        newMenu = new UIMenu("Native UI", "~r~NATIVEUI SHOWCASE");
        _menuPool.Add(newMenu);
        for (int i = 0; i < 20; i++)
        {
            newMenu.AddItem(new UIMenuItem("PageFiller", "Sample description that takes more than one line. Moreso, it takes way more than two lines since it's so long. Wow, check out this length!"));
        }
        newMenu.RefreshIndex();
        mainMenu.BindMenuToItem(newMenu, menuItem);
    
    }

    public void OnItemChange(UIMenu sender, int index)
    {
        sender.MenuItems[index].SetLeftBadge(UIMenuItem.BadgeStyle.None);
    }

    public void OnCheckboxChange(UIMenu sender, UIMenuCheckboxItem checkbox, bool Checked)
    {
        if (sender != mainMenu || checkbox != ketchupCheckbox) return; // We only want to detect changes from our menu.
        UI.Notify("~r~Ketchup status: ~b~" + Checked);
    }

    public void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        if (sender != mainMenu || list != dishesListItem) return; // We only want to detect changes from our menu.
        string dish = list.IndexToItem(index).ToString();
        UI.Notify("Preparing ~b~" + dish +"~w~...");
    }

    public void OnItemSelect(UIMenu sender, UIMenuItem selectedItem, int index)
    {
        if (sender != mainMenu || selectedItem != cookItem) return; // We only want to detect changes from our menu and our button.
        // You can also detect the button by using index
        string dish = dishesListItem.IndexToItem(dishesListItem.Index).ToString();
        bool ketchup = ketchupCheckbox.Checked;

        string output = ketchup
            ? "You have ordered ~b~{0}~w~ ~r~with~w~ ketchup."
            : "You have ordered ~b~{0}~w~ ~r~without~w~ ketchup.";
        UI.ShowSubtitle(String.Format(output, dish));
    }

    public void OnTick(object o, EventArgs e)
    {
        _menuPool.ProcessMenus();
    }

    public void OnKeyDown(object o, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.F5) // Our menu on/off switch
        {
            mainMenu.Visible = !mainMenu.Visible;
        }
    }
}
Clone this wiki locally