2018-03-06 19:50:41 -05:00
|
|
|
function Picker()
|
|
|
|
{
|
|
|
|
this.memory = "";
|
|
|
|
this.el = document.createElement("input");
|
|
|
|
this.el.id = "picker"
|
|
|
|
this.original = null;
|
|
|
|
|
|
|
|
this.start = function()
|
|
|
|
{
|
2018-05-07 18:47:09 -04:00
|
|
|
this.el.setAttribute("placeholder",`${dotgrid.tool.style().color} ${dotgrid.tool.settings.size.width}x${dotgrid.tool.settings.size.height}`)
|
2018-05-06 19:15:23 -04:00
|
|
|
|
2018-03-06 19:50:41 -05:00
|
|
|
dotgrid.controller.set("picker");
|
|
|
|
dotgrid.interface.el.className = "picker"
|
|
|
|
this.el.focus()
|
|
|
|
this.original = dotgrid.tool.style().color
|
|
|
|
this.el.value = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stop = function()
|
|
|
|
{
|
|
|
|
this.cancel();
|
|
|
|
dotgrid.controller.set();
|
|
|
|
dotgrid.interface.el.className = ""
|
|
|
|
this.el.blur()
|
|
|
|
this.el.value = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
this.validate = function()
|
|
|
|
{
|
2018-05-06 19:05:25 -04:00
|
|
|
var parts = this.parse(this.el.value)
|
2018-03-21 03:10:09 -04:00
|
|
|
|
2018-05-06 19:05:25 -04:00
|
|
|
if(parts.color){ this.set_color(parts.color); }
|
2018-05-07 00:51:58 -04:00
|
|
|
if(parts.size){ this.set_size(parts.size); }
|
2018-05-06 19:05:25 -04:00
|
|
|
|
2018-05-10 16:46:50 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-05-07 00:51:58 -04:00
|
|
|
dotgrid.controller.set();
|
|
|
|
dotgrid.interface.el.className = ""
|
|
|
|
this.el.blur()
|
|
|
|
this.el.value = ""
|
2018-05-06 19:05:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.set_color = function(color)
|
|
|
|
{
|
|
|
|
dotgrid.tool.style().color = color;
|
|
|
|
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? color : "none";
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 19:05:25 -04:00
|
|
|
this.set_size = function(size)
|
|
|
|
{
|
|
|
|
dotgrid.set_size(size);
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:50:41 -05:00
|
|
|
this.cancel = function()
|
|
|
|
{
|
|
|
|
if(!this.original){ return; }
|
|
|
|
dotgrid.tool.style().color = this.original;
|
2018-03-07 02:43:46 -05:00
|
|
|
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? this.original : "none";
|
2018-05-10 16:46:50 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.update = function()
|
|
|
|
{
|
2018-05-07 00:51:58 -04:00
|
|
|
var parts = this.parse(this.el.value)
|
|
|
|
if(!parts.color){ return; }
|
2018-03-06 19:50:41 -05:00
|
|
|
|
2018-05-07 00:51:58 -04:00
|
|
|
dotgrid.tool.style().color = parts.color;
|
|
|
|
dotgrid.tool.style().fill = dotgrid.tool.style().fill != "none" ? parts.color : "none";
|
2018-05-10 16:46:50 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.listen = function(e)
|
|
|
|
{
|
|
|
|
if(e.key == "Enter"){
|
|
|
|
this.validate();
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
2018-05-06 19:05:25 -04:00
|
|
|
this.parse = function(value)
|
|
|
|
{
|
|
|
|
var parts = value.split(" ");
|
|
|
|
var color = null;
|
|
|
|
var size = null;
|
|
|
|
|
|
|
|
for(id in parts){
|
|
|
|
var part = parts[id];
|
|
|
|
if(is_color(part) && !color){ color = part; }
|
|
|
|
if(is_size(part) && !size){ size = { width:parseInt(part.toLowerCase().split("x")[0]),height:parseInt(part.toLowerCase().split("x")[1]) }; }
|
|
|
|
}
|
|
|
|
return {color:color,size:size}
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_size(val)
|
|
|
|
{
|
|
|
|
if(val.toLowerCase().indexOf("x") < 1){ return false; }
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_color(val)
|
2018-03-21 03:10:09 -04:00
|
|
|
{
|
2018-05-06 19:05:25 -04:00
|
|
|
if(val.length != 4 && val.length != 7){
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var re = /\#[0-9A-Fa-f]/g;
|
2018-03-21 03:10:09 -04:00
|
|
|
return re.test(val)
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:50:41 -05:00
|
|
|
this.el.onkeyup = function(event){ dotgrid.picker.listen(event); };
|
|
|
|
}
|