How to color selected words in a sentence?

Blitz3D Forums/Blitz3D Programming/How to color selected words in a sentence?

Guy Fawkes(Posted 2013) [#1]
Hi all, I'm working on a part of my game, and was wondering how to color a single word by using some small text in a sentence like so:

"[c=$FF0000]my[/c] [c=#00FF00]colored[/c] [c=#0000FF]text[/c] here"

I've been trying for the last 10 hours to get this code working, but I can't...

If anyone could help, that would be GREAT!

Here's the code:



Thank you all so kindly!


Midimaster(Posted 2013) [#2]
instead of drawing the complete sentence with one TEXT() command , you have to devide the sentence into parts and draw the parts with a lot of TEXT() commands.

Here is an executable sample, that demonstrates the dividing into parts:

Graphics 800,600
T$="Abc[c=1]def[/c]ghi"
Cls
DrawColorText T
Flip 0
WaitKey


Function DrawColorText(t$)
	Print " step:     raw string:              part text:"
	Print "-----------------------------------------------"
	Text X,380,"final text:"
	Repeat 
		i=i+1
		Print "   " + i + "       " + t

		markA=Instr(t,"[",1)

		If markA=1
			; color command is next:
			markB=Instr(t,"]",markA)
			ChangeColor Mid(t,markA+1,markB-markA-1)
			t=Mid(t,markB+1)
			
		ElseIf markA>0
			; text part is next:
			part$=Left(t,markA-1)
			Text 300,i*20, Part
			Text X,400,part
			x=x+StringWidth(part)
			t=Mid(t,markA)

		Else
			; rest of the text
			Text 300,i*20, t
			Text X,400,t
			t=""
		EndIf
		
		
	Until t=""

End Function



Function ChangeColor(Col$)
	Print "            -->" + Col
	If Col="c=1"
		Color 255,0,0
	ElseIf Col="/c"
		Color 255,255,255
	EndIf

End Function



Guy Fawkes(Posted 2013) [#3]
Hi, Midimaster. Thank you very much for your help! I just wanted to know one thing though? Why is "[/c]" showing up in the sentence? Shouldn't that mark the end of the colored word or letter, thereby having it disappear?



Thanks again!


Midimaster(Posted 2013) [#4]
When I start the exe I get a real perfect output: The main output is the sentence at the koordinates 0,400 (left down corrner below "final text:").

I added the other printouts only for a better understanding, how the algo works....

Can you send a screenshot of what you are seeing?


Guy Fawkes(Posted 2013) [#5]
EDIT: Nvm, figured it out! =D

Thanks again, Midimaster! You are amazing! :D

For those of whom wish to have the fix, here it is!

EDIT 2: This code has been fixed to include the colors of the rainbow + 3 extra :P

ColoredWords.bb:




Stevie G(Posted 2013) [#6]
I appreciate you're trying to incorporate some sort of html here but it would be rediculously fiddly to create the colours for each string. What I do is have a text string and a colour string both of the same length. Simple to write and simple to handle.

e.g.



Jesus .... took me about 10 goes to get this code in a box ...


Guy Fawkes(Posted 2013) [#7]
Ooooooooo, I LIKE this! Thanks to both of you! :D


skidracer(Posted 2013) [#8]
that adds another dimension to text,

that is way cool Stevie
.........***..............