News:

Function Finder  Find all the functions within source code files

Main Menu

Coding your own Two Option Yes / No - Dialog Example - LearnToCode

Started by kevin, October 01, 2022, 09:40:23 PM

Previous topic - Next topic

kevin

Coding your own  Two Option  Yes / No - Dialog Example - LearnToCode

    Programming dialogs can be pretty painful, as we often end up hard coding stuff into our applications for each question screen.  But for something as simple as YES / NO selection dialog / question then all we really need do is pass in a string contain all the options and let the function cut up the options and display it for us..  Which is what the example frame work source code does bellow.

    The idea here is when we need to ask the user of our program a YES/NO styled question;  we call our custom code which lays the question and options out for us.   So we're no concerned with the display and mouse hover/ selection controls at all.  We just call the function can it returns the STRING of the YES / NO option selected.   Making it more readable in the future.  


    If you have any questions regarding the code then ask bellow.

PlayBASIC Code: [Select]
      loadfont "Arial",1, 45

// Call our dialog with our options
YouSelected$=GUI_Dialog_OPTION("You You Wanna ?? |YES|NO")

if YouSelected$="YES"
print "YEP DO THAT THING"
endif

if YouSelected$="NO"
print "NAH.. DONT DO THAT THING"
endif

if YouSelected$=""
print "ABORTED- DO NOTHNG"
endif

Sync
waitkey


// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------


Function GUI_Dialog_OPTION(Message$)

Dim t$(100)
count=splittoarray(Message$,"|",t$())

// we're expecting 3 things, the message and the YES / NO text
if Count=3

State=false

do

cls

sw=getsurfacewidth()
sh=getsurfaceheight()
xpos=sw/2
ypos=(sh-(GetTExtHeight(Message$)*3))*0.40
centertext Xpos,ypos,t$(0)


// Get the max Width of the two options (Left option)
MaxWidth=GetTExtWidth(t$(1))

if MaxWidth<GetTExtWidth(t$(2))
MaxWidth=GetTExtWidth(t$(2))
endif

// Padding between options
Padding =32

// move the pen down two rows
TH=GetTExtHEight("|")
Ypos+=TH*2

// draw the left button
X1=Xpos - (MaxWidth+PAdding)
X2=X1+MaxWidth
Hover =GUI_BUTTON(X1,Ypos,X2,Ypos+TH,T$(1))

X1=Xpos + (PAdding)
X2=X1+MaxWidth

Hover+=GUI_BUTTON(X1,Ypos,X2,Ypos+TH,T$(2))*2

if Hover
if mousebutton()=1
if Hover=1 then Selection$=t$(1)
if Hover=2 then Selection$=t$(2)
endif
endif

if Len(Selection$) then State=true
if Esckey() then State=true


// Here we'd be calling our RENDER fucntion
sync

loop State


else

#print "Incorrect Parameters for GUI-DIALOG- THINGY"
endif

EndFunction Selection$





// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------


Function GUI_BUTTON(X1,Y1,X2,Y2,ThisText$)
Hover=MouseInBox(X1,Y1,X2,Y2)
c =rgbalphaadd(rgb(30,120,200),$404040*Hover)
c1=rgbalphaadd(rgb(15,40,100),$404040*Hover)

shadebox x1-8,y1,x2+8,y2,c,c,c1,c1
boxc x1-8,y1,x2+8,y2,false,c

centertext (X1+x2)/2,y1,ThisTExt$
EndFunction Hover






  Related Articles:

       Simple text Menu
       Custom File Dialog / File Request
       Action GUI
       Menu GUI
       Drag Rectangles/ GUI Selection