TGC Codebase Backup



Slider Bar by bobbel

16th Jul 2009 4:40
Summary

I needed i slidebar, so i thought i'd code one. and it seems kinda easy and handy so i though lets post it here Here's the code with some comments. I'm not sure of you'll understan



Description

I needed i slidebar, so i thought i'd code one. and it seems kinda easy and handy so i though lets post it here

Here's the code with some comments. I'm not sure of you'll understand it, cause i really suck at comments :p

Its really easy. You declare an empty array, Dim Sliders() as Slider. After that, you call MakeSLider as Many times as you want. It'll return the slider ID. In the main loop you call HandleSLiders to update the sliders. That's all! And if you want to get the value, you do something like Val = Sliders(SliderID).Value#. If you find any bugs be sure to post!



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Sync On
Sync Rate 0
Backdrop On

// UDT For a SLider
Type Slider
     X // The X position
     Y // The Y Position
     Width // The Width of the Bar
     Height // The Height of teh Bar
     Min# // The minium value. Must be lower than the Max# Value
     Max# // The maximum Value. Must be more than the Min# value
     Value# // The current value. Alter if needed
     BarColor // The color of the Bar
     IndiColor // The color of the Indicator
     Used // indicates if the bar is in use. 1 = On, 0 = Off
EndType

// The array
Dim Sliders() as Slider

// Make a slider and store the ID in a value, so you can get the value
// of that slider using Sliders(Slider1).Value#
Slider1 = MakeSlider(10, 10, 0, 100, 100, 10, RGB(255, 255, 255), RGB(0, 0, 0))

Do
  Set Cursor 0, 30
  Ink RGB(255, 255, 255)
  Print Screen FPS()
  
  // This is all you need to update the SLiders
  HandleSliders()
  
  Sync
Loop

End

Function MakeSlider(X, Y, Min#, Max#, Width, Height, BarColor, IndiColor)
         // Here we just add another array item and 
         // Fill them with the parameters
         Array Insert at Bottom Sliders()
         Ar = Array Count(Sliders())
         
         Sliders(Ar).X = X
         Sliders(Ar).Y = Y
         Sliders(Ar).Min# = Min#
         Sliders(Ar).Max# = Max#
         Sliders(Ar).Width = Width
         Sliders(Ar).Height = Height
         // Give it a random value between the min and max parameters. alter if needed
         Sliders(Ar).Value# = Rnd(Max#-Min#)+Min#
         Sliders(Ar).BarColor = BarColor
         Sliders(Ar).IndiColor = IndiColor
         // A slider is standard enabled
         Sliders(Ar).Used = 1
EndFunction Ar

Function HandleSliders()
         // Loop through the SLiders
         For Ar = 0 To Array Count(Sliders())
             // If used = 1
             If Sliders(Ar).Used
                // Make sure the value isn't more than Max or Min
                Sliders(Ar).Value# = CapValue(Sliders(Ar).Value#, Sliders(Ar).Min#, Sliders(Ar).Max#)
                
                // Set the right bar color
                Ink Sliders(Ar).BarColor, 0
                // Draw the Bar
                Box Sliders(Ar).X, Sliders(Ar).Y+Sliders(Ar).Height*0.33, Sliders(Ar).X+Sliders(Ar).Width, Sliders(Ar).Y+Sliders(Ar).Height*0.66
                
                // Set the Indicator Color
                Ink Sliders(Ar).IndiColor, 0
                // calculate the difference between min and max
                Diff# = Sliders(Ar).Max# - Sliders(Ar).Min#
                // Calculate the difference between Current Value and Min
                Val# = Sliders(Ar).Value# - Sliders(Ar).Min#
                // Calculate the percentage of value
                Perc# = Val#/Diff#
                L = Sliders(Ar).X+Sliders(Ar).Width*Perc#-2
                T = Sliders(Ar).Y
                R = Sliders(Ar).X+Sliders(Ar).Width*Perc#+2
                B = Sliders(Ar).Y+Sliders(Ar).Height
                MX = MouseX()
                MY = MouseY()
                
                Box L, T, R, B
                
                // Do the mouse slider thing
                If MX => Sliders(Ar).X AND MX <= Sliders(Ar).X+Sliders(Ar).Width AND MY => Sliders(Ar).Y AND MY <= Sliders(Ar).Y+Sliders(Ar).Height AND Mouseclick() && 1
                   // Calculate the mouse offest from the slidebar's X
                   MDiff# = MouseX() - Sliders(Ar).X
                   // Save the Width variable
                   Diff# = Sliders(Ar).Width
                   // Calculate the percentage of the MouseOffset in the Total Width
                   MPerc# = MDiff# / Diff#
                   // Calculate the value with the percentage
                   MVal# = (Sliders(Ar).Max#-Sliders(Ar).Min#)*MPerc#+Sliders(Ar).Min#
                   // Apply the Value
                   Sliders(Ar).Value# = MVal#
                EndIf
                
                // Displays the current value. can be removed if not needed.
                Ink Sliders(Ar).BarColor, 0
                Text Sliders(Ar).X+Sliders(Ar).Width+5, Sliders(Ar).Y, Str$(Sliders(Ar).Value#) 
             EndIf 
         Next X
EndFunction

Function CapValue(ValO#, Min#, Max#)
         Val# = ValO#
         If Val# < Min# Then Val# = Min#
         If Val# > Max# Then Val# = Max#
EndFunction Val#