Thread: Making toggle buttons (checkboxes, etc)

Results 1 to 10 of 10
  1. #1 Making toggle buttons (checkboxes, etc) 


    Omar's Avatar
    Join Date
    Dec 2007
    Posts
    279
    Thanks given
    640
    Thanks received
    783
    Rep Power
    5000
    A few people in #free-help were asking about creating "toggle buttons", so here's how to do it. Big thanks to Dane; all code in this thread (minus the actual button creation part) is from his 186 refactor.

    Interfaces have various "enabled" and "disabled" properties that let you change how the interface is displayed based on its current state. Examples are things like changing text color when enabled/disabled, or, in our case, changing the sprite based on the state.

    See below for a list of these properties (186):

    Code:
        public String messageDisabled;
        public String messageEnabled;
        public int colorDisabled;
        public int colorEnabled;
        public Sprite spriteDisabled;
        public Sprite spriteEnabled;
        public Model modelDisabled;
        public Model modelEnabled;
    The method below determines whether or not an interface is enabled. "scriptCompareType" is essentially which operator to use when making the comparison against the expected value ("2" being >=, "3" being <=, "4" being ==, and anything else being !=).

    Code:
        public final boolean isInterfaceEnabled(final RSInterface rsInterface) {
            if (rsInterface.scriptCompareType == null) {
                return false;
            }
    
            for (int n = 0; n < rsInterface.scriptCompareType.length; n++) {
                int a = executeInterface(rsInterface, n);
                int b = rsInterface.scriptCompareValue[n];
    
                if (rsInterface.scriptCompareType[n] == 2) {
                    if (a >= b) {
                         return false;
                    }
                } else if (rsInterface.scriptCompareType[n] == 3) {
                    if (a <= b) {
                         return false;
                    }
                } else if (rsInterface.scriptCompareType[n] == 4) {
                    if (a == b) {
                         return false;
                    }
                } else if (a != b) {
                     return false;
                }
            }
            return true;
        }
    In the code above, you'll see a call to a method named "executeInterface()". Below, you can find an excerpt of it containing the relevant parts, otherwise, if you want to see the full thing, click the link. What this method does is essentially execute a CS1 script and return a value. There are a few different opcodes that will determine what exactly we want to perform, but, in our case, we only need to care about #5, which fetches and accumulates a value from a varp, and #0 which returns the accumulator.

    Code:
        public final int executeInterface(RSInterface parent, int script) {
            if (parent.script == null || script >= parent.script.length) {
                return -2;
            }
    
            try {
                int[] code = parent.script[script];
                int a = 0;
                int position = 0;
    
                for (; ; ) {
                    int opcode = code[position++];
    
                    if (opcode == 0) {
                        return a;
                    }
    
                ...
    
                if (opcode == 5) {
                    a += variables[code[position++]];
                }
                ...
            }
        }
    Now that we have some of the pre-requisite knowledge out of the way, we can move onto the buttons themselves. When the button type of an interface is a toggle button (4) and the opcode of the first CS1 script is 5, we execute the block of code below. Essentially, it fetches the value of a varp and then sets the varp to be equal to 1 minus the value of itself. This allows us to create a toggle button by flipping between 1 and 0 (if our value is 1, subtracting 1 by 1 will set it to 0, and if it is currently 0, subtracting 1 by 0 will ofc be 1).

    Code:
        if (type == 739) {
            out.putOpcode(101);
            out.putShort(c);
            RSInterface w = RSInterface.instances[c];
            if (w.script != null && w.script[0][0] == 5) {
                int j = w.script[0][1];
                variables[j] = 1 - variables[j];
                updateVarp(j);
                sidebarRedraw = true;
            }
        }
    Finally, we can put all we've learned together and build some methods to create a toggle button:

    Code:
        public static void addToggleButton(final int index, final int parentIndex, final int varpIndex, final String tooltip, final String enabledSprite, final String disabledSprite) {
            final Sprite enabled = Sprite.fetchSprite(enabledSprite);
            final Sprite disabled = Sprite.fetchSprite(disabledSprite);
            final RSInterface rsInterface = instances[index] = new RSInterface();
            rsInterface.index = index;
            rsInterface.parent = parentIndex;
            rsInterface.script = new int[][]{{5, varpIndex, 0}};
            rsInterface.scriptCompareValue = new int[]{1};
            rsInterface.scriptCompareType = new int[]{1};
            rsInterface.width = enabled.myWidth;
            rsInterface.height = enabled.myHeight;
            rsInterface.buttonType = TOGGLE_BUTTON; // 4
            rsInterface.type = TYPE_SPRITE; // 5
            rsInterface.spriteEnabled = enabled;
            rsInterface.spriteDisabled = disabled;
            rsInterface.tooltip = tooltip;
        }
    
        public static void addToggleButton(final int index, final int parentIndex, final int varpIndex, final String tooltip, final String spriteGroup) {
            // SPRITE_TEMPLATE = "%s,%d"
            addToggleButton(index, parentIndex, varpIndex, tooltip, String.format(SPRITE_TEMPLATE, spriteGroup, 0), String.format(SPRITE_TEMPLATE, spriteGroup, 1));
        }
    
        public static void addToggleButton(final int index, final int varpIndex, final String tooltip, final String spriteGroup) {
            addToggleButton(index, index, varpIndex, tooltip, spriteGroup);
        }


    Thank u for listening. Special thanks to:

    Last edited by Omar; 01-28-2021 at 07:59 AM.
    Attached image
    Reply With Quote  
     


  2. #2  
    Registered Member
    rebecca's Avatar
    Join Date
    Aug 2017
    Posts
    1,071
    Thanks given
    862
    Thanks received
    915
    Rep Power
    5000
    useful knowledge, i suggest people interested in interfaces read this and reduce hardcoding habits when not needed.
    cs1 can apply to a bunch of different places and can make some (bad) server code obsolete, make sure u read the opcodes =] (e.g skill requirements, coordinates)

    this can be used for different things, such as changing the color of text/rectangles, changing text from one string to another, changing spellbook interfaces and a few other things. (for example, changing NO to YES, or things like sprites changing based on things like skill requirements, coordinates, having an item etc)
    Last edited by rebecca; 01-28-2021 at 12:21 PM.
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    plz dont take my wizard mind bombs Women's Avatar
    Join Date
    Mar 2010
    Posts
    1,881
    Thanks given
    724
    Thanks received
    1,162
    Rep Power
    4763
    ooo so informative

    very nice contribution! anime button
    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Registered Member
    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    26
    Posts
    5,103
    Thanks given
    1,818
    Thanks received
    1,767
    Rep Power
    2438
    good work bbc
    Quote Originally Posted by blakeman8192 View Post
    Keep trying. Quitting is the only true failure.
    Spoiler for skrrrrr:

    Attached image
    Reply With Quote  
     

  7. Thankful user:


  8. #5  
    Respected Member


    George's Avatar
    Join Date
    Mar 2009
    Posts
    7,099
    Thanks given
    2,226
    Thanks received
    3,146
    Rep Power
    5000
    This is nuts! Top goat contribution!
    Attached image

    Spoiler for Spoilers!:
    Attached image
    Attached image
    Attached image
    Attached image
    Reply With Quote  
     

  9. Thankful users:


  10. #6  
    DESIGNER

    Lynch's Avatar
    Join Date
    Feb 2016
    Age
    25
    Posts
    235
    Thanks given
    35
    Thanks received
    343
    Rep Power
    5000
    Woah very nice and useful contribution mate!
    Reply With Quote  
     

  11. Thankful user:


  12. #7  
    'Slutty McFur'

    Owain's Avatar
    Join Date
    Sep 2014
    Age
    26
    Posts
    2,894
    Thanks given
    2,360
    Thanks received
    2,200
    Rep Power
    5000
    hot stuff, hot stuff.


    Spoiler for wat:
    Attached image
    Attached image

    Attached image


    Reply With Quote  
     

  13. Thankful user:


  14. #8  
    nice


    Join Date
    Jul 2014
    Posts
    740
    Thanks given
    382
    Thanks received
    562
    Rep Power
    4239
    good release
    Reply With Quote  
     

  15. #9  
    Registered Member

    Join Date
    Feb 2010
    Posts
    3,253
    Thanks given
    1,145
    Thanks received
    909
    Rep Power
    2081
    nice, cs1 is surprisingly poorly documented, i added the 377 cache to an mitb deob a while back and had to add opcode 8, didn't find an explanation anywhere
    Reply With Quote  
     

  16. Thankful user:


  17. #10  
    No oneMaking toggle buttons (checkboxes, etc)

    Mr. Remix's Avatar
    Join Date
    Apr 2020
    Posts
    117
    Thanks given
    21
    Thanks received
    132
    Rep Power
    1083
    Nice work, thanks for sharing this!
    Discord : Remix#1157
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Sigex - Final complete music toggle button
    By sigex in forum Tutorials
    Replies: 44
    Last Post: 01-28-2010, 09:07 AM
  2. Make save buttons client sided
    By netzo in forum Help
    Replies: 0
    Last Post: 09-17-2009, 03:23 PM
  3. Replies: 3
    Last Post: 07-16-2009, 03:38 AM
  4. Replies: 3
    Last Post: 04-10-2008, 10:57 AM
  5. Making Login Buttons/Boxs (FREE!)
    By ThatGuyGlazed in forum Graphics
    Replies: 7
    Last Post: 03-08-2008, 03:51 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •