Added some config files and refactored dragbars and buttonmenu

This commit is contained in:
Erik Tiekstra
2017-05-09 12:51:13 +02:00
parent c73e1ed9b4
commit 4c0b388805
13 changed files with 182 additions and 148 deletions
+38 -37
View File
@@ -1,42 +1,42 @@
'use strict'; 'use strict';
var TextButton = require('./TextButton'); var TextButton = require('./TextButton');
var config = require('../config');
/**
* Creates the menu with text-buttons settings provided. Settings will override
* the defaults from the config files.
* @param {Object} canvas
* @param {Object=} settings
*/
function ButtonMenu(canvas, settings) { function ButtonMenu(canvas, settings) {
var _items = []; var _textButtons = [];
var _width = 0; var _width = 0;
settings = $.extend({}, config.buttonMenu, settings);
var _settings = $.extend({}, settings, {
background: '#444',
fontSize: 14,
height: 30,
margin: 5,
padding: 20
});
/** /**
* Calculates the width of all contained items * Calculates the width of all contained items
* @return {Number} * @return {Number}
*/ */
function calcWidth() { function setWidth() {
var width = 0; var startWidth = 0;
_items.forEach(function (o) { _textButtons.forEach(function(textButton) {
width += o.width; startWidth += textButton.width;
}); });
width += _settings.margin * (_items.length - 1); _width = startWidth += settings.margin * (_textButtons.length - 1);
return width;
} }
/** /**
* Adds items to the button-menu.
* @param {String} text * @param {String} text
* @param {Function} callback * @param {Function} callback
* @return {ButtonMenu} * @return {ButtonMenu}
*/ */
this.addItem = function (text, callback) { this.addItem = function(text, callback) {
_items.push( _textButtons.push(
new TextButton(text, _settings) new TextButton(text, settings)
.onClick(callback) .onClick(callback)
.set({ __group: this }) .set({__group: this})
); );
return this; return this;
}; };
@@ -44,25 +44,24 @@ function ButtonMenu(canvas, settings) {
/** /**
* @return {ButtonMenu} * @return {ButtonMenu}
*/ */
this.bringToFront = function () { this.bringToFront = function() {
_items.forEach(function (o) { _textButtons.forEach(function(textButton) {
o.bringToFront(); textButton.bringToFront();
}); });
return this; return this;
}; };
/** /**
* @return {Array} * @return {Array}
*/ */
this.getItems = function () { this.getItems = function() {
return _items; return _textButtons;
}; };
/** /**
* @return {Number} * @return {Number}
*/ */
this.getWidth = function () { this.getWidth = function() {
_width = calcWidth();
return _width; return _width;
}; };
@@ -70,21 +69,23 @@ function ButtonMenu(canvas, settings) {
* Recalculate boundaries and add all items to canvas if required * Recalculate boundaries and add all items to canvas if required
* @return {ButtonMenu} * @return {ButtonMenu}
*/ */
this.render = function () { this.render = function() {
setWidth();
var start = canvas.width / 2 - this.getWidth() / 2; var start = canvas.width / 2 - this.getWidth() / 2;
var currPos = Math.round(start); var currPos = Math.round(start);
_items.forEach(function (o) { _textButtons.forEach(function(textButton) {
o.set({ top: 10, left: currPos }) textButton.set({top: 10, left: currPos})
.setCoords(); .setCoords();
if (!o.canvas) { if (!textButton.canvas) {
o.addTo(canvas); textButton.addTo(canvas);
} }
currPos += Math.round(o.width + _settings.margin); currPos += Math.round(textButton.width + settings.margin);
// Disable moving of buttons // Disable moving of text-buttons
o.lockMovementX = o.lockMovementY = true; textButton.lockMovementX = textButton.lockMovementY = true;
}); });
canvas.renderAll(); canvas.renderAll();
+4 -4
View File
@@ -86,12 +86,12 @@ util.setProperties(Canvas.prototype, {
image: { image: {
get: Canvas.prototype.get_image get: Canvas.prototype.get_image
}, },
topMenu: { buttonMenu: {
get: function () { get: function () {
if (!this.__topMenu) { if (!this.__buttonMenu) {
this.__topMenu = new ButtonMenu(this); this.__buttonMenu = new ButtonMenu(this);
} }
return this.__topMenu; return this.__buttonMenu;
} }
}, },
viewport: { viewport: {
+7 -15
View File
@@ -1,5 +1,7 @@
'use strict'; 'use strict';
var config = require('../config');
var DragHandle = function (handler) { var DragHandle = function (handler) {
var _currentAxis; var _currentAxis;
var _dragging; var _dragging;
@@ -33,20 +35,10 @@ var DragHandle = function (handler) {
}; };
function getRect(text, width) { function getRect(text, width) {
var txt = new fabric.Text(text, { var txt = new fabric.Text(text, config.dragHandle.text);
fontFamily: 'breuer', var rect = new fabric.Rect($.extend({}, config.dragHandle.rect, {
fontSize: 12,
fill: '#fff',
originX: 'center',
top: 2
});
var rect = new fabric.Rect({
fill: '#666',
width: width, width: width,
height: 20, }));
originX: 'center',
});
return new fabric.Group([rect, txt]).set({ return new fabric.Group([rect, txt]).set({
evented: false, evented: false,
@@ -81,8 +73,8 @@ var DragHandle = function (handler) {
this.__handles.h.addTo(this.getCanvas()).centerH().set({ top: tracks.h.top }).setCoords(); this.__handles.h.addTo(this.getCanvas()).centerH().set({ top: tracks.h.top }).setCoords();
this.__handles.v.addTo(this.getCanvas()) this.__handles.v.addTo(this.getCanvas())
.setAngle(90) .setAngle(-90)
.centerV().set({ left: tracks.v.left + tracks.v.width }) .centerV().set({ left: tracks.v.left })
.setCoords(); .setCoords();
this.getCanvas() this.getCanvas()
+26 -13
View File
@@ -1,8 +1,13 @@
'use strict'; 'use strict';
var DragHandle = require('./DragHandle'); var DragHandle = require('./DragHandle');
var config = require('../config');
function DragbarHandler(canvas) { function DragbarHandler(canvas) {
var _items = { h: null, v: null }; var _dragbars = {
h: null,
v: null
};
/** /**
* @return {DragbarHandler} * @return {DragbarHandler}
@@ -20,7 +25,7 @@ function DragbarHandler(canvas) {
this.__handles.init(); this.__handles.init();
[].concat(_items.h, _items.v).forEach(function (o) { [].concat(_dragbars.h, _dragbars.v).forEach(function (o) {
o.bringToFront(); o.bringToFront();
}); });
@@ -39,14 +44,14 @@ function DragbarHandler(canvas) {
*/ */
this.clear = function () { this.clear = function () {
canvas canvas
.remove(_items.h) .remove(_dragbars.h)
.remove(_items.v) .remove(_dragbars.v)
.renderAll(); .renderAll();
return this; return this;
}; };
this.getTracks = function () { this.getTracks = function () {
return _items; return _dragbars;
}; };
/** /**
@@ -55,13 +60,21 @@ function DragbarHandler(canvas) {
this.init = function () { this.init = function () {
this.clear(); this.clear();
_items.h = new fabric.Rect({ axis: 'h', fill: '#bbb', width: canvas.image.width, height: 20 }) _dragbars.h = new fabric.Rect($.extend({}, config.dragBar, {
.addTo(canvas) axis: 'h',
.centerH().set({top: canvas.height - 30}).setCoords(); width: canvas.image.width,
height: config.dragBar.size,
}))
.addTo(canvas)
.centerH().set({ top: canvas.height - 30 }).setCoords();
_items.v = new fabric.Rect({ axis: 'v', fill: '#bbb', width: 20, height: canvas.image.height }) _dragbars.v = new fabric.Rect($.extend({}, config.dragBar, {
.addTo(canvas) axis: 'v',
.set({left: canvas.width - 30}).setCoords(); width: config.dragBar.size,
height: canvas.image.height
}))
.addTo(canvas)
.set({ left: canvas.width - 40 }).setCoords();
canvas.image.on('moving', this.sync); canvas.image.on('moving', this.sync);
@@ -72,8 +85,8 @@ function DragbarHandler(canvas) {
* @return {DragbarHandler} * @return {DragbarHandler}
*/ */
this.sync = function () { this.sync = function () {
_items.h.set({left: canvas.image.left}).setCoords(); _dragbars.h.set({left: canvas.image.left}).setCoords();
_items.v.set({top: canvas.image.top}).setCoords(); _dragbars.v.set({top: canvas.image.top}).setCoords();
return this; return this;
}; };
} }
+8 -8
View File
@@ -13,7 +13,7 @@ function ImageEditor(args) {
// Initialize canvas // Initialize canvas
_canvas = new Canvas('canvas'); _canvas = new Canvas('canvas');
_canvas.topMenu _canvas.buttonMenu
.addItem(['Show Murals Panel', 'Hide Murals Panel'], function () { .addItem(['Show Murals Panel', 'Hide Murals Panel'], function () {
_canvas.viewport.gores.enable(this.isActive()); _canvas.viewport.gores.enable(this.isActive());
}) })
@@ -70,13 +70,13 @@ function ImageEditor(args) {
return this; return this;
}; };
util.setProperties(this, { util.setProperties(this, {
canvas: { canvas: {
get: function () { get: function () {
return _canvas; return _canvas;
} }
} }
}); });
} }
$(function () { $(function () {
+44 -52
View File
@@ -1,101 +1,68 @@
'use strict'; 'use strict';
var defaults = { var config = require('../config');
activeBackground: '#000',
background: '#000',
color: '#fff',
fontFamily: 'breuer',
fontSize: 12,
padding: 10,
};
/** /**
* @param {String} text * Creates a text-button with text/settings provided.
* @param {String} text - text to be shown inside the button
* @param {Object} settings * @param {Object} settings
*/ */
function TextButton(text, settings) { function TextButton(text, settings) {
fabric.Group.call(this); fabric.Group.call(this);
var _callback = function () {}; var _callback = null;
var _rect = null; var _rect = null;
var _text = null; var _text = null;
settings = $.extend({}, defaults, settings); settings = $.extend({}, settings, {
text: Array.isArray(text) ? text[0] : text,
settings.text = Array.isArray(text) ? text[0] : text; textActive: Array.isArray(text) ? text[1] : text,
settings.textActive = Array.isArray(text) ? text[1] : text; });
var _rectSettings = { var _rectSettings = {
fill: settings.background, fill: settings.background,
width: settings.width, width: settings.width,
height: settings.height, height: settings.height,
originX: 'center', originX: settings.originX,
originY: 'center', originY: settings.originY,
}; };
var _textSettings = { var _textSettings = {
fill: settings.color, fill: settings.color,
fontFamily: settings.fontFamily, fontFamily: settings.fontFamily,
fontSize: settings.fontSize, fontSize: settings.fontSize,
originX: 'center', originX: settings.originX,
originY: 'center', originY: settings.originY,
}; };
// If width or height is omitted, the size for this button needs to be // If width or height is omitted, the size for this button needs to be
// calculated when setting Text. // calculated when setting Text.
// An initial value for width and height needs to be set, else we won't // An initial value for width and height needs to be set, else we won't
// render correctly. // render correctly.
if ((settings.dynamicW = !settings.width)) { if (!settings.width) {
settings.dynamicW = true;
_rectSettings.width = 1; _rectSettings.width = 1;
} }
if (!settings.height) {
if ((settings.dynamicH = !settings.height)) { settings.dynamicH = true;
_rectSettings.height = 1; _rectSettings.height = 1;
} }
// Make sure we're interactive // Make sure we're interactive
this.selectable = true; this.selectable = true;
this.hoverCursor = settings.cursor;
this.hoverCursor = 'default';
// Initialize text and rectangle // Initialize text and rectangle
_text = new fabric.Text('', _textSettings); _text = new fabric.Text('', _textSettings);
_rect = new fabric.Rect(_rectSettings); _rect = new fabric.Rect(_rectSettings);
this.addWithUpdate(_rect)
.addWithUpdate(_text);
var Event = { this.addWithUpdate(_rect).addWithUpdate(_text);
mouseDown: function (e) {
this.__active = !this.__active;
_rect.setFill(this.__active ? settings.activeBackground : settings.background);
this.setText(settings[this.isActive() ? 'textActive' : 'text']);
_callback.call(this, e);
if (this.__group) {
this.__group.render();
}
}
};
/**
* @return {Boolean}
*/
this.isActive = function () {
return this.__active;
};
this.onClick = function (callback) {
_callback = callback || function () {};
this.on('mousedown', Event.mouseDown);
return this;
};
/** /**
* @param {String} text * @param {String} text
* @return {TextButton} * @return {TextButton}
*/ */
this.setText = function (text) { this.setText = function(text) {
_text.setText((text || '').toString()).setCoords(); _text.setText((text || '').toString()).setCoords();
if (settings.dynamicW) { if (settings.dynamicW) {
@@ -109,6 +76,31 @@ function TextButton(text, settings) {
return this.setCoords(); return this.setCoords();
}; };
/**
* @return {Boolean}
*/
this.isActive = function() {
return this.__active;
};
function handleMouseDown(e) {
this.__active = !this.__active;
_rect.setFill(settings[this.isActive() ? 'activeBackground' : 'background']);
this.setText(settings[this.isActive() ? 'textActive' : 'text']);
_callback.call(this, e);
if (this.__group) {
this.__group.render();
}
}
this.onClick = function(callback) {
_callback = callback || function() {};
this.on('mousedown', handleMouseDown);
return this;
};
if (settings.text) { if (settings.text) {
this.setText(settings.text); this.setText(settings.text);
} }
+9 -9
View File
@@ -121,7 +121,7 @@ function Viewport(canvas) {
this.rulers.reset(); this.rulers.reset();
canvas.aperture.setTransparent(true); canvas.aperture.setTransparent(true);
canvas.image.__moved = false; canvas.image.__moved = false;
canvas.topMenu.bringToFront(); canvas.buttonMenu.bringToFront();
}; };
util.setProperties(this, { util.setProperties(this, {
@@ -131,16 +131,16 @@ function Viewport(canvas) {
} }
}, },
gores: { gores: {
get: function () { get: function () {
return this.__goreHandler || return this.__goreHandler ||
(this.__goreHandler = new GoreHandler(this)); (this.__goreHandler = new GoreHandler(this));
} }
}, },
rulers: { rulers: {
get: function () { get: function () {
return this.__rulerHandler || return this.__rulerHandler ||
(this.__rulerHandler = new RulerHandler(this)); (this.__rulerHandler = new RulerHandler(this));
} }
} }
}); });
} }
@@ -1,9 +0,0 @@
'use strict';
module.exports = {
activeBackground: '#000',
background: '#444',
fontSize: 14,
height: 30,
padding: 20
};
+7
View File
@@ -0,0 +1,7 @@
'use strict';
module.exports = {
buttonMenu: require('./config/button-menu'),
dragHandle: require('./config/drag-handle'),
dragBar: require('./config/drag-bar'),
};
+14
View File
@@ -0,0 +1,14 @@
'use strict';
module.exports = {
color: '#ffffff',
fontFamily: 'breuer',
fontSize: 14,
background: '#000000',
activeBackground: '#444444',
cursor: 'pointer',
originX: 'center',
originY: 'center',
padding: 20,
margin: 5,
};
+6
View File
@@ -0,0 +1,6 @@
'use strict';
module.exports = {
fill: '#bbbbbb',
size: 20,
};
+16
View File
@@ -0,0 +1,16 @@
'use strict';
module.exports = {
text: {
fontFamily: 'breuer',
fontSize: 12,
fill: '#ffffff',
originX: 'center',
top: 2,
},
rect: {
fill: '#666666',
height: 20,
originX: 'center',
},
};
+3 -1
View File
@@ -1,7 +1,9 @@
'use strict';
(function () { (function () {
'use strict';
require('./components/ImageEditor/fabric.ext/ObjectDefaults'); require('./components/ImageEditor/fabric.ext/ObjectDefaults');
//require('./components/ImageEditor/fabric.ext/BaseEventHandlers'); //require('./components/ImageEditor/fabric.ext/BaseEventHandlers');
require('./components/ImageEditor/ImageEditor'); require('./components/ImageEditor/ImageEditor');
}()); }());