Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Finding Words in a Paragraph

Author
Message
Rknight
13
Years of Service
User Offline
Joined: 25th Sep 2003
Location: NJ
Posted: 19th Mar 2017 20:03
Is there a simple way to display a large amount of text in a text box and be able to know what word the mouse is hovering over in it?
Phaelax
DBPro Master
14
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Agartha
Posted: 19th Mar 2017 22:22
I think you'd have to write your own kind of text box and possibly pre-calculate the start of each word if the font isn't a fixed width.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
easter bunny
4
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 20th Mar 2017 21:11
Quote: "Is there a simple way to display a large amount of text in a text box "

Nope. Doing so would be possible, but somewhat complicated. My recommendation would be to use an array of text objects, one for each word, and position them manually using the size of the previous text object. This would be simple enough, but I imagine things would get complicated if you have multiline text and you want to centre-align it

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
Rknight
13
Years of Service
User Offline
Joined: 25th Sep 2003
Location: NJ
Posted: 20th Mar 2017 23:33
This. . . is really really clumsy.
Rknight
13
Years of Service
User Offline
Joined: 25th Sep 2003
Location: NJ
Posted: 20th Mar 2017 23:35
Right. Is there an alternative to AppGameKit that anyone can recommend that can do this simply? Handle text?
easter bunny
4
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 21st Mar 2017 04:35 Edited at: 21st Mar 2017 04:37
Well if you want things to be simpler, moving to a different development option probably wont help much lol. Most are a fair bit more complicated in terms of deployment, as well as just coding the game in general. Sure moving to a different option might give you the ability to detect if a particular word is under the cursor, but at what cost?

If you want a really simple game dev option [RAD], try GM:Studio, Construct 2, Stency, GameSalad etc. If you're looking to actually code your game, some decent options (off the top of my head) are Cocos2d, Monogame, MonkeyX, and Corona SDK. However I've tried literally all of those, and I keep coming back to AGK. Sure some things in AppGameKit suck, but you can work around most of them. On the other hand, AppGameKit has so many amazing benefits that these other options don't. Like instantly deploying your game to test on your mobile, fast compile times, one click deploy to any platform.

Alternative IDEs aside, if the idea of using an array of text objects doesn't appeal to you (so much that you would leave our beloved AGK), there are other ways of doing it.
So one way would be to create your own custom text type and use the same technique I outlined above. So it would work the way I said, but you would write a custom type that includes an array containing each text object.
You can then create your own function to create text which automatically splits it into separate text objects and places them. Whenever you move or modify the text, you would use your own custom functions that do it for all text objects. It would take a bit of effort to set up the system, but once you've made it, it would be as simple to use as the inbuilt text commands.

Another option (which might appeal to you more) is to simply calculate the hit box for a particular word using GetTextCharX to find the offset.
Literally you could probably use this function (untested)

+ Code Snippet

This will let you know if a certain substring is being pressed. It would be easy to modify it so it automatically parses the string for spaces and iterates through each substring.

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
BatVink
Moderator
14
Years of Service
Recently Online
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Mar 2017 07:07 Edited at: 21st Mar 2017 07:09
My first comment would be that what you are trying to do is outside the realms of a game language to manage. I can understand why you might want to do it, but AppGameKit is a game engine, not a word processor. Therefore you need to code the solution yourself.

Easter Bunny's approach is one I agree with. You have the ability to get the position of every character in the text.
For speed, you can store the positions of each word in an array. The primary key is the position, not the word. I would consider a structure like this:


+ Code Snippet

From this you can efficiently save and retrieve the Y positions of many words sharing the same line
You can then find the word within the relevant X position
Words are saved using Easter Bunny's technique on getting the character positions. Use the text box position and dimensions to add the offsets.
Wrap it all up in a function and you can simply pass a text object to the function and return your typed array of data needed to track the word under the mouse.

In summary when getting the word, you find the line that covers the Y position of the mouse. Then you find the word in the sub-array that covers the X position for the mouse. For a paragraph of 10 lines with an average of 10 words per line, you can do this with 8 tests or less. Once you have found a word, you don't check again until you have left the bounds of the current word.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Login to post a reply

Server time is: 2017-06-28 21:46:07
Your offset time is: 2017-06-28 21:46:07