GFX Scroll Bar full screen, need example

BlitzPlus Forums/BlitzPlus Programming/GFX Scroll Bar full screen, need example

Apollonius(Posted 2005) [#1]
I'm looking for an example, of a Box(using the rectangle drawing thing), with a scroll bar in it, that can move up and now, and show text and an image.

Not using gadgets. Just drawing commands from blitzplus.

It would really be appreciated. I'm looking on how to do that. Cause I want to make a little guild to move custom made window in the game, with rpg stats and stuff like that.


Kevin_(Posted 2005) [#2]
OK, heres some really old code that I did. Its untested, but it allows you to view a document on the screen. Hope it helps....

[Code]
; Simple Document Displayer By Prof (Un-Tested)
; Last tested/compiled 21/10/02
;
; This set of functions can be used to display a document in a window.
; The document must be a raw-asci file. Scroll bars are automatically
; calculated so the user can scroll through the document.
;
AppTitle("Document Displayer")
Graphics 640,480,32,2
SetBuffer BackBuffer()
ClsColor(190,190,190):Cls:Flip

Dim Tex$(1000) ; Reserve 1000 lines in memory
fntArial=LoadFont("Arial",14,True,False,False) ; Try different fonts
SetFont fntArial


f$=RequestFile$("Open a text file","*.*")
ViewDocument(f$,10,10,400,300)


End

; ****************************************************************

Function ViewDocument(filename$,WinX,WinY,WinW,WinH)
; If a filename is given then the file is opened otherwise DATA
; statements are Read in (at the end of this code)
; WinX,WinY,WinW,WinH is the windows position and size
;
; It works out the number of lines to display based on the current
; font size and the height of the window. Also clips the length of
; the text to the window width.
;
; Open the file if the filename exists otherwise read DATA statements

debug=False

If filename$<>"" And FileType(filename$)=True
fileIn=ReadFile(filename$):LinesInDoc#=0
Repeat
LinesInDoc=LinesInDoc+1:tex$(LinesInDoc)=ReadLine$(FileIn)
Until Eof(FileIn)
CloseFile(FileIn):tex$(LinesInDoc+1)="*" ; put stopper at the end
Else
Restore TextDATA:LinesInDoc=1
Repeat
Read d$:Tex$(LinesInDoc)=d$:LinesInDoc=LinesInDoc+1
Until d$="*"
EndIf

Index#=1.0 ; First line of text
LinesInWindow#=(WinH/FontHeight()) ; No of lines of text in the window
DocSize#=(LinesInDoc)*FontHeight() ; Length of whole doc in pixels
ButtonSize#=16 ; Size of the scroll buttons
Button1X#=WinX+WinW-ButtonSize-1 ; X Position of top scroll button
Button1Y#=WinY+1 ; Y Position of top scroll button
Button2X#=WinX+WinW-ButtonSize-1 ; X Position of bottom scroll button
Button2Y#=WinY+WinH-ButtonSize-1 ; Y Position of bottom scroll button
ButtonDistance#=WinH-(ButtonSize*2) ; Distance between scroll buttons
t#=DocSize/(LinesInWindow*FontHeight()) ; Proportion variable
ThumbH#=(ButtonDistance/t#)-2 ; Height of scroll thumb
ThumbRange#=ButtonDistance-ThumbH ; Thumb range
Divider#=(LinesInDoc)/(ThumbRange+ThumbH) ; Proportion variable
ThumbX=WinX+WinW-ButtonSize-1 ; X Position of scroll thumb
ThumbYMin=WinY+ButtonSize+1 ; Minimum Y coordinate
ThumbYMax=((WinY+WinH)-Buttonsize-ThumbH-1) ; Maximum Y coorditate
ThumbY=ThumbYMin ; Y Position of scroll thumb
ThumbW=ButtonSize ; Width of scroll thumb


HighLightedBox(WinX,WinY,WinW,WinH,True) ; The Main Window
DrawScrollButton(1,Button1X,Button1Y,ButtonSize,ButtonSize,0)
DrawScrollButton(2,Button2X,Button2Y,ButtonSize,ButtonSize,0)
DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
ShowPage(Index,LinesInWindow,WInX,WinY,WinW,WinH)

;MainLoop
Flip
Repeat
If ScrollDownActivated(Button2X,Button2Y,ButtonSize,ButtonSize)
DrawScrollButton(2,Button2X,Button2Y,ButtonSize,ButtonSize,True)
While ThumbY<ThumbYMax And ScrollDownActivated(Button2X,Button2Y,ButtonSize,ButtonSize)
Index#=Index#+Divider#
ShowPage(Index#,LinesInWindow,WinX,WinY,WinW,WinH)
ThumbY=ThumbY+1
DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
Flip
Wend
DrawScrollButton(2,Button2X,Button2Y,ButtonSize,ButtonSize,False)
Flip
EndIf

If ScrollUpActivated(Button1X,Button1Y,ButtonSize,ButtonSize)
DrawScrollButton(1,Button1X,Button1Y,ButtonSize,ButtonSize,True)
While ThumbY>ThumbYMin And ScrollUpActivated(Button1X,Button1Y,ButtonSize,ButtonSize)
Index#=Index#-Divider#
ShowPage(Index#,LinesInWindow,WinX,WinY,WinW,WinH)
ThumbY=ThumbY-1: DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
Flip
Wend
DrawScrollButton(1,Button1X,Button1Y,ButtonSize,ButtonSize,False)
Flip
EndIf

If PointInArea(MouseX(),MouseY(),ThumbX,ThumbY,ThumbW,ThumbH) And MouseDown(1) ; moving scroll bar
While MouseDown(1)
ThumbY=MouseY()-(ThumbH/2)
ThumbY=KeepInRange(ThumbY,ThumbYMin,ThumbYMax)
DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
Index#=((ThumbY-ThumbYMin)*Divider#)+1
ShowPage(Index#,LinesInWindow,WinX,WinY,WinW,WinH)
Flip
Wend
EndIf

If debug
; Remove this code if you wish
Color 190,190,190:Rect 0,420,GraphicsWidth(),40
Color 0,0,0
Text 0,400,"WinX="+WinX+" WinY="+WinY+" WinW="+WinW+" WinH="+winH+" DocSize="+DocSize+"px"+" Lines In Doc="+LinesInDoc+" Lines In Window="+LinesInWindow
Text 0,425,"ThumbX="+ThumbX+" ThumbY="+ThumbY+" ThumbW="+ThumbW+" ThumbH="+ThumbH+ " Thumb Range="+ThumbRange
Text 0,450,"Index#="+Index#+" Divider="+Divider+" Button Distance="+ButtonDistance
Flip
EndIf
Until KeyHit(1)
End Function

; **********************************************************************

Function ScrollDownActivated(X,Y,W,H)
;tests to see if scrolling down has been activated
; returns TRUE if it has. x,y,w,h is the area of a scroll button
If (KeyDown(208)) Or (PointInArea(MouseX(),MouseY(),X,Y,W,H) And MouseDown(1))
Return True
EndIf
End Function

; **********************************************************************

Function ScrollUpActivated(X,Y,W,H)
; tests to see if scrolling down has been activated
If (KeyDown(200)) Or (PointInArea(MouseX(),MouseY(),X,Y,W,H) And MouseDown(1))
Return True
EndIf
End Function

; **********************************************************************

Function ShowPage(Index,LinesInWindow,WinX,WinY,WinW,WinH)
; Puts a page of text in the window

FH=FontHeight():yPos=WinY
Color 230,230,230:Rect WinX+1,WinY+1,WinW-2-16,WinH-2 ;clear window
Color 0,0,0
Viewport WinX,WinY,WinW-16-1,WinH-1 ;clip to the window area
For d = Index To (Index+LinesInWindow)-1 Step 1
Text WinX+4,YPos,tex$(d):YPos=YPos+FH
Next
Viewport 0,0,GraphicsWidth(),GraphicsHeight()
End Function

; **********************************************************************

Function DrawScrollThumb(x,y,w,h,WinX,WinY,WinW,WinH)
; Draws the scroll thumb (bit you can drap up & down)
; Draw area behind the thumb first
Color 215,215,215 : Rect WinX+WinW-w-1,WinY+w+1,w,WinH-2-(w*2)
; Now draw the actual thumb
HighLightedBox(x,y,w,h,False)
End Function

; **********************************************************************

Function DrawScrollButton(n,x,y,w,h,Inverse)
; n= the button number
; 1=top, 2=bottom, 3=left, 4=right
HighLightedBox(x,y,w,h,Inverse)
DrawScrollButtonArrow(n,x,y,w,h,Inverse)
End Function

; **********************************************************************

Function HighlightedBox(x,y,w,h,Inverse)
; draws a highlighted box with the light coming
; from the top left.
; If Inverse is true, the box is inversed
If inverse=True
c1=255:c2=90
Else
c1=90:c2=255
EndIf
Color 190,190,190:Rect x,y,w,h ; Draw solid area
Color c2,c2,c2:Line X,Y,X+W-1,Y :Line X,Y,X,Y+H-1 ; Top & Left
Color c1,c1,c1:Line X+W-1,Y,X+W-1,Y+H-1:Line X,Y+H-1,X+W-1,Y+H-1; Bottom & Right
End Function

; **********************************************************************

Function DrawScrollButtonArrow(n,x,y,w,h,Inverse)
; Draws a black arrow
Color 0,0,0
If n=1
Rect x+7,y+6+Inverse,2,1
Rect x+6,y+7+Inverse,4,1
Rect x+5,y+8+Inverse,6,1
Rect x+4,y+9+Inverse,8,1
ElseIf n=2
Rect x+4,y+6+Inverse,8,1
Rect x+5,y+7+Inverse,6,1
Rect x+6,y+8+Inverse,4,1
Rect x+7,y+9+Inverse,2,1
EndIf
End Function

; **********************************************************************

Function PointInArea(px,py,x,y,w,h)
; Function to check if px,py is inside the area x,y,w,h
; Returns TRUE if inside
If px>x And px<x+w
If py>y And py<y+h
Return True
EndIf
End If
End Function

; **********************************************************************

Function KeepInRange(n,Min,Max)
;Keeps n within a range defined by Min & Max.
If n<Min
n=Min
EndIf
If n>Max
n=Max
EndIf
Return n
End Function

; **********************************************************************

Function ClipText $ (s$,width)
; clips a string to a width in pixels
If StringWidth(s$)>=(width-4)
P=1:temp$=""
Repeat
c$=Mid$(s$,p,1)
temp$=temp$+c$
w=StringWidth(Temp$)
p=p+1
Until w>=(width-4)
s$=Left$(Temp$,p-2)
End If
Return s$
End Function


; The following DATA statements are not used if opening an
; external file. Use these DATA statements if you wish to
; include the document within the final executable.

.TextDATA
Data "REPORT: No document name given or the file does not exist."
Data "----------------------------------------------------------"
Data " "
Data " "
Data " "
Data " "
Data " "
Data "This text is from the data statements from within the"
Data "program itself. Handy if you want to keep all of the"
Data "documentation within the final executable."
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data "*"

[/Code]


Apollonius(Posted 2005) [#3]
Thanks.. but,
this is way to complex for me... And not exactly what I'm looking for. I need a simple, box either way drawing commands or images and a scroll bar. Text and Images in the box, that i can scroll.

Dummy Tutorial lol.


Kevin_(Posted 2005) [#4]
Thats what it does. Have you tried it? Just modify it to include pictures as well. I don't think you realize whats involved. The example above is very basic text viewer. Just modify it to do what you want it to do.


CS_TBL(Posted 2005) [#5]
Why don't you want B+ gadgets, while posting in the B+ section? It's the events that make all this so easy..

If you want to be GUI-independent, while using things like events and canvases, then prepare for this:

- 300 lines of code for an easy-to-use button-system using images and canvases.

- 200 lines of code for a remote help-window/display, if you want help for those buttons. Think: statusbar of photoshop, but then in a remote window.

- 130 lines of code for a text-formatter/generator for that help-display (soft-enters, indenting, etc.)

- if you don't load images from HD, prepare for a nice 600lines (or more) texture generator, to calc buttons and other widgets with, based on sripts, I recommend it over loading images, because you can easily change global properties of all your own widgets then with a few commands.. like color, shape, font etc.

- if you don't use native fonts, but want to use your own, prepare for 30 short lines for a font-unpacker, and another small app to create that packed-format.

I'm now more or less summing-up my own includefiles.. :) Anyway, it works like that for me, 100% banks, 0% types, and only using a handful of initialisation-lines and a handful of event-calls in the mainloop.

Additionally, if you use banks, and you need different objects to work together, then you'll love a 'linker' for that! 80 lines..

So, Prof's way might be too complex for you.. it's certainly less code than my solution ^_^. Kaisuo: check my tutorial in blitz+ tutorials, about "your own gadgets".. I use that method for a while already.. and I think it rox. Try to learn from it..


I'm actually a fan of all this low-level peeking and poking.. this, in combination with making big apps, explains you why you actually want classes, without reading elite'ish c++ books about it..


Apollonius(Posted 2005) [#6]
hmmk,

I used to be able to read and understand long piece of codes.. but its been a while since I touched B+ :P


Kevin_(Posted 2005) [#7]
I used to be able to read and understand long piece of codes.. but its been a while since I touched B+ :P

Yes, sorry Kaisuo. Its not commented very well at all and its badley written to be honest. One of my poorer efforts I'm afraid.