|
Post by Chris Iverson on May 10, 2020 1:15:02 GMT -5
Here's some code I came up with, to play with text and graphics positioning inside of graphicboxes.
I was writing this to be a base for a kind of UI, like for a game or similar.
It essentially creates a virtual text "window". It creates a box, with a title, and positions the supplied text inside the box, with letter wrapping(I wouldn't call it word wrapping since it will split words currently) and line cutoff if too many lines are supplied.
This demo should work regardless of the text font or size you choose.
It's currently set up to be auto-resizing, if you resize the window. It will resize the "box", and redraw the text inside of it.
You could also make the box a fixed size and position, and there's an alternate code route you could enable that allows using it as a virtual text box.
WindowWidth = 640 WindowHeight = 480
graphicbox #test.gb, 0, 0, WindowWidth, WindowHeight open "test" for window as #test
'Base the graphicbox's size on the window's client area, instead of the full 'window size(including titlebar and borders) ' 'This is to match up with RESIZEHANDLER's values, which give 'clientarea sizes, not full window sizes. struct rect, l as long, t as long, r as long, b as long
hwnd = hwnd(#test) calldll #user32, "GetClientRect",_ hwnd as ulong,_ rect as struct,_ ret as long
ClientWidth = rect.r.struct ClientHeight = rect.b.struct
#test.gb, "locate 0 0 ";ClientWidth;" ";ClientHeight #test, "refresh"
#test, "resizehandler [resize]" #test, "trapclose [quit]"
#test.gb, "color black" #test.gb, "down"
'Enable this to test at different fonts/sizes. '#test.gb, "font courier_new 20"
'Enable this to allow editing of the entered character string. 'Also recommended to comment out dispString$, to start with a blank screen to "type" with.
'#test.gb, "when characterInput [doInput]" dispString$ = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at dui molestie, maximus felis et, iaculis justo. Fusce imperdiet vel lacus luctus fermentum. Nunc posuere velit nec tincidunt viverra. Nullam volutpat tristique mattis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque commodo lectus a fringilla bibendum. Mauris scelerisque turpis non posuere molestie. Donec vel tempus ligula. Vivamus convallis mollis condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum a nisl quis pellentesque. Nullam ut dolor efficitur odio gravida venenatis a a ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean gravida ipsum id magna mattis, et tempor leo pulvinar. Praesent odio nulla, aliquam efficitur nulla id, consectetur dictum metus." + chr$(13) + chr$(13) + _ "Proin finibus finibus neque, tincidunt tempus leo imperdiet nec. Vivamus dolor felis, mollis vitae leo vitae, luctus facilisis felis. Vestibulum sed velit eget lorem tincidunt feugiat. Ut justo urna, blandit eu orci vel, mollis rhoncus nunc. Curabitur bibendum cursus pellentesque. Cras sagittis libero in justo bibendum finibus. In quis faucibus ex, et finibus dui. Nam finibus tempus neque, ut tincidunt arcu eleifend vel. Aenean tortor lectus, pharetra quis ipsum nec, suscipit euismod nunc. In dignissim massa at elit luctus scelerisque. Sed magna sapien, pretium a ex eget, egestas auctor ligula." + chr$(13) + chr$(13) + _ "Fusce rhoncus ac urna vel iaculis. Sed eget leo ac turpis porta scelerisque vel eu ex. Nulla vehicula venenatis tortor, eget imperdiet sem tincidunt sed. Morbi ligula nisl, porttitor eget eleifend vel, gravida nec tortor. Suspendisse vel condimentum dolor. Suspendisse posuere, augue ac luctus facilisis, erat sapien hendrerit mi, id ullamcorper dui purus ut elit. Mauris lobortis fermentum mi, vitae fringilla arcu tristique porta. Vestibulum vehicula porta odio id pellentesque. Vivamus convallis, est ut cursus rhoncus, nunc velit aliquet diam, vestibulum lobortis leo nulla a massa. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ac ante mauris. Vivamus a convallis quam, et mollis arcu." + chr$(13) + chr$(13) + _ "Donec ac nisl accumsan, euismod enim sed, pharetra ipsum. Vestibulum euismod libero ac risus placerat, eget faucibus leo volutpat. Aliquam eget est elit. Donec sit amet consequat sem. Pellentesque leo nulla, consequat et tristique eget, convallis id urna. Nulla nec enim ante. Nulla nec placerat nisi. Nulla placerat cursus purus ac porta. Proin lectus nisi, semper non placerat ut, ullamcorper vel risus. Nulla bibendum scelerisque erat ut feugiat. Donec in orci sit amet nulla convallis faucibus. Nulla imperdiet nunc a massa feugiat dignissim. Integer tincidunt tellus porttitor dui elementum aliquam. Donec condimentum turpis vel lorem ullamcorper, congue blandit arcu eleifend. Sed auctor hendrerit rutrum." + chr$(13) + chr$(13) + _ "Duis rhoncus feugiat urna et auctor. Maecenas nec molestie ligula, et suscipit lacus. Vivamus molestie nunc urna, vel scelerisque arcu placerat eu. Nullam sed felis felis. In non lacus ex. Duis et justo erat. Praesent non magna ac libero ultrices pharetra. Praesent elit ipsum, lacinia eget sodales ut, efficitur facilisis erat."
#test.gb, "setfocus"
'Initial flush, even if blank, so the first "delsegment" when repainting the screen 'doesn't crash #test.gb, "flush"
'Box can be placed anywhere boxX = 10 boxY = 10
'This is to make the box margin equal on all sizes. You could also 'make the box a custom size here. boxW = ClientWidth - (boxX * 2) - 2 boxH = ClientHeight - (boxY * 2) - 2 'boxW = 200 'boxH = 200
'This is to get the height of a "line" in the current font, so we know '(roughly) how much space we have to work with. #test.gb, "place 0 0" #test.gb, "\ " #test.gb, "posxy x y"
lineHeight = y
'This sets the allowed side margins of text on the inside of the box. boxSideMargin = 10
'The "title" of the box is inset further than the side margins inside the box, 'to make it stand out further. titlePosX = boxX + (boxSideMargin * 2) titlePosY = boxY + (lineHeight / 3)
'Determining initital string position inside the box, as well as the string 'width cutoff necessary to keep the margin on both sides equal strPosX = boxX + boxSideMargin strPosY = boxY + lineHeight + (lineHeight / 3) cutoff = boxW - (boxSideMargin * 2)
gosub [refreshScreen] wait
[doInput] if len(Inkey$) > 1 then modifier = asc(left$(Inkey$, 1)) end if
char$ = right$(Inkey$, 1) charVal = asc(char$)
if charVal = _VK_BACK then if swallowFirstBackspace = 1 then dispString$ = left$(dispString$, len(dispString$) - 1) gosub [refreshScreen] swallowFirstBackspace = 0 else swallowFirstBackspace = 1 end if end if
if charVal = _VK_RETURN then if swallowFirstReturn = 1 then dispString$ = dispString$ + char$ swallowFirstReturn = 0 else swallowFirstReturn = 1 end if end if
if charVal > 31 AND charVal < 127 then dispString$ = dispString$ + char$ gosub [refreshScreen] end if
wait
[refreshScreen] lines = 1 for x = 1 to len(dispString$) letter$ = mid$(dispString$, x, 1) curLine$ = curLine$ + letter$
if asc(letter$) = 13 then lines = lines + 1 if ((lines * lineHeight) + ((lineHeight / 3)*2)) >= boxH then exit for end if
outString$ = outString$ + curLine$ curLine$ = "" goto [skipTest] end if
#test.gb, "stringwidth? curLine$ width"
if width > cutoff then curLine$ = left$(curLine$, len(curLine$) - 1) lines = lines + 1 if ((lines * lineHeight) + ((lineHeight / 3) * 2)) >= boxH then exit for end if
outString$ = outString$ + curLine$ + chr$(13) curLine$ = letter$ end if
[skipTest] next x
outString$ = outString$ + curLine$
#test.gb, "delsegment 1" #test.gb, "fill white"
#test.gb, "place ";boxX;" ";boxY #test.gb, "box ";boxW+boxX;" ";boxH+boxY
#test.gb, "place ";titlePosX;" ";titlePosY #test.gb, "\TITLE"
'#test, "place ";strPosX;" ";strPosY #test.gb, "place ";strPosX;" ";strPosY #test.gb, "\" + outString$ #test.gb, "flush"
outString$ = "" curLine$ = "" return
[resize] boxW = WindowWidth - (boxX * 2) - 2 boxH = WindowHeight - (boxY * 2) - 2 cutoff = boxW - (boxSideMargin * 2) #test.gb, "locate 0 0 ";WindowWidth;" ";WindowHeight #test, "refresh" gosub [refreshScreen] wait
[quit] close #test
|
|
|
Post by svajoklis on May 10, 2020 3:46:06 GMT -5
Don't know why, but the window text keeps flickering I tried out the character input option and it takes quite a while for the window to start responding to input. Not sure why, it feels like there are a lot of refreshes happening before my input starts being processed but I can't seem to find out where they happen. Since you can measure characters, upon loading font you could build a character size table (or build it dynamically on characters that have not yet been measured), then you wouldn't have to redraw things that didn't change. On adding a character or removing one at the end you would only have to draw/clear the rectangle the character occupies. Well either that or leverage the stringwidth more for measurements. Would look super interesting to build some reusable components to draw something like the Windows 3.1 UI elements with the borders and buttons, since it's mostly straight single color lines or some small icons that could be drawn from some array of string commands or something
|
|
timur77
Junior Member
Someday I will tell my grandsons that I am older than the Internet. And it will blow their brain.
Posts: 79
|
Post by timur77 on May 10, 2020 9:29:08 GMT -5
Could you outline the task once more to understand what you want to do.
The program code presented works correctly without flickering
|
|
|
Post by Brandon Parker on May 10, 2020 10:55:39 GMT -5
Chris, I think I would have gone the Word$() route, but see if this successfully splits the words out for the line separation.
NoMainWin WindowWidth = 640 WindowHeight = 480
graphicbox #test.gb, 0, 0, WindowWidth, WindowHeight open "test" for window as #test
'Base the graphicbox's size on the window's client area, instead of the full 'window size(including titlebar and borders) ' 'This is to match up with RESIZEHANDLER's values, which give 'clientarea sizes, not full window sizes. struct rect, l as long, t as long, r as long, b as long
hwnd = hwnd(#test) calldll #user32, "GetClientRect",_ hwnd as ulong,_ rect as struct,_ ret as long
ClientWidth = rect.r.struct ClientHeight = rect.b.struct
#test.gb, "locate 0 0 ";ClientWidth;" ";ClientHeight #test, "refresh"
#test, "resizehandler [resize]" #test, "trapclose [quit]"
#test.gb, "color black" #test.gb, "down"
'Enable this to test at different fonts/sizes. '#test.gb, "font courier_new 20"
'Enable this to allow editing of the entered character string. 'Also recommended to comment out dispString$, to start with a blank screen to "type" with.
'#test.gb, "when characterInput [doInput]" dispString$ = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at dui molestie, maximus felis et, iaculis justo. Fusce imperdiet vel lacus luctus fermentum. Nunc posuere velit nec tincidunt viverra. Nullam volutpat tristique mattis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque commodo lectus a fringilla bibendum. Mauris scelerisque turpis non posuere molestie. Donec vel tempus ligula. Vivamus convallis mollis condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum a nisl quis pellentesque. Nullam ut dolor efficitur odio gravida venenatis a a ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean gravida ipsum id magna mattis, et tempor leo pulvinar. Praesent odio nulla, aliquam efficitur nulla id, consectetur dictum metus." + chr$(13) + chr$(13) + _ "Proin finibus finibus neque, tincidunt tempus leo imperdiet nec. Vivamus dolor felis, mollis vitae leo vitae, luctus facilisis felis. Vestibulum sed velit eget lorem tincidunt feugiat. Ut justo urna, blandit eu orci vel, mollis rhoncus nunc. Curabitur bibendum cursus pellentesque. Cras sagittis libero in justo bibendum finibus. In quis faucibus ex, et finibus dui. Nam finibus tempus neque, ut tincidunt arcu eleifend vel. Aenean tortor lectus, pharetra quis ipsum nec, suscipit euismod nunc. In dignissim massa at elit luctus scelerisque. Sed magna sapien, pretium a ex eget, egestas auctor ligula." + chr$(13) + chr$(13) + _ "Fusce rhoncus ac urna vel iaculis. Sed eget leo ac turpis porta scelerisque vel eu ex. Nulla vehicula venenatis tortor, eget imperdiet sem tincidunt sed. Morbi ligula nisl, porttitor eget eleifend vel, gravida nec tortor. Suspendisse vel condimentum dolor. Suspendisse posuere, augue ac luctus facilisis, erat sapien hendrerit mi, id ullamcorper dui purus ut elit. Mauris lobortis fermentum mi, vitae fringilla arcu tristique porta. Vestibulum vehicula porta odio id pellentesque. Vivamus convallis, est ut cursus rhoncus, nunc velit aliquet diam, vestibulum lobortis leo nulla a massa. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ac ante mauris. Vivamus a convallis quam, et mollis arcu." + chr$(13) + chr$(13) + _ "Donec ac nisl accumsan, euismod enim sed, pharetra ipsum. Vestibulum euismod libero ac risus placerat, eget faucibus leo volutpat. Aliquam eget est elit. Donec sit amet consequat sem. Pellentesque leo nulla, consequat et tristique eget, convallis id urna. Nulla nec enim ante. Nulla nec placerat nisi. Nulla placerat cursus purus ac porta. Proin lectus nisi, semper non placerat ut, ullamcorper vel risus. Nulla bibendum scelerisque erat ut feugiat. Donec in orci sit amet nulla convallis faucibus. Nulla imperdiet nunc a massa feugiat dignissim. Integer tincidunt tellus porttitor dui elementum aliquam. Donec condimentum turpis vel lorem ullamcorper, congue blandit arcu eleifend. Sed auctor hendrerit rutrum." + chr$(13) + chr$(13) + _ "Duis rhoncus feugiat urna et auctor. Maecenas nec molestie ligula, et suscipit lacus. Vivamus molestie nunc urna, vel scelerisque arcu placerat eu. Nullam sed felis felis. In non lacus ex. Duis et justo erat. Praesent non magna ac libero ultrices pharetra. Praesent elit ipsum, lacinia eget sodales ut, efficitur facilisis erat."
#test.gb, "setfocus"
'Initial flush, even if blank, so the first "delsegment" when repainting the screen 'doesn't crash #test.gb, "flush"
'Box can be placed anywhere boxX = 10 boxY = 10
'This is to make the box margin equal on all sizes. You could also 'make the box a custom size here. boxW = ClientWidth - (boxX * 2) - 2 boxH = ClientHeight - (boxY * 2) - 2 'boxW = 200 'boxH = 200
'This is to get the height of a "line" in the current font, so we know '(roughly) how much space we have to work with. #test.gb, "place 0 0" #test.gb, "\ " #test.gb, "posxy x y"
lineHeight = y
'This sets the allowed side margins of text on the inside of the box. boxSideMargin = 10
'The "title" of the box is inset further than the side margins inside the box, 'to make it stand out further. titlePosX = boxX + (boxSideMargin * 2) titlePosY = boxY + (lineHeight / 3)
'Determining initital string position inside the box, as well as the string 'width cutoff necessary to keep the margin on both sides equal strPosX = boxX + boxSideMargin strPosY = boxY + lineHeight + (lineHeight / 3) cutoff = boxW - (boxSideMargin * 2)
gosub [refreshScreen] wait
[doInput] if len(Inkey$) > 1 then modifier = asc(left$(Inkey$, 1)) end if
char$ = right$(Inkey$, 1) charVal = asc(char$)
if charVal = _VK_BACK then if swallowFirstBackspace = 1 then dispString$ = left$(dispString$, len(dispString$) - 1) gosub [refreshScreen] swallowFirstBackspace = 0 else swallowFirstBackspace = 1 end if end if
if charVal = _VK_RETURN then if swallowFirstReturn = 1 then dispString$ = dispString$ + char$ swallowFirstReturn = 0 else swallowFirstReturn = 1 end if end if
if charVal > 31 AND charVal < 127 then dispString$ = dispString$ + char$ gosub [refreshScreen] end if
wait
[refreshScreen] lines = 1 : charCount = 1 for x = 1 to len(dispString$) letter$ = mid$(dispString$, x, 1) curLine$ = curLine$ + letter$
if asc(letter$) = 13 then lines = lines + 1 if ((lines * lineHeight) + ((lineHeight / 3)*2)) >= boxH then exit for end if
outString$ = outString$ + curLine$ curLine$ = "" goto [skipTest] end if
lineLength = (lineLength + 1) If Asc(letter$) = 32 Then lastSpace = charCount lastDocSpace = x End If
#test.gb, "stringwidth? curLine$ width"
if width > cutoff then 'curLine$ = left$(curLine$, len(curLine$) - 1) curLine$ = Left$(curLine$, lastSpace) lines = lines + 1 if ((lines * lineHeight) + ((lineHeight / 3) * 2)) >= boxH then exit for end if
outString$ = outString$ + curLine$ + chr$(13) curLine$ = "" x = lastDocSpace : lastDocSpace = 0 : lastSpace = 0 : charCount = 1 Else charCount = (charCount + 1) end if
[skipTest] next x
outString$ = outString$ + curLine$
#test.gb, "delsegment 1" #test.gb, "fill white"
#test.gb, "place ";boxX;" ";boxY #test.gb, "box ";boxW+boxX;" ";boxH+boxY
#test.gb, "place ";titlePosX;" ";titlePosY #test.gb, "\TITLE"
'#test, "place ";strPosX;" ";strPosY #test.gb, "place ";strPosX;" ";strPosY #test.gb, "\" + outString$ #test.gb, "flush"
outString$ = "" curLine$ = "" return
[resize] boxW = WindowWidth - (boxX * 2) - 2 boxH = WindowHeight - (boxY * 2) - 2 cutoff = boxW - (boxSideMargin * 2) #test.gb, "locate 0 0 ";WindowWidth;" ";WindowHeight #test, "refresh" gosub [refreshScreen] wait
[quit] close #test
{:0)
Brandon Parker
|
|
|
Post by Rod on May 10, 2020 13:20:10 GMT -5
Yeah, no flickering for me. What OS is flickering? All the time? Or for the few milliseconds it takes to draw the text?
|
|
|
Post by svajoklis on May 10, 2020 15:24:48 GMT -5
It was Windows 10 and it seems to flicker after resizing the window to a pretty big size. It doesn't happen all the time though. When it does happen it does so something like two times a second.
|
|
|
Post by Chris Iverson on May 10, 2020 16:52:34 GMT -5
The flickering that is sometimes visible is just a side effect of redrawing the whole screen. LB doesn't do buffered drawing natively, so if you clear the screen and redraw it, that will sometimes be visible.
As to why it sometimes happens multiple times after resizing, that's because LB sometimes fires the resize event multiple times after a resize. I'm not quite sure why, I'm assuming it's something to do with the way Windows sends the resize message notifications.
Add PRINT "resizing" to the code, just after the beginning of the [resize] branch, and you'll see it fire multiple times when you resize.
You can add code to the resize handler to prevent it from redrawing if the window size hasn't actually changed.
First, at the beginning of the code, when the clientarea is first determined, save those values as oldWidth and oldHeight:
ClientWidth = rect.r.struct ClientHeight = rect.b.struct
oldWidth = ClientWidth oldHeight = ClientHeight
And then, for the first part of the resize handler, if the new size matches the old one, stop executing and just wait. If they don't match, save the new size as the old size:
[resize] if ((oldWidth = WindowWidth) AND (oldHeight = WindowHeight)) then wait
oldWidth = WindowWidth oldHeight = WindowHeight
|
|
|
Post by Brandon Parker on May 10, 2020 23:28:54 GMT -5
Chris, I rewrote the [refreshScreen] routine, and I am 99.999% sure it wraps correctly at this point. Note that also turn off the resizeHandler when first entering it since it does not work otherwise. This makes sense to me since I would really want to only handle one resize event that Windows sends out ... the last one.
WindowWidth = 640 WindowHeight = 480
GraphicBox #test.gb, 0, 0, WindowWidth, WindowHeight Open "test" For window As #test
'Base the graphicbox's size on the window's client area, instead of the full 'window size(including titlebar and borders) ' 'This is to match up with RESIZEHANDLER's values, which give 'clientarea sizes, not full window sizes. Struct rect, l As long, t As long, r As long, b As long
hwnd = hwnd(#test) CallDLL #user32, "GetClientRect", hwnd As ulong, _ rect As Struct, _ ret As long
ClientWidth = rect.r.struct ClientHeight = rect.b.struct
oldWidth = ClientWidth oldHeight = ClientHeight
#test.gb, "locate 0 0 ";ClientWidth;" ";ClientHeight #test, "refresh"
#test, "resizehandler [resize]" #test, "trapclose [quit]"
#test.gb, "color black" #test.gb, "down"
'Enable this to test at different fonts/sizes. '#test.gb, "font courier_new 20"
'Enable this to allow editing of the entered character string. 'Also recommended to comment out dispString$, to start with a blank screen to "type" with.
'#test.gb, "when characterInput [doInput]" dispString$ = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at dui molestie, maximus felis et, iaculis justo. Fusce imperdiet vel lacus luctus fermentum. Nunc posuere velit nec tincidunt viverra. Nullam volutpat tristique mattis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque commodo lectus a fringilla bibendum. Mauris scelerisque turpis non posuere molestie. Donec vel tempus ligula. Vivamus convallis mollis condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum a nisl quis pellentesque. Nullam ut dolor efficitur odio gravida venenatis a a ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean gravida ipsum id magna mattis, et tempor leo pulvinar. Praesent odio nulla, aliquam efficitur nulla id, consectetur dictum metus." + chr$(13) + chr$(13) + _ "Proin finibus finibus neque, tincidunt tempus leo imperdiet nec. Vivamus dolor felis, mollis vitae leo vitae, luctus facilisis felis. Vestibulum sed velit eget lorem tincidunt feugiat. Ut justo urna, blandit eu orci vel, mollis rhoncus nunc. Curabitur bibendum cursus pellentesque. Cras sagittis libero in justo bibendum finibus. In quis faucibus ex, et finibus dui. Nam finibus tempus neque, ut tincidunt arcu eleifend vel. Aenean tortor lectus, pharetra quis ipsum nec, suscipit euismod nunc. In dignissim massa at elit luctus scelerisque. Sed magna sapien, pretium a ex eget, egestas auctor ligula." + chr$(13) + chr$(13) + _ "Fusce rhoncus ac urna vel iaculis. Sed eget leo ac turpis porta scelerisque vel eu ex. Nulla vehicula venenatis tortor, eget imperdiet sem tincidunt sed. Morbi ligula nisl, porttitor eget eleifend vel, gravida nec tortor. Suspendisse vel condimentum dolor. Suspendisse posuere, augue ac luctus facilisis, erat sapien hendrerit mi, id ullamcorper dui purus ut elit. Mauris lobortis fermentum mi, vitae fringilla arcu tristique porta. Vestibulum vehicula porta odio id pellentesque. Vivamus convallis, est ut cursus rhoncus, nunc velit aliquet diam, vestibulum lobortis leo nulla a massa. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ac ante mauris. Vivamus a convallis quam, et mollis arcu." + chr$(13) + chr$(13) + _ "Donec ac nisl accumsan, euismod enim sed, pharetra ipsum. Vestibulum euismod libero ac risus placerat, eget faucibus leo volutpat. Aliquam eget est elit. Donec sit amet consequat sem. Pellentesque leo nulla, consequat et tristique eget, convallis id urna. Nulla nec enim ante. Nulla nec placerat nisi. Nulla placerat cursus purus ac porta. Proin lectus nisi, semper non placerat ut, ullamcorper vel risus. Nulla bibendum scelerisque erat ut feugiat. Donec in orci sit amet nulla convallis faucibus. Nulla imperdiet nunc a massa feugiat dignissim. Integer tincidunt tellus porttitor dui elementum aliquam. Donec condimentum turpis vel lorem ullamcorper, congue blandit arcu eleifend. Sed auctor hendrerit rutrum." + chr$(13) + chr$(13) + _ "Duis rhoncus feugiat urna et auctor. Maecenas nec molestie ligula, et suscipit lacus. Vivamus molestie nunc urna, vel scelerisque arcu placerat eu. Nullam sed felis felis. In non lacus ex. Duis et justo erat. Praesent non magna ac libero ultrices pharetra. Praesent elit ipsum, lacinia eget sodales ut, efficitur facilisis erat."
#test.gb, "setfocus"
'Initial flush, even if blank, so the first "delsegment" when repainting the screen 'doesn't crash #test.gb, "flush"
'Box can be placed anywhere boxX = 10 boxY = 10
'This is to make the box margin equal on all sizes. You could also 'make the box a custom size here. boxW = ClientWidth - (boxX * 2) - 2 boxH = ClientHeight - (boxY * 2) - 2 'boxW = 200 'boxH = 200
'This is to get the height of a "line" in the current font, so we know '(roughly) how much space we have to work with. #test.gb, "place 0 0" #test.gb, "\ " #test.gb, "posxy x y"
lineHeight = y
'This sets the allowed side margins of text on the inside of the box. boxSideMargin = 10
'The "title" of the box is inset further than the side margins inside the box, 'to make it stand out further. titlePosX = boxX + (boxSideMargin * 2) titlePosY = boxY + (lineHeight / 3)
'Determining initital string position inside the box, as well as the string 'width cutoff necessary to keep the margin on both sides equal strPosX = boxX + boxSideMargin strPosY = boxY + lineHeight + (lineHeight / 3) cutoff = boxW - (boxSideMargin * 2)
GoSub [refreshScreen] Wait
[doInput] If len(Inkey$) > 1 Then modifier = asc(left$(Inkey$, 1)) End If
char$ = right$(Inkey$, 1) charVal = asc(char$)
If charVal = _VK_BACK Then If swallowFirstBackspace = 1 Then dispString$ = left$(dispString$, len(dispString$) - 1) GoSub [refreshScreen] swallowFirstBackspace = 0 Else swallowFirstBackspace = 1 End If End If
If charVal = _VK_RETURN Then If swallowFirstReturn = 1 Then dispString$ = dispString$ + char$ swallowFirstReturn = 0 Else swallowFirstReturn = 1 End If End If
If charVal > 31 And charVal < 127 Then dispString$ = dispString$ + char$ GoSub [refreshScreen] End If Wait
[refreshScreen] lines = 0 For x = 1 To len(dispString$) lines = lines + 1 If ((lines * lineHeight) + ((lineHeight / 3) * 2)) >= boxH Then Exit For End If nextWord$ = "" : currentLine$ = "" : currentWidth = 0 : lineWord = 0 : returnFound = 0 While (Mid$(dispString$, x) <> "") And (currentWidth < cutoff) Scan nextWord$ = currentLine$;Word$(Mid$(dispString$, x), (lineWord + 1), " ");" " If Instr(nextWord$, chr$(13)) Then nextWord$ = UpTo$(nextWord$, chr$(13)) : returnFound = 1 lineWord = (lineWord + 1) #test.gb, "StringWidth? nextWord$ currentWidth" If (currentWidth < cutoff) Then currentLine$ = nextWord$ If (returnFound = 1) Then x = (Len(currentLine$) + x) currentLine$ = currentLine$ + chr$(13) Exit While End If Else x = (Len(currentLine$) + (x - 1)) currentLine$ = currentLine$ + chr$(13) Exit While End If Wend outString$ = outString$ + currentLine$ Next x
#test.gb, "delsegment 1" #test.gb, "fill white"
#test.gb, "place ";boxX;" ";boxY #test.gb, "box ";boxW+boxX;" ";boxH+boxY
#test.gb, "place ";titlePosX;" ";titlePosY #test.gb, "\TITLE"
'#test, "place ";strPosX;" ";strPosY #test.gb, "place ";strPosX;" ";strPosY #test.gb, "\" + outString$ #test.gb, "flush"
outString$ = "" Return
[resize] #test, "resizehandler" If ((oldWidth = WindowWidth) And (oldHeight = WindowHeight)) Then #test, "resizehandler [resize]" Wait End If oldWidth = WindowWidth oldHeight = WindowHeight boxW = WindowWidth - (boxX * 2) - 2 boxH = WindowHeight - (boxY * 2) - 2 cutoff = boxW - (boxSideMargin * 2) #test.gb, "locate 0 0 ";WindowWidth;" ";WindowHeight #test, "refresh" GoSub [refreshScreen] #test, "resizehandler [resize]" Wait
[quit] Close #test
{:0)
Brandon Parker
|
|
|
Post by Chris Iverson on May 10, 2020 23:52:38 GMT -5
I haven't found a way to break it yet. Thanks, Brandon!
Also, nice catch with turning off the resize handler while handling a resize. If you do that, you can actually remove all of the oldSize code, that stores and compares the old size against the new one. It won't fire multiple times if you handle the resize while the event is turned off.
Also, I noticed you added a 'scan' to the middle of the refreshScreen. Were you finding issues with responsiveness? I don't have a problem with the 'scan' on its own, but if you add that there, you then have to turn off the characterInput handler while handling input, and turn it back on afterwards, or you'll get a crash if another key press comes in while you're handling the first one. You'll also miss key events if the person presses keys simultaneously, or fast enough in sequence.
EDIT: I did find one issue with the word wrap as it's currently implemented - if you get a word by itself that is longer than the size of a single line, all text drawing will cut off at that word. If it reaches a word it HAS to break, it simply refuses to do so, and won't render any more text.
That's going to be an interesting one to fix.
|
|
|
Post by Brandon Parker on May 11, 2020 0:31:19 GMT -5
LOL...
I was just laying in bed trying to go to sleep and was thinking the same thing. I was like..."Naahhhh, he wouldn't make it that small" .... doohhhh...
I already have a mental process for resolving that issue by splitting the word and adding a hyphen. I will look at it in the morning and update the code.
I enjoy challenges!!
{:0)
Brandon Parker
|
|
|
Post by Brandon Parker on May 11, 2020 9:55:02 GMT -5
Chris, Try this out; I changed the sixth word from "consectetur" to "consecteturium" and the ninth word from "Aliquam" to "Aliquamazing" for testing. It seems to work at first glance, but put it through some tests of your own.
WindowWidth = 640 WindowHeight = 480
GraphicBox #test.gb, 0, 0, WindowWidth, WindowHeight Open "test" For window As #test
'Base the graphicbox's size on the window's client area, instead of the full 'window size(including titlebar and borders) ' 'This is to match up with RESIZEHANDLER's values, which give 'clientarea sizes, not full window sizes. Struct rect, l As long, t As long, r As long, b As long
hwnd = hwnd(#test) CallDLL #user32, "GetClientRect", hwnd As ulong, _ rect As Struct, _ ret As long
ClientWidth = rect.r.struct ClientHeight = rect.b.struct
#test.gb, "locate 0 0 ";ClientWidth;" ";ClientHeight #test, "refresh"
#test, "resizehandler [resize]" #test, "trapclose [quit]"
#test.gb, "color black" #test.gb, "down"
'Enable this to test at different fonts/sizes. '#test.gb, "font courier_new 20"
'Enable this to allow editing of the entered character string. 'Also recommended to comment out dispString$, to start with a blank screen to "type" with.
'#test.gb, "when characterInput [doInput]" dispString$ = "Lorem ipsum dolor sit amet, consecteturium adipiscing elit. Aliquamazing at dui molestie, maximus felis et, iaculis justo. Fusce imperdiet vel lacus luctus fermentum. Nunc posuere velit nec tincidunt viverra. Nullam volutpat tristique mattis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque commodo lectus a fringilla bibendum. Mauris scelerisque turpis non posuere molestie. Donec vel tempus ligula. Vivamus convallis mollis condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum a nisl quis pellentesque. Nullam ut dolor efficitur odio gravida venenatis a a ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean gravida ipsum id magna mattis, et tempor leo pulvinar. Praesent odio nulla, aliquam efficitur nulla id, consectetur dictum metus." + chr$(13) + chr$(13) + _ "Proin finibus finibus neque, tincidunt tempus leo imperdiet nec. Vivamus dolor felis, mollis vitae leo vitae, luctus facilisis felis. Vestibulum sed velit eget lorem tincidunt feugiat. Ut justo urna, blandit eu orci vel, mollis rhoncus nunc. Curabitur bibendum cursus pellentesque. Cras sagittis libero in justo bibendum finibus. In quis faucibus ex, et finibus dui. Nam finibus tempus neque, ut tincidunt arcu eleifend vel. Aenean tortor lectus, pharetra quis ipsum nec, suscipit euismod nunc. In dignissim massa at elit luctus scelerisque. Sed magna sapien, pretium a ex eget, egestas auctor ligula." + chr$(13) + chr$(13) + _ "Fusce rhoncus ac urna vel iaculis. Sed eget leo ac turpis porta scelerisque vel eu ex. Nulla vehicula venenatis tortor, eget imperdiet sem tincidunt sed. Morbi ligula nisl, porttitor eget eleifend vel, gravida nec tortor. Suspendisse vel condimentum dolor. Suspendisse posuere, augue ac luctus facilisis, erat sapien hendrerit mi, id ullamcorper dui purus ut elit. Mauris lobortis fermentum mi, vitae fringilla arcu tristique porta. Vestibulum vehicula porta odio id pellentesque. Vivamus convallis, est ut cursus rhoncus, nunc velit aliquet diam, vestibulum lobortis leo nulla a massa. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ac ante mauris. Vivamus a convallis quam, et mollis arcu." + chr$(13) + chr$(13) + _ "Donec ac nisl accumsan, euismod enim sed, pharetra ipsum. Vestibulum euismod libero ac risus placerat, eget faucibus leo volutpat. Aliquam eget est elit. Donec sit amet consequat sem. Pellentesque leo nulla, consequat et tristique eget, convallis id urna. Nulla nec enim ante. Nulla nec placerat nisi. Nulla placerat cursus purus ac porta. Proin lectus nisi, semper non placerat ut, ullamcorper vel risus. Nulla bibendum scelerisque erat ut feugiat. Donec in orci sit amet nulla convallis faucibus. Nulla imperdiet nunc a massa feugiat dignissim. Integer tincidunt tellus porttitor dui elementum aliquam. Donec condimentum turpis vel lorem ullamcorper, congue blandit arcu eleifend. Sed auctor hendrerit rutrum." + chr$(13) + chr$(13) + _ "Duis rhoncus feugiat urna et auctor. Maecenas nec molestie ligula, et suscipit lacus. Vivamus molestie nunc urna, vel scelerisque arcu placerat eu. Nullam sed felis felis. In non lacus ex. Duis et justo erat. Praesent non magna ac libero ultrices pharetra. Praesent elit ipsum, lacinia eget sodales ut, efficitur facilisis erat."
#test.gb, "setfocus"
'Initial flush, even if blank, so the first "delsegment" when repainting the screen 'doesn't crash #test.gb, "flush"
'Box can be placed anywhere boxX = 10 boxY = 10
'This is to make the box margin equal on all sizes. You could also 'make the box a custom size here. boxW = ClientWidth - (boxX * 2) - 2 boxH = ClientHeight - (boxY * 2) - 2 'boxW = 200 'boxH = 200
'This is to get the height of a "line" in the current font, so we know '(roughly) how much space we have to work with. #test.gb, "place 0 0" #test.gb, "\ " #test.gb, "posxy x y"
lineHeight = y
'This sets the allowed side margins of text on the inside of the box. boxSideMargin = 10
'The "title" of the box is inset further than the side margins inside the box, 'to make it stand out further. titlePosX = boxX + (boxSideMargin * 2) titlePosY = boxY + (lineHeight / 3)
'Determining initital string position inside the box, as well as the string 'width cutoff necessary to keep the margin on both sides equal strPosX = boxX + boxSideMargin strPosY = boxY + lineHeight + (lineHeight / 3) cutoff = boxW - (boxSideMargin * 2)
GoSub [refreshScreen] Wait
[doInput] If len(Inkey$) > 1 Then modifier = asc(left$(Inkey$, 1)) End If
char$ = right$(Inkey$, 1) charVal = asc(char$)
If charVal = _VK_BACK Then If swallowFirstBackspace = 1 Then dispString$ = left$(dispString$, len(dispString$) - 1) GoSub [refreshScreen] swallowFirstBackspace = 0 Else swallowFirstBackspace = 1 End If End If
If charVal = _VK_RETURN Then If swallowFirstReturn = 1 Then dispString$ = dispString$ + char$ swallowFirstReturn = 0 Else swallowFirstReturn = 1 End If End If
If charVal > 31 And charVal < 127 Then dispString$ = dispString$ + char$ GoSub [refreshScreen] End If Wait
[refreshScreen] lines = 0 For x = 1 To Len(dispString$) lines = lines + 1 If ((lines * lineHeight) + ((lineHeight / 3) * 2)) >= boxH Then Exit For End If nextWord$ = "" : currentLine$ = "" : currentWidth = 0 : lineWord = 0 : returnFound = 0 While (Mid$(dispString$, x) <> "") And (currentWidth < cutoff) 'Scan nextWord$ = currentLine$;Word$(Mid$(dispString$, x), (lineWord + 1), " ");" " If Instr(nextWord$, chr$(13)) Then nextWord$ = UpTo$(nextWord$, chr$(13)) : returnFound = 1 lineWord = (lineWord + 1) #test.gb, "StringWidth? nextWord$ currentWidth" If (currentWidth < cutoff) Then currentLine$ = nextWord$ If (returnFound = 1) Then x = (Len(currentLine$) + x) currentLine$ = currentLine$ + chr$(13) Exit While End If Else 'Let's split the word that is too long to display If (lineWord = 1) And (currentWidth > cutoff) Then 'Test if we can even get a single letter in with a hyphen; if we can't we are doomed -> ERROR OUT splitSingleWord$ = "a-" #test.gb, "StringWidth? splitSingleWord$ currentWidth" If (currentWidth > cutoff) Then 'We can't even fit a single character in so let's bail out of everything Notice "Error - Window Size!";chr$(13);"Window size too small for minimum line length!" x = Len(dispString$) Exit While End If currentWidth = 0 : splitSingleWord$ = "" : i = 1 : nextWord$ = Trim$(nextWord$) While (currentWidth < cutoff) splitSingleWord$ = UpTo$(splitSingleWord$, "-");Mid$(nextWord$, i, 1) If (Len(splitSingleWord$) < Len(nextWord$)) Then splitSingleWord$ = splitSingleWord$;"-" #test.gb, "StringWidth? splitSingleWord$ currentWidth" currentLine$ = splitSingleWord$ i = (i + 1) Else currentLine$ = splitSingleWord$ Exit While End If Wend If (UpTo$(currentLine$, "-") = nextWord$) Then currentLine$ = nextWord$ x = (Len(UpTo$(currentLine$, "-")) + (x - 1)) currentLine$ = currentLine$ + chr$(13) Exit While End If x = (Len(currentLine$) + (x - 1)) currentLine$ = currentLine$ + chr$(13) Exit While End If Wend 'Remove any leading spaces from the line While (Mid$(currentLine$, 1, 1) = " ") currentLine$ = After$(currentLine$, " ") Wend outString$ = outString$ + currentLine$ Next x
#test.gb, "delsegment 1" #test.gb, "fill white"
#test.gb, "place ";boxX;" ";boxY #test.gb, "box ";boxW+boxX;" ";boxH+boxY
#test.gb, "place ";titlePosX;" ";titlePosY #test.gb, "\TITLE"
'#test, "place ";strPosX;" ";strPosY #test.gb, "place ";strPosX;" ";strPosY #test.gb, "\" + outString$ #test.gb, "flush"
outString$ = "" Return
[resize] #test, "resizehandler" boxW = WindowWidth - (boxX * 2) - 2 boxH = WindowHeight - (boxY * 2) - 2 cutoff = boxW - (boxSideMargin * 2) #test.gb, "locate 0 0 ";WindowWidth;" ";WindowHeight #test, "refresh" GoSub [refreshScreen] #test, "resizehandler [resize]" Wait
[quit] Close #test
{:0)
Brandon Parker
|
|
|
Post by Brandon Parker on May 11, 2020 13:43:27 GMT -5
Nope... it's still not 100%. I'll look into it.
{:0)
Brandon Parker
|
|
|
Post by Brandon Parker on May 11, 2020 17:35:46 GMT -5
Ok, here's another update. It at least fixes the issue I was seeing. I also named the drawing segment because I prefer names and not relying on ensuring that a segment is always a specific number; at least the name can never be changed. Oh, and I added a "CLS" after the "DelSegment" command; it just seems to draw better (looks less glitchy to me for some reason).
NoMainWin WindowWidth = 640 WindowHeight = 480
GraphicBox #test.gb, 0, 0, WindowWidth, WindowHeight Open "test" For window As #test
'Base the graphicbox's size on the window's client area, instead of the full 'window size(including titlebar and borders) ' 'This is to match up with RESIZEHANDLER's values, which give 'clientarea sizes, not full window sizes. Struct rect, l As long, t As long, r As long, b As long
hwnd = hwnd(#test) CallDLL #user32, "GetClientRect", hwnd As ulong, _ rect As Struct, _ ret As long
ClientWidth = rect.r.struct ClientHeight = rect.b.struct
#test.gb, "Locate 0 0 ";ClientWidth;" ";ClientHeight #test, "Refresh"
#test, "ResizeHandler [resize]" #test, "TrapClose [quit]"
#test.gb, "Color Black" #test.gb, "Down"
'Enable this to test at different fonts/sizes. '#test.gb, "font courier_new 20"
'Enable this to allow editing of the entered character string. 'Also recommended to comment out dispString$, to start with a blank screen to "type" with.
'#test.gb, "when characterInput [doInput]" dispString$ = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at dui molestie, maximus felis et, iaculis justo. Fusce imperdiet vel lacus luctus fermentum. Nunc posuere velit nec tincidunt viverra. Nullam volutpat tristique mattis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque commodo lectus a fringilla bibendum. Mauris scelerisque turpis non posuere molestie. Donec vel tempus ligula. Vivamus convallis mollis condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum a nisl quis pellentesque. Nullam ut dolor efficitur odio gravida venenatis a a ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean gravida ipsum id magna mattis, et tempor leo pulvinar. Praesent odio nulla, aliquam efficitur nulla id, consectetur dictum metus." + chr$(13) + chr$(13) + _ "Proin finibus finibus neque, tincidunt tempus leo imperdiet nec. Vivamus dolor felis, mollis vitae leo vitae, luctus facilisis felis. Vestibulum sed velit eget lorem tincidunt feugiat. Ut justo urna, blandit eu orci vel, mollis rhoncus nunc. Curabitur bibendum cursus pellentesque. Cras sagittis libero in justo bibendum finibus. In quis faucibus ex, et finibus dui. Nam finibus tempus neque, ut tincidunt arcu eleifend vel. Aenean tortor lectus, pharetra quis ipsum nec, suscipit euismod nunc. In dignissim massa at elit luctus scelerisque. Sed magna sapien, pretium a ex eget, egestas auctor ligula." + chr$(13) + chr$(13) + _ "Fusce rhoncus ac urna vel iaculis. Sed eget leo ac turpis porta scelerisque vel eu ex. Nulla vehicula venenatis tortor, eget imperdiet sem tincidunt sed. Morbi ligula nisl, porttitor eget eleifend vel, gravida nec tortor. Suspendisse vel condimentum dolor. Suspendisse posuere, augue ac luctus facilisis, erat sapien hendrerit mi, id ullamcorper dui purus ut elit. Mauris lobortis fermentum mi, vitae fringilla arcu tristique porta. Vestibulum vehicula porta odio id pellentesque. Vivamus convallis, est ut cursus rhoncus, nunc velit aliquet diam, vestibulum lobortis leo nulla a massa. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ac ante mauris. Vivamus a convallis quam, et mollis arcu." + chr$(13) + chr$(13) + _ "Donec ac nisl accumsan, euismod enim sed, pharetra ipsum. Vestibulum euismod libero ac risus placerat, eget faucibus leo volutpat. Aliquam eget est elit. Donec sit amet consequat sem. Pellentesque leo nulla, consequat et tristique eget, convallis id urna. Nulla nec enim ante. Nulla nec placerat nisi. Nulla placerat cursus purus ac porta. Proin lectus nisi, semper non placerat ut, ullamcorper vel risus. Nulla bibendum scelerisque erat ut feugiat. Donec in orci sit amet nulla convallis faucibus. Nulla imperdiet nunc a massa feugiat dignissim. Integer tincidunt tellus porttitor dui elementum aliquam. Donec condimentum turpis vel lorem ullamcorper, congue blandit arcu eleifend. Sed auctor hendrerit rutrum." + chr$(13) + chr$(13) + _ "Duis rhoncus feugiat urna et auctor. Maecenas nec molestie ligula, et suscipit lacus. Vivamus molestie nunc urna, vel scelerisque arcu placerat eu. Nullam sed felis felis. In non lacus ex. Duis et justo erat. Praesent non magna ac libero ultrices pharetra. Praesent elit ipsum, lacinia eget sodales ut, efficitur facilisis erat."
#test.gb, "SetFocus"
'Initial flush, even if blank, so the first "delsegment" when repainting the screen 'doesn't crash #test.gb, "Flush dataScreen"
'Box can be placed anywhere boxX = 10 boxY = 10
'This is to make the box margin equal on all sizes. You could also 'make the box a custom size here. boxW = ClientWidth - (boxX * 2) - 2 boxH = ClientHeight - (boxY * 2) - 2 'boxW = 200 'boxH = 200
'This is to get the height of a "line" in the current font, so we know '(roughly) how much space we have to work with. #test.gb, "Place 0 0" #test.gb, "\ " #test.gb, "POSXY x y"
lineHeight = y
'This sets the allowed side margins of text on the inside of the box. boxSideMargin = 10
'The "title" of the box is inset further than the side margins inside the box, 'to make it stand out further. titlePosX = boxX + (boxSideMargin * 2) titlePosY = boxY + (lineHeight / 3)
'Determining initital string position inside the box, as well as the string 'width cutoff necessary to keep the margin on both sides equal strPosX = boxX + boxSideMargin strPosY = boxY + lineHeight + (lineHeight / 3) cutoff = boxW - (boxSideMargin * 2)
GoSub [refreshScreen] Wait
[doInput] If len(Inkey$) > 1 Then modifier = asc(left$(Inkey$, 1)) End If
char$ = right$(Inkey$, 1) charVal = asc(char$)
If charVal = _VK_BACK Then If swallowFirstBackspace = 1 Then dispString$ = left$(dispString$, len(dispString$) - 1) GoSub [refreshScreen] swallowFirstBackspace = 0 Else swallowFirstBackspace = 1 End If End If
If charVal = _VK_RETURN Then If swallowFirstReturn = 1 Then dispString$ = dispString$ + char$ swallowFirstReturn = 0 Else swallowFirstReturn = 1 End If End If
If charVal > 31 And charVal < 127 Then dispString$ = dispString$ + char$ GoSub [refreshScreen] End If Wait
[refreshScreen] lines = 0 For x = 1 To Len(dispString$) Scan lines = lines + 1 If ((lines * lineHeight) + ((lineHeight / 3) * 2)) >= boxH Then Exit For End If nextWord$ = "" : currentLine$ = "" : currentWidth = 0 : lineWord = 0 : returnFound = 0 While (Mid$(dispString$, x) <> "") And (currentWidth < cutoff) 'Scan nextWord$ = currentLine$;Word$(Mid$(dispString$, x), (lineWord + 1), " ") 'Remove any leading spaces from the line to be tested While (Mid$(nextWord$, 1, 1) = " ") nextWord$ = After$(nextWord$, " ") Wend If (nextWord$ <> "") Then nextWord$ = nextWord$;" " If Instr(nextWord$, chr$(13)) Then nextWord$ = UpTo$(nextWord$, chr$(13)) : returnFound = 1 lineWord = (lineWord + 1) #test.gb, "StringWidth? nextWord$ currentWidth" If (currentWidth < cutoff) Then currentLine$ = nextWord$ If (returnFound = 1) Then x = (Len(currentLine$) + x) currentLine$ = currentLine$ + chr$(13) Exit While End If Else 'Let's split the word that is too long to display If (lineWord = 1) And (currentWidth > cutoff) Then 'Test if we can even get a single letter in with a hyphen; if we can't we are doomed -> ERROR OUT splitSingleWord$ = "W-" #test.gb, "StringWidth? splitSingleWord$ currentWidth" If (currentWidth > cutoff) Then 'We can't even fit a single character in so let's bail out of everything Notice "Error - Window Size!";chr$(13);"Window size too small for minimum line length!" x = Len(dispString$) Exit While End If currentWidth = 0 : splitSingleWord$ = "" : i = 1 : nextWord$ = Trim$(nextWord$) While (currentWidth < cutoff) splitSingleWord$ = UpTo$(splitSingleWord$, "-");Mid$(nextWord$, i, 1) If (Len(splitSingleWord$) < Len(nextWord$)) Then splitSingleWord$ = splitSingleWord$;"-" #test.gb, "StringWidth? splitSingleWord$ currentWidth" currentLine$ = splitSingleWord$ i = (i + 1) Else currentLine$ = splitSingleWord$ Exit While End If Wend If (UpTo$(currentLine$, "-") = nextWord$) Then currentLine$ = nextWord$ x = (Len(UpTo$(currentLine$, "-")) + (x - 1)) currentLine$ = currentLine$ + chr$(13) Exit While End If x = (Len(currentLine$) + (x - 1)) currentLine$ = currentLine$ + chr$(13) Exit While End If Else Exit While End If Wend 'Remove any leading spaces from the line While (Mid$(currentLine$, 1, 1) = " ") currentLine$ = After$(currentLine$, " ") Wend outString$ = outString$ + currentLine$ Next x
#test.gb, "DelSegment dataScreen;CLS" #test.gb, "Fill White"
#test.gb, "Place ";boxX;" ";boxY #test.gb, "Box ";boxW+boxX;" ";boxH+boxY
#test.gb, "Place ";titlePosX;" ";titlePosY #test.gb, "\TITLE"
'#test, "place ";strPosX;" ";strPosY #test.gb, "Place ";strPosX;" ";strPosY #test.gb, "\" + outString$ #test.gb, "Flush dataScreen"
outString$ = "" Return
[resize] #test, "ResizeHandler" If ((oldWidth = WindowWidth) And (oldHeight = WindowHeight)) Then #test, "ResizeHandler [resize]" Wait End If boxW = WindowWidth - (boxX * 2) - 2 boxH = WindowHeight - (boxY * 2) - 2 cutoff = boxW - (boxSideMargin * 2) #test.gb, "Locate 0 0 ";WindowWidth;" ";WindowHeight #test, "Refresh" GoSub [refreshScreen] #test, "ResizeHandler [resize]" Wait
[quit] Close #test End
Updated again
{:0)
Brandon Parker
|
|
timur77
Junior Member
Someday I will tell my grandsons that I am older than the Internet. And it will blow their brain.
Posts: 79
|
Post by timur77 on May 21, 2020 10:09:25 GMT -5
I did not understand why to use the API function and why the interception of the keys "_VK_BACK" and "_VK_RETURN".
I wrote my version of the program. Added a "popup" menu where you can choose a font. With one click on the text, a label appears and the editing mode is activated, with a second click exit the editing mode. You can write, the "BACKSPACE", "DELETE" and "ENTER" buttons work. Sometimes dry out some roughness.
nomainwin
UpperLeftX = int(DisplayWidth/2)-400 UpperLeftY = int(DisplayHeight/2)-300 WindowWidth = 800 WindowHeight = 600
Global PKor, ykor xmax=WindowWidth-50 ymax=WindowHeight-24 sh=16 dp1=10 dp2=33 chosenFont$="Old_English_Text_MT "+str$(sh) mwr=0 korrw=0 bufbackspace=0 Dim stroka$(1000)
text$="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at dui molestie, maximus felis et, iaculis justo. Fusce imperdiet vel lacus luctus fermentum. Nunc posuere velit nec tincidunt viverra. Nullam volutpat tristique mattis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque commodo lectus a fringilla bibendum. Mauris scelerisque turpis non posuere molestie. Donec vel tempus ligula. Vivamus convallis mollis condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum a nisl quis pellentesque. Nullam ut dolor efficitur odio gravida venenatis a a ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean gravida ipsum id magna mattis, et tempor leo pulvinar. Praesent odio nulla, aliquam efficitur nulla id, consectetur dictum metus." + chr$(13) + chr$(13) + _ "Proin finibus finibus neque, tincidunt tempus leo imperdiet nec. Vivamus dolor felis, mollis vitae leo vitae, luctus facilisis felis. Vestibulum sed velit eget lorem tincidunt feugiat. Ut justo urna, blandit eu orci vel, mollis rhoncus nunc. Curabitur bibendum cursus pellentesque. Cras sagittis libero in justo bibendum finibus. In quis faucibus ex, et finibus dui. Nam finibus tempus neque, ut tincidunt arcu eleifend vel. Aenean tortor lectus, pharetra quis ipsum nec, suscipit euismod nunc. In dignissim massa at elit luctus scelerisque. Sed magna sapien, pretium a ex eget, egestas auctor ligula." + chr$(13) + chr$(13) + _ "Fusce rhoncus ac urna vel iaculis. Sed eget leo ac turpis porta scelerisque vel eu ex. Nulla vehicula venenatis tortor, eget imperdiet sem tincidunt sed. Morbi ligula nisl, porttitor eget eleifend vel, gravida nec tortor. Suspendisse vel condimentum dolor. Suspendisse posuere, augue ac luctus facilisis, erat sapien hendrerit mi, id ullamcorper dui purus ut elit. Mauris lobortis fermentum mi, vitae fringilla arcu tristique porta. Vestibulum vehicula porta odio id pellentesque. Vivamus convallis, est ut cursus rhoncus, nunc velit aliquet diam, vestibulum lobortis leo nulla a massa. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ac ante mauris. Vivamus a convallis quam, et mollis arcu." + chr$(13) + chr$(13) + _ "Donec ac nisl accumsan, euismod enim sed, pharetra ipsum. Vestibulum euismod libero ac risus placerat, eget faucibus leo volutpat. Aliquam eget est elit. Donec sit amet consequat sem. Pellentesque leo nulla, consequat et tristique eget, convallis id urna. Nulla nec enim ante. Nulla nec placerat nisi. Nulla placerat cursus purus ac porta. Proin lectus nisi, semper non placerat ut, ullamcorper vel risus. Nulla bibendum scelerisque erat ut feugiat. Donec in orci sit amet nulla convallis faucibus. Nulla imperdiet nunc a massa feugiat dignissim. Integer tincidunt tellus porttitor dui elementum aliquam. Donec condimentum turpis vel lorem ullamcorper, congue blandit arcu eleifend. Sed auctor hendrerit rutrum." + chr$(13) + chr$(13) + _ "Duis rhoncus feugiat urna et auctor. Maecenas nec molestie ligula, et suscipit lacus. Vivamus molestie nunc urna, vel scelerisque arcu placerat eu. Nullam sed felis felis. In non lacus ex. Duis et justo erat. Praesent non magna ac libero ultrices pharetra. Praesent elit ipsum, lacinia eget sodales ut, efficitur facilisis erat."
graphicbox #win.gb, 5, 5, WindowWidth-20, WindowHeight-43 open "Text in window" for window as #win #win, "trapclose [close]" #win.gb, "when rightButtonUp [PMenu]" #win.gb, "when leftButtonUp [Write]" #win.gb, "when characterInput [Change]" #win.gb "font "+chosenFont$ #win, "resizehandler [resized]"
mwr=0 if (xmax=WindowWidth-60 and ymax=WindowHeight-25) or xmax<1 then if xmax<1 then xmax=1:wait gosub [resized2] wait [resized] mwr=0 if (xmax=WindowWidth-60 and ymax=WindowHeight-25) or xmax<1 then if xmax<1 then xmax=1:wait dp1=0 dp2=0 gosub [resized2] wait '============================================= [resized2] #win.gb, "locate 5, 5, ";WindowWidth-10-dp1;", ";WindowHeight-10-dp2 xmax=WindowWidth-60 ymax=WindowHeight-25 korr=0 nrstr=1
[L2] stroka$(nrstr)="" korr=korr+1
[L1] if korr>len(text$) then goto [L3]
slovo$="" ksl=0 [L5] if korr+ksl>len(text$) then goto [L6] if mid$(text$,korr+ksl,1)=chr$(13) then goto [L6] if mid$(text$,korr+ksl,1)=chr$(32) and ksl>0 and mid$(text$,korr+ksl-1,1)<>chr$(32) Then goto [L6] slovo$=slovo$+mid$(text$,korr+ksl,1) ksl=ksl+1 goto [L5]
[L6] if mid$(text$,korr+ksl,1)=chr$(13) and slovo$="" then slovo$=chr$(13)
strokaP$="" strokaP$=stroka$(nrstr)+slovo$ #win.gb, "stringwidth? strokaP$ dlinna" if slovo$=chr$(13) then stroka$(nrstr)=stroka$(nrstr)+slovo$:goto [L4] if dlinna<xmax then stroka$(nrstr)=stroka$(nrstr)+slovo$ korr=korr+ksl goto [L1] else korr=korr-1 end if
[L4] if nrstr+30+dp2>ymax then goto [L3] nrstr=nrstr+1 goto [L2]
[L3] #win.gb, "cls; color black" ii=0 for i=1 to nrstr ii=ii+sh*1.7 if ii+30+dp2>ymax then EXIT FOR #win.gb "place 20 ";ii+25 #win.gb, "\";stroka$(i) next i
#win.gb, "down; size 3" #win.gb, "place 10 10" #win.gb, "box ";xmax+40-dp1;" ";ymax+5-dp2 #win.gb, "place 30 ";sh+5 #win.gb, "\TITLE" #win.gb, "flush" #win, "refresh" return '============================================= [PMenu] popupmenu "&Font", [Font] wait
[Font] fontdialog chosenFont$, chosenFont$ shbuf=0 ind=1 for i=len(chosenFont$) to 1 step (-1) if mid$(chosenFont$,1,1)=" " and asc(mid$(chosenFont$,i+1,1))>47 and asc(mid$(chosenFont$,i+1,1))<58 then exit for if asc(mid$(chosenFont$,i,1))>47 and asc(mid$(chosenFont$,i,1))<58 then shbuf=shbuf+val(mid$(chosenFont$,i,1))*10^(ind-1):ind=ind+1 next i sh=shbuf #win.gb "font "+chosenFont$ gosub [resized2] wait '============================================= [Write] mwr=(mwr=0) if mwr=0 then gosub [resized2]:wait [Write2] xmm=MouseX ymm=MouseY yKor=int((ymm-25) / (sh*1.7))+1 xKor=len(stroka$(yKor)) for i=1 to len(stroka$(yKor)) buf$=mid$(stroka$(yKor),1,i) #win.gb, "stringwidth? buf$ dlinna" if dlinna>(xmm-20) then xKor=i:exit for next i
buf$=mid$(stroka$(yKor),1,xKor-1) #win.gb, "stringwidth? buf$ dlinna" #win.gb, "place ";20+dlinna;" ";yKor*sh*1.7+25 #win.gb, "size 2;color red" #win.gb, "goto ";20+dlinna;" ";yKor*sh*1.7+25-sh
wait '============================================= [Change] if mwr=0 then wait key$ = Inkey$ if asc(right$(key$, 1))=16 or asc(right$(key$, 1))=20 then wait '- - - - - - - - - - - - - - - - - - - - - - - if asc(left$(key$, 1))=13 and asc(right$(key$, 1))=10 then key$=chr$(13) if len(key$)=1 and (asc(key$)>31 and asc(key$)<127) or (asc(key$)>191 and asc(key$)<256) or key$=chr$(13) then PKor=0 for i=1 to yKor-1 PKor=PKor+len(stroka$(i)) next i PKor=PKor+len(mid$(stroka$(yKor),1,xKor)) STbefor$=mid$(text$,1,PKor-1) STafter$=mid$(text$,PKor,len(text$)-len(mid$(text$,1,PKor-1))) text$=STbefor$+key$+STafter$ gosub [resized2] end if '- - - - - - - - - - - - - - - - - - - - - - - if asc(left$(key$,1))=0 and bufbackspace=0 then bufbackspace=8:wait if asc(left$(key$,1))=8 and bufbackspace=8 then bufbackspace=0 PKor=0 for i=1 to yKor-1 PKor=PKor+len(stroka$(i)) next i PKor=PKor+len(mid$(stroka$(yKor),1,xKor)) if PKor<1 then wait STbefor$=mid$(text$,1,PKor-2) STafter$=mid$(text$,PKor,len(text$)-len(mid$(text$,1,PKor-1))) text$=STbefor$+STafter$ PKor=PKor-2 gosub [resized2] end if '- - - - - - - - - - - - - - - - - - - - - - - if asc(right$(key$,1))=46 then PKor=0 for i=1 to yKor-1 PKor=PKor+len(stroka$(i)) next i PKor=PKor+len(mid$(stroka$(yKor),1,xKor)) STbefor$=mid$(text$,1,PKor-1) STafter$=mid$(text$,PKor+1,len(text$)-len(mid$(text$,1,PKor))) text$=STbefor$+STafter$ PKor=PKor-1 gosub [resized2] end if '- - - - - - - - - - - - - - - - - - - - - - - bufst=0 for i=1 to yKor+1 sKor=i if bufst+len(stroka$(i))<=PKor then bufst=bufst+len(stroka$(i)) else exit for next i MouseY=int(sh*1.7*(sKor)+25-2) buf$=mid$(stroka$(sKor),1,PKor-bufst) #win.gb, "stringwidth? buf$ dlinna" MouseX=20+dlinna+2 goto [Write2] wait '============================================= [close] close #win end
|
|