Panels and labels.

BlitzPlus Forums/BlitzPlus Programming/Panels and labels.

Tiger(Posted 2004) [#1]
How do I change label background color??
I what it to be like my panel color.


CS_TBL(Posted 2004) [#2]
iirc you can't ..

What you can do, however, is make your own label, using a canvas.. that canvas can ofcourse have its own color, or even an image!


Tiger(Posted 2004) [#3]
Okay, thanks for the help.
It work fine now.


Kevin_(Posted 2004) [#4]
You can do it with a panel also. Just load your image into a small panel. I prefer this method if i am not updating things and you dont have to worry about buffering and fliping the canvas to display the results.


CS_TBL(Posted 2004) [#5]
yeah.. but you can only put an image on a panel by directly loading it from disk. You can't use an excisting image handle with SetPanelImage ..

So, if you have 20 panels (acting as Labels) all with the same image, then you have this image 20x in mem (I guess.. not sure tho :)

If you use canvases then you can use one image handle for all!

Which might be a potential feature request :) "use an imagehandle for SetPanelImage instead of a file-only"


Kevin_(Posted 2004) [#6]
CS_TBL...

Yes you are quite right. You have load it in from disk but no additional memory is used because the bitmap panel area had already been allocated. All you are doing is loading another bitmap in that area. I find it easier to manage using panels rather than canvases. If you had 20 small canvases you have to flip them all when they change and they take up lots more memory. I think its just easier to load an image into a panel then you can forget about it.

Both methods work but I think 20 canvases for 20 labels is a bit of an overkill.

p.s. I have requested a feature to draw to panels directly but no luck yet :(


CS_TBL(Posted 2004) [#7]
but no additional memory is used because the bitmap panel area had already been allocated.


whoa.. so each panel is in fact a real bitmap w/o an image?

So, a panel of 128 x 128 (RGB-)pixels w/o an image does take up (128*128*3) 48kb of mem ?


ps. 20 canvases for 20 labels (or 'gadgets') = overkill ? See my gadget tutorial in 'tutorials' :)


Kevin_(Posted 2004) [#8]
Intersting stuff CS_TBL. I see you are using a polling technique to check for each gadget rather than use the Windows Event System. The problem with polling though is that it can slow things down because your program is constantly checking for gadgets instead of just waiting for an event to occur. I like it though.

By the way, anything that is displayed on your screen is a bitmap, not just panels.


CS_TBL(Posted 2004) [#9]
I made an ultra-deluxe image-button for my own stuff with this bank-stuff, and accidentaly I created 4096 (64x64 objects rather than the width&height :) buttons by putting the wrong value in a certain field, it was 'doable', but not anything near professionalism.

If you don't overdo it, then the speedloss is neglectable I'd say..

Until B-Max (that hopefully offers a way to make your own gadgets using native -fast- techniques rather than polling) I'd say that this is a very handy way to make your own gadgets.


Everything a bitmap.. isn't that a bit over the top ? If I have a tabber with 10 tabs, and each tab is 1024*1024 in size, then the mem it takes would be 10*1024*1024*3.. which is 30mb .. for just 10 empty panels. Do other languages this as well ? (msvc, borland etc.)


Kevin_(Posted 2004) [#10]
CS_TBL...

All computers work the same way regardless of what language is used. The good thing about Blitz products is that Mark has done all of the hard work for us so we can concentrate on designing stuff. The operating system also does alot for us behind the scenes (like allocating memory). Ever tried running lots of apps then closing them all quickly? Your hard drive goes crazy because the Virtual Memory has to write back the saved bitmap area's of each application. (Assuming you are low on physical memory.)

Here is a little example of a bitmap. Minimize your browser then click in some space on your desktop. Now press ALT & PRINT SCR together. Now open MS Paint. Select PASTE from the EDIT menu. Hey! Your desktop in MS Paint!

As for your tabber example I think that is a little unrealistic and probably wouldn't work.


CS_TBL(Posted 2004) [#11]
Anyway, here's my charity project for this day :)

;
; Nicelabel
;
; by CS^TBL
;
; ---------------------------------------------
;
; Nicelabel:
;
; label=CreateNicelabel("txt",x,y,width,height,parent)
;
; SetNicelabelText(label,"blah")
;
; SetNicelabelLayout(label,fillstyle,borderstyle,textR,textG,textB,fillR1,fillG1,fillB1,fillR2,fillG2,fillB2)
;
; fillstyle:
;   0 solid color (rgb1)
;   1 vertical gradient (rgb1->rgb2)
;   2 horizontal gradient (rgb1->rgb2)
;
; borderstyle:
;   0 no border
;   1 black border
;   2 sunken 3d border thickness 1
;   3 sunken 3d border thickness 2
;


app=CreateWindow("Nicelabel example - CS^TBL",0,0,640,480)

label=CreateNicelabel("Hello world!",100,100,192,18,app)
SetNicelabelLayout(label,1,2,0,0,0,192,192,192,128,128,128)

label2=CreateNicelabel("Uhm..",100,120,192,18,app)

label3=CreateNicelabel("Yeah..!",100,140,192,18,app)
SetNicelabelLayout(label3,1,1,0,0,0,192,192,192,128,128,128)

label4=CreateNicelabel("Whop!",100,260,192,22,app)
SetNicelabelLayout(label4,2,3,255,255,255,0,0,255,0,0,128)

quit=False
Repeat
	WaitEvent()
	If EventID()=$803 quit=True
Until quit

FreeBank label

End

Function SetNicelabelLayout(bank,fillstyle=1,borderstyle=2,textR=0,textG=0,textB=0,fillR1=192,fillG1=192,fillB1=192,fillR2=128,fillG2=128,fillB2=128)

	If Not bank Break "Bank required!"

	PokeByte bank,260,Limit(fillstyle,0,2)
	PokeByte bank,261,Limit(borderstyle,0,3)
	
	PokeByte bank,262,Limit(textR,0,255)
	PokeByte bank,263,Limit(textG,0,255)
	PokeByte bank,264,Limit(textB,0,255)
	
	PokeByte bank,265,Limit(fillR1,0,255)
	PokeByte bank,266,Limit(fillG1,0,255)
	PokeByte bank,267,Limit(fillB1,0,255)

	PokeByte bank,268,Limit(fillR2,0,255)
	PokeByte bank,269,Limit(fillG2,0,255)
	PokeByte bank,270,Limit(fillB2,0,255)
	
	UpdateNicelabel(bank)

End Function

Function CreateNicelabel(txt$,x,y,width,height,parent)

	If Not parent Break "Wrong Parent!"
	
	If (width*height)=0 Break "Image size zero"
	
	If Len(txt$)>255 Break "Text too long ... comeon dude.. it's just supposed to be a f*ck*ng LABEL :)"
	
	bank=CreateBank(256 + 4 + 1+1 + 1+1+1 + 1+1+1 + 1+1+1)
	
	txt$=LSet$(txt$,256)
	
	For t=0 To 255
		PokeByte bank,t,Asc(Mid$(txt$,1+t,1))
	Next
	
	PokeByte bank,260,0 ; fillstyle
	PokeByte bank,261,0 ; borderstyle
	
	PokeByte bank,262,0 ; textR
	PokeByte bank,263,0 ; textG
	PokeByte bank,264,0 ; textB
	
	PokeByte bank,265,192 ; fillR1
	PokeByte bank,266,192 ; fillG1
	PokeByte bank,267,192 ; fillB1

	PokeByte bank,268,128 ; fillR2
	PokeByte bank,269,128 ; fillG2
	PokeByte bank,270,128 ; fillB2
	
	canvas=CreateCanvas(x,y,width,height,parent)
	
	PokeInt bank,256,canvas

	UpdateNicelabel(bank)	
	
	Return bank
	
End Function

Function SetNicelabelText(bank,txt$)
	If Not bank Break "Wrong Parent!"

	If Len(txt$)>255 Break "Text too long ... comeon dude.. it's just supposed to be a f*ck*ng LABEL :)"
	txt$=LSet$(txt$,256)
	For t=0 To 255
		PokeByte bank,t,Asc(Mid$(txt$,1+t,1))
	Next
	
	UpdateNicelabel(bank)

End Function

Function UpdateNicelabel(bank)

	If Not bank Break "Bank required!"
	
	canvas=PeekInt(bank,256)
	
	width=GadgetWidth(canvas)
	height=GadgetHeight(canvas)

	fillstyle=PeekByte(bank,260)
	borderstyle=PeekByte(bank,261)

	textR=PeekByte(bank,262)
	textG=PeekByte(bank,263)
	textB=PeekByte(bank,264)
	
	fillR1=PeekByte(bank,265)
	fillG1=PeekByte(bank,266)
	fillB1=PeekByte(bank,267)

	fillR2=PeekByte(bank,268)
	fillG2=PeekByte(bank,269)
	fillB2=PeekByte(bank,270)
	
	txt$=""
	For t=0 To 255
		txt$=txt$+Chr$(PeekByte(bank,t))
	Next
	
	txt$=Trim$(txt$)
	
	SetBuffer CanvasBuffer(canvas)

		Select fillstyle
	
			Case 0 ; solid color (rgb1)
				ClsColor fillR1,fillG1,fillB1:Cls
				Color textR,textG,textB
				Color textR,textG,textB:Text width/2,height/2,txt$,True,True
	
			Case 1 ; vertical gradient (top:rgb1 bottom:rgb2)
	
				For y=0 To height-1

					ph#=Float(y)/Float(height)

					r=LinInt(fillR1,fillR2,ph#)
					g=LinInt(fillG1,fillG2,ph#)
					b=LinInt(fillB1,fillB2,ph#)
					
					Color r,g,b:Line 0,y,width-1,y
	
				Next
				
				Color textR,textG,textB:Text width/2,height/2,txt$,True,True

			Case 2 ; horizontal gradient (left:rgb1 right:rgb2)
			
				For x=0 To width-1
				
					ph#=Float(x)/Float(width)
				
					r=LinInt(fillR1,fillR2,ph#)
					g=LinInt(fillG1,fillG2,ph#)
					b=LinInt(fillB1,fillB2,ph#)
					
					Color r,g,b:Line x,0,x,height-1
	
				Next
				
				Color textR,textG,textB:Text width/2,height/2,txt$,True,True

		End Select		
		
		Select borderstyle
			Case 0 ; no border
			Case 1 ; black border
			
				Color 0,0,0
				Rect 0,0,width,height,False

			Case 2 ; sunken 3d border
			
				Color 255,255,255:	Line 1,height-1,width-1,height-1:	Line width-1,1,width-1,height-1
				Color 0,0,0:	Line 0,0,width-2,0:		Line 0,0,0,height-2

			Case 3 ; sunken 3d border thickness 2

				Color 255,255,255:
				Line 1,height-1,width-1,height-1
				Line 2,height-2,width-2,height-2
				Line width-1,1,width-1,height-1
				Line width-2,2,width-2,height-2
				
				Color 0,0,0
				Line 0,0,width-2,0
				Line 1,1,width-3,1
				Line 0,0,0,height-2
				Line 1,1,1,height-3
				
				
		End Select
		
	FlipCanvas canvas
	
End Function


; global functions.. anyone should have them anyway, very handy stuff.. :)

Function Limit(value,minvalue,maxvalue)
	If value<minvalue Return minvalue
	If value>maxvalue Return maxvalue
	Return value
End Function

Function Break(s$)
	Notify s$
	End
End Function

Function LinInt#(v1#,v2#,phase#)
	Return (v1#+((v2#-v1#)*phase#))
End Function