summaryrefslogtreecommitdiff
path: root/js/tool.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/tool.js')
-rw-r--r--js/tool.js124
1 files changed, 93 insertions, 31 deletions
diff --git a/js/tool.js b/js/tool.js
index b1a52f0..e75bf8d 100644
--- a/js/tool.js
+++ b/js/tool.js
@@ -1,9 +1,18 @@
var Tool = Model({
- init: function (span) {
- this.el = span
- this.lex = new Lex (span)
- this.name = span.innerHTML
- this.span = span
+ init: function (el) {
+ this.el = el
+ this.lex = new Lex (el)
+ this.name = el.innerHTML
+ },
+ bind: function(){
+ var tool = this
+ tool.el.addEventListener('mousedown', function(e){
+ tool.focus()
+ })
+ if (tool.memorable) {
+ // console.log(tool.name, localStorage.getItem("ascii.tools." + tool.name) )
+ tool.use( localStorage.getItem("ascii.tools." + tool.name) == "true" )
+ }
},
use: function(){},
done: function(){},
@@ -11,19 +20,18 @@ var Tool = Model({
// focused && focused.blur()
current_tool && current_tool.blur()
current_tool = this
- this.span.classList.add('focused')
+ this.el.classList.add('focused')
this.use()
if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
},
blur: function(){
current_tool = null
- this.span.classList.remove('focused')
+ this.el.classList.remove('focused')
this.done()
}
})
var FileTool = Tool.extend({
-
focus: function(){
if (current_filetool === this) {
this.blur()
@@ -31,20 +39,87 @@ var FileTool = Tool.extend({
}
current_filetool && current_filetool.blur()
current_filetool = this
- this.span.classList.add('focused')
+ this.el.classList.add('focused')
this.use()
if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
},
blur: function(){
current_filetool = null
- this.span.classList.remove('focused')
+ this.el.classList.remove('focused')
this.done()
}
})
+var RadioItem = Tool.extend({
+ init: function(group, el){
+ this.group = group
+ this.el = el
+ },
+ focus: function(){
+ this.el.classList.add('focused')
+ },
+ blur: function(){
+ this.el.classList.remove('focused')
+ },
+ bind: function(){
+ var control = this
+ this.el.addEventListener('mousedown', function(){
+ control.group.use(control)
+ })
+ }
+})
+
+var RadioGroup = Tool.extend({
+ init: function(el){
+ this.el = el
+ this.controls = {}
+ var names = el.innerHTML.split(' ')
+ el.innerHTML = ''
+ var group = this
+ names.forEach(function(value){
+ var el = document.createElement('span')
+ el.classList.add('radio','tool')
+ var control = new RadioItem(group, el)
+ if (value.substr(0,1) === '*') {
+ control.value = value = value.substr(1)
+ group.use(control)
+ }
+ control.value = el.innerHTML = value
+ group.controls[value] = control
+ group.el.appendChild(el)
+ })
+ },
+ use: function(control){
+ if (typeof control === 'string') {
+ control = this.controls[control]
+ }
+ this.selected_control && this.selected_control.blur()
+ this.value = control.value
+ this.selected_control = control
+ control.focus()
+ control.use()
+ if (this.memorable){
+ localStorage.setItem("ascii.tools." + this.name, this.value)
+ }
+ },
+ bind: function(){
+ var tool = this
+ for (var n in this.controls){
+ this.controls[n].bind()
+ }
+ if (tool.memorable) {
+ var value = localStorage.getItem("ascii.tools." + tool.name)
+ if (value) tool.use(value)
+ }
+ }
+})
+
+
+
+
var Checkbox = Tool.extend({
- init: function (span){
- this.__init(span)
+ init: function (el){
+ this.__init(el)
var name = this.name.replace(/^[x_] /,"")
var state = localStorage.getItem("ascii." + name) || this.name[0] == "x"
this.name = name
@@ -59,42 +134,29 @@ var Checkbox = Tool.extend({
var BlurredCheckbox = Checkbox.extend({
focus: function(){
- // if (current_filetool && current_filetool.name == "shader") {
- // shader.toggle(false)
- // current_tool.blur()
- // current_tool = controls.circle
- // }
this.use()
- // if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
},
blur: function(){
- this.span.classList.remove('focused')
+ this.el.classList.remove('focused')
this.done()
}
})
var BlurredTool = Tool.extend({
focus: function(){
- // if (current_filetool && current_filetool.name == "shader") {
- // shader.toggle(false)
- // current_tool.blur()
- // current_tool = controls.circle
- // }
this.use()
- // if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
},
blur: function(){
- this.span.classList.remove('focused')
+ this.el.classList.remove('focused')
this.done()
}
})
var HiddenCheckbox = BlurredCheckbox.extend({
- init: function (span){
- this.el = span
- this.lex = new Lex (span)
- this.name = span.innerHTML
- this.span = span
+ init: function (el){
+ this.el = el
+ this.lex = new Lex (el)
+ this.name = el.innerHTML
var state = this.name[0] == "o"
this.update(state)
},