News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

SingleTextPopUp - a small text enter FX popup

Started by XpMe_v1.2, December 03, 2008, 05:16:37 PM

Previous topic - Next topic

XpMe_v1.2

Here is a small text to enter popup menu I think you will find handy to use.
The max text to enter(at the start of the program) can be changed to suit your needs and the popup will appear the size to hold it.
It pops up with a right mouse click and is auto centered. Plus dragable by holding the right mouse down over it and moving the mouse around while still holding down the right mouse button.
Feel free to change it any way you wish.
I'm using it in my updated Eyepiece program.
FX images are used as to not loose the text and images if a screensaver kicks in.
Written in playbasic 1.64c

PlayBASIC Code: [Select]
; PROJECT : SingleTextPopUp
; AUTHOR : XpMe v1.2
; CREATED : 11/29/2008
; EDITED : 12/3/2008
; ---------------------------------------------------------------------
Type PopUp_button
img(3)
x,y,w,h ,dark,grey,RedA,RedB,green,lime,blue,blueLit,edge ,Txt$,MaxTxt,Fnt
EndType
Dim Pops As PopUp_button
Pops_SetUp_Variables_Images()
`--------
`--------
Do
Cls RGB(78, 78, 78)
mb = MouseButton() : If mb = 2 Then DoPops()
Sync
Loop
End
`--------
`--------
Function Pops_SetUp_Variables_Images()
Pops.Fnt = LoadNewFont("Courier New", 16, 0)
SetFont Pops.Fnt
Pops.MaxTxt = 14 ` max amount of text allowed for input
R = GetTextWidth("W") * Pops.MaxTxt ` get text width for setting width of popup image
Pops.w = 72 + R
Pops.h = 22
Pops.img(0) = NewImage(Pops.w ,Pops.h) ` popup image
Pops.dark = RGB( 5, 5, 5) ` the colors
Pops.grey = RGB(105, 105, 105)
Pops.RedA = RGB(200, 20, 21)
Pops.RedB = RGB(173, 154, 88)
Pops.green = RGB( 80, 177, 80)
Pops.lime = RGB(211, 255, 211)
Pops.blue = RGB( 35, 33, 100)
Pops.blueLit= RGB(177, 177, 245)
Pops.edge = RGB(173, 174, 188)
Pops.Txt$ = "all game music" ` the text you will see
RenderToImage Pops.img(0) ` create the popup image
Cls Pops.grey
ShadeBox 0 , 0, Pops.w - 1 ,Pops.h - 1 ,Pops.dark ,Pops.dark ,Pops.dark ,Pops.dark
ShadeBox 1 , 1, Pops.w - 2 ,Pops.h - 2 ,Pops.edge ,Pops.edge ,Pops.edge ,Pops.edge
ShadeBox 2 , 2, Pops.w - 3 ,Pops.h - 3 ,Pops.RedA ,Pops.RedB ,Pops.RedA ,Pops.RedB
PopsTxtBoxes( 3 ,3, Pops.w - 64 , Pops.h - 5 , 0 ,0, "") ` call function to create the button hot areas
PopsTxtBoxes(Pops.w -61 ,3, Pops.w - 43 , Pops.h - 5 ,Pops.w - 59 ,2, "<<")
PopsTxtBoxes(Pops.w -40 ,3, Pops.w - 20 , Pops.h - 5 ,Pops.w - 38 ,3, "ok")
PopsTxtBoxes(Pops.w -17 ,3, Pops.w - 5 , Pops.h - 5 ,Pops.w - 14 ,3, "X")
RenderToScreen
PrepareFXImage Pops.img(0) ` set image to FX so it will not be flushed if screen saver kicks in
`--------
n = 1 : Pops.img(n) = getFxButt(17,13, "<<" ,1,-2) ` create a popup hover button
n = 2 : Pops.img(n) = getFxButt(19,13, "ok" ,1,-1) ` create a popup hover button
n = 3 : Pops.img(n) = getFxButt(11,13, "X" ,2,-1) ` create a popup hover button
`--------
Ink RGB(111,0,0) ` set text color for typing
PrepareFxFont Pops.Fnt ` set font to FX so text will not be visible on FX images
EndFunction
`--------
`-------- create the button areas
Function PopsTxtBoxes(x,y,w,h ,TxtX,TxtY,Txt$)
BoxC x ,y ,w,h ,0, Pops.grey
ShadeBox x+1,y+1 ,w,h , Pops.blueLit ,Pops.lime ,Pops.blueLit ,Pops.lime
Ink Pops.blue
Text TxtX ,TxtY ,Txt$
EndFunction
`--------
`-------- create the popup hover buttons
Function getFxButt(w,h, Txt$,TxX,TxY)
img = NewImage(w ,h)
RenderToImage img
ShadeBox 0 ,0 ,w ,h ,Pops.green ,Pops.lime ,Pops.green ,Pops.lime
Ink Pops.dark
Text TxX ,TxY ,Txt$
RenderToScreen
PrepareFXImage img ` set image to FX so it will not be flushed if screen saver kicks in
EndFunction img
`--------
`-------- center the popup image
Function PositionBKGimg(mx,my,ww,hh)
Pops.x = mx - Pops.w / 2 : If Pops.x < 1 Then Pops.x = 1
w = ww - Pops.w - 1 : If Pops.x > w Then Pops.x = w
Pops.y = my - Pops.h / 2 : If Pops.y < 1 Then Pops.y = 1
h = hh - Pops.h - 1 : If Pops.y > h Then Pops.y = h
EndFunction
`--------
`--------
Function DoPops()
SetFont Pops.Fnt
TitleScreen Pops.Txt$
ww = GetScreenWidth()
hh = GetScreenHeight()
tmpbackimg = NewImage(1,1)
GetImage tmpbackimg ,0,0,ww,hh ` capture the screen background
PositionBKGimg(mx,my ,ww,hh) ` call for the X and Y center of the popup image
Do
mb = MouseButton()
mx = MouseX()
my = MouseY()
If mb = 2 Then PositionBKGimg(mx,my ,ww,hh) ` drag the popup image around when right button is down and moving
DrawImage tmpbackimg , 0,0 ,0 ` the temp background image
DrawImage Pops.img(0), Pops.x ,Pops.y ,0 ` the popup image
Text Pops.x+5 ,Pops.y + 2 , Pops.Txt$ ` entered TEXT for the popup image

eee = GetTextWidth(Pops.Txt$)
u$ = Pops.Txt$
LLL = Len(Pops.Txt$) ` show the '_' if less than 14 letters
If LLL < Pops.MaxTxt Then BoxC Pops.x+6+eee ,Pops.y+14 ,Pops.x+6+eee+8 ,Pops.y+16 ,1,RGB(0,0,99)
`---
Letter$ = Inkey$()
If LLL < Pops.MaxTxt And Letter$ <> "" Then Pops.Txt$ = Pops.Txt$ + Letter$ : Letter$ = "" : FlushKeys ` check for text input
If ScanCode() = 14
FlushKeys : If LLL > 0 Then Pops.Txt$ = Left$(Pops.Txt$, LLL - 1) ` check for backspace key
EndIf
`---
`---
If my > Pops.y + 3 And my < Pops.y + 16
`---
`---
If mx > Pops.x + Pops.w - 60 And mx < Pops.x + Pops.w - 41
If mb < 2 Then DrawImage Pops.img(1) ,Pops.x + Pops.w - 60 ,Pops.y+4 ,0
If mb = 1
If LLL > 0 Then Pops.Txt$ = Left$(Pops.Txt$,LLL-1) : FlushMouse ` check for '<<' (backspace)
EndIf
EndIf
`---
If mx > Pops.x + Pops.w - 39 And mx < Pops.x + Pops.w - 20
If mb < 2 Then DrawImage Pops.img(2) ,Pops.x + Pops.w - 39 ,Pops.y+4 ,0
If mb = 1
TitleScreen Pops.Txt$ : DeleteImage tmpbackimg : FlushMouse : ExitDo ` check for 'ok' (keep the text)
EndIf
EndIf
`---
If mx > Pops.x + Pops.w - 16 And mx < Pops.x + Pops.w - 4
If mb < 2 Then DrawImage Pops.img(3) ,Pops.x + Pops.w - 16 ,Pops.y+4 ,0
If mb = 1
Pops.Txt$ = GetScreenTitle$() : DeleteImage tmpbackimg : FlushMouse : ExitDo ` check for 'X' cancel(exit popup)
EndIf
EndIf
`---
`---
EndIf
`---
`---
Sync
Loop
EndFunction
` end of code
Login required to view complete source code


...XpMe v1.2

http://tilemusic.com/

XpMe_v1.2

I have moved on up to play basic 1.74
and the code works with it except that
the 2 FLUSHKEYS commands inside
this code had to be removed.
I haven't had the time yet to see
what is equal to the FLUSHKEYS command.
But the code does work.
...XpMe v1.2

http://tilemusic.com/

thaaks

Nice stuff. Works like a charm, although I needed to add a setfps 60 command at the beginning. Otherwise the endless main loop eats too much processor time.

You can avoid the Flushkeys error if you change Flushkeys to Flushkeys()  ;)

Seems FlushKeys() moved from being a command to becoming a function that requires brackets...

Cheers,
Tommy

XpMe_v1.2

in 1.64 using the Flushkeys does the job.
in 1.74 using the Flushkeys() does'nt do the
job(it's like it's not even there). At least for me.
thaaks... If your using 1.75 tell me if Flushkeys()
works for you if used with the above code.
Maybe i'm doing it wrong for this version since i'm just
now trying it out.
...XpMe v1.2

http://tilemusic.com/

thaaks

I used PBFX 1.74 and your code snippet from the first post and changed FlushKeys to FlushKeys() and it "just worked" (TM)  :)

XpMe_v1.2

Well not for me when I changed it to FlushKeys() .
The backspace key shoots right through the FlushKeys() command
when it should not. It should delay a little making you tap the
backspace key to remove the letters one at a time.
Some of the above code needs rewritten to work in 1.74
...XpMe v1.2

http://tilemusic.com/

thaaks

Quote from: XpMe v1.2 on December 11, 2008, 07:38:10 AM
The backspace key shoots right through the FlushKeys() command
when it should not.
I can confirm that. I just gave it a quick try and made my "it's working in general" assumption  ;)

Looking forward to a proper PBFX version.

Cheers,
Tommy

XpMe_v1.2

In general it's good for 1.64 which is where I had used the code in a program that
I updated to use FX text and images (Eyepiece.exe). So it's done its job fo me.
Any new programs that I do will only be in 1.74 or above.
And I have many but not enough time. So the unfinished programs will have to be
slowly reworked to go into 1.74 which also will force me to get use to using it.
...XpMe v1.2

http://tilemusic.com/