Advertisement
Guest User

class Dialog

a guest
Jan 16th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.70 KB | None | 0 0
  1. module rgp.dialog;
  2. private import rengine.list; /// container
  3. private import rengine.type.image; /// image (analog texture)
  4. private import rengine.type.coord; /// coordinates
  5. private import rengine.type.color; /// color
  6. private import derelict.sdl2.ttf;
  7. private import derelict.sdl2.sdl;
  8. private import std.string;
  9. private import std.stdio : writeln;
  10.  
  11. class Dialog
  12. {
  13.     public string text; /// input text
  14.     public uint presy_x = 0; /// Counter position when moving to a new line
  15.  
  16.     public bool isEnd = false; /// isEnd?
  17.     public uint depth = 0;
  18.  
  19.     public float current = 1f; /// current sumbol
  20.     public float speed   = 0.1f; /// symbol/second
  21.  
  22.     private ListHandle!Image tex; /// container with textures
  23.     public uint sy    = 0; /// line position in the dialog
  24.  
  25.     public Color color = Color(0,0,0,255);
  26.     public Color background = Color(255,255,255,255);
  27.     public Coord position = Coord(0,0);
  28.     private TTF_Font* font;
  29.  
  30.     private int oldTime = 0;
  31.     private int FrameRate = 100;
  32.  
  33.     public uint size_x = 256; /// sie dialog
  34.  
  35.     public this()
  36.     {
  37.         auto tt = new Image;
  38.         tex.add(tt);
  39.     }
  40.  
  41.     public ListHandle!Image step(SDL_Renderer* e) /// called when drawing
  42.     {
  43.         if(oldTime + FrameRate > SDL_GetTicks()) {
  44.             return tex;
  45.         }
  46.  
  47.         oldTime = SDL_GetTicks();
  48.  
  49.         if(current < text.length)
  50.             current += speed;
  51.         else
  52.         {
  53.             isEnd = true;
  54.             return tex;
  55.         }
  56.  
  57.         Image tt;
  58.  
  59.         string temp = text[presy_x .. cast(int)current]; /// Copy line from text
  60.         //writeln(temp);
  61.  
  62.         if(!tex.get(sy).isNull)
  63.             tex.get(sy).free();
  64.  
  65.         // @DELETEME: temp = temp[0 .. $ - 1] ~ '\0';
  66.         const(char*) t = (temp.toStringz); /// to 'c' string
  67.         tex.get(sy).makeFromSurface(e,TTF_RenderUTF8_Blended(font,t,cast(SDL_Color)color)); /// the process of translating a string into a texture
  68.         //writeln("W",tex.get(sy).getWidth);
  69.         debug if(tex.get(sy).isNull) /// ERROR HANDLING
  70.             assert(null,"ERROR");
  71.  
  72.         if(tex.get(sy).getWidth > size_x) /// If the texture exceeds the size, go to the next line
  73.         {
  74.             sy += 1;
  75.             auto tx = new Image;
  76.             tex.add(tx);
  77.             presy_x = cast(int)current;
  78.             current += 1f;
  79.         }
  80.  
  81.         return tex;
  82.     }
  83.  
  84.     public void loadFont(string path,int size = 14)
  85.     {
  86.         import std.file : exists;
  87.         if(exists(path))
  88.             font = TTF_OpenFont(path.toStringz,size);
  89.         else
  90.             writeln("Not find font");
  91.         if(font == null)
  92.         {
  93.             writeln("SDL_ERROR!",TTF_GetError);
  94.         }
  95.     }
  96.  
  97.     public void setFont(TTF_Font* t)
  98.     {
  99.         font = t;
  100.     }
  101.  
  102.     public int getX()
  103.     {
  104.         return position.x;
  105.     }
  106.  
  107.     public int getY()
  108.     {
  109.         return position.y;
  110.     }
  111.  
  112.     public Coord getCoord()
  113.     {
  114.         return position;
  115.     }
  116.  
  117.     public void setCoord(Coord c)
  118.     {
  119.         position = c;
  120.     }
  121.  
  122.     public int getHeight()
  123.     {
  124.         return TTF_FontHeight(font);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement