Ooops
February 27, 2021, 03:01:57 AM

Author Topic: [bmx] Simple Progress Bars by Luke111 [ 1+ years ago ]  (Read 418 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Simple Progress Bars by Luke111 [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Simple Progress Bars
Author : Luke111
Posted : 1+ years ago

Description : Simple Progress Bars in Max2D

Code :
Code: BlitzMax
  1. Rem
  2.         This is a really simple progress bar I made for my WIP game.
  3.         I created and tested it in about 30 minutes.
  4.         I hope someone finds it useful!
  5. End Rem
  6.  
  7. Type SimpleProgressBar
  8.         Field minValue:Short, maxValue:Short, currentValue:Short 'these are shorts to make it a bit easier since they don't support negatives...
  9.         Field colorR:Byte,colorG:Byte,colorB:Byte 'background color for the progress bar.
  10.         Field x:Float,y:Float,w:Float,h:Float '(x,y) coordinates for the bar, followed by width and height of it.
  11.         Field text:String = "" 'the text to place in the middle of the filled bar.
  12.        
  13.         Method Create:SimpleProgressBar(_x:Float,_y:Float,_w:Float,_h:Float,_minValue:Short,_maxValue:Short,_text:String="",_colorR:Byte=0,_colorG:Byte=255,_colorB:Byte=0)
  14.                 x = _x
  15.                 y = _y
  16.                 w = _w
  17.                 h = _h
  18.                 text = _text
  19.                 minValue = _minValue
  20.                 currentValue = minValue 'the current value defaults to the minimum value...
  21.                 maxValue = _maxValue
  22.                 colorR = _colorR
  23.                 colorG = _colorG
  24.                 colorB = _colorB
  25.                 Return Self
  26.         End Method
  27.        
  28.         'method to set the values of the progress and the text as well.
  29.         Method SetValue(value:Short,_text:String = Null)
  30.                 'only set the new value if it is in range.
  31.                 If value >= minValue And value <= maxValue Then
  32.                         currentValue = value
  33.                 EndIf
  34.                 'set the text if specified.
  35.                 If _text <> Null Then
  36.                         text = _text
  37.                 EndIf
  38.         End Method
  39.        
  40.         'call this during the rendering part of the main loop.
  41.         Method Render()
  42.                 'get current color
  43.                 Local tempR:Int,tempG:Int,tempB:Int
  44.                 GetColor(tempR,tempG,tempB)
  45.                
  46.                 'get opposite color (used for non-filled background as well as overlayed text)
  47.                 Local oppositeColorR:Byte,oppositeColorG:Byte,oppositeColorB:Byte
  48.                 If colorR > 64 Then
  49.                         oppositeColorR = 0
  50.                 Else
  51.                         oppositeColorR = 255
  52.                 EndIf
  53.                 If colorG > 64 Then
  54.                         oppositeColorG = 0
  55.                 Else
  56.                         oppositeColorG = 255
  57.                 EndIf
  58.                 If colorB > 64 Then
  59.                         oppositeColorB = 0
  60.                 Else
  61.                         oppositeColorB = 255
  62.                 EndIf
  63.                
  64.                 'non-filled background...
  65.                 SetColor oppositeColorR,oppositeColorG,oppositeColorB
  66.                 DrawRect x,y,w,h
  67.                
  68.                 'filled background...
  69.                 SetColor colorR,colorG,colorB
  70.                 'finds and rounds the progress percent
  71.                 Local filledPercent:Int = (currentValue - minValue) * 100 / (maxValue - minValue)
  72.                 'finds and rounds the width of the filled part.
  73.                 Local filledWidth:Int = filledPercent * w / 100
  74.                 DrawRect x,y,filledWidth,h 'draw the filled part!
  75.                
  76.                 'overlayed text...
  77.                 If text <> "" And TextWidth(text) < filledWidth Then 'only draw it if it can fit in the filled part.
  78.                         SetColor oppositeColorR,oppositeColorG,oppositeColorB
  79.                         DrawText text,x + ((filledWidth / 2) - (TextWidth(text) / 2)),y 'draw the text in the middle of the filled part (horizontally)
  80.                 EndIf
  81.                
  82.                 SetColor tempR,tempG,tempB
  83.         End Method
  84. End Type
  85.  
  86. '****DEMO****
  87. Graphics 800,600,0 'windowed
  88.  
  89. 'create two progress bars, one on the top, and one on the bottom.
  90. Local top:SimpleProgressBar = New SimpleProgressBar.Create(200,25,400,50,0,100,"0") 'this one is the default green, and goes from 0 to 100.
  91. Local bottom:SimpleProgressBar = New SimpleProgressBar.Create(200,100,400,50,0,500,"0",255,0,0) 'this one is red, and goes from 0 to 500.
  92.  
  93. Local startTime:Int = MilliSecs() 'used to help with the timing of the progress in the progress bars...
  94.  
  95. While Not KeyHit(KEY_ESCAPE)
  96.         If MilliSecs() - startTime > 50 Then 'update every 50 milliseconds..
  97.                 startTime = MilliSecs()
  98.                 If top.currentValue >= top.maxValue Then
  99.                         top.SetValue(top.minValue,String(top.minValue))
  100.                 Else
  101.                         top.SetValue(top.currentValue + 1,String(top.currentValue + 1))
  102.                 EndIf
  103.                 If bottom.currentValue >= bottom.maxValue Then
  104.                         bottom.SetValue(bottom.minValue,String(bottom.minValue))
  105.                 Else
  106.                         bottom.SetValue(bottom.currentValue + 1,String(bottom.currentValue + 1))
  107.                 EndIf
  108.         EndIf
  109.         Cls()
  110.         'render the bars...
  111.         top.Render()
  112.         bottom.Render()
  113.         Flip()
  114. Wend


Comments :


virtlands(Posted 1+ years ago)

 It is colorful, and interesting.  Thanks.  [img]img809.imageshack.us/img809/8028/progressbarip.html">


dna(Posted 1+ years ago)

 Thanks for posting this. There was another post about progress bars that was way too complicated.Actually an even simpler one would be better but this can be converted to B3D.


Luke111(Posted 1+ years ago)

 Thanks for saying thanks!I will post a B3D/B+ version in the near future.


Guy Fawkes(Posted 1+ years ago)

 Hi all. I know this is an old thread, but AWESOME! =D


Luke111(Posted 1+ years ago)

 Thanks! I didn't realize how awesome it is, until I stopped programming for 6 months and came back to it...Nice name. :-)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal