Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Otreum (posted 2014-01-22 10:56:18, viewed 107 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510   511   512   513   514   515   516   517   518   519   520   521   522   523   524   525   526   527   528   529   530   531   532   533   534   535   536   537   538   539   540   541   542   543   544   545   546   547   548   549   550   551   552   553   554   555   556   557   558   559   560   561   562   563   564   565   566   567   568   569   570   571   572   573   574   575   576   577   578   579   580   581   582   583   584   585   586   587   588   589   590   591   592   593   594   595   596   597   598   599   600   601   602   603   604   605   606   607   608   609   610   611   612   613   614   615   616   617   618   619   620   621   622   623   624   625   626   627   628   629   630   631   632   633   634   635   636   637   638   639   640   641   642   643   644   645   646   647   648   649   650   651   652   653   654   655   656   657   658   659   660   661   662   663   664   665   666   667   668   669   670   671   672   673   674   675   676   677   678   679   680   681   682   683   684   685   686   687   688   689   690   691   692   693   694   695   696   697   698   699   700   701   702   703   704   705   706   707   708   709   710   711   712   713   714   715   716   717   718   719   720   721   722   723   724   725   726   727   728   729   730   731   732   733   734   735   736   737   738   739   740   741   742   743   744   745   746   747   748   749   750   751   752   753   754   755   756   757   758   759   760   761   762   763   764   765   766   767   768   769   770   771   772   773   774   775   776   777   778   779   780   781   782   783   784  
package Engine.Utils;

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.badlogic.gdx.scenes.scene2d.utils.Disableable;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Pools;

/** A slider is a horizontal indicator that allows a user to set a value. The slider has a range (min, max) and a stepping between
 * each value the slider represents.
 * <p>
 * {@link ChangeEvent} is fired when the slider knob is moved. Cancelling the event will move the knob to where it was previously.
 * <p>
 * The preferred height of a slider is determined by the larger of the knob and background. The preferred width of a slider is
 * 140, a relatively arbitrary size.
 * @author mzechner
 * @author Nathan Sweet */
public class MultiRangeSlider extends Widget implements Disableable {
   private MRSSliderStyle style;
   
   //KNOB STUFF
   private float min, max;
   private float stepSize;
   private float value, animateFromValue;
   private float sliderPos; 
   private float animateDuration, animateTime;
   private Interpolation animateInterpolation = Interpolation.linear;
   //KNOB 2 STUFF
   private float min2, max2;
   private float value2, animateFromValue2;
   private float sliderPos2;
   private float animateDuration2, animateTime2;
   private Interpolation animateInterpolation2 = Interpolation.linear;
   
   //3rd value
   float min3, max3;
   float value3;
   
   //vals
   float val1,val2,val3;
   
   private final boolean vertical;
   int draggingPointer = -1;

   private float[] snapValues;
   private float threshold;
   boolean disabled;
   
   float x;
   float y;

   public MultiRangeSlider (float min, float max,  float min2, float max2, float min3, float max3, float stepSize, boolean vertical, Skin skin) {
      this(min, max, min2, max2, min3, max3, stepSize, vertical, skin.get("default-" + (vertical ? "vertical" : "horizontal"), MRSSliderStyle.class));
   }

   public MultiRangeSlider (float min, float max, float min2, float max2, float min3, float max3, float stepSize, boolean vertical, Skin skin, String styleName) {
      this(min, max, min2, max2, min3, max3, stepSize, vertical, skin.get(styleName, MRSSliderStyle.class));
   }

   /** Creates a new slider. It's width is determined by the given prefWidth parameter, its height is determined by the maximum of
    * the height of either the slider {@link NinePatch} or slider handle {@link TextureRegion}. The min and max values determine
    * the range the values of this slider can take on, the stepSize parameter specifies the distance between individual values.
    * E.g. min could be 4, max could be 10 and stepSize could be 0.2, giving you a total of 30 values, 4.0 4.2, 4.4 and so on.
    * @param min the minimum value
    * @param max the maximum value
    * @param stepSize the step size between values
    * @param style the {@link SliderStyle} */
   public MultiRangeSlider (float min, float max, float min2, float max2, float min3, float max3,float stepSize, boolean vertical, MRSSliderStyle style) {
      if (min > max) throw new IllegalArgumentException("min must be > max: " + min + " > " + max); //If min value is higher than the maximum, throw out an error.
      if (stepSize <= 0) throw new IllegalArgumentException("stepSize must be > 0: " + stepSize); //if the step size is lower than 0, throw out an error, there needs to be some sort of step size.
      if (min2 > max2) throw new IllegalArgumentException("min must be > max: " + min2 + " > " + max2); //If min value is higher than the maximum, throw out an error.
      if (stepSize <= 0) throw new IllegalArgumentException("stepSize must be > 0: " + stepSize); //if the step size is lower than 0, throw out an error, there needs to be some sort of step size.
      setStyle(style);
      this.min = min; //minimum value of the range of knob
      this.max = max; //maximum value of the range of knob
      this.min2 = min2; //minimum value of the range of knob2
      this.max2 = max2; //minimum value of the range of knob2
      this.min3 = min3;
      this.max3 = max3;
      this.stepSize = stepSize; //Determines how many steps the knob makes between min and max
      this.vertical = vertical; //Determines if this is a vertical slider or not
      this.value = min;
      this.value2 = min2;
      this.value3 = min3;
      this.val1 = value;
      this.val2 = value2;
      this.val3 = value3;
      setWidth(getPrefWidth());
      setHeight(getPrefHeight());
      this.sliderPos = max * 0.35f;
      this.sliderPos2 = max2 * 0.75f;
      initKnob(sliderPos);
      initKnob2(sliderPos2);
      
      addListener(new InputListener() {
         
         float x;
         
         boolean isKnobTouched = isTouchingKnob(sliderPos, x);
         boolean isKnobTouched2 = isTouchingKnob2(sliderPos2, x);
         
         public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            if (disabled) return false;
            if (draggingPointer != -1) return false;
            draggingPointer = pointer;
            
            isKnobTouched = isTouchingKnob(sliderPos, x);
            isKnobTouched2 = isTouchingKnob2(sliderPos2, x);
            
            if (isKnobTouched == true){
               
               calculatePositionAndValue(x, y);
            }
            if (isKnobTouched2 == true){
               calculatePositionAndValue2(x, y);
            }
            return true;
         }

         public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            if (pointer != draggingPointer) return;
            draggingPointer = -1;   
            this.x = x;
            
            if(isKnobTouched == true){
               
               if (!calculatePositionAndValue(x, y)) {
               // Fire an event on touchUp even if the value didn't change, so listeners can see when a drag ends via isDragging.
               ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
               fire(changeEvent);
               Pools.free(changeEvent);
               }
            }
            if (isKnobTouched2 == true){
               
               if (!calculatePositionAndValue2(x, y)) {
                  // Fire an event on touchUp even if the value didn't change, so listeners can see when a drag ends via isDragging.
                  ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
                  fire(changeEvent);
                  Pools.free(changeEvent);
               }
            }
            
         }

         public void touchDragged (InputEvent event, float x, float y, int pointer) {
            this.x = x;
            
            if(isKnobTouched == true){
               
               calculatePositionAndValue(x, y);
            }
            if (isKnobTouched2 == true){
               
               calculatePositionAndValue2(x, y);
            }
         }
      });
   }

   public void setStyle (MRSSliderStyle style) {
      if (style == null) throw new IllegalArgumentException("style cannot be null.");
      this.style = style;
      invalidateHierarchy();
   }

   /** Returns the slider's style. Modifying the returned style may not have an effect until {@link #setStyle(SliderStyle)} is
    * called. */
   public MRSSliderStyle getStyle () {
      return style;
   }

   public void act (float delta) {
      super.act(delta);
      animateTime -= delta;
   }

   @Override
   public void draw (Batch batch, float parentAlpha) {
      MRSSliderStyle style = this.style;
      boolean disabled = this.disabled;
      final Drawable knob = (disabled && style.disabledKnob != null) ? style.disabledKnob : style.knob;
      final Drawable knob2 = (disabled && style.disabledKnob2 != null) ? style.disabledKnob2 : style.knob2;
      
      final Drawable bg = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
      
      final Drawable knobBefore = (disabled && style.disabledKnobBefore != null) ? style.disabledKnobBefore : style.knobBefore;
      final Drawable knobAfter = (disabled && style.disabledKnobAfter != null) ? style.disabledKnobAfter : style.knobAfter;
      
      final Drawable knob2Before = (disabled && style.disabledKnob2Before != null) ? style.disabledKnob2Before : style.knob2Before;
      final Drawable knob2After = (disabled && style.disabledKnob2After != null) ? style.disabledKnob2After : style.knob2After;

      Color color = getColor();
      float x = getX();
      float y = getY();
      float width = getWidth();
      float height = getHeight();
            
      float knobHeight = knob == null ? 0 : knob.getMinHeight();
      float knobWidth = knob == null ? 0 : knob.getMinWidth();
      
      float knob2Height = knob2 == null ? 0 : knob2.getMinHeight();
      float knob2Width = knob2 == null ? 0 : knob2.getMinWidth();
      
      float value = getVisualValue();
      float value2 = getVisualValue2();
      
      batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

      if (vertical) { //IF THIS IS A VERTICAL SLIDER
         bg.draw(batch, x + (int)((width - bg.getMinWidth()) * 0.5f), y, bg.getMinWidth(), height);

         float sliderPosHeight = height - (bg.getTopHeight() + bg.getBottomHeight());
         float sliderPos2Height = height - (bg.getTopHeight() + bg.getBottomHeight());
         
         if (min != max) {
            sliderPos = (value - min) / (max - min) * (sliderPosHeight - knobHeight);
            sliderPos = Math.max(0, sliderPos);
            sliderPos = Math.min(sliderPosHeight - knobHeight, sliderPos) + bg.getBottomHeight();
         }
         if (min2 != max2){
            sliderPos2 = (value2 - min2) / (max2 - min2) * (sliderPos2Height - knob2Height);
            sliderPos2 = Math.max(0, sliderPos2);
            sliderPos2 = Math.min(sliderPos2Height - knob2Height, sliderPos2) + bg.getBottomHeight();
         }

         //Get the mid point of the knobs (half their height)
         float knobHeightHalf = knobHeight * 0.5f; 
         float knob2HeightHalf = knob2Height * 0.5f;
         
         //KNOB BEFORE AND AFTER - This determines what is drawn infront of and behind the knob
         if (knobBefore != null) {
            knobBefore.draw(batch, x + (int)((width - knobBefore.getMinWidth()) * 0.5f), y, knobBefore.getMinWidth(),
               (int)(sliderPos + knobHeightHalf));
         }
         if (knobAfter != null) {
            knobAfter.draw(batch, x + (int)((width - knobAfter.getMinWidth()) * 0.5f), y + (int)(sliderPos + knobHeightHalf),
               knobAfter.getMinWidth(), height - (int)(sliderPos + knobHeightHalf));
         }
         //KNOB2 BEFORE AND AFTER - This determines what is drawn infront of and behind the knob
         if (knob2Before != null) {
            knob2Before.draw(batch, x + (int)((width - knob2Before.getMinWidth()) * 0.5f), y, knob2Before.getMinWidth(),
               (int)(sliderPos2 + knob2HeightHalf));
         }
         
         if (knob2After != null) {
            knob2After.draw(batch, x + (int)((width - knob2After.getMinWidth()) * 0.5f), y + (int)(sliderPos2 + knob2HeightHalf),
                  knob2After.getMinWidth(), height - (int)(sliderPos2 + knob2HeightHalf));
         }
         if (knob != null) {
            knob.draw(batch, x + (int)((width - knobWidth) * 0.5f), (int)(y + sliderPos), knobWidth, knobHeight);
         }
         if (knob2 != null){
            knob2.draw(batch, x + (int)((width - knob2Width) * 0.5f), (int)(y + sliderPos2), knob2Width, knob2Height);
         }
      } else { //IF A HORIZONTAL SLIDER
         bg.draw(batch, x, y + (int)((height - bg.getMinHeight()) * 0.5f), width, bg.getMinHeight());

         float sliderPosWidth = width - (bg.getLeftWidth() + bg.getRightWidth());
         float sliderPos2Width = width - (bg.getLeftWidth() + bg.getRightWidth());
         if (min != max) {
            sliderPos = (value - min) / (max - min) * (sliderPosWidth - knobWidth);
            sliderPos = Math.max(0, sliderPos);
            sliderPos = Math.min(sliderPosWidth - knobWidth, sliderPos) + bg.getLeftWidth();
         }
         if(min2 != max2){
            sliderPos2 = (value2 - min2) / (max2 - min2) * (sliderPos2Width - knob2Width);
            sliderPos2 = Math.max(0, sliderPos2);
            sliderPos2 = Math.min(sliderPos2Width - knob2Width, sliderPos2) + bg.getLeftWidth();
         }

         float knobHeightHalf = knobHeight * 0.5f;
         float knob2HeightHalf = knobHeight * 0.5f;
         
         //KNOB BEFORE AND AFTER - This determines what is drawn infront of and behind the knob
         if (knobBefore != null) {
            knobBefore.draw(batch, x, y + (int)((height - knobBefore.getMinHeight()) * 0.5f), (int)(sliderPos + knobHeightHalf),
               knobBefore.getMinHeight());
         }
         if (knobAfter != null) {
            knobAfter.draw(batch, x + (int)(sliderPos + knobHeightHalf), y + (int)((height - knobAfter.getMinHeight()) * 0.5f),
               width - (int)(sliderPos + knobHeightHalf), knobAfter.getMinHeight());
         }
         if (knob != null)
            knob.draw(batch, (int)(x + sliderPos), (int)(y + (height - knobHeight) * 0.5f), knobWidth, knobHeight);
         
         //KNOB2 BEFORE AND AFTER  - This determines what is drawn infront of and behind the knob
         if (knob2Before != null) {
            knob2Before.draw(batch, x, y + (int)((height - knob2Before.getMinHeight()) * 0.5f), (int)(sliderPos2 + knob2HeightHalf),
                  knob2Before.getMinHeight());
         }
         if (knob2After != null) {
            knob2After.draw(batch, x + (int)(sliderPos2 + knob2HeightHalf), y + (int)((height - knob2After.getMinHeight()) * 0.5f),
                  width - (int)(sliderPos2 + knob2HeightHalf), knob2After.getMinHeight());
         }
         if (knob2 != null)
            knob2.draw(batch, (int)(x + sliderPos2), (int)(y + (height - knob2Height) * 0.5f), knob2Width, knob2Height);
      }
   }

   //CALCULATE POSITION AND VALUE FOR KNOB
   boolean calculatePositionAndValue (float x, float y) {
      final Drawable knob = (disabled && style.disabledKnob != null) ? style.disabledKnob : style.knob;
      
      final Drawable bg = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;

      float value;
      float oldPosition = sliderPos;

      if (vertical) {
         float height = getHeight() - bg.getTopHeight() - bg.getBottomHeight();
         float knobHeight = knob == null ? 0 : knob.getMinHeight();
         
         sliderPos = y - bg.getBottomHeight() - knobHeight * 0.5f;
         value = min + (max - min) * (sliderPos / (height - knobHeight));
         
         sliderPos = Math.max(0, sliderPos);
         sliderPos = Math.min(height - knobHeight, sliderPos);
      } else {
         float width = getWidth() - bg.getLeftWidth() - bg.getRightWidth();
         float knobWidth = knob == null ? 0 : knob.getMinWidth();
                  
         sliderPos = x - bg.getLeftWidth() - knobWidth * 0.5f;
         
         value = min + (max - min) * (sliderPos / (width - knobWidth));
         
         sliderPos = Math.max(0, sliderPos);
         sliderPos = Math.min(width - knobWidth, sliderPos);
      }
      

      float oldValue = value;
      
      boolean valueSet = setValue(value);
      pushKnob2Right(sliderPos, sliderPos2);
      
      if (value == oldValue) sliderPos = oldPosition;

      
      setVals(value, this.value2);
      
      return valueSet;
      
   }
   boolean calculatePositionAndValue2 (float x2, float y2) {
      
      final Drawable knob2 = (disabled && style.disabledKnob2 != null) ? style.disabledKnob2 : style.knob2;
      
      final Drawable bg2 = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
      
      float value2;
      float oldPosition2 = sliderPos2;
      
      if (vertical) {
         float height2 = getHeight() - bg2.getTopHeight() - bg2.getBottomHeight();
         float knob2Height = knob2 == null ? 0 : knob2.getMinHeight();
         sliderPos2 = y2 - bg2.getBottomHeight() - knob2Height * 0.5f;
         value2 = min2 + (max2 - min2) * (sliderPos2 / (height2 - knob2Height));
         sliderPos2 = Math.max(0, sliderPos2);
         sliderPos2 = Math.min(height2 - knob2Height, sliderPos2);
      } else {
         float width2 = getWidth() - bg2.getLeftWidth() - bg2.getRightWidth();
         float knob2Width = knob2 == null ? 0 : knob2.getMinWidth();
         
         sliderPos2 = x2 - bg2.getLeftWidth() - knob2Width * 0.5f;
         value2 = min2 + (max2 - min2) * (sliderPos2 / (width2 - knob2Width));
         sliderPos2 = Math.max(0, sliderPos2);
         sliderPos2 = Math.min(width2 - knob2Width, sliderPos2);
      }
      
      float oldValue2 = value2;
      boolean valueSet2 = setValue2(value2);
      
      pushKnobLeft(this.sliderPos, sliderPos2);
      if (value2 == oldValue2) sliderPos2 = oldPosition2;
      
      
      setVals(this.value, value2);
      return valueSet2;
   }
   
   public void pushKnob2Right(float sliderPos, float sliderPos2){

      if(((sliderPos + 25) >= sliderPos2)){ //If right side of first knob touches left side of second knob
         if(value > value2 - 3){
         this.sliderPos = (sliderPos2 - 12);
         this.sliderPos2 = (sliderPos + 12);
         
         this.value2 = clamp(value + 3);
         
         this.value = clamp(value2 - 3);
         }
      }
   }
   
   public void pushKnobLeft(float sliderPos, float sliderPos2){
      if((sliderPos2 <= (sliderPos + 15))){
         if (value2 < value + 3){
         this.sliderPos2 = (sliderPos + 12);
         this.sliderPos = (sliderPos2 - 12);
         
         this.value = clamp(value2 - 3);
         
         this.value2 = clamp(value + 3);
         }
      }
   }
   
   boolean initKnob (float sliderPos) {
      final Drawable knob = (disabled && style.disabledKnob != null) ? style.disabledKnob : style.knob;
      
      final Drawable bg = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
      
      float value;
      float oldPosition = sliderPos;
      
      if (vertical) {
         float height = getHeight() - bg.getTopHeight() - bg.getBottomHeight();
         float knobHeight = knob == null ? 0 : knob.getMinHeight();
         
         value = min + (max - min) * (sliderPos / (height - knobHeight));
         
         sliderPos = Math.max(0, sliderPos);
         sliderPos = Math.min(height - knobHeight, sliderPos);
      } else {
         float width = getWidth() - bg.getLeftWidth() - bg.getRightWidth();
         float knobWidth = knob == null ? 0 : knob.getMinWidth();
         
         value = min + (max - min) * (sliderPos / (width - knobWidth));
         
         sliderPos = Math.max(0, sliderPos);
         sliderPos = Math.min(width - knobWidth, sliderPos);
      }
      
      float oldValue = value;
      
      boolean valueSet = setValue(value);
      if (value == oldValue) sliderPos = oldPosition;
      
      return valueSet;
      
   }

   // CALCULATE POSITION AND VALUE FOR KNOB 2
   

   boolean initKnob2 (float slider) {
      
      final Drawable knob2 = (disabled && style.disabledKnob2 != null) ? style.disabledKnob2 : style.knob2;
      
      final Drawable bg2 = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
      
      float value2;
      float oldPosition2 = sliderPos2;
      
      if (vertical) {
         float height2 = getHeight() - bg2.getTopHeight() - bg2.getBottomHeight();
         float knob2Height = knob2 == null ? 0 : knob2.getMinHeight();
         
         value2 = min2 + (max2 - min2) * (sliderPos2 / (height2 - knob2Height));
         sliderPos2 = Math.max(0, sliderPos2);
         sliderPos2 = Math.min(height2 - knob2Height, sliderPos2);
      } else {
         float width2 = getWidth() - bg2.getLeftWidth() - bg2.getRightWidth();
         float knob2Width = knob2 == null ? 0 : knob2.getMinWidth();
         
         value2 = min2 + (max2 - min2) * (sliderPos2 / (width2 - knob2Width));
         sliderPos2 = Math.max(0, sliderPos2);
         sliderPos2 = Math.min(width2 - knob2Width, sliderPos2);
      }
      
      float oldValue2 = value2;
      boolean valueSet2 = setValue2(value2);
      if (value2 == oldValue2) sliderPos2 = oldPosition2;
      return valueSet2;
   }
   
   public boolean isTouchingKnob(float sliderPos, float xpos){
      int x = (int) xpos;

      int slideposA = (int) sliderPos;
      int slideposB = (int) (sliderPos + 15);
      
      boolean isKnobTouched = false;
      
      if (x >= slideposA && x <= slideposB){
         isKnobTouched = true;
      }
      return isKnobTouched;
      
   };
   public boolean isTouchingKnob2(float sliderPos, float xpos){
      int x = (int) xpos;
      
      int slideposA = (int) sliderPos;
      int slideposB = (int) (sliderPos + 15);
      
      boolean isKnobTouched = false;

      if (x >= slideposA && x <= slideposB){
         isKnobTouched = true;
      }
      return isKnobTouched;
      
   };
   /** Returns true if the slider is being dragged. */
   public boolean isDragging () {
      return draggingPointer != -1;
   }

   //GET KNOB VALUE
   public float getValue () {
      return value;
   }
   
   //GET KNOB 2 VALUE
   public float getValue2 () {
      return value2;
   }

   /** If {@link #setAnimateDuration(float) animating} the slider value, this returns the value current displayed. */
   public float getVisualValue () {
      if (animateTime > 0){
         return animateInterpolation.apply(animateFromValue, value, 1 - animateTime / animateDuration);
      }
      return value;
   }
   public float getVisualValue2 () {
      if (animateTime2 > 0) {
         return animateInterpolation2.apply(animateFromValue2, value2, 1 - animateTime2 / animateDuration2);
      }
      return value2;
   }

   /** Sets the slider position, rounded to the nearest step size and clamped to the minumum and maximim values.
    * {@link #clamp(float)} can be overidden to allow values outside of the sliders min/max range.
    * @return false if the value was not changed because the slider already had the value or it was canceled by a listener. */
   //SET VALUE FOR KNOB
   public boolean setValue (float value) {
      value = snap(clamp(Math.round(value / stepSize) * stepSize));
      float oldValue = this.value;
      if (value == oldValue) return false;
      float oldVisualValue = getVisualValue();
      this.value = value;
      ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
      boolean cancelled = fire(changeEvent);
      if (cancelled)
         this.value = oldValue;
      else if (animateDuration > 0) {
         animateFromValue = oldVisualValue;
         animateTime = animateDuration;
      }
      Pools.free(changeEvent);
      return !cancelled;
   }
   //SET VALUE FOR KNOB 2
   public boolean setValue2 (float value2) {
      value2 = snap(clamp(Math.round(value2 / stepSize) * stepSize));
      float oldValue = this.value2;
      if (value2 == oldValue) return false;
      float oldVisualValue = getVisualValue2();
      this.value2 = value2;
      ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
      boolean cancelled = fire(changeEvent);
      if (cancelled)
         this.value2 = oldValue;
      else if (animateDuration2 > 0) {
         animateFromValue2 = oldVisualValue;
         animateTime2 = animateDuration2;
      }
      Pools.free(changeEvent);
      return !cancelled;
   }

   /** Clamps the value to the sliders min/max range. This can be overidden to allow a range different from the slider knob's
    * range. */
   protected float clamp (float v) {
      return MathUtils.clamp(v, min, max);
   }

   //SET RANGE FOR KNOB
   /** Sets the range of this slider. The slider's current value is reset to min. */
   public void setRange (float min, float max) {
      if (min > max) throw new IllegalArgumentException("min must be <= max");
      this.min = min;
      this.max = max;
      if (value < min)
         setValue(min);
      else if (value > max) setValue(max);
   }
   
   //SET RANGE FOR KNOB2
   /** Sets the range of this slider. The slider's current value is reset to min. */
   public void setRange2 (float min2, float max2) {
      if (min2 > max2) throw new IllegalArgumentException("min must be <= max");
      this.min2 = min2;
      this.max2 = max2;
      if (value2 < min2)
         setValue2(min2);
      else if (value2 > max2) setValue2(max2);
   }
   
   public void setVals(float value, float value2){
      float v1 = clamp(Math.round(value));
      float v2 = clamp(Math.round(value2));
      
      this.val1 = v1;
      this.val2 = v2 - v1;
      this.val3 = max - (val2 + val1);
   }
   
   public float getVal1(){
      return val1;
   }
   public float getVal2(){
      return val2;
   }
   public float getVal3(){
      return val3;
   }

   /** Sets the step size of the slider */
   public void setStepSize (float stepSize) {
      if (stepSize <= 0) throw new IllegalArgumentException("steps must be > 0: " + stepSize);
      this.stepSize = stepSize;
   }

   //GET PREF WIDTH FOR KNOB
   public float getPrefWidth () {
      if (vertical) {
         final Drawable knob = (disabled && style.disabledKnob != null) ? style.disabledKnob : style.knob;
         final Drawable bg = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
         return Math.max(knob == null ? 0 : knob.getMinWidth(), bg.getMinWidth());
      } else
         return 140;
   }
   
   //GET PREF WIDTH FOR KNOB 2
   public float getPrefWidth2 () {
      if (vertical) {
         final Drawable knob2 = (disabled && style.disabledKnob2 != null) ? style.disabledKnob2 : style.knob2;
         final Drawable bg2 = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
         return Math.max(knob2 == null ? 0 : knob2.getMinWidth(), bg2.getMinWidth());
      } else
         return 140;
   }
   
   //GET PREF HEIGHT FOR KNOB
   public float getPrefHeight () {
      if (vertical)
         return 140;
      else {
         final Drawable knob = (disabled && style.disabledKnob != null) ? style.disabledKnob : style.knob;
         final Drawable bg = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
         return Math.max(knob == null ? 0 : knob.getMinHeight(), bg.getMinHeight());
      }
   }
   
   //GET PREF HEIGHT FOR KNOB2
   public float getPrefHeight2 () {
      if (vertical)
         return 140;
      else {
         final Drawable knob2 = (disabled && style.disabledKnob2 != null) ? style.disabledKnob2 : style.knob2;
         final Drawable bg2 = (disabled && style.disabledBackground != null) ? style.disabledBackground : style.background;
         return Math.max(knob2 == null ? 0 : knob2.getMinHeight(), bg2.getMinHeight());
      }
   }

   public float getMinValue () {
      return this.min;
   }
   public float getMinValue2 () {
      return this.min2;
   }

   public float getMaxValue () {
      return this.max;
   }
   public float getMaxValue2 () {
      return this.max2;
   }

   public float getStepSize () {
      return this.stepSize;
   }

   /** If > 0, changes to the slider value via {@link #setValue(float)} will happen over this duration in seconds. */
   public void setAnimateDuration (float duration) {
      this.animateDuration = duration;
   }

   /** Sets the interpolation to use for {@link #setAnimateDuration(float)}. */
   public void setAnimateInterpolation (Interpolation animateInterpolation) {
      if (animateInterpolation == null) throw new IllegalArgumentException("animateInterpolation cannot be null.");
      this.animateInterpolation = animateInterpolation;
   }

   /** Will make this slider snap to the specified values, if the knob is within the threshold */
   public void setSnapToValues (float[] values, float threshold) {
      this.snapValues = values;
      this.threshold = threshold;
   }

   /** Returns a snapped value, or the original value */
   private float snap (float value) {
      if (snapValues == null) return value;
      for (int i = 0; i < snapValues.length; i++) {
         if (Math.abs(value - snapValues[i]) <= threshold) return snapValues[i];
      }
      return value;
   }

   public void setDisabled (boolean disabled) {
      this.disabled = disabled;
   }

   public boolean isDisabled () {
      return disabled;
   }

   /** The style for a slider, see {@link Slider}.
    * @author mzechner
    * @author Nathan Sweet */

   static public class MRSSliderStyle {
      /** The slider background, stretched only in one direction. */
      public Drawable background;
      /** Optional. **/
      public Drawable disabledBackground;
      /** Optional, centered on the background. */
      public Drawable knob, disabledKnob;
      /** Optional. */
      public Drawable knobBefore, knobAfter, disabledKnobBefore, disabledKnobAfter;
      
      public Drawable knob2, disabledKnob2;
      public Drawable knob2Before, knob2After, disabledKnob2Before, disabledKnob2After;
      
      public MRSSliderStyle () {
      }
      
      public MRSSliderStyle (Drawable background, Drawable knob, Drawable knob2) {
         this.background = background;
         this.knob = knob;
         this.knob2 = knob2;
      }
      
      public MRSSliderStyle (MRSSliderStyle style) {
         this.background = style.background;
         this.knob = style.knob;
         this.knob2 = style.knob2;
      }
   }
   
   

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (76 views)
2014-04-15 18:08:23

BurntPizza (69 views)
2014-04-15 03:46:01

UprightPath (81 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!