• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

UIButton

Level 19
Joined
Mar 18, 2012
Messages
1,716

UIButton


Creates buttons for full screen systems. Pure visual.

JASS:
library UIButton uses UIBasic/* v 1.0
**************************************************************
*
*   Creates buttons for full screen systems. 
*   Those are interactable fields within an UI.
*   Buttons can store data and changed their model.
*   
*   Can be of type destructable or image.
*
**************************************************************
*/
    globals
        //*  Access via UI_GetLastCreatedButton()
        private integer lastCreated = 0
    endglobals

    struct UIButton //* [SIZE]
        
        //*  Available native types for a button:
        //*  ====================================
        private destructable dest
        private image        img
        
        readonly player      user
        readonly integer     typeId
        readonly real        scale
        readonly Track       track
        
        //*  Button textures:
        //*  ================
        readonly string  tempStr
        readonly integer tempId
        private  string  defStr
        private  integer defId
        method operator defaultStr takes nothing returns string
            return defStr
        endmethod
        method operator defaultId takes nothing returns integer
            return defId
        endmethod
        method operator defaultStr= takes string path returns nothing
            set defaultId = 0
            set defStr    = path
        endmethod
        method operator defaultId= takes integer id returns nothing
            set defStr = null
            set defId  = id
        endmethod


        //*  Enable/Disable buttons i.e. button.enabled = false
        //* Disabled buttons do not display their image handle.
        boolean enabled
        
        //*  Transfer data:
        //*  ==============
        //* There to save reference to other systems on a button.
        //* i.e. store heal ability: button.data = 'Ahea'
        //* UnitAddAbility(myUnit, clicked.button.data)
        integer data
        
        //*  Wrappers to x, y, z and facing of the Track instance.
        //! textmacro PUBLIC_UI_IMAGE_BUTTON_OPERATOR takes NAME
            public method operator $NAME$ takes nothing returns real
                return this.track.$NAME$
            endmethod
        //! endtextmacro
        //! runtextmacro PUBLIC_UI_IMAGE_BUTTON_OPERATOR("x") 
        //! runtextmacro PUBLIC_UI_IMAGE_BUTTON_OPERATOR("y") 
        //! runtextmacro PUBLIC_UI_IMAGE_BUTTON_OPERATOR("z") 
        //! runtextmacro PUBLIC_UI_IMAGE_BUTTON_OPERATOR("facing") 
        
        method purge takes nothing returns nothing
            if (img != null) then
                call ReleaseImage(img)
                set img = null
            endif
            if (dest != null) then
                call RemoveDestructable(dest)
                set dest = null
            endif
        endmethod
        
        method remove takes nothing returns nothing
            call purge()
            set tempStr = null
            set tempId  = 0
            if (enabled) then
                if (defStr != null) then
                    set img = NewImage(defStr, UI_TILE_SIZE*scale, UI_TILE_SIZE*scale, x - UI_TILE_SIZE/2*scale, y - UI_TILE_SIZE/2*scale, 0, IMAGE_TYPE_OCCLUSION_MASK)
                    call SetImageRenderAlways(img, GetLocalClient() == user)
                elseif (0 != defId) then
                    set dest = CreateDestructableZ(defId, x, y, z - UI_DEFAULT_BUTTON_Z*scale, facing, scale, 0)
                    call ShowDestructable(dest, GetLocalClient() == user)
                endif
            endif
        endmethod

        method addDest takes integer id, boolean flag returns nothing
            call purge()
            set tempId = id
            if (enabled) and (flag) then
                set dest = CreateDestructableZ(id, x, y, z - UI_DEFAULT_BUTTON_Z*scale, facing, scale, 0)
                call ShowDestructable(dest, GetLocalClient() == user)
                debug call ThrowWarning(GetDestructableTypeId(dest)== 0, "UIButton", "add", "id", 0, "Invalid destructable id ( 0 )")
            endif
        endmethod
        
        method addImage takes string file, boolean flag returns nothing
            call purge()
            set tempStr = file
            if (enabled) and (flag) then
                set img = NewImage(file, UI_TILE_SIZE*scale, UI_TILE_SIZE*scale, x - UI_TILE_SIZE/2*scale, y - UI_TILE_SIZE/2*scale, 0, IMAGE_TYPE_OCCLUSION_MASK)
                call SetImageRenderAlways(img, GetLocalClient() == user)
            endif
        endmethod
        
        method show takes boolean flag returns nothing
            local boolean b = (tempStr == null) and (tempId == 0) 
            
            if not (flag) then
                call purge()
            elseif (b) then 
                call remove()
            elseif (tempStr != null) then
                call addImage(tempStr, true)
            elseif (tempId != 0) then
                call addDest(tempId, true)
            endif
        endmethod
        
        method destroy takes nothing returns nothing
            call purge()
            set user  = null
            set data  = 0
            set track = 0
            call deallocate()
        endmethod
        
        static method createDest takes integer whichType, player p, integer objectId, Track whichTrack, real size, boolean isEnabled returns thistype
      local thistype this = thistype.allocate()
            set user      = p
            set defId     = objectId
            set defStr    = null
            set scale     = size
            set enabled   = isEnabled
            set typeId    = whichType
            set track     = whichTrack
            set tempId    = 0
            
            debug call ThrowError((not IsPlayerUser(p)), "UIButton", "create", "p", this, "Invalid player handle ( Computer player ); ID:[" + I2S(GetPlayerId(p)) + "]")
            debug call ThrowWarning((p == null), "UIButton", "create", "p", this, "Invalid player handle ( null )!")
            set lastCreated = this
            return this
       endmethod
        
        static method createImg takes integer whichType, player p, string file, Track whichTrack, real size, boolean isEnabled returns thistype
      local thistype this = thistype.allocate()
            set user      = p
            set defStr    = file
            set defId     = 0
            set tempStr   = null
            set scale     = size
            set enabled   = isEnabled
            set typeId    = whichType
            set track     = whichTrack
            
            debug call ThrowWarning((p == null),         "UIButton", "create", "p", this, "Invalid player handle ( null )!")
            debug call ThrowError((not IsPlayerUser(p)), "UIButton", "create", "p", this, "Invalid player handle ( Computer player ); ID:[" + I2S(GetPlayerId(p)) + "]")
            set lastCreated = this
            return this
        endmethod
            
    endstruct

    constant function UI_GetLastCreatedButton takes nothing returns UIButton
        return lastCreated
    endfunction
    
endlibrary
Last edited:
Back
Top