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  * Circle 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_circle_tool {
39 	VG_Tool _inherit;
40 	VG_Circle *vcCur;
41 } VG_CircleTool;
42 
43 static void
Init(void * p)44 Init(void *p)
45 {
46 	VG_CircleTool *t = p;
47 
48 	t->vcCur = NULL;
49 }
50 
51 static __inline__ void
AdjustRadius(VG_Circle * vc,VG_Vector vPos)52 AdjustRadius(VG_Circle *vc, VG_Vector vPos)
53 {
54 	vc->r = VG_Distance(vPos, VG_Pos(vc->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_CircleTool *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->vcCur == NULL) {
68 			if (!(pCenter = VG_NearestPoint(vv, vPos, NULL))) {
69 				pCenter = VG_PointNew(vg->root, vPos);
70 			}
71 			t->vcCur = VG_CircleNew(vg->root, pCenter, 1.0f);
72 			AdjustRadius(t->vcCur, vPos);
73 		} else {
74 			AdjustRadius(t->vcCur, vPos);
75 			t->vcCur = NULL;
76 		}
77 		return (1);
78 	case AG_MOUSE_RIGHT:
79 		if (t->vcCur != NULL) {
80 			VG_Delete(t->vcCur);
81 			t->vcCur = NULL;
82 		}
83 		return (1);
84 	default:
85 		return (0);
86 	}
87 }
88 
89 static int
MouseMotion(void * p,VG_Vector vPos,VG_Vector vRel,int buttons)90 MouseMotion(void *p, VG_Vector vPos, VG_Vector vRel, int buttons)
91 {
92 	VG_CircleTool *t = p;
93 	VG_View *vv = VGTOOL(t)->vgv;
94 	VG_Point *pEx;
95 
96 	if (t->vcCur != NULL) {
97 		AdjustRadius(t->vcCur, vPos);
98 		VG_Status(vv, _("Set radius: %.2f"), t->vcCur->r);
99 	} else {
100 		if ((pEx = VG_NearestPoint(vv, vPos, NULL))) {
101 			VG_Status(vv, _("Use Point%u as center"),
102 			    (Uint)VGNODE(pEx)->handle);
103 		} else {
104 			VG_Status(vv, _("Circle center at %.2f,%.2f"),
105 			    vPos.x, vPos.y);
106 		}
107 	}
108 	return (0);
109 }
110 
111 static void
PostDraw(void * p,VG_View * vv)112 PostDraw(void *p, VG_View *vv)
113 {
114 	VG_Tool *t = p;
115 	int x, y;
116 
117 	VG_GetViewCoords(vv, t->vCursor, &x,&y);
118 	AG_DrawCircle(vv, x,y, 3, VG_MapColorRGB(vv->vg->selectionColor));
119 }
120 
121 VG_ToolOps vgCircleTool = {
122 	N_("Circle"),
123 	N_("Insert circle by centerpoint and radius."),
124 	&vgIconCircle,
125 	sizeof(VG_CircleTool),
126 	0,
127 	Init,
128 	NULL,			/* destroy */
129 	NULL,			/* edit */
130 	NULL,			/* predraw */
131 	PostDraw,
132 	NULL,			/* selected */
133 	NULL,			/* deselected */
134 	MouseMotion,
135 	MouseButtonDown,
136 	NULL,			/* mousebuttonup */
137 	NULL,			/* keydown */
138 	NULL			/* keyup */
139 };
140