2017-12-27 19:51:54 +00:00
|
|
|
# UIGroup.nas. A group of UI Elements that can be scrolled through
|
|
|
|
var GroupElement =
|
|
|
|
{
|
|
|
|
|
2017-12-31 16:10:52 +00:00
|
|
|
new : func (pageName, svg, elementNames, size, highlightElement, arrow=0, scrollTroughElement=nil, scrollThumbElement=nil, scrollHeight=0, style=nil)
|
2017-12-27 19:51:54 +00:00
|
|
|
{
|
|
|
|
var obj = {
|
|
|
|
parents : [ GroupElement ],
|
|
|
|
_pageName : pageName,
|
|
|
|
_svg : svg,
|
|
|
|
_style : style,
|
2017-12-31 16:10:52 +00:00
|
|
|
_scrollTroughElement : nil,
|
|
|
|
_scrollThumbElement : nil,
|
|
|
|
_scrollBaseTransform : nil,
|
2017-12-27 19:51:54 +00:00
|
|
|
|
|
|
|
# A hash mapping keys to the element name prefix of an SVG element
|
|
|
|
_elementNames : elementNames,
|
|
|
|
|
|
|
|
# The size of the group. For each of the ._elementNames hash values there
|
|
|
|
# must be an SVG Element [pageName][elementName]{0...pageSize}
|
|
|
|
_size : size,
|
|
|
|
|
|
|
|
# Current size of the selectable elements.
|
|
|
|
_currentSize : 0,
|
|
|
|
|
|
|
|
# ElementName to be highlighted. Must be an hash value from ._elementNames
|
|
|
|
_highlightElement : highlightElement,
|
|
|
|
|
|
|
|
# Whether this is an arrow'd list. If so then highlightElement will be
|
|
|
|
# hidden/shown for highlighting purposes.
|
|
|
|
_arrow : arrow,
|
|
|
|
|
2017-12-31 16:10:52 +00:00
|
|
|
# Length of the scroll bar.
|
|
|
|
_scrollHeight : scrollHeight,
|
|
|
|
|
|
|
|
# List of values to display
|
|
|
|
_values : [],
|
|
|
|
|
|
|
|
# List of SVG elements to display the values
|
2017-12-27 19:51:54 +00:00
|
|
|
_elements : [],
|
2017-12-31 16:10:52 +00:00
|
|
|
|
|
|
|
# Cursor index into the elements array
|
|
|
|
_crsrIndex : -1,
|
|
|
|
|
|
|
|
# Page index
|
|
|
|
_pageIndex : 0,
|
2017-12-27 19:51:54 +00:00
|
|
|
};
|
|
|
|
|
2017-12-31 16:10:52 +00:00
|
|
|
# Optional scroll bar elements, consisting of the Thumb and the Trough *,
|
|
|
|
# which will be used to display the scroll position.
|
|
|
|
# * Yes, these are the terms of art for the elements.
|
|
|
|
assert(((scrollTroughElement == nil) and (scrollThumbElement == nil)) or
|
|
|
|
((scrollTroughElement != nil) and (scrollThumbElement != nil)),
|
|
|
|
"Both the scroll trough element and the scroll thumb element must be defined, or neither");
|
|
|
|
|
|
|
|
if (scrollTroughElement != nil) {
|
|
|
|
obj._scrollTroughElement = svg.getElementById(pageName ~ scrollTroughElement);
|
|
|
|
assert(obj._scrollTroughElement != nil, "Unable to find scroll element " ~ pageName ~ scrollTroughElement);
|
|
|
|
}
|
|
|
|
if (scrollThumbElement != nil) {
|
|
|
|
obj._scrollThumbElement = svg.getElementById(pageName ~ scrollThumbElement);
|
|
|
|
assert(obj._scrollThumbElement != nil, "Unable to find scroll element " ~ pageName ~ scrollThumbElement);
|
|
|
|
obj._scrollBaseTransform = obj._scrollThumbElement.getTranslation();
|
|
|
|
}
|
|
|
|
|
2017-12-27 19:51:54 +00:00
|
|
|
if (style == nil) obj._style = PFD.DefaultStyle;
|
|
|
|
|
|
|
|
for (var i = 0; i < size; i = i + 1) {
|
|
|
|
if (obj._arrow == 1) {
|
2018-01-03 20:24:41 +00:00
|
|
|
append(obj._elements, PFD.HighlightElement.new(pageName, svg, highlightElement ~ i, i, obj._style));
|
2017-12-27 19:51:54 +00:00
|
|
|
} else {
|
|
|
|
append(obj._elements, PFD.TextElement.new(pageName, svg, highlightElement ~ i, i, obj._style));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
# Set the values of the group. values_array is an array of hashes, each of which
|
|
|
|
# has keys that match those of ._elementNames
|
|
|
|
setValues : func (values_array) {
|
2017-12-31 16:10:52 +00:00
|
|
|
me._values = values_array;
|
|
|
|
me._pageIndex = 0;
|
|
|
|
me._crsrIndex = 0;
|
2017-12-27 19:51:54 +00:00
|
|
|
|
2017-12-31 16:10:52 +00:00
|
|
|
if (size(me._values) > me._size) {
|
|
|
|
# Number of elements exceeds our ability to display them, so enable
|
|
|
|
# the scroll bar.
|
|
|
|
me._scrollThumbElement.setVisible(1);
|
|
|
|
me._scrollTroughElement.setVisible(1);
|
|
|
|
} else {
|
|
|
|
# There is no scrolling to do, so hide the scrollbar.
|
|
|
|
me._scrollThumbElement.setVisible(0);
|
|
|
|
me._scrollTroughElement.setVisible(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
me.displayPage();
|
|
|
|
},
|
|
|
|
|
|
|
|
nextPage : func() {
|
|
|
|
if (size(me._values) > ((me._pageIndex +1) * me._size)) {
|
|
|
|
me._pageIndex = me._pageIndex + 1;
|
|
|
|
me._crsrIndex = 0;
|
|
|
|
me.displayPage();
|
|
|
|
} else {
|
|
|
|
me._crsrIndex = me._currentSize -1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
previousPage : func() {
|
|
|
|
if (me._pageIndex > 0) {
|
|
|
|
me._pageIndex = me._pageIndex - 1;
|
|
|
|
me._crsrIndex = me._size -1;
|
|
|
|
me.displayPage();
|
|
|
|
} else {
|
|
|
|
me._crsrIndex = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
displayPage : func () {
|
|
|
|
# Determine how many elements to display in this page
|
|
|
|
me._currentSize = math.min(me._size, size(me._values) - me._size * me._pageIndex);
|
2017-12-27 19:51:54 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < me._currentSize; i = i + 1) {
|
2017-12-31 16:10:52 +00:00
|
|
|
var value = me._values[i + me._size * me._pageIndex];
|
|
|
|
foreach (var k; keys(value)) {
|
2017-12-27 19:51:54 +00:00
|
|
|
if (k == me._highlightElement) {
|
2017-12-31 16:10:52 +00:00
|
|
|
me._elements[i].setVisible(1);
|
2017-12-27 19:51:54 +00:00
|
|
|
me._elements[i].unhighlightElement();
|
2017-12-31 16:10:52 +00:00
|
|
|
me._elements[i].setValue(value[k]);
|
2017-12-27 19:51:54 +00:00
|
|
|
} else {
|
|
|
|
var name = me._pageName ~ k ~ i;
|
|
|
|
var element = me._svg.getElementById(name);
|
|
|
|
assert(element != nil, "Unable to find element " ~ name);
|
|
|
|
element.setVisible(1);
|
2017-12-31 16:10:52 +00:00
|
|
|
element.setText(value[k]);
|
2017-12-27 19:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Hide any further elements
|
|
|
|
if (me._currentSize < me._size) {
|
|
|
|
for (var i = me._currentSize; i < me._size; i = i + 1) {
|
|
|
|
foreach (var k; me._elementNames) {
|
|
|
|
if (k == me._highlightElement) {
|
|
|
|
me._elements[i].setVisible(0);
|
2017-12-31 16:10:52 +00:00
|
|
|
me._elements[i].setValue("");
|
2017-12-27 19:51:54 +00:00
|
|
|
} else {
|
2017-12-31 16:10:52 +00:00
|
|
|
var name = me._pageName ~ k ~ i;
|
|
|
|
var element = me._svg.getElementById(name);
|
|
|
|
assert(element != nil, "Unable to find element " ~ name);
|
|
|
|
element.setVisible(0);
|
|
|
|
element.setText("");
|
2017-12-27 19:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 16:10:52 +00:00
|
|
|
if ((me._scrollThumbElement != nil) and (me._size < size(me._values))) {
|
|
|
|
# Shift the scrollbar if it's relevant
|
|
|
|
var numScrollPositions = math.ceil(size(me._values) / me._size) -1;
|
|
|
|
me._scrollThumbElement.setTranslation([
|
|
|
|
me._scrollBaseTransform[0],
|
|
|
|
me._scrollBaseTransform[1] + me._scrollHeight * (me._pageIndex / numScrollPositions)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
2017-12-27 19:51:54 +00:00
|
|
|
|
|
|
|
# Methods to add dynamic elements to the group. Must be called in the
|
|
|
|
# scroll order, as they are simply appended to the end of the list of elements!
|
2018-01-03 20:24:41 +00:00
|
|
|
addHighlightElement : func(name, value) {
|
|
|
|
append(me._elements, HighlightElement.new(me._pageName, me._svg, name, value));
|
2017-12-27 19:51:54 +00:00
|
|
|
},
|
|
|
|
addTextElement : func(name, value) {
|
|
|
|
append(me._elements, TextElement.new(me._pageName, me._svg, name, value));
|
|
|
|
},
|
|
|
|
|
|
|
|
showCRSR : func() {
|
|
|
|
if (me._currentSize == 0) return;
|
|
|
|
me._crsrIndex = 0;
|
|
|
|
me._elements[me._crsrIndex].highlightElement();
|
|
|
|
},
|
|
|
|
hideCRSR : func() {
|
|
|
|
if (me._crsrIndex == -1) return;
|
|
|
|
me._elements[me._crsrIndex].unhighlightElement();
|
|
|
|
me.setCRSR(-1);
|
|
|
|
},
|
|
|
|
setCRSR : func(index) {
|
|
|
|
me._crsrIndex = math.min(index, me._currentSize -1);
|
|
|
|
},
|
|
|
|
getCursorElementName : func() {
|
|
|
|
if (me._crsrIndex == -1) return nil;
|
|
|
|
return me._elements[me._crsrIndex].name;
|
|
|
|
},
|
|
|
|
isCursorOnDataEntryElement : func() {
|
|
|
|
if (me._crsrIndex == -1) return 0;
|
|
|
|
return isa(me._elements[me._crsrIndex], DataEntryElement);
|
|
|
|
},
|
|
|
|
enterElement : func() {
|
|
|
|
if (me._crsrIndex == -1) return;
|
|
|
|
return me._elements[me._crsrIndex].enterElement();
|
|
|
|
},
|
|
|
|
getValue : func() {
|
|
|
|
if (me._crsrIndex == -1) return nil;
|
|
|
|
return me._elements[me._crsrIndex].getValue();
|
|
|
|
},
|
|
|
|
clearElement : func() {
|
|
|
|
if (me._crsrIndex == -1) return;
|
|
|
|
me._elements[me._crsrIndex].clearElement();
|
|
|
|
},
|
|
|
|
incrSmall : func(value) {
|
|
|
|
if (me._crsrIndex == -1) return;
|
2017-12-31 16:10:52 +00:00
|
|
|
|
2017-12-27 19:51:54 +00:00
|
|
|
var incr_or_decr = (value > 0) ? 1 : -1;
|
|
|
|
if (me._elements[me._crsrIndex].isInEdit()) {
|
|
|
|
# We're editing, so pass to the element.
|
|
|
|
#print("Moving cursor to next character entry");
|
|
|
|
me._elements[me._crsrIndex].incrSmall();
|
|
|
|
} else {
|
|
|
|
# Move to next selection element
|
|
|
|
me._elements[me._crsrIndex].unhighlightElement();
|
2017-12-31 16:10:52 +00:00
|
|
|
|
|
|
|
me._crsrIndex = me._crsrIndex + incr_or_decr;
|
|
|
|
|
|
|
|
if (me._crsrIndex < 0 ) me.previousPage();
|
|
|
|
if (me._crsrIndex == me._currentSize) me.nextPage();
|
|
|
|
|
2017-12-27 19:51:54 +00:00
|
|
|
me._elements[me._crsrIndex].highlightElement();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
incrLarge : func(val) {
|
|
|
|
if (me._crsrIndex == -1) return;
|
|
|
|
var incr_or_decr = (val > 0) ? 1 : -1;
|
|
|
|
if (me._elements[me._crsrIndex].isInEdit()) {
|
|
|
|
# We're editing, so pass to the element.
|
|
|
|
#print("Moving cursor to next character entry");
|
|
|
|
me._elements[me._crsrIndex].incrLarge();
|
|
|
|
} else {
|
|
|
|
# Move to next selection element
|
|
|
|
me._elements[me._crsrIndex].unhighlightElement();
|
2017-12-31 16:10:52 +00:00
|
|
|
|
|
|
|
me._crsrIndex = me._crsrIndex + incr_or_decr;
|
|
|
|
|
|
|
|
if (me._crsrIndex < 0 ) me.previousPage();
|
|
|
|
if (me._crsrIndex == me._currentSize) me.nextPage();
|
|
|
|
|
2017-12-27 19:51:54 +00:00
|
|
|
me._elements[me._crsrIndex].highlightElement();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|