Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (605)
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  
  Simple Slick Textbox engine just incase..  (Read 2660 times)
0 Members and 1 Guest are viewing this topic.
Offline SkyAphid
« Posted 2012-01-11 01:19:55 »

I had some problems with Unicode where the text wouldn't draw, even after all the necessary setup protocols. AngelCodeFont was a bit of overkill, and the indexes in TrueTypeFont weren't working for whatever reason. So I wrote this, I felt it'd be right to share it, in case someone else needed it too.

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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
package edu.bgp.global.utilities;

import java.awt.Font;
import java.util.List;
import java.util.ArrayList;

import org.newdawn.slick.Image;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Input;
import org.newdawn.slick.GameContainer;

/**
 *
 * @author Brayden McKinney
 * Written on 1/10/12
 *
 *If you use this, you don't have to give credit. Modify as you please, but if you do, share it.
 *I made this, because Slicks normal font rendering code wasn't working properly. This tries to simplify
 *text rendering in RPG games and the like.
 *
 */


public class textBoxBase{
   TrueTypeFont trueTypeFont;
   Image textBox;
   Image textDown;
   
   int line = 0; //Line being rendered. Draws 3 of theses when rendered. So increment accordingly.
   int size = 0; //The amount of lines to draw.
   int cooldown = 0; //Cooldown of the clicker, so text doesn't scroll too fast.
   static final int COOLDOWNSET = 20; //Amount of time I wait for my cooldown.
   
   float boxFader = 0;
   int boxSlider = 160;
   int textFader = 0;
   
   public boolean rendering = false;
   
   String textToRender[]; //Text spliced and ready.
   String personTalking = ""; //Person talking.
   List <String> textQueue = new ArrayList<String>(); //Text waiting for render.
   List <String> nameQueue = new ArrayList<String>(); //Name queue.
   
   public textBoxBase(){
      try{
         trueTypeFont = new TrueTypeFont(new Font("Arial", Font.ITALIC, 16), true);
         textBox = new Image("res/gui/textBox.png");
         textDown = new Image("res/gui/textDown.png");
      }catch(SlickException e){}
   }
   
   //Render code, place in the loop of the game if you want the textbox in that part of the engine.
   public void render(Graphics g, GameContainer gc){
      Input input = gc.getInput();
     
      //Draw boxes and it's parts.
      g.drawImage(textBox, 90, 360+boxSlider, new Color(1f,1f,1f,boxFader));
     
      if (cooldown == 0){
         g.drawImage(textDown, 650, 500+boxSlider,new Color(1f,1f,1f,boxFader));
      }
     
      if (rendering){
         //Faders to make it more smexy.
         if (boxFader < 1){
            boxFader += 0.10;
            boxSlider -= 15;
         }
         
         if (textFader != 255){
            textFader += 15;
         }
         
         try{
            trueTypeFont.drawString(170, 410, personTalking, Color.black);
            trueTypeFont.drawString(200, 430, textToRender[line] , new Color(0, 0, 0, textFader));
            trueTypeFont.drawString(200, 450, textToRender[line+1] , new Color(0, 0, 0, textFader));
            trueTypeFont.drawString(200, 470, textToRender[line+2] , new Color(0, 0, 0, textFader));
         }catch(Exception e){}
     
         if (input.isMouseButtonDown(input.MOUSE_LEFT_BUTTON)){
            //Scroll text.
            if (line <= size && cooldown == 0){line+=3; cooldown = COOLDOWNSET; textFader = 0;}
         }
         
         //If lines are at the length, shut down text rendering.
         if (line > size){
            rendering = false;
         }
         
         //Cooldown.
         if (cooldown != 0){cooldown--;}
      }
     
      if (!rendering){
         line = 0;
         
         //Check text queue. Move on to next text if so, if not, it shuts the text box down.
         if (!textQueue.isEmpty()){
            spliceText(textQueue.get(0), nameQueue.get(0));
            textQueue.remove(0);
            nameQueue.remove(0);
         }else{
            if (boxFader != 0){
               boxFader -= 0.30;
               boxSlider+=40;
            }
         }
         
         textFader = 0;
      }
   }
   
   //Read and cut text where neccesary, then place it in the array; then tell the render to
   //render the text. It also assigns the name of the person talking.
   public void spliceText(String text, String name){
      int mark = 0;
      personTalking = name;
      textToRender = new String[text.length()/50+4];
      size = text.length()/50;
        for (int x = 0; x < text.length(); x++){
           if (x > mark+50){
              textToRender[mark/50] = text.substring(mark, x);
              textToRender[(mark/50)+1] = "";
              textToRender[(mark/50)+2] = "";
              mark = x;
           }else{
              if (mark+50 > text.length()){
                 textToRender[mark/50] = text.substring(mark, text.length());
              }
           }
        }
        rendering = true;
   }
   
   //Send messages you want rendered, if one is already being rendered, text is added to a queue.
   //It also adds names of the person talking the same way.
   public void newMessage(String text, String name){
      if (rendering == false){
         spliceText(text, name);
      }else if (rendering){
         textQueue.add(text);
         nameQueue.add(name);
      }
   }
}


I uploaded the older version so people could optimize the code in their own preferred ways (Yes, I know it's not fully optimized. lol).

Also, if you do modify it, please place it in this thread. It needs an auto spacer (So words are full of each line), which I haven't even implemented into my own yet, so if you make one please do share.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline SkyAphid
« Reply #1 - Posted 2012-01-13 20:33:15 »

This is a more optimized version, it also can space the text right so you don't have to manually.

Here's the code, it's not commented, but if you read the comments in the older version, you'll be able to understand it and the changes just fine. Honestly, this is mainly for the newcomers to java. I'm not saying I'm awesome or anything, but I know when I was new at programming, code like this made me jump for joy.

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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
package edu.bgp.global.utilities;

import java.awt.Font;
import java.util.List;
import java.util.ArrayList;

import org.newdawn.slick.Image;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Input;
import org.newdawn.slick.GameContainer;

/**
 *
 * @author Brayden McKinney
 * Written on 1/10/12
 *
 *If you use this, you don't have to give credit. Modify as you please, but if you do, share it.
 *I made this, because Slicks normal font rendering code wasn't working properly. This tries to simplify
 *text rendering in RPG games and the like.
 *
 */


public class textBoxBase{
   TrueTypeFont trueTypeFont;
   Image textBox;
   Image textDown;
   
   int line = 0;
   int size = 0;
   int cooldown = 0;
   static final int COOLDOWNSET = 20;
   static final int TEXTBOXLENGTH = 40;
   
   float boxFader = 0;
   int boxSlider = 160;
   int textFader = 0;
   
   public boolean rendering = false;
   
   String textToRender[];
   String personTalking = "";
   List <String> textQueue = new ArrayList<String>();
   List <String> nameQueue = new ArrayList<String>();
   
   public textBoxBase(){
      try{
         trueTypeFont = new TrueTypeFont(new Font("Arial", Font.ITALIC, 16), true);
         textBox = new Image("res/gui/textBox.png");
         textDown = new Image("res/gui/textDown.png");
      }catch(SlickException e){}
   }
   
   public void render(Graphics g, GameContainer gc){
      Input input = gc.getInput();
     
      g.drawImage(textBox, 90, 360+boxSlider, new Color(1f,1f,1f,boxFader));
     
      if (cooldown == 0){
         g.drawImage(textDown, 650, 500+boxSlider,new Color(1f,1f,1f,boxFader));
      }
     
      if (rendering){
         if (boxFader < 1){
            boxFader += 0.10;
            boxSlider -= 15;
         }
         
         if (textFader != 255){
            textFader += 15;
         }
         
         try{
            trueTypeFont.drawString(170, 410, personTalking, Color.black);
            trueTypeFont.drawString(200, 430, textToRender[line] , new Color(0, 0, 0, textFader));
            trueTypeFont.drawString(200, 450, textToRender[line+1] , new Color(0, 0, 0, textFader));
            trueTypeFont.drawString(200, 470, textToRender[line+2] , new Color(0, 0, 0, textFader));
         }catch(Exception e){}
     
         if (input.isMouseButtonDown(input.MOUSE_LEFT_BUTTON)){
            if (line <= size && cooldown == 0){line+=3; cooldown = COOLDOWNSET; textFader = 0;}
         }
         
         if (line > size){
            rendering = false;
         }
         
         if (cooldown != 0){cooldown--;}
      }
     
      if (!rendering){
         line = 0;
         
         if (!textQueue.isEmpty()){
            spliceText(textQueue.get(0), nameQueue.get(0));
            textQueue.remove(0);
            nameQueue.remove(0);
         }else{
            if (boxFader != 0){
               boxFader -= 0.30;
               boxSlider+=40;
            }
         }
         
         textFader = 0;
      }
   }

   public void spliceText(String text, String name){
      int mark = 0;
      personTalking = name;
     
      size = text.length()/TEXTBOXLENGTH;
      textToRender = new String[size+10];
     
        for (int x = 0; x < text.length(); x++){
           if (x > mark+TEXTBOXLENGTH){
              int newMark = x;
             
              if (text.charAt(x) != ' ' && newMark == x){
                 for (int a = x; a < text.length(); a++){
                    if (text.charAt(a) == ' '){
                       newMark = a;
                       break;
                    }
                 }
              }
             
              if (text.charAt(x) != ' ' && newMark == x){
                 for (int a = x; a > mark; a--){
                    if (text.charAt(a) == ' '){
                       newMark = a;
                       break;
                    }
                 }
              }
             
              textToRender[mark/TEXTBOXLENGTH] = text.substring(mark, newMark);
              textToRender[(mark/TEXTBOXLENGTH)+1] = "";
              textToRender[(mark/TEXTBOXLENGTH)+2] = "";
             
              mark = newMark;  
           }else{
              if (mark+50 > text.length()){
                 textToRender[mark/TEXTBOXLENGTH] = text.substring(mark, text.length());
              }
           }
        }
        rendering = true;
   }
   
   public void newMessage(String text, String name){
      if (rendering == false){
         spliceText(text, name);
      }else if (rendering){
         textQueue.add(text);
         nameQueue.add(name);
      }
   }
}

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (16 views)
2014-12-28 10:39:27

CopyableCougar4 (20 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!