package ru.vagrant_ai.questionmarkgame.util;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import ru.vagrant_ai.questionmarkgame.main.Game;
public class Dialogue {
static byte phase = 0; //0 - text still appearing on the screen; 1 - text already appeared
static String head;
static Image image; //all icons must be 108x108
static Color border = new Color(110,110,110);
static Color portrait = new Color(45,192,255);
static boolean util_end = false;
static byte util_iter = 0;
static short util_shown_length = 0;
static String show_string[] = new String[3];
public static boolean perform(Graphics g)
{
if (show_string[0] == null || head == null || image == null)
return true;
Input input = Game.app.getInput();
if (input.isKeyPressed(Input.KEY_SPACE))
{
if (phase == 1)
return true;
else if (phase == 0)
{
phase = 1;
}
}
update();
render(g);
return false;
}
private static void update()
{
if (util_shown_length < show_string[0].length()+show_string[1].length()+show_string[2].length())
util_iter++;
if (util_iter > 3)
{
util_shown_length++;
if (phase == 1) //checks were separated because of early space press feature
{
util_end = true;
util_shown_length = (short) (show_string[0].length()+show_string[1].length()+show_string[2].length());
}
else if (util_shown_length == show_string[0].length()+show_string[1].length()+show_string[2].length())
{
util_end = true;
phase = 1;
}
util_iter = 0;
}
}
private static void render(Graphics g)
{
drawBounds(g);
drawText(g);
drawIcon(g);
}
private static void drawBounds(Graphics g)
{
int app_y = Game.getAppY();
int app_x = Game.getAppX();
g.setColor(Color.black); //dialog box background color
g.fillRect(14, app_y-157, app_x-27, 143);
g.setColor(border); //dialog box corner
g.setLineWidth(1);
g.drawRect(10, app_y-160, app_x-20, 150);
g.setColor(Color.white); //dialog box 2nd corner
g.setLineWidth(6);
g.drawRect(14, app_y-157, app_x-27, 143);
g.setColor(new Color(218,70,70)); //icon background
g.fillRect(30, app_y-141, 112, 112);
g.setColor(portrait); //icon border
g.setLineWidth(4);
g.drawRect(30, app_y-141, 112, 112);
Text.drawString(78, 157, app_y-144, ""+head, Color.white);
}
private static void drawText(Graphics g)
{
int substr0 = (util_shown_length>show_string[0].length()?show_string[0].length():util_shown_length);
Text.drawString(60, 157, Game.getAppY()-108, ""+show_string[0].substring(0, substr0), Color.white);
int substr1 = (util_shown_length>show_string[1].length()+substr0?show_string[1].length():util_shown_length-substr0);
Text.drawString(60, 157, Game.getAppY()-108+28, ""+show_string[1].substring(0, substr1), Color.white);
int substr2 = (util_shown_length>show_string[2].length()+substr0+substr1?show_string[2].length():util_shown_length-(substr0+substr1));
Text.drawString(60, 157, Game.getAppY()-108+56, ""+show_string[2].substring(0, substr2), Color.white);
}
private static void drawIcon(Graphics g)
{
image.draw(32, Game.getAppY()-138); //all icons must be 108x108
}
/* UTIL */
public static void modify(String header, String message, String image_name)
{
if (header.equals("") || message.equals("") || image_name.equals(""))
return;
phase = 0;
util_iter = 0;
util_shown_length = 0;
head = "["+header+"]";
util_end = false;
for (int i = 0; i < 3; ++i)
show_string[i] = "";
int util = 0;
int util_sub_pos = 0;
for (int i = 0; i < message.length(); ++i)
{
if (message.charAt(i) == '/')
{
show_string[util] = message.substring(util_sub_pos, i);
i++;
util_sub_pos = i;
util++;
}
if (i == message.length()-1)
show_string[util] = message.substring(util_sub_pos, message.length());
}
if (show_string[0] == "")
show_string[0] = message;
image = Util.loadImage("particle/dialog_icons/"+image_name+".png");
}
}