Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  RPG Dialogue Boxes  (Read 5289 times)
0 Members and 1 Guest are viewing this topic.
Offline ratchet12340

Senior Newbie


Exp: 2 years



« Posted 2014-06-10 23:27:41 »

Hi friends,

I've been using the Slick2D engine to get into java game development, and I have been trying to develop an RPG. I've encountered an issue with trying to properly handle text/dialogue boxes. An example of what I'm essentially trying to do (click on the little application at the top of the article):

http://code.tutsplus.com/tutorials/make-an-rpg-style-text-system-for-your-next-game--active-3678

I'd follow this tutorial, but it's in Flash and I'm not quite sure how to pull whatever algorithm that's being used to my situation. So, any suggestions as to how I could approach this? Any help would be fantastic  Smiley

EDIT: I believe the term here I'm looking for is "text wrapping," and essentially I just want to wrap the text to the text line when it no longer fits the box.
Offline cubemaster21
« Reply #1 - Posted 2014-06-13 15:53:20 »

This is how I handle text wrapping in my Font.java class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public String[] split(String fLine, float maxWidthInPixels, float scale){
      String[] lines = new String[10];
      int lineArrayIndex = 0;
      float curWidth = 0;
      int lastSpaceIndex = 0;
      int startIndex = 0;
      for(int i = 0;i < fLine.length();i++){
         String temp = fLine.substring(i, i + 1);// letter
         if(temp.equals(" ")) lastSpaceIndex = i + 1; // the + 1 accounts for the space, so that the space isn't place at the beginning of the next line.
         curWidth += getLength(temp) * scale;
         
         if(curWidth >= maxWidthInPixels){
            i = lastSpaceIndex;
            lines[lineArrayIndex] = fLine.substring(startIndex, lastSpaceIndex);
            startIndex = lastSpaceIndex;
            curWidth = 0;
            lineArrayIndex ++;
           
         }
      }
      lines[lineArrayIndex] = fLine.substring(startIndex, fLine.length());
      return lines;
   }

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline tom_mai78101
« Reply #2 - Posted 2014-06-13 16:13:42 »

Another variant that I used from many helpers here at JGO.

Click to Play


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
   public static ArrayList<Map.Entry<String, Boolean>> toLines(String all, final int regex) {
      ArrayList<Map.Entry<String, Boolean>> lines = new ArrayList<>();
      String[] words = all.split("\\s");
      String line = "";
      int length = 0;
      for (String w : words) {
         if (length + w.length() + 1 > regex) {
            if (w.length() >= regex) {
               line += w;
               lines.add(new AbstractMap.SimpleEntry<String, Boolean>(line, false));
               line = "";
               continue;
            }
            lines.add(new AbstractMap.SimpleEntry<String, Boolean>(line, false));
            line = "";
            length = 0;
         }
         if (length > 0) {
            line += " ";
            length += 1;
         }
         line += w;
         length += w.length();
      }
      if (line.length() > 0)
         lines.add(new AbstractMap.SimpleEntry<String, Boolean>(line, false));
      return lines;
   }


Full source code.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ratchet12340

Senior Newbie


Exp: 2 years



« Reply #3 - Posted 2014-06-18 01:50:56 »

@cubemaster21: I like your implementation, I'll take a look and see if I can do my own like yours.

@tom_mai78101: Your implementation looks pretty neat, but would you mind explaining it a little? with the abstract maps and what not?

thanks guys  Smiley
Offline tom_mai78101
« Reply #4 - Posted 2014-06-19 03:03:24 »

@tom_mai78101: Your implementation looks pretty neat, but would you mind explaining it a little? with the abstract maps and what not?

The AbstractMap is a class object that isn't used most of the time. I used the SimpleEntry class object that is inside AbstractMap to create a Map.Entry<K, V> object to pass it to my array. This is usually used for holding double data together.

See here for AbstractMap.SimpleEntry usages:
http://stackoverflow.com/questions/8689725/map-entry-how-to-use-it
http://stackoverflow.com/questions/18863910/what-is-map-entryk-v-interface
http://stackoverflow.com/questions/3110547/java-how-to-create-new-entry-key-value

And more on why to use AbstractMap.SimpleEntry:
http://stackoverflow.com/questions/6044923/generic-pair-class
http://stackoverflow.com/questions/16897486/what-is-the-use-of-pair-class-and-why-have-the-implemented-here



The code above takes two parameters, one full String sentence, and the regex value. The full String sentence may be something like:

1  
String str = "Can I have a piece of pie for my little sister? She is very hungry.";


It must not be something like this:

1  
2  
String str1 = "Cam I have a piece of pie for my little sister?";
String str2 = "She is very hungry";


The regex value determines how many letters it can fit in one dialogue line before it wraps the sentence to the next line. In my case, I have the max length set to 18, which means in one dialogue line, the most a sentence can be placed in there must be 18 letters or less. Anything more than 18 will have the word that exceeds the limit be wrapped to the next line.

For example,

1  
String str = "ABCDE FGHIJ KLMNO PQRST";  //20 letters + 3 whitespaces = 23 letters > 18-letters limit.


Once this sentence is passed into the method, it will result in something like this:

1  
2  
3  
//Note the white space at the end of this sentence.
String str1 = "ABCDE FGHIJ KLMNO ";  //15 letters + 3 whitespaces = 18 letters.
String str2 = "PQRST"; //5 letters + 0 whitespaces.


So in the dialogue box, it will show up something like this:

1  
2  
3  
4  
//  '_' means whitespace. It is explicitly shown for clarity.

ABCDE_FGHIJ_KLMNO_
PQRST


If you need more explanations, please leave a reply.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (143 views)
2017-09-30 02:57:34

theagentd (206 views)
2017-09-26 18:23:31

cybrmynd (290 views)
2017-08-02 12:28:51

cybrmynd (282 views)
2017-08-02 12:19:43

cybrmynd (292 views)
2017-08-02 12:18:09

Sralse (282 views)
2017-07-25 17:13:48

Archive (950 views)
2017-04-27 17:45:51

buddyBro (1088 views)
2017-04-05 03:38:00

CopyableCougar4 (1653 views)
2017-03-24 15:39:42

theagentd (1413 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!