1 /*
2 Copyright (c) 2015 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dgl.ui.textlineinput;
30 
31 import std.stdio;
32 
33 import dlib.core.memory;
34 import dlib.math.vector;
35 import dlib.image.color;
36 
37 import dgl.core.interfaces;
38 import dgl.core.event;
39 import dgl.ui.font;
40 import dgl.ui.textline;
41 
42 class TextLineInput: TextListener, Drawable
43 {
44     TextLine textLine;
45     TextLine cursor;
46     bool alignToWindow = true;
47     int oldWindowHeight;
48 
49     this(EventManager emngr, Font font, Vector2f pos)
50     {
51         super(emngr);
52 
53         textLine = New!TextLine(font, "", pos);
54         textLine.alignment = Alignment.Left;
55         textLine.color = Color4f(1, 1, 1);
56 
57         cursor = New!TextLine(font, "_", pos);
58         cursor.alignment = Alignment.Left;
59         cursor.color = Color4f(1, 1, 1);
60 
61         oldWindowHeight = emngr.windowHeight;
62     }
63 
64     override void onKeyDown(int key)
65     {
66         super.onKeyDown(key);
67 
68         textLine.setText(toString);
69         cursor.setPosition(textLine.textWidth + cursor.textWidth, cursor.position.y);
70     }
71 
72     float timer = 0.0f;
73     bool cursorState = true;
74 
75     override void draw(double dt)
76     {
77         processEvents();
78 
79         textLine.draw(dt);
80 
81         timer += dt;
82         if (timer >= 0.5f)
83         {
84             cursorState = !cursorState;
85             timer = 0.0f;
86         }
87 
88         if (cursorState)
89             cursor.draw(dt);
90     }
91 
92     ~this()
93     {
94         //writeln("Deleting TextLineInput...");
95         textLine.free();
96         cursor.free();
97     }
98 
99     override void free()
100     {
101         Delete(this);
102     }
103 
104     override void onResize(int width, int height)
105     {
106         if (alignToWindow)
107         {
108             float y = cast(float)oldWindowHeight - textLine.position.y;
109             textLine.position.y = height - y;
110             cursor.position.y = height - y;
111             oldWindowHeight = height;
112         }
113     }
114 }