Thread: Sliders on Interfaces

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31
  1. #1 Sliders on Interfaces 
    nice


    Join Date
    Jul 2014
    Posts
    708
    Thanks given
    341
    Thanks received
    472
    Discord
    View profile
    Rep Power
    3695
    Wanted to use sliders on one of the interfaces i was making and the existing release of sliders that i found used several unnecessary things and the code for interfaces that used sliders was all stuck into 1 method, so i decided to write my own much cleaner version.

    Create a new class called WidgetSlider
    Code:
    /**
     * @Author Suic
     * Created on 9/9/2020
     */
    public class WidgetSlider {
    
        private float minValue, maxValue, value;
        private int sliderWidth;
        private SimpleImage background, handle;
    
    
        public WidgetSlider(int minValue, int maxValue, int defaultValue, SimpleImage background, SimpleImage handle) {
            this.minValue = minValue;
            this.maxValue = maxValue;
            this.value = defaultValue;
            this.background = background;
            this.handle = handle;
            this.sliderWidth = background.width;
        }
    
        public void render(int x, int y) {
            background.drawSprite(x, y);
            int handleX = (int) MathUtils.map(value, minValue, maxValue, 0, sliderWidth - (handle.width / 2f));
            handle.drawSprite(x + handleX, y - 1);
        }
    
        public void handleSlider(int x) {
            value = MathUtils.map(x, 0, sliderWidth - 1, minValue, maxValue); // mapping to sliderWidth - 1 because x goes from 0 to width - 1
        }
    
        public float getValue() {
            return value;
        }
    }
    map method:
    Code:
    public static float map(float value, float istart, float istop, float ostart, float ostop) {
            return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
        }
    in Widget/RSInterface:
    Code:
        public WidgetSlider widgetSlider;
    
        public static void addSlider(int id, int minValue, int maxValue, int defaultValue, int backgroundSprite, int handleSprite) {
            Widget widget = addInterface(id);
            SimpleImage background = Client.spriteCache.get(backgroundSprite);
            SimpleImage handle = Client.spriteCache.get(handleSprite);
            widget.widgetSlider = new WidgetSlider(minValue, maxValue, defaultValue, background, handle);
            widget.width = background.width;
            widget.height = background.height;
            widget.type = TYPE_SLIDER;
        }
    in buildInterfaceMenu inside where it iterates thru each component

    Code:
    if (child.type == Widget.TYPE_SLIDER && mousePressed) {
                        if (mouseX >= childX && mouseY >= childY
                            && mouseX < childX + child.width && mouseY < childY + child.height) {
                            int xPos = mouseX - childX;
                            child.widgetSlider.handleSlider(xPos);
                        }
                    }
    in drawInterface inside where it iterates thru each component
    Code:
    } else if (child.type == Widget.TYPE_SLIDER) {
                        WidgetSlider widgetSlider = child.widgetSlider;
                        widgetSlider.render(child_x_in_bounds, child_y_in_bounds);
    Usage:
    Code:
    addSlider(id, 10, 75, 10, 615, 606);
    Example of it used on a interface(i know that the slider sprites don't fit the interface, only used it for testing):
    Reply With Quote  
     


  2. #2  
    Banned

    Join Date
    Jun 2019
    Posts
    203
    Thanks given
    99
    Thanks received
    44
    Rep Power
    0
    Sick interface! And GJ on improving the slider.
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Donator
    snurf's Avatar
    Join Date
    Oct 2015
    Posts
    582
    Thanks given
    70
    Thanks received
    90
    Rep Power
    0
    That is quite impressive!

    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Registered Member

    Join Date
    Aug 2019
    Posts
    14
    Thanks given
    0
    Thanks received
    17
    Rep Power
    853
    Sick interface!
    Reply With Quote  
     

  7. #5  
    Registered Member

    Join Date
    Feb 2010
    Posts
    3,236
    Thanks given
    1,140
    Thanks received
    873
    Discord
    View profile
    Rep Power
    1872
    Nice interface though

    Consider using static factory method over constructor i.e

    Code:
    public static WidgetSlider createSlider(int min, int max, int default, SimpleImage background, SimpleImage handle) {
    	return new WidgetSlider(min, max, default, background, handle);
    }
    And making widget slider private

    Code:
     
    private WidgetSlider slider;
    
    slider = WidgetSlider.createSlider(...);
    
    widget.setSlider(slider);
    Reply With Quote  
     

  8. #6  
    nice


    Join Date
    Jul 2014
    Posts
    708
    Thanks given
    341
    Thanks received
    472
    Discord
    View profile
    Rep Power
    3695
    Quote Originally Posted by Fire Cape View Post
    Nice interface though

    Consider using static factory method over constructor i.e

    Code:
    public static WidgetSlider createSlider(int min, int max, int default, SimpleImage background, SimpleImage handle) {
    	this.min = min;
    	this.max = max;
    	this.default = default;
    	this.background = background;
    	this.handle = handle;
    	return new WidgetSlider(min, max, default, background, handle);
    }
    And making widget slider private

    Code:
     
    private WidgetSlider slider;
    
    slider = WidgetSlider.createSlider(...);
    
    widget.setSlider(slider);
    thanks, could you explain why i should use a static factory method over constructor in this case? i did some quick research on it but still not 100% certain whether i understood it correctly or not
    Reply With Quote  
     

  9. #7  
    Registered Member

    Join Date
    Feb 2010
    Posts
    3,236
    Thanks given
    1,140
    Thanks received
    873
    Discord
    View profile
    Rep Power
    1872
    Quote Originally Posted by Suic View Post
    thanks, could you explain why i should use a static factory method over constructor in this case? i did some quick research on it but still not 100% certain whether i understood it correctly or not
    1. Flexibility, say you wanted a method where you wanted to create WidgetSlider but without a default (maybe it is 0) you can name a static factory method whereas you cannot name a constructor.

    2. Access, you can keep the WidgetSlider constructor private for encapsulation purposes

    3. If you only needed to use a single instance of WidgetSlider you could use a singleton to limit object creation to only one particular WidgetSlider instance, this will reduce the need to create an object everytime the method is called since it can reuse the same instance.
    Reply With Quote  
     

  10. #8  
    Contributor
    Women's Avatar
    Join Date
    Mar 2010
    Posts
    1,857
    Thanks given
    695
    Thanks received
    1,112
    Discord
    View profile
    Rep Power
    4264
    looks great gj
    Reply With Quote  
     

  11. #9  
    #1 Interface Services

    Herb's Avatar
    Join Date
    Nov 2015
    Posts
    573
    Thanks given
    131
    Thanks received
    655
    Discord
    View profile
    Rep Power
    3765
    Nice interface man!
    Spoiler for signature:
    Reply With Quote  
     

  12. #10  
    nice


    Join Date
    Jul 2014
    Posts
    708
    Thanks given
    341
    Thanks received
    472
    Discord
    View profile
    Rep Power
    3695
    Quote Originally Posted by Fire Cape View Post
    1. Flexibility, say you wanted a method where you wanted to create WidgetSlider but without a default (maybe it is 0) you can name a static factory method whereas you cannot name a constructor.

    2. Access, you can keep the WidgetSlider constructor private for encapsulation purposes

    3. If you only needed to use a single instance of WidgetSlider you could use a singleton to limit object creation to only one particular WidgetSlider instance, this will reduce the need to create an object everytime the method is called since it can reuse the same instance.
    hmm, i don't think in this case a factory method gives me any adventage here, afaik in general they're used when the creation is complex, has defaults or i can give it a better name, also imo patching fields is bad(modifying the fields directly outside the constructor/an instance method) eg in the createSlider() method:
    Code:
    widgetSlider.minValue = minValue;
    I think better than a factory method would be a Builder but that sounds like overkill to me in this scenario.
    correct me if im wrong on something tho, i just based this reply off some research
    Reply With Quote  
     

Page 1 of 4 123 ... LastLast

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. Replies: 5
    Last Post: 01-02-2009, 05:33 AM
  2. Replies: 13
    Last Post: 05-10-2008, 12:58 AM
  3. Replies: 17
    Last Post: 04-28-2008, 01:10 PM
  4. Fading on interfaces / displays and such
    By Clienthax in forum RS2 Client
    Replies: 15
    Last Post: 04-12-2008, 03:02 PM
  5. Replies: 30
    Last Post: 02-10-2008, 04:48 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
  •