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  * Arc 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/iconmgr.h>
34 #include <agar/vg/vg.h>
35 #include <agar/vg/vg_view.h>
36 #include <agar/vg/icons.h>
37 
38 typedef struct vg_arc_tool {
39 	VG_Tool _inherit;
40 	VG_Arc *vaCur;
41 } VG_ArcTool;
42 
43 static void
Init(void * p)44 Init(void *p)
45 {
46 	VG_ArcTool *t = p;
47 
48 	t->vaCur = NULL;
49 }
50 
51 static __inline__ void
AdjustRadius(VG_Arc * va,VG_Vector vPos)52 AdjustRadius(VG_Arc *va, VG_Vector vPos)
53 {
54 	va->r = VG_Distance(vPos, VG_Pos(va->p));
55 }
56 
57 static int
MouseButtonDown(void * p,VG_Vector vPos,int button)58 MouseButtonDown(void *p, VG_Vector vPos, int button)
59 {
60 	VG_ArcTool *t = p;
61 	VG_View *vv = VGTOOL(t)->vgv;
62 	VG *vg = vv->vg;
63 	VG_Point *pCenter;
64 
65 	switch (button) {
66 	case AG_MOUSE_LEFT:
67 		if (t->vaCur == NULL) {
68 			if (!(pCenter = VG_NearestPoint(vv, vPos, NULL))) {
69 				pCenter = VG_PointNew(vg->root, vPos);
70 			}
71 			t->vaCur = VG_ArcNew(vg->root, pCenter, 1.0f,
72 			    0.0f, (float)VG_PI);
73 			AdjustRadius(t->vaCur, vPos);
74 		} else {
75 			AdjustRadius(t->vaCur, vPos);
76 			t->vaCur = NULL;
77 		}
78 		return (1);
79 	case AG_MOUSE_RIGHT:
80 		if (t->vaCur != NULL) {
81 			VG_Delete(t->vaCur);
82 			t->vaCur = NULL;
83 		}
84 		return (1);
85 	default:
86 		return (0);
87 	}
88 }
89 
90 static int
MouseMotion(void * p,VG_Vector vPos,VG_Vector vRel,int buttons)91 MouseMotion(void *p, VG_Vector vPos, VG_Vector vRel, int buttons)
92 {
93 	VG_ArcTool *t = p;
94 	VG_View *vv = VGTOOL(t)->vgv;
95 	VG_Point *pEx;
96 
97 	if (t->vaCur != NULL) {
98 		AdjustRadius(t->vaCur, vPos);
99 		VG_Status(vv, _("Set radius: %.2f"), t->vaCur->r);
100 	} else {
101 		if ((pEx = VG_NearestPoint(vv, vPos, NULL))) {
102 			VG_Status(vv, _("Use Point%u as center"),
103 			    (Uint)VGNODE(pEx)->handle);
104 		} else {
105 			VG_Status(vv, _("Arc center at %.2f,%.2f"),
106 			    vPos.x, vPos.y);
107 		}
108 	}
109 	return (0);
110 }
111 
112 static void
PostDraw(void * p,VG_View * vv)113 PostDraw(void *p, VG_View *vv)
114 {
115 	VG_Tool *t = p;
116 	int x, y;
117 
118 	VG_GetViewCoords(vv, t->vCursor, &x,&y);
119 	AG_DrawCircle(vv, x,y, 3, VG_MapColorRGB(vv->vg->selectionColor));
120 }
121 
122 VG_ToolOps vgArcTool = {
123 	N_("Arc"),
124 	N_("Insert arcs from centerpoint and angles."),
125 	&vgIconCircle,
126 	sizeof(VG_ArcTool),
127 	0,
128 	Init,
129 	NULL,			/* destroy */
130 	NULL,			/* edit */
131 	NULL,			/* predraw */
132 	PostDraw,
133 	NULL,			/* selected */
134 	NULL,			/* deselected */
135 	MouseMotion,
136 	MouseButtonDown,
137 	NULL,			/* mousebuttonup */
138 	NULL,			/* keydown */
139 	NULL			/* keyup */
140 };
141