1 module unde.guitk.label;
2 
3 import derelict.sdl2.sdl;
4 import derelict.sdl2.ttf;
5 import derelict.sdl2.image;
6 
7 import std.stdio;
8 import std.math;
9 import std..string;
10 
11 import unde.global_state;
12 import unde.guitk.lib;
13 
14 class Label:UIEntry
15 {
16     private SDL_Rect _rect;
17     private UIPage _page;
18     private bool _focus;
19 
20     private string label;
21     private SDL_Color color;
22     private int fontsize;
23     
24     this(UIPage page, GlobalState gs, string label, int x, int y, int fontsize=9,
25             SDL_Color color = SDL_Color(0xFF, 0xFF, 0xFF, 0xFF))
26     {
27         _page = page;
28         this.label = label;
29         this.fontsize = fontsize;
30         this.color = color;
31 
32         _rect.x = x;
33         _rect.y = y;
34 
35         int line_height = cast(int)(round(SQRT2^^fontsize)*1.2);
36 
37         auto tt = gs.text_viewer.font.get_line_from_cache(label, 
38                 fontsize, 0, line_height, color);
39         if (!tt && !tt.texture)
40         {
41             throw new Exception("Can't create text_surface: "~
42                     TTF_GetError().fromStringz().idup());
43         }
44 
45         _rect.w = tt.w;
46         _rect.h = tt.h;
47     }
48 
49     @property SDL_Rect rect() {return _rect;}
50     
51     void on_draw(GlobalState gs)
52     {
53         int line_height = cast(int)(round(SQRT2^^fontsize)*1.2);
54 
55         auto tt = gs.text_viewer.font.get_line_from_cache(label, 
56                 fontsize, 0, line_height, color);
57         if (!tt && !tt.texture)
58         {
59             throw new Exception("Can't create text_surface: "~
60                     TTF_GetError().fromStringz().idup());
61         }
62 
63         SDL_Rect rect;
64         rect.x = _rect.x;
65         rect.y = _rect.y;
66         rect.w = tt.w;
67         rect.h = tt.h;
68 
69         auto r = SDL_RenderCopy(gs.renderer, tt.texture, null, &rect);
70         if (r < 0)
71         {
72             writefln(
73                     "Label.on_draw() %s: Error while render copy: %s", 
74                     label,
75                     SDL_GetError().fromStringz() );
76         }
77     }
78 
79     void process_event(GlobalState gs, SDL_Event event)
80     {
81     }
82 
83     @property UIPage page() {return _page;}
84 
85     @property ref bool focus() {return _focus;}
86 
87     void on_set_focus(GlobalState gs)
88     {
89     }
90 
91     void on_unset_focus(GlobalState gs)
92     {
93     }
94 
95     void set_keybar(GlobalState gs)
96     {
97     }
98 }
99