BBTimeline/bbtimeline-foreground-canva...

210 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-10-18 13:44:49 +00:00
class BBTimelineForegroundCanvas extends BBTimelineCanvas {
constructor(parentEl, el) {
super(parentEl, el);
}
initialiseOptions() {
super.initialiseOptions();
2023-10-18 13:44:49 +00:00
const a = this;
a.Options = {
Marker: {
BorderColour: "#3A5D9C",
BorderWidth: 2,
BackColour: "#D4DEEF",
Width: 10
},
Label: {
Colour: "#3A5D9C",
Font: "9pt Arial",
Margin: {
X: 2,
Y: 10
},
Line: {
Colour: "#A6A6A6",
Width: 1,
}
}
};
2023-10-18 13:44:49 +00:00
}
initialiseComponents() {
super.initialiseComponents();
const a = this;
2023-10-19 23:22:29 +00:00
a.CTX.canvas.addEventListener('mousedown', function (e) {
if (!a.Parent.Enabled) {
return;
}
var event = a.Parent.FindEventsByCoords(e.offsetX, e.offsetY, false);
2023-10-19 23:22:29 +00:00
if (event == null) {
return;
}
if (a.Parent.Debug) console.log(event);
a.Parent.OnMouseDown(this, e, event);
});
a.CTX.canvas.addEventListener('click', function (e) {
if (!a.Parent.Enabled) {
return;
}
var event = a.Parent.FindEventsByCoords(e.offsetX, e.offsetY, false);
2023-10-19 23:22:29 +00:00
if (event == null) {
return;
}
if (a.Parent.Debug) console.log(event);
a.Parent.OnClick(this, e, event);
});
a.CTX.canvas.addEventListener('dblclick', function (e) {
if (!a.Parent.Enabled) {
return;
}
var event = a.Parent.FindEventsByCoords(e.offsetX, e.offsetY, false);
2023-10-19 23:22:29 +00:00
if (event == null) {
return;
}
if (a.Parent.Debug) console.log(event);
a.Parent.OnDblClick(this, e, event);
});
if (a.Parent.EnableHotTracking) {
a.CTX.canvas.addEventListener('mousemove', function (e) {
if (!a.Parent.Enabled) {
return;
}
var event = a.Parent.FindEventsByCoords(e.offsetX, e.offsetY, false);
if (event != null) {
a.Container.style.cursor = 'pointer';
} else {
a.Container.style.cursor = 'default';
2023-10-19 23:22:29 +00:00
}
// Hottracking
if (a.Parent.EnableHotTracking) {
const point = { X: e.offsetX, Y: e.offsetY };
if (a.isPointInRectangle(a.Parent.Layer.Background.GraphRectangle, point)) {
if (a.Parent.Debug) console.log(point);
2023-10-19 23:22:29 +00:00
a.Parent.Layer.Flourish.XPos = point.X;
2023-10-19 23:22:29 +00:00
a.Parent.OnMouseMove(this, e, event);
} else {
// Clear hot tracking
a.Parent.Layer.Flourish.XPos = -1;
}
a.Parent.Layer.Flourish.Invalidate();
2023-10-19 23:22:29 +00:00
}
2023-10-19 23:22:29 +00:00
});
}
2023-10-18 13:44:49 +00:00
a.Invalidate();
}
Invalidate() {
const a = this;
const rect = a.Parent.Layer.Background.GraphRectangle;
a.Clear();
2023-10-23 00:21:53 +00:00
let startPosY = 0;
2023-10-19 23:22:29 +00:00
const visibleEvents = a.Parent.VisibleEvents;
2023-10-23 00:21:53 +00:00
if (a.Parent.XAxis.Position == 'top') {
startPosY = (rect.Y + a.Options.Marker.Width + 20);
2023-10-23 00:21:53 +00:00
} else {
startPosY = (rect.Y + a.Options.Marker.Width);
2023-10-23 00:21:53 +00:00
}
// Clear for collisions detection
visibleEvents.forEach(function (e, i) {
e.HitBox = { X: 0, Y:0, W: 0, H: 0};
});
2023-10-18 13:44:49 +00:00
visibleEvents.forEach(function (e, i) {
// Calculate Y position
let posY = a.calcMarkerPosition(e.Position.X, startPosY);
2023-10-23 00:21:53 +00:00
let posY2 = 0;
if (a.Parent.XAxis.Position == 'top') {
posY2 = a.Parent.Layer.Background.GraphRectangle.Y;
} else {
posY2 = (a.Parent.Layer.Background.GraphRectangle.Y + a.Parent.Layer.Background.GraphRectangle.H);
}
2023-10-18 13:44:49 +00:00
if (a.Options.Label.Line.Width > 0) {
a.drawVerticalLine(e.Position.X, posY, posY2, a.Options.Label.Line.Width, a.Options.Label.Line.Colour);
}
2023-10-18 13:44:49 +00:00
const markerRectangle = a.drawCircle(e.Position.X, posY, a.Options.Marker.Width, a.Options.Marker.BorderWidth, e.BorderColour, e.BackColour);
2023-10-18 13:44:49 +00:00
e.Position = { X: e.Position.X, Y: posY };
if (a.Parent.ShowMarkerLabel) {
const labelRectangle = a.drawText((markerRectangle.X + markerRectangle.W + a.Options.Label.Margin.X), markerRectangle.Y, e.Label, a.Options.Label.Font, a.Options.Label.Colour, "left");
e.HitBox = a.combineRectangle(markerRectangle, labelRectangle);
} else {
e.HitBox = markerRectangle;
}
2023-10-18 13:44:49 +00:00
if (a.Parent.Debug) a.drawRectangle(e.HitBox, "red");
if (a.Parent.Debug) console.log(e);
});
}
2023-10-19 23:22:29 +00:00
OnMouseDown(e) {
if (!a.Parent.Enabled) {
return;
}
var event = a.Parent.FindEventsByCoords(e.offsetX, e.offsetY, false);
2023-10-19 23:22:29 +00:00
if (event == null) {
return;
}
if (a.Parent.Debug) console.log(event);
a.Parent.OnMouseDown(this, e, event);
}
2023-10-18 13:44:49 +00:00
calcMarkerPosition(x, y) {
const a = this;
const rect = a.Parent.Layer.Background.GraphRectangle;
let hasMoved = false;
let posY = y;
for (let i=rect.Y; i<(rect.Y + rect.H); i++)
2023-10-18 13:44:49 +00:00
{
posY = i;
2023-10-18 13:44:49 +00:00
var clippedEvent = a.Parent.FindEventsByCoords(x, posY, true);
2023-10-18 13:44:49 +00:00
if (clippedEvent == null) {
hasMoved = true;
break;
}
}
if (!hasMoved) {
posY = y;
}
return posY;
}
}