Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/mod/gui/common/ResizeBar.js
- This file was added.
function ResizeBar(object, side, width, objectsHooked, isVisibleCondition, onDragDown) | |||||
{ | |||||
this.object = object; | |||||
this.side = side; | |||||
this.halfWidth = width === undefined ? | |||||
ResizeBar.defaults.width / 2 : | |||||
width / 2; | |||||
this.objectsHooked = objectsHooked ? objectsHooked : []; | |||||
elexis: (still no .0) | |||||
this.isVisibleCondition = isVisibleCondition ? | |||||
isVisibleCondition : | |||||
() => !this.object.hidden; | |||||
this.onDragDown = onDragDown ? onDragDown : () => { }; | |||||
this.lastIn = false; | |||||
Done Inline Actions{} elexis: `{}` | |||||
this.dragging = false; | |||||
for (let i = 0; i < this.objectsHooked.length; ++i) | |||||
this.objectsHooked[i][0] = this.objectsHooked[i][0]; | |||||
} | |||||
ResizeBar.defaults = { "width": 14 }; | |||||
ResizeBar.mouse = { | |||||
"x": 0, | |||||
"y": 0, | |||||
"set": function (mouse) | |||||
{ | |||||
this.x = mouse.x; | |||||
this.y = mouse.y; | |||||
} | |||||
}; | |||||
ResizeBar.mousePress = Object.assign({}, ResizeBar.mouse); | |||||
ResizeBar.mouseRelease = Object.assign({}, ResizeBar.mouse); | |||||
ResizeBar.dragging = false; | |||||
ResizeBar.prototype.mouseInside = function () | |||||
{ | |||||
if (!this.isVisibleCondition()) | |||||
return false; | |||||
Done Inline ActionsStill no to these 'compatibility' functions. Let the caller be explicit, always accept an object, or always accept a string elexis: Still no to these 'compatibility' functions. Let the caller be explicit, always accept an… | |||||
let rect = this.object.getComputedSize(); | |||||
switch (this.side) | |||||
{ | |||||
case "left": | |||||
case "right": | |||||
return Math.abs(rect[this.side] - ResizeBar.mouse.x) <= this.halfWidth && | |||||
rect.top + this.halfWidth < ResizeBar.mouse.y && | |||||
rect.bottom - this.halfWidth > ResizeBar.mouse.y; | |||||
case "top": | |||||
case "bottom": | |||||
return Math.abs(rect[this.side] - ResizeBar.mouse.y) <= this.halfWidth && | |||||
rect.left + this.halfWidth < ResizeBar.mouse.x && | |||||
rect.right - this.halfWidth > ResizeBar.mouse.x; | |||||
Done Inline Actionseach case on a separate line elexis: each case on a separate line | |||||
default: | |||||
warn(`No side by name: "${this.side}"\n` + Error().stack); | |||||
} | |||||
}; | |||||
/** | |||||
* Returns the complementary parameter | |||||
*/ | |||||
Done Inline ActionssideC, side0, sideX don't inform the reader what C 0 X mean elexis: sideC, side0, sideX don't inform the reader what C 0 X mean | |||||
ResizeBar.prototype.sideC = function (side) | |||||
{ | |||||
switch (side ? side : this.side) | |||||
{ | |||||
case "left": return "right"; | |||||
case "right": return "left"; | |||||
case "top": return "bottom"; | |||||
case "bottom": return "top"; | |||||
} | |||||
}; | |||||
/** | |||||
* Returns the opposite parameter | |||||
*/ | |||||
ResizeBar.prototype.sideO = function (side) | |||||
{ | |||||
switch (side ? side : this.side) | |||||
{ | |||||
case "left": return "top"; | |||||
case "right": return "bottom"; | |||||
case "top": return "left"; | |||||
case "bottom": return "right"; | |||||
} | |||||
}; | |||||
/** | |||||
* Returns the opposite and complementary paramter | |||||
*/ | |||||
ResizeBar.prototype.sideX = function (side) | |||||
{ | |||||
return this.sideC(this.sideO()); | |||||
}; | |||||
elexisUnsubmitted Done Inline ActionssideC -> sideComplementary elexis: sideC -> sideComplementary
sideO -> sideOpposite
sideX -> sideOppositeComplementary
(or more… | |||||
ResizeBar.prototype.sideSign = function (side) | |||||
{ | |||||
switch (side ? side : this.side) | |||||
{ | |||||
case "right": | |||||
case "bottom": | |||||
return +1; | |||||
default: | |||||
return -1; | |||||
} | |||||
}; | |||||
ResizeBar.prototype.sideMouse = function (side) | |||||
{ | |||||
switch (side ? side : this.side) | |||||
{ | |||||
case "left": | |||||
case "right": | |||||
return "x"; | |||||
default: | |||||
return "y"; | |||||
} | |||||
}; | |||||
ResizeBar.prototype.viewResizeBar = function () | |||||
{ | |||||
let absPos = this.object.getComputedSize(); | |||||
let iSize = GUISize(); | |||||
iSize[this.side] = absPos[this.side]; | |||||
iSize[this.sideC()] = absPos[this.side]; | |||||
iSize[this.sideO()] = absPos[this.sideO()]; | |||||
iSize[this.sideX()] = absPos[this.sideX()]; | |||||
let fSize = Object.assign({}, iSize); // Copy | |||||
elexisUnsubmitted Done Inline ActionsScriptInterface.cpp provides a clone() function elexis: ScriptInterface.cpp provides a `clone()` function | |||||
fSize[this.side] = iSize[this.side] + this.sideSign() * this.halfWidth; | |||||
fSize[this.sideC()] = iSize[this.side] - this.sideSign() * this.halfWidth; | |||||
animateObject(ResizeBar.bar, { | |||||
"onStart": bar => | |||||
{ | |||||
bar.size = iSize; | |||||
bar.hidden = false; | |||||
}, | |||||
"size": fSize | |||||
}); | |||||
}; | |||||
Done Inline Actionsone statement per line, bar => {\n... elexis: one statement per line, bar => {\n...
statements end with a semicolon: `hidden = true;\n}` | |||||
ResizeBar.prototype.hideResizeBar = function () | |||||
{ | |||||
let iSize = ResizeBar.bar.size; | |||||
let centerPos = (iSize[this.side] + iSize[this.sideC()]) * 0.5; | |||||
iSize[this.side] = centerPos; | |||||
iSize[this.sideC()] = centerPos; | |||||
animateObject(ResizeBar.bar, { | |||||
"size": iSize, | |||||
"onComplete": bar => bar.hidden = true | |||||
Done Inline Actionsone statement per line if (x)\n elexis: one statement per line `if (x)\n` | |||||
}); | |||||
}; | |||||
ResizeBar.prototype.tick = function () | |||||
{ | |||||
if (!ResizeBar.dragging) | |||||
Done Inline Actionsno else after return elexis: no else after return | |||||
{ | |||||
let currentIn = this.mouseInside(); | |||||
// Double check needed for bars intersection case (first added will have priority) | |||||
if (this.lastIn == currentIn && !ResizeBar.bar.hidden) | |||||
return; | |||||
if (currentIn) | |||||
this.viewResizeBar(); | |||||
else | |||||
this.hideResizeBar(); | |||||
this.lastIn = currentIn; | |||||
return currentIn; | |||||
} | |||||
if (this.dragging) | |||||
{ | |||||
let size = ResizeBar.bar.size; | |||||
size[this.side] = ResizeBar.mouse[this.sideMouse()] + this.sideSign() * this.halfWidth; | |||||
size[this.sideC()] = ResizeBar.mouse[this.sideMouse()] - this.sideSign() * this.halfWidth; | |||||
ResizeBar.bar.size = size; | |||||
} | |||||
}; | |||||
ResizeBar.prototype.dragUp = function () | |||||
{ | |||||
if (!this.mouseInside()) | |||||
return false; | |||||
ResizeBar.mousePress.set(ResizeBar.mouse); | |||||
ResizeBar.dragging = true; | |||||
this.dragging = true; | |||||
return true; | |||||
}; | |||||
ResizeBar.prototype.dragDown = function () | |||||
{ | |||||
if (!this.dragging) | |||||
return false; | |||||
ResizeBar.mouseRelease.set(ResizeBar.mouse); | |||||
let displacement = ResizeBar.mouseRelease[this.sideMouse()] - ResizeBar.mousePress[this.sideMouse()]; | |||||
if (displacement) | |||||
{ | |||||
animateObject( | |||||
this.object, | |||||
{ | |||||
"size": { [this.side]: this.object.size[this.side] + displacement }, | |||||
"onComplete": this.onDragDown | |||||
} | |||||
); | |||||
for (let [object, side] of this.objectsHooked) | |||||
animateObject( | |||||
object, | |||||
{ "size": { [side]: object.size[side] + displacement } } | |||||
Done Inline ActionsA resizeBar and a ResizeBar object sound confusing. elexis: A `resizeBar` and a `ResizeBar` object sound confusing. | |||||
Done Inline Actionsnaming checks nani: naming checks | |||||
); | |||||
} | |||||
ResizeBar.dragging = false; | |||||
this.dragging = false; | |||||
return true; | |||||
}; | |||||
/** | |||||
* Creates a resize bar for an GUIObject. | |||||
* | |||||
* Example: | |||||
* | |||||
* resizeBar( | |||||
* Engine.GetGUIObjectByName("chatPanel"), | |||||
* "left", | |||||
* 20, | |||||
* [ | |||||
* [Engine.GetGUIObjectByName("profilePanel"), "right"], | |||||
* [Engine.GetGUIObjectByName("leftButtonPanel"), "right"], | |||||
* ], | |||||
* () => !Engine.GetGUIObjectByName("profilePanel").hidden, | |||||
* () => warn("chatPanel resized") | |||||
* ); | |||||
* | |||||
* @param {GUIObject} object Main XML object to resize. | |||||
Done Inline Actionsif? elexis: if? | |||||
* @param {String} side Object side to resize: "left", "right", "top", "bottom". | |||||
* @param {Number} width Resize bar width, undefined value will assign the | |||||
* default width. | |||||
* @param {Array[]} [objectsHooked] Other XML objects that will also resize with | |||||
* the main XML object. | |||||
* In the form of [[GUIObject1,side1],[GUIObject2,side2],...] | |||||
* @param {Function} [isVisibleCondition] Condition that makes the resize bar | |||||
* visible/enabled if it returns true. By default the condition is the property | |||||
* hidden of the main XML object, visible/enabled if not hidden. | |||||
* @param {Function} [onDragDown] Call executed after dragging down the bar. | |||||
*/ | |||||
var resizeBar = function (object, side, width, objectsHooked, isVisibleCondition, onDragDown) | |||||
{ | |||||
resizeBar.list.push(new ResizeBar(object, side, width, objectsHooked, isVisibleCondition, onDragDown)); | |||||
}; | |||||
resizeBar.list = []; | |||||
resizeBar.onEvent = function (ev) | |||||
{ | |||||
if (resizeBar.firstTick) | |||||
return; | |||||
switch (ev.type) | |||||
{ | |||||
case "mousebuttondown": | |||||
for (let bar of resizeBar.list) | |||||
if (bar.dragUp()) break; | |||||
break; | |||||
case "mousebuttonup": | |||||
for (let bar of resizeBar.list) | |||||
if (bar.dragDown()) break; | |||||
break; | |||||
case "mousemotion": | |||||
ResizeBar.mouse.set(ev) | |||||
break; | |||||
elexisUnsubmitted Done Inline Actions-1 tab Coding_Conventions elexis: -1 tab Trac:Coding_Conventions | |||||
} | |||||
} | |||||
resizeBar.firstTick = true; | |||||
resizeBar.onTick = function () | |||||
{ | |||||
// Don't process anything at the first tick (error otherwise) | |||||
if (resizeBar.firstTick) | |||||
{ | |||||
resizeBar.firstTick = false; | |||||
ResizeBar.bar = Engine.GetGUIObjectByName("glResizeBar"); | |||||
return; | |||||
} | |||||
for (let bar of resizeBar.list) | |||||
if (bar.tick()) break; | |||||
} | |||||
/* THIS NEEDS TO BE ADDED ON FILES WHERE YOU WANT TO USE IT | |||||
function handleInputBeforeGui(ev) | |||||
{ | |||||
resizeBar.onEvent(ev); | |||||
return false; | |||||
} | |||||
function onTick(){ | |||||
resizeBar.onTick(); | |||||
} | |||||
*/ |
Wildfire Games · Phabricator
(still no .0)