2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 RED.touch = RED.touch||{};
18 RED.touch.radialMenu = (function() {
23 var isOutside = false;
24 var activeOption = null;
27 function createRadial(obj,pos,options) {
30 var w = $("body").width();
31 var h = $("body").height();
33 touchMenu = d3.select("body").append("div")
42 .on('touchstart',function() {
44 d3.event.preventDefault();
50 var menu = touchMenu.append("div")
53 top: (pos[1]-80)+"px",
54 left:(pos[0]-80)+"px",
55 "border-radius": "80px",
58 background: "rgba(255,255,255,0.6)",
59 border: "1px solid #666"
63 var createMenuOpt = function(x,y,opt) {
64 opt.el = menu.append("div")
69 "border-radius": "20px",
73 border: "2px solid #666",
74 "text-align": "center",
78 opt.el.html(opt.name);
81 opt.el.style({"border-color":"#ccc",color:"#ccc"});
87 opt.el.on('touchstart',function() {
88 opt.el.style("background","#999");
89 d3.event.preventDefault();
90 d3.event.stopPropagation();
92 opt.el.on('touchend',function() {
95 d3.event.preventDefault();
96 d3.event.stopPropagation();
100 var n = options.length;
101 var dang = Math.max(Math.PI/(n-1),Math.PI/4);
103 for (var i=0;i<n;i++) {
104 var x = Math.floor(Math.cos(ang)*80);
105 var y = Math.floor(Math.sin(ang)*80);
106 if (options[i].name) {
107 createMenuOpt(x,y,options[i]);
113 var hide = function() {
120 obj.on('touchend.radial',function() {
121 obj.on('touchend.radial',null);
122 obj.on('touchmenu.radial',null);
126 activeOption.onselect();
131 } else if (isOutside) {
138 obj.on('touchmove.radial',function() {
140 var touch0 = d3.event.touches.item(0);
141 var p = [touch0.pageX - pos[0],touch0.pageY-pos[1]];
142 for (var i=0;i<menuOpts.length;i++) {
143 var opt = menuOpts[i];
145 if (p[0]>opt.x-30 && p[0]<opt.x+30 && p[1]>opt.y-30 && p[1]<opt.y+30) {
146 if (opt !== activeOption) {
147 opt.el.style("background","#999");
150 } else if (opt === activeOption) {
151 opt.el.style("background","#fff");
154 opt.el.style("background","#fff");
159 var d = Math.abs((p[0]*p[0])+(p[1]*p[1]));
160 isOutside = (d > 80*80);