1 /*
2 * Copyright (c) 2008 Hypertriton, Inc. <http://hypertriton.com/>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23 * USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 /*
27 * Text tool.
28 */
29
30 #include <agar/core/core.h>
31 #include <agar/gui/widget.h>
32 #include <agar/gui/primitive.h>
33 #include <agar/gui/textbox.h>
34 #include <agar/gui/iconmgr.h>
35 #include <agar/vg/vg.h>
36 #include <agar/vg/vg_view.h>
37 #include <agar/vg/icons.h>
38
39 typedef struct vg_text_tool {
40 VG_Tool _inherit;
41 VG_Text *vtIns; /* Text being inserted */
42 char text[VG_TEXT_MAX];
43 } VG_TextTool;
44
45 static void
Init(void * p)46 Init(void *p)
47 {
48 VG_TextTool *t = p;
49
50 t->vtIns = NULL;
51 Strlcpy(t->text, "<text>", sizeof(t->text));
52 }
53
54 static int
MouseButtonDown(void * p,VG_Vector vPos,int button)55 MouseButtonDown(void *p, VG_Vector vPos, int button)
56 {
57 VG_TextTool *t = p;
58 VG_View *vv = VGTOOL(t)->vgv;
59 VG *vg = vv->vg;
60 VG_Point *p1, *p2;
61
62 switch (button) {
63 case AG_MOUSE_LEFT:
64 if (t->vtIns == NULL) {
65 if (!(p1 = VG_NearestPoint(vv, vPos, NULL))) {
66 p1 = VG_PointNew(vg->root, vPos);
67 }
68 p2 = VG_PointNew(vg->root, vPos);
69 t->vtIns = VG_TextNew(vg->root, p1, p2);
70 VG_TextString(t->vtIns, t->text);
71 if (VGTOOL(t)->p != NULL)
72 VG_TextSubstObject(t->vtIns, VGTOOL(t)->p);
73 } else {
74 if ((p2 = VG_NearestPoint(vv, vPos,
75 t->vtIns->p2))) {
76 VG_DelRef(t->vtIns, t->vtIns->p2);
77 VG_Delete(t->vtIns->p2);
78 t->vtIns->p2 = p2;
79 VG_AddRef(t->vtIns, p2);
80 } else {
81 VG_SetPosition(t->vtIns->p2, vPos);
82 }
83 t->vtIns = NULL;
84 }
85 return (1);
86 case AG_MOUSE_RIGHT:
87 if (t->vtIns != NULL) {
88 VG_Delete(t->vtIns);
89 t->vtIns = NULL;
90 }
91 return (1);
92 default:
93 return (0);
94 }
95 }
96
97 static void
PostDraw(void * p,VG_View * vv)98 PostDraw(void *p, VG_View *vv)
99 {
100 VG_TextTool *t = p;
101 int x, y;
102
103 VG_GetViewCoords(vv, VGTOOL(t)->vCursor, &x,&y);
104 AG_DrawCircle(vv, x,y, 3, VG_MapColorRGB(vv->vg->selectionColor));
105 }
106
107 static int
MouseMotion(void * p,VG_Vector vPos,VG_Vector vRel,int b)108 MouseMotion(void *p, VG_Vector vPos, VG_Vector vRel, int b)
109 {
110 VG_TextTool *t = p;
111 VG_View *vv = VGTOOL(t)->vgv;
112 VG_Point *pEx;
113 VG_Vector pos;
114 float theta, rad;
115
116 if (t->vtIns != NULL) {
117 pEx = t->vtIns->p1;
118 pos = VG_Pos(pEx);
119 theta = VG_Atan2(vPos.y - pos.y,
120 vPos.x - pos.x);
121 rad = VG_Hypot(vPos.x - pos.x,
122 vPos.y - pos.y);
123 if ((pEx = VG_NearestPoint(vv, vPos, t->vtIns->p2))) {
124 VG_Status(vv, _("End baseline at Point%u"),
125 (Uint)VGNODE(pEx)->handle);
126 } else {
127 VG_Status(vv,
128 _("End baseline at %.2f,%.2f "
129 "(%.2f|%.2f\xc2\xb0)"),
130 vPos.x, vPos.y, rad, VG_Degrees(theta));
131 }
132 VG_SetPosition(t->vtIns->p2, vPos);
133 } else {
134 if ((pEx = VG_NearestPoint(vv, vPos, NULL))) {
135 VG_Status(vv, _("Start baseline at Point%u"),
136 (Uint)VGNODE(pEx)->handle);
137 } else {
138 VG_Status(vv, _("Start baseline at %.2f,%.2f"), vPos.x,
139 vPos.y);
140 }
141 }
142 return (0);
143 }
144
145 static void *
Edit(void * p,VG_View * vv)146 Edit(void *p, VG_View *vv)
147 {
148 VG_TextTool *t = p;
149 AG_Box *box = AG_BoxNewVert(NULL, AG_BOX_EXPAND);
150 AG_Textbox *tb;
151
152 AG_LabelNew(box, 0, _("Text: "));
153 tb = AG_TextboxNewS(box, AG_TEXTBOX_MULTILINE|AG_TEXTBOX_HFILL, NULL);
154 AG_TextboxBindUTF8(tb, t->text, sizeof(t->text));
155 return (box);
156 }
157
158 VG_ToolOps vgTextTool = {
159 N_("Text"),
160 N_("Insert text entity."),
161 &vgIconText,
162 sizeof(VG_TextTool),
163 0,
164 Init,
165 NULL, /* destroy */
166 Edit,
167 NULL, /* predraw */
168 PostDraw,
169 NULL, /* selected */
170 NULL, /* deselected */
171 MouseMotion,
172 MouseButtonDown,
173 NULL, /* mousebuttonup */
174 NULL, /* keydown */
175 NULL /* keyup */
176 };
177