• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

CR - Buttons

JASS:
library Buttons requires GUI

define {
    private MAX_BUTTONS = 300
    Buttons_SELECTION_ID_1 = 'B01N'
    Buttons_SELECTION_ID_2 = 'B00Z'
    Buttons_SELECTION_ID_3 = 'B01O'
    Buttons_SELECTION_ID_4 = 'B01P'
    Buttons_SELECTION_ID_5 = 'B02R'
    Buttons_SLIDER_ID = 'B01K'
}

// globals
    int ButtonCount = 0
    int array Button[MAX_BUTTONS]
    trackable array Button_Trackable[MAX_BUTTONS]
    destructable array Button_Icon[MAX_BUTTONS]
    destructable array Button_Selection[MAX_BUTTONS]
    texttag array Button_Text[MAX_BUTTONS]
    int array Button_Coords[MAX_BUTTONS][2]
    
    
    public int GetByTrackable(trackable clicked_trackable){
        int current_button = 0
            while(current_button++ < ButtonCount) { if Button_Trackable[current_button] == clicked_trackable { return current_button } }
        return 0
    }
    
    public int GetFree(){
        int current_button = 0
            while (current_button++ < ButtonCount) { if Button[current_button] == 0 { return current_button } }
        return 0
    }
    
    public int GetByData(int cell){
        int current_button = 0
            while (current_button++ < ButtonCount) { if Button[current_button] == cell { return current_button } }
        return 0
    }
    
    public int GetByCoords(int x, int y){
        int current_button = 0
            while (current_button++ < ButtonCount){
                if (Button_Coords[current_button][1] == x and Button_Coords[current_button][2] == y) { return current_button } }
        return 0
    }
    
    public void Remove(int id){
        Button[id] = 0
        RemoveDestructable(Button_Icon[id])
        RemoveDestructable(Button_Selection[id])
        DestroyTextTag(Button_Text[id])
    }
    
    public void RemoveAll(){
        int current_button = 0
            while (current_button++ < ButtonCount){
                Button[current_button] = 0
                RemoveDestructable(Button_Icon[current_button])
                RemoveDestructable(Button_Selection[current_button]) 
                DestroyTextTag(Button_Text[current_button])
            }
    }
    
    public void AddSelectionEffect(int button_id, int variation){
        real true_x = (Left_X + Button_Coords[button_id][1] * 64) + 32., true_y = (Left_Y + Button_Coords[button_id][2] * 64) + 32.
        int selection_id
            if variation == 1 { selection_id = Buttons_SELECTION_ID_1 }
            elseif variation == 2 { selection_id = Buttons_SELECTION_ID_2 }
            elseif variation == 3 { selection_id = Buttons_SELECTION_ID_3 }
            elseif variation == 4 { selection_id = Buttons_SELECTION_ID_4 }
            elseif variation == 5{ selection_id = Buttons_SELECTION_ID_5 }
            Button_Selection[button_id] = CreateDestructable(selection_id, true_x, true_y, 0., 1., 0)
    }
    
    // add a text for button
    public void SetText(string txt, real size, real offset_x, real offset_y, int button_id){
        real true_x = ((Left_X + Button_Coords[button_id][1] * 64) + 32.) + offset_x, true_y = ((Left_Y + Button_Coords[button_id][2] * 64) + 32.) + offset_y
            Button_Text[button_id] = CreateTextTag()
            SetTextTagText(Button_Text[button_id], txt, (size * 0.023) / 10.)
            SetTextTagPos(Button_Text[button_id], true_x, true_y, 40.)
    }
    
    public void SetAttachEx(int button_id, int db_id, int icon_id){
        real true_x , true_y
            if (db_id == 0 or Button[button_id] != 0) { return }
            true_x = (Left_X + Button_Coords[button_id][1] * 64) + 32.; true_y = (Left_Y + Button_Coords[button_id][2] * 64) + 32.
            Button[button_id] = db_id
                // if button has icon
                if icon_id > 0 {
                    Button_Icon[button_id] = CreateDestructable(icon_id, true_x, true_y, 0., 1.1, 0)
                }
    }
    
    // that attach database data at button
    public int SetAttach(int id, int icon_id, int x, int y){
        int free_button, potential_button
        real true_x , true_y
                if id == 0 { return 0 }
                if (x == -1 or y == -1) { free_button = GetFree() }
                else {
                    potential_button = GetByCoords(x, y)
                        if potential_button != 0 { free_button = potential_button }
                        else { free_button = GetFree() }
                }
            true_x = (Left_X + Button_Coords[free_button][1] * 64) + 32.; true_y = (Left_Y + Button_Coords[free_button][2] * 64) + 32.
            Button[free_button] = id
                // if button has icon
                if icon_id > 0 {
                    Button_Icon[free_button] = CreateDestructable(icon_id, true_x, true_y, 0., 1.1, 0)
                }
        return free_button
    }
    
    public int Add(int cell_x, int cell_y, trigger click_react){
        int button_id
        real true_x = (Left_X + cell_x * 64) + 32., true_y = (Left_Y + cell_y * 64) + 32.
        trackable new_trackable           
            button_id = GetByCoords(cell_x, cell_y)
            if Button_Trackable[button_id] != null  { 
                TriggerRegisterTrackableHitEvent(click_react, Button_Trackable[button_id])
                return button_id
            }
            else {
                new_trackable = CreateTrackable("war3mapImported\\GUI_Icon_Base_Trackable.mdx", true_x, true_y, 0.)
                ButtonCount++
                Button[ButtonCount] = 0
                Button_Trackable[ButtonCount] = new_trackable
                Button_Coords[ButtonCount][1] = cell_x
                Button_Coords[ButtonCount][2] = cell_y
                Button_Text[ButtonCount] = null
                TriggerRegisterTrackableHitEvent(click_react, new_trackable)
            }
        new_trackable = null
        return ButtonCount
    }
    
endlibrary
Top