I thought I had figured this problem out a while ago but I haven't
I'm making a basic bare-bones user interface, with buttons, scrollbars and textboxes and such, and I am having trouble with my textbox. I can't figure out how to properly erase characters from the content string.
I can't use entry$(1) because that would mean that the entire content of the textbox has to be in the entry buffer. What I am doing is using entry$() to add individual characters to the content string, and manually subtracting characters from the end when keystate(14)=1. But i'm having problems with that; when I hold down the backspace key for a short period, it gets to erase about three characters, but if I hold it any longer it starts adding spaces to the end of it, which is the opposite of what's supposed to happen.
my test project is media-free, and here is the code
main.dba
sync on : sync rate 0
disable escapekey
gosub initialize_userInterface
MyButton = uiCreateButton( 5,5,0,0, "Press Me", 0xFFFFFFFF,0xFF888888, 0xFF000000,0xFFFFFFFF )
MyTextbox = uiCreateTextbox( screen width( ) - 640, 80, 36, 0, 0xFFFFFFFF, 0xFF888888 )
make camera 1
position camera 1, 0,0,-150
color backdrop 1, 0xFF0000FF
set camera to image 1, 1, 640,480
make object cube 1, 50
time = timer( )
while escapekey( ) = 0
cls
ink 0xFFFFFFFF, 0
if uiButtonPress( MyButton ) = 1
text 5,50,"hello world"
uiDepressButton( MyButton )
else
uiReleaseButton( MyButton )
endif
uiTextboxSelect( MyTextbox )
if uiTextboxes(MyTextbox).focus = 1
uiTextboxWrite( MyTextbox )
endif
uiDrawButton( MyButton )
uiDrawTextbox( MyTextbox )
yrotate object 1, object angle y( 1 )+(2.0*((timer( )-time)/16.6))
xrotate object 1, object angle x( 1 )+(2.0*((timer( )-time)/16.6))
rotate limb 1, 0, 0,0,limb angle z(1, 0)-(2.0*((timer( )-time)/16.6))*sin(object angle y( 1 ))
paste image 1, screen width( )-640, screen height( )-480
time = timer( )
sync
endwhile
end
userInterface.dba
// User Interface for anything that I want to have an interface for
initialize_userInterface:
type ui_button
x as integer
y as integer
w as integer
h as integer
caption as string
active as boolean
down as boolean
fgcolor as DWORD
bgcolor as DWORD
altfgcolor as DWORD
altbgcolor as DWORD
endtype
type ui_scrollbar
x as integer
y as integer
thickness as integer
length as integer
minimum as integer
maximum as integer
seek as float
position as integer
fgcolor as DWORD
bgcolor as DWORD
altfgcolor as DWORD
active as boolean
endtype
type ui_listbox
x as integer
y as integer
w as integer
h as integer
items as string
itemcount as integer
visiblerange as string
selecteditem as integer
endtype
type ui_textbox
x as integer
y as integer
w as integer
h as integer
maxchars as integer
charwidth as integer
cursor as integer
focus as boolean
active as boolean
content as string
caption as string
fgcolor as DWORD
bgcolor as DWORD
endtype
global dim uiButtons(-1) as ui_button
global dim uiScrollbars(-1) as ui_scrollbar
global dim uiListboxes(-1) as ui_listbox
global dim uiTextboxes(-1) as ui_textbox
global uiTextEraseDelay as integer
uiTextEraseDelay = timer( )
return
//function declarations and definitions
/////////////////////////////////////////////////////////////////// BUTTON FUNCTIONS ////////////////////////////////////////////////////////
function uiCreateButton( x as integer,y as integer,w as integer,h as integer, caption as string, fgcolor as DWORD,bgcolor as DWORD, altfgcolor as DWORD,altbgcolor as DWORD )
array insert at bottom uiButtons()
b = array count( uiButtons() )
uiButtons(b).x = x
uiButtons(b).y = y
if w < text width( caption ) then w = text width( caption )+20
uiButtons(b).w = w
if h < text height( caption ) then h = text height( caption ) + 10
uiButtons(b).h = h
uiButtons(b).caption = caption
uiButtons(b).fgcolor = fgcolor
uiButtons(b).bgcolor = bgcolor
uiButtons(b).altfgcolor = altfgcolor
uiButtons(b).altbgcolor = altbgcolor
uiButtons(b).active = 1
endfunction b
function uiButtonPress( bton as integer )
mousx = mousex( )
mousy = mousey( )
mousc = mouseclick( )
if mousx >= uiButtons(bton).x and mousx <= uiButtons(bton).x+uiButtons(bton).w
if mousy >= uiButtons(bton).y and mousy <=uiButtons(bton).y+uiButtons(bton).h
if mousc = 1
exitfunction 1
endif
endif
endif
endfunction 0
function uiDrawButton( bton as integer )
if uiButtons(bton).down = 0
ink uiButtons(bton).bgcolor, 0
else
ink uiButtons(bton).altbgcolor, 0
endif
box uiButtons(bton).x,uiButtons(bton).y, uiButtons(bton).x+uiButtons(bton).w,uiButtons(bton).y+uiButtons(bton).h
if uiButtons(bton).down = 0
ink uiButtons(bton).fgcolor, 0
else
ink uiButtons(bton).altfgcolor, 0
endif
center text uiButtons(bton).x+(uiButtons(bton).w/2.0), (uiButtons(bton).y+(uiButtons(bton).h/2.0) - (text height( uiButtons(bton).caption )/2.0)), uiButtons(bton).caption
endfunction
function uiDepressButton( bton as integer )
uiButtons(bton).down = 1
endfunction
function uiReleaseButton( bton as integer )
uiButtons(bton).down = 0
endfunction
//////////////////////////////////////////////////////////////// SCROLLBAR FUNCTIONS /////////////////////////////////////////////////////
function uiCreateScrollbar( x as integer,y as integer, thik as integer,leng as integer, mn as integer,mx as integer, fg as DWORD,bg as DWORD,altfg as DWORD )
array insert at bottom uiScrollbars()
i = array count( uiScrollbars() )
uiScrollbars(i).x = x
uiScrollbars(i).y = y
uiScrollbars(i).thickness = thik
uiScrollbars(i).length = leng
uiScrollbars(i).minimum = mn
uiScrollbars(i).maximum = mx
uiScrollbars(i).fgcolor = fg
uiScrollbars(i).bgcolor = bg
uiScrollbars(i).altfgcolor = altfg
uiScrollbars(i).seek = 0.0
uiScrollbars(i).position = 0
uiScrollbars(i).active = 1
endfunction i
///////////////////////////////////////////////////////////////// TEXTBOX FUNCTIONS ///////////////////////////////////////////////////////
function uiCreateTextbox( x as integer,y as integer, charwidth as integer, mx as integer, fg as DWORD,bg as DWORD )
array insert at bottom uiTextboxes()
i = array count( uiTextboxes() )
uiTextboxes(i).x = x
uiTextboxes(i).y = y
uiTextboxes(i).charwidth = charwidth
wid = (text width("W")*charwidth)+10
uiTextboxes(i).w = wid
heit = text height("W")+10
uiTextboxes(i).h = heit
uiTextboxes(i).maxchars = mx
uiTextboxes(i).fgcolor = fg
uiTextboxes(i).bgcolor = bg
uiTextboxes(i).cursor = 0
uiTextboxes(i).active = 1
uiTextboxes(i).focus = 0
uiTextboxes(i).content = ""
endfunction i
function uiTextboxSelect( textbox as integer )
x = uiTextboxes(t).x
y = uiTextboxes(t).y
h = uiTextboxes(t).h
w = uiTextboxes(t).w
t = textbox
mousx = mousex( )
mousy = mousey( )
mousc = mouseclick( )
if mousc = 1
if mousx >= x and mousx <= x+w
if mousy >= y and mousy <= y+h
uiTextboxes(t).focus = 1
clear entry buffer
else
uiTextboxes(t).focus = 0
endif
else
uiTextboxes(t).focus = 0
endif
endif
endfunction
function uiTextboxWrite( textbox as integer )
t = textbox
if scancode( ) = 14
if timer( )-uiTextEraseDelay >= 150
clear entry buffer
uiTextboxes(t).content = left$(uiTextboxes(t).content, len(uiTextboxes(t).content)-1)
uiTextEraseDelay = timer( )
endif
endif
write$ = entry$( )
uiTextboxes(t).content = uiTextboxes(t).content + write$
clear entry buffer
endfunction
function uiDrawTextbox( textbox as integer )
t = textbox
ink uiTextboxes(t).bgcolor, 0
box uiTextboxes(t).x, uiTextboxes(t).y, uiTextboxes(t).x+uiTextboxes(t).w, uiTextboxes(t).y+uiTextboxes(t).h
if uiTextboxes(t).focus = 1
ink uiTextboxes(t).fgcolor, 0
line uiTextboxes(t).x, uiTextboxes(t).y, uiTextboxes(t).x+uiTextboxes(t).w, uiTextboxes(t).y
line uiTextboxes(t).x, uiTextboxes(t).y, uiTextboxes(t).x, uiTextboxes(t).y+uiTextboxes(t).h
line uiTextboxes(t).x+uiTextboxes(t).w, uiTextboxes(t).y, uiTextboxes(t).x+uiTextboxes(t).w, uiTextboxes(t).y+uiTextboxes(t).h
line uiTextboxes(t).x, uiTextboxes(t).y+uiTextboxes(t).h, uiTextboxes(t).x+uiTextboxes(t).w, uiTextboxes(t).y+uiTextboxes(t).h
endif
if len(uiTextboxes(t).content) > uiTextboxes(t).charwidth
uiTextboxes(t).caption = right$(uiTextboxes(t).content, uiTextboxes(t).charwidth)
else
uiTextboxes(t).caption = uiTextboxes(t).content
endif
textx = uiTextboxes(t).x + 5
texty = uiTextboxes(t).y + 5
ink uiTextboxes(t).fgcolor, 0
text textx,texty, uiTextboxes(t).caption
endfunction
halp
OH WAIT
i just fixed it
new uiTextboxWrite() function:
function uiTextboxWrite( textbox as integer )
t = textbox
if scancode( ) = 14
if timer( )-uiTextEraseDelay >= 150
clear entry buffer
uiTextboxes(t).content = left$(uiTextboxes(t).content, len(uiTextboxes(t).content)-1)
uiTextEraseDelay = timer( )
endif
exitfunction
endif
write$ = entry$( )
uiTextboxes(t).content = uiTextboxes(t).content + write$
clear entry buffer
endfunction
Remember those old guys? They made epic renders, I think one of them was called DaVinci, and all they used was MS Paint. Sometimes it's just skill....