| 1 |
"use strict"; |
| 2 |
/* |
| 3 |
Widget management code. |
| 4 |
|
| 5 |
Typically, widget management is done with a tightly siloed hierarchy, with |
| 6 |
widgets in containers, which are in containers, etc. There is a hierarchy |
| 7 |
like that here (though rather flat since I don't need much), but there's also |
| 8 |
a global registry of widgets alongside the hierarchy. This makes widgets |
| 9 |
easier to work with for the author of the latex document. |
| 10 |
|
| 11 |
I've also avoided any kind of clever abstraction around the widget concept. |
| 12 |
This can mean that there's a certain amount of boilerplate (not DRY), but |
| 13 |
it also means that things aren't tangled up. Widgets can be individually |
| 14 |
modified without worries about side-effects. |
| 15 |
|
| 16 |
All widgets use a register() method rather than a constructor. This |
| 17 |
is because the user should be able to specify a widget repeatedly |
| 18 |
without actually creating a new one every time it's specified. |
| 19 |
|
| 20 |
So, the user should say something like |
| 21 |
let w = RandomWidgetType.register(arguments); |
| 22 |
to create a widget from the drawing code. If register() has never been |
| 23 |
called for the particular widget, then a new widget *is* created and |
| 24 |
a reference to it is placed in global storage. If this widget was created |
| 25 |
earlier, then the reference to it is taken from global storage and returned. |
| 26 |
So, register() is something like an object factory, but it won't make the |
| 27 |
same object more than once. |
| 28 |
|
| 29 |
Every widget is distinguished by its type and the (name of the) figure it |
| 30 |
belongs to. In addition, if a figure has several widgets of the same type, |
| 31 |
then the user must provide an optional name for that particular widget. For |
| 32 |
example, if there are three ButtonWidgets for a given figure, then they |
| 33 |
might be created by |
| 34 |
let b1 = ButtonWidget.register(whatever,"first"); |
| 35 |
let b2 = ButtonWidget.register(whatever,"second"); |
| 36 |
let b3 = ButtonWidget.register(whatever,"third"); |
| 37 |
|
| 38 |
------------------------- |
| 39 |
|
| 40 |
One of the ticklish issues is how to associate widgets with their |
| 41 |
figures. In most languages, this problem is solved by explicitly using |
| 42 |
"this" somehow. In Java, you might say something like |
| 43 |
new Widget(this); |
| 44 |
to indicate that the owner of the widget is the class from which the widget |
| 45 |
was constructed. I would rather not do that because it's the kind of |
| 46 |
boilerplate arcana that the user shouldn't have to think about. |
| 47 |
|
| 48 |
JS provides a couple of ways to determine who made a call to a particular |
| 49 |
function. The easiest way is like this |
| 50 |
|
| 51 |
function example() { |
| 52 |
let caller = example.caller.name; |
| 53 |
console.log(caller); |
| 54 |
} |
| 55 |
|
| 56 |
This should print the name of the function that invoked example(). |
| 57 |
|
| 58 |
Another way is very easy to do, but it's been deprecated. See |
| 59 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller |
| 60 |
Even though it is deprecated, every browser supports it, according to the |
| 61 |
above link. Also, my guess is that this is very commonly used since |
| 62 |
it's so handy. They won't be getting rid of it any time soon. |
| 63 |
|
| 64 |
If they do get rid of caller.name, then another way to obtain this |
| 65 |
information is by creating a bogus Error and examining the call stack: |
| 66 |
|
| 67 |
function example() { |
| 68 |
|
| 69 |
let stack = new Error().stack; |
| 70 |
let caller = stack.split('\n')[1].trim(); |
| 71 |
|
| 72 |
// caller is now inforamtion about the function calling this one, |
| 73 |
// including the file name and a bunch of other stuff I don't care about. |
| 74 |
// I just want to know the name of the function. |
| 75 |
// It *appears* (but not extensively tested) that the function name |
| 76 |
// occurs before the first '@'.; |
| 77 |
let callingFcn = caller.split('@')[0]; |
| 78 |
console.log(callingFcn); |
| 79 |
} |
| 80 |
|
| 81 |
I wanted to use caller.name, even though it is deprecated, because it is |
| 82 |
so much easier. Unfortunately, it is not allowed in strict mode. |
| 83 |
Since everything that appears within a class is strict, that makes its use |
| 84 |
awkward. I tried to get around this by defining a top-level function (outside |
| 85 |
any class) that does nothing but return the value I want, but that doesn't |
| 86 |
work either. It seems like, as soon as you enter a class, the data you want |
| 87 |
is stripped off (or something). Also, now that I've moved to TypeScript any |
| 88 |
violation of "strict" is even harder. Since I'll have to define a seperate class |
| 89 |
anyway, go ahead and use the Error-based approach. |
| 90 |
|
| 91 |
To top it off, the method via generating an Error is sensitive to the |
| 92 |
broswer because different browsers format this information differently. |
| 93 |
|
| 94 |
------------------- |
| 95 |
|
| 96 |
BUG: Future Widgets...? |
| 97 |
|
| 98 |
Because LoopWidget takes such a crazy number of options, it would be nice |
| 99 |
to have several widgets that merely call LoopWidget. This wouldn't really |
| 100 |
be any different, but would simplify things for the user. Basically, these |
| 101 |
would have a reduced set of arguments to register() and just hand everything |
| 102 |
off to LoopWidget. They would be a mere glue skeleton. Same goes for |
| 103 |
OpenAnimWidget. |
| 104 |
|
| 105 |
Image Building Widget |
| 106 |
For something like (say) a Mandelbrot set. This takes time to generate |
| 107 |
and you might want to draw it in an open-ended way. It's not reasonable |
| 108 |
to allow the user to move through time and see the state of the image |
| 109 |
at different time steps. The widget should store an off-screen image |
| 110 |
and making the animation "run faster" would mean calling it more frequently |
| 111 |
so that it can do more calculation. So it would use some of the same |
| 112 |
animation infrastructure, but in a different way. The most that makes |
| 113 |
sense here is a pause/run (to reduce computational load) and something |
| 114 |
to increase or decrease the load. This would often be something for which |
| 115 |
it does not make sense to generate tikz. Any JS rendering of a mandelbrot |
| 116 |
set (say) would be terrible for inclusion in a book. You'd generate a |
| 117 |
printed figure like that in some other way, even if an on-line animation |
| 118 |
would be instructive. |
| 119 |
|
| 120 |
Various restrictions on how a DraggableDot can be moved would be handy. |
| 121 |
Restricting to a particular line or arc wouldn't be hard, but restricting to |
| 122 |
fall on a given Path2D would be hard. The JS implementation of Path2D is |
| 123 |
poor, and I would need to reimplement the entire thing from the |
| 124 |
ground up. I might need to do some of that anyway for the best tikz output |
| 125 |
(I did some stuff along those lines already). |
| 126 |
|
| 127 |
Draggable Line |
| 128 |
Similar to Draggable dot. In fact, it's not clear that this needs its own |
| 129 |
widget. A line is determined by two points, so the user could use a |
| 130 |
draggable dot and just draw the line himself. The only advantage I can |
| 131 |
see to a draggable *line* is that the user could mouse-down on any |
| 132 |
portion of the line. In fact, that could be done with DraggableDot since |
| 133 |
the "dot" could be an entire line. |
| 134 |
|
| 135 |
Scroll bar for number selection. |
| 136 |
Similar to a spinner, but the user drags a dot along a line. |
| 137 |
Not clear whether a numerical value should appear. |
| 138 |
|
| 139 |
Checkboxes and Radio buttons |
| 140 |
Gack |
| 141 |
|
| 142 |
Drop-down menu |
| 143 |
Stuff like this gets more fiddly. If you have a drop-down menu and |
| 144 |
it has a vertical scroll bar for multiple selections, then it's even worse. |
| 145 |
|
| 146 |
*/ |
| 147 |
// There is only one of these, so everything is static. It manages all |
| 148 |
// the widgets in the program. |
| 149 |
class WidgetManager { |
| 150 |
static register(w) { |
| 151 |
this.theList.push(w); |
| 152 |
if (WidgetManager.theWidgets.has(w.betterOwner)) |
| 153 |
WidgetManager.theWidgets.get(w.betterOwner).push(w); |
| 154 |
else |
| 155 |
WidgetManager.theWidgets.set(w.betterOwner, [w]); |
| 156 |
} |
| 157 |
static knownWidget(o, t, n) { |
| 158 |
// If there is a widget with the given owner (o), type (t) and name(n) |
| 159 |
// in theList, then return it; return null otherwise. |
| 160 |
// BUG: Using a string as owner feels particularly bad. |
| 161 |
for (let i = 0; i < this.theList.length; i++) { |
| 162 |
let curW = this.theList[i]; |
| 163 |
if (o !== curW.owner) |
| 164 |
continue; |
| 165 |
if (t !== curW.type) |
| 166 |
continue; |
| 167 |
if (n === curW.name) |
| 168 |
return curW; |
| 169 |
} |
| 170 |
return null; |
| 171 |
} |
| 172 |
static mouseDown(theFig, x, y) { |
| 173 |
// theFig should match one of the Widget object's owner fields. |
| 174 |
// (x,y) is given relative to he origin of the figure. |
| 175 |
if (WidgetManager.theWidgets.has(theFig) === false) |
| 176 |
return; |
| 177 |
// The only thing to do here is to pass the event to each widget and |
| 178 |
// see if it wants it. I suppose that I *could* store the area for |
| 179 |
// each widget, and do an initial test here, but this is easier. |
| 180 |
let wlist = WidgetManager.theWidgets.get(theFig); |
| 181 |
for (let i = 0; i < wlist.length; i++) { |
| 182 |
if (wlist[i].mouseDown(x, y) === true) |
| 183 |
// First come, first serve. |
| 184 |
return; |
| 185 |
} |
| 186 |
} |
| 187 |
static mouseMove(theFig, x, y) { |
| 188 |
// As above. |
| 189 |
if (WidgetManager.mouseOwner === null) |
| 190 |
return; |
| 191 |
if (WidgetManager.theWidgets.has(theFig) === false) |
| 192 |
return; |
| 193 |
// We only accept mouse moves if the mouse is on the figure that |
| 194 |
// "owns" the mouse event. |
| 195 |
if (WidgetManager.mouseOwner.betterOwner !== theFig) |
| 196 |
return; |
| 197 |
WidgetManager.mouseOwner.mouseMove(x, y); |
| 198 |
} |
| 199 |
static mouseUp(theFig, x, y) { |
| 200 |
// As above. |
| 201 |
if (WidgetManager.mouseOwner === null) |
| 202 |
return; |
| 203 |
if (WidgetManager.theWidgets.has(theFig) === false) |
| 204 |
return; |
| 205 |
if (WidgetManager.mouseOwner.betterOwner !== theFig) |
| 206 |
// Mouse was released over *a* figure, but not the figure with |
| 207 |
// the owning widget. Tell the correct widget about the release, using |
| 208 |
// bogus coordinates so that the mouse-up is sure to be off the widget. |
| 209 |
WidgetManager.mouseOwner.mouseUp(10000000000000, 10000000000000); |
| 210 |
//console.log("up up"); |
| 211 |
WidgetManager.mouseOwner.mouseUp(x, y); |
| 212 |
} |
| 213 |
} |
| 214 |
// All the widgets known to the program. |
| 215 |
// Each of these is an instance of the Widget class. |
| 216 |
// BUG: I could get rid of this and just use theWidgets. It's redundant, |
| 217 |
// and theWidgets is actually easier. |
| 218 |
// BUG: Yes, get rid of this. |
| 219 |
WidgetManager.theList = []; |
| 220 |
// Also a complete list of all widgets, but indexed by the figure |
| 221 |
// to which the widget belongs. This makes it easier to pass events |
| 222 |
// to the proper recipent. So this is a hash map taking a figure name |
| 223 |
// (as a string) to an array of Widget objects. |
| 224 |
WidgetManager.theWidgets = new Map(); |
| 225 |
// This is BOGUS. Certain path operations require a CanvasRenderingContext2D, |
| 226 |
// even when the question is one of pure abstract geometry, like |
| 227 |
// isPointInPath(), and the only way to get one of these is from a canvas. |
| 228 |
// Using the visible canvas and ctx for this is prone to all kinds of misuse |
| 229 |
// and mistakes, so create a bogus ctx here. |
| 230 |
// BUG: The long-term solution is not to rely on js for this at all. Write |
| 231 |
// my own code for Bezier curves and the like. I'm partway there already. |
| 232 |
WidgetManager.bogusCanvas = document.createElement('canvas'); |
| 233 |
WidgetManager.bogusCtx = WidgetManager.bogusCanvas.getContext('2d'); |
| 234 |
// Whether a widget "owns" a recent mouse-down event. This is needed |
| 235 |
// for things like dragging. If this is null, then the next mouse-down |
| 236 |
// is up for grabs. Otherwise, it points to the relevant Widget object. |
| 237 |
WidgetManager.mouseOwner = null; |
| 238 |
// This isn't being used yet, but it will be needed to handle keyboard |
| 239 |
// events. The idea is that a widget ""takes ownership" of a mouse-down |
| 240 |
// with mouseOwner, and that widget is the same one that has focus for |
| 241 |
// any future keyboard events too. The only real difference is that |
| 242 |
// focusOwner is "stickier." This goes to null whenever a user clicks |
| 243 |
// on something other than a widget. |
| 244 |
WidgetManager.focusOwner = null; |
| 245 |
function getCaller() { |
| 246 |
// Irritating function to get around strict mode. See the comment at the |
| 247 |
// top of the file. I want the caller of the thing that calls this. |
| 248 |
// |
| 249 |
// BUG: I was never entirely happy with this, and now that it depends |
| 250 |
// on the particular browser, I am even less happy about it. It's a |
| 251 |
// question of the lesser of two evils: this function or requiring |
| 252 |
// the user to provide a boilerplate 'this' or the like. |
| 253 |
let stack = new Error().stack; |
| 254 |
// The exact format of the information in stack depends on the particular |
| 255 |
// browser. Firefox produces something like this: |
| 256 |
// |
| 257 |
// getCaller@http://localhost:8000/widgets.js:375:15 |
| 258 |
// register@http://localhost:8000/widgets.js:1557:18 |
| 259 |
// geartest01@http://localhost:8000/geartest01.js:142:26 |
| 260 |
// render@http://localhost:8000/layout.js:628:11 |
| 261 |
// renderFrame@http://localhost:8000/widgets.js:538:13 |
| 262 |
// etc. |
| 263 |
// |
| 264 |
// While MS Edge/Chrome produces |
| 265 |
// |
| 266 |
// Error |
| 267 |
// at getCaller (widgets.js:375:15) |
| 268 |
// at Function.register (widgets.js:1557:18) |
| 269 |
// at geartest01 (geartest01.js:142:26) |
| 270 |
// at FigurePanel.render (layout.js:628:5) |
| 271 |
// etc. |
| 272 |
// |
| 273 |
// Depending on the Browser, stack needs to be parsed differently. |
| 274 |
// theBrowser was defined in main.js. |
| 275 |
// The digit value (e.g., '2') indicates how many steps back in the the |
| 276 |
// stack to go. |
| 277 |
if (theBrowser === "Firefox") { |
| 278 |
let caller = stack.split('\n')[2].trim(); |
| 279 |
// caller is now information about a function in the call stack, |
| 280 |
// including the file name and a bunch of other stuff I don't care about. |
| 281 |
// I just want to know the name of the function. |
| 282 |
// The function name occurs before the first '@'. |
| 283 |
let callingFcn = caller.split('@')[0]; |
| 284 |
return callingFcn; |
| 285 |
} |
| 286 |
else if (theBrowser === "Chrome") { |
| 287 |
let caller = stack.split('\n')[3].trim(); |
| 288 |
// Here, space-deliminting works better. |
| 289 |
let callingFcn = caller.split(' ')[1]; |
| 290 |
// But this may return something like 'FigurePanel.bezier'; what I want |
| 291 |
// is just 'bezier'. |
| 292 |
if (callingFcn.indexOf('.') > -1) |
| 293 |
callingFcn = callingFcn.split('.')[1]; |
| 294 |
return callingFcn; |
| 295 |
} |
| 296 |
else { |
| 297 |
console.log("IMPOSSIBLE ERROR DUE TO UNKNOWN BROWSER TYPE!!!"); |
| 298 |
return ""; |
| 299 |
} |
| 300 |
} |
| 301 |
// Base class for all widgets. No code outside this file should |
| 302 |
// ever access this class directly. It's not abstract because there |
| 303 |
// are certain actions common to every case in the constructor. |
| 304 |
class Widget { |
| 305 |
constructor(owner, type, x, y, scale, hide, name) { |
| 306 |
// Every widget is owned by a particular figure. The "owner" is |
| 307 |
// the name of the figure function, just as in latex. |
| 308 |
// BUG: Change this to the AugmentedDrawingFunction. Thus, get rid |
| 309 |
// of this and use betterOwner. |
| 310 |
this.owner = ""; |
| 311 |
// JS isn't very good about types, so it's clearer to tag sub-classes with |
| 312 |
// the name of that sub-class rather than mess with typeof or whatever. |
| 313 |
// This should be "LoopWidget" or whatever the name is of the sub-class. |
| 314 |
this.type = ""; |
| 315 |
// Name to distinguish this widget from all others of the same |
| 316 |
// type that belong to the same figure. So, the triple (owner,type,name) |
| 317 |
// fully distinguishes this Widget from all others. |
| 318 |
// |
| 319 |
// In principle, this variable could have been avoided as a user-provided |
| 320 |
// value and he wouldn't have to come up with an 'extra' name for the widget, |
| 321 |
// but it wouldn't be easy. The WidgetManager (or something) would have to |
| 322 |
// come up with a unique ID and *that* would require that the user invoke |
| 323 |
// something like a "starting to create widgets" and "done creating widgets" |
| 324 |
// commands. Overall, this seems less fussy for him. |
| 325 |
this.name = ""; |
| 326 |
// The (x,y) is where the widget should be drawn relative to the |
| 327 |
// rectangle of the figure. Often, widgetX will be negative to put the |
| 328 |
// widget in the margin of the page. |
| 329 |
// It's tempting to call these fields x and y, but it would |
| 330 |
// be easy to accidentally reuse those names. |
| 331 |
this.widgetX = 0; |
| 332 |
this.widgetY = 0; |
| 333 |
// To scale the drawing of a widget up or down. |
| 334 |
this.scale = 1.0; |
| 335 |
// Occassionally, it may make sense to hide a widget. This is different |
| 336 |
// than being non-visible because the widget is off-screen. If the |
| 337 |
// widget has hide == true, then it is *never* shown. For example, the |
| 338 |
// way animations work, you have to have an animation widget to run the |
| 339 |
// animation, even if you don't want to see the widget. |
| 340 |
this.hide = true; |
| 341 |
// Due to the fact that this tracks the owner of the widget, and how |
| 342 |
// it is done, it is IMPORTANT that no sub-class has its own constructor. |
| 343 |
this.owner = owner; |
| 344 |
this.type = type; |
| 345 |
this.widgetX = x; |
| 346 |
this.widgetY = y; |
| 347 |
this.scale = scale; |
| 348 |
this.hide = hide; |
| 349 |
this.name = name; |
| 350 |
this.betterOwner = getAugmentedFunction(owner); |
| 351 |
// When a widget is contructed it must be registered in a global list. |
| 352 |
WidgetManager.register(this); |
| 353 |
} |
| 354 |
draw(ctx) { |
| 355 |
// Every sub-class must implement this method. |
| 356 |
console.log("Called Widget.draw()!!!"); |
| 357 |
} |
| 358 |
// BUG: These methods that are "never" supposed to be called |
| 359 |
// will be called for the widgets based on the DOM, like ButtonWidget. |
| 360 |
// That's fine -- don't panic! I want to get rid of these DOM-based |
| 361 |
// widgets anyway. DOM = cesspool. |
| 362 |
mouseDown(x, y) { |
| 363 |
// Every sub-class must implement this method. |
| 364 |
// Return true iff the widget wants to "take ownership" of this event. |
| 365 |
console.log("Called Widget.mouseDown()!!! " + this.name); |
| 366 |
return false; |
| 367 |
} |
| 368 |
mouseMove(x, y) { |
| 369 |
// As above, but returns nothing. |
| 370 |
console.log("Called Widget.mouseMove()!!!"); |
| 371 |
} |
| 372 |
mouseUp(x, y) { |
| 373 |
// As above, but returns nothing. |
| 374 |
console.log("Called Widget.mouseUp()!!!"); |
| 375 |
} |
| 376 |
} |
| 377 |
// Animations require some infrastructure. |
| 378 |
// BUG: Maybe this stuff should be in AnimationWidget. |
| 379 |
function doAnimation(theWidget) { |
| 380 |
// This is called to render a frame of an animation. It is generated via |
| 381 |
// the usual event-loop, so we schedule it just as we do for things like |
| 382 |
// mouse-downs and scroll events. |
| 383 |
// theWidget is the one that "runs" the animation, like a LoopWidget. |
| 384 |
let id = Events.getID(); |
| 385 |
doAnimationGuts(id, theWidget); |
| 386 |
} |
| 387 |
async function doAnimationGuts(id, theWidget) { |
| 388 |
// Scheduling is handled in a way similar to doScroll() in main.ts. |
| 389 |
await Events.waitMyTurn(id); |
| 390 |
await renderFrame(theWidget); |
| 391 |
// Advance to the next frame. |
| 392 |
theWidget.curStep += theWidget.stepsPerFrame; |
| 393 |
theWidget.advanceFrame(); |
| 394 |
// Don't forget this or the program is bricked! |
| 395 |
Events.allDone(id); |
| 396 |
} |
| 397 |
async function renderFrame(theWidget) { |
| 398 |
// Calls the code to render the relevant figure. It renders the *entire* |
| 399 |
// figure, widget and all. |
| 400 |
// |
| 401 |
// NOTE: This is used for animations, but it is also used to ensure |
| 402 |
// that any change to a widget (and resulting changes to a figure) |
| 403 |
// is shown. |
| 404 |
// |
| 405 |
// BUG: It's tempting to mention this in the user manual since users might |
| 406 |
// find it useful. OTOH, that shouldn't be encouraged, and this is the kind |
| 407 |
// of thing that might change in a later version. |
| 408 |
// The "owner" is the function (from the latex document) that created |
| 409 |
// theWidget. |
| 410 |
let myFunc = getAugmentedFunction(theWidget.owner); |
| 411 |
let fpc = myFunc.figurePanelClass; |
| 412 |
// This is generally synchronous, but it doesn't hurt anything to tack an |
| 413 |
// async on here. Maybe somebody will write one that *is* asynchronous. |
| 414 |
await fpc.render(); |
| 415 |
} |
| 416 |
function getFigureRect(theWidget) { |
| 417 |
// Returns the width and height of the rectangle of the widget. |
| 418 |
// The height is in two parts: the height above the x-axis, and the height |
| 419 |
// below the x-axis. |
| 420 |
// The units are pdf points, and the width is relative to the left |
| 421 |
// margin. So the width matches the usual coordinate system for drawing |
| 422 |
// the figure, and this width is equal to the text width, as reported |
| 423 |
// by latex. So x in the range [0,width] should be limited to the area |
| 424 |
// below the text. |
| 425 |
let myFunc = getAugmentedFunction(theWidget.owner); |
| 426 |
let fpc = myFunc.figurePanelClass; |
| 427 |
let answer = { w: fpc.textWidth, ha: fpc.h - fpc.lowerPadding, hb: fpc.lowerPadding }; |
| 428 |
return answer; |
| 429 |
} |
| 430 |
// Base class for widgets that run animations. See LoopAnimWidget and |
| 431 |
// OpenAnimWidget. There is a fair amount of overlap between the two types |
| 432 |
// of animation class, and it is tempting to pull more stuff up to this level for |
| 433 |
// DRY reasons, but it seems cleaner and clearer to limit this to what's needed |
| 434 |
// to run the animations with doAnimation() and related functions. |
| 435 |
class AnimationWidget extends Widget { |
| 436 |
constructor() { |
| 437 |
super(...arguments); |
| 438 |
// Animations run as a series of frames, and this is the frame being displayed. |
| 439 |
// This value may be open-ended or it may "loop back" so that animation repeats. |
| 440 |
this.curStep = 0; |
| 441 |
// How much to advance the above with each frame -- an integer. Animations |
| 442 |
// can be made to run faster by increasing this value, thereby skipping frames. |
| 443 |
this.stepsPerFrame = 1; |
| 444 |
// The process id for the call to setInterval(). |
| 445 |
this.animID = 0; |
| 446 |
} |
| 447 |
advanceFrame() { |
| 448 |
// This is why the class is abstract. It moves curStep to the next frame, |
| 449 |
// however that should be done for the particular animation. |
| 450 |
console.log("Calling abstract AnimationWidget.advanceFrame()!"); |
| 451 |
} |
| 452 |
} |
| 453 |
// A LoopWidget is to be used when an animiation runs in a repeating loop. |
| 454 |
// You must have one of these for an animation to run, even if the widget |
| 455 |
// itself is invisible. |
| 456 |
class LoopAnimWidget extends AnimationWidget { |
| 457 |
constructor() { |
| 458 |
super(...arguments); |
| 459 |
// These are as passed to register(). See that method for a description. |
| 460 |
// They really shouldn't be touched outside this class. The boolean values |
| 461 |
// are whether certain elements of the widget are visible (and hence |
| 462 |
// available for interaction). |
| 463 |
this.steps = 100; |
| 464 |
this.start = 0; |
| 465 |
this.timeStep = 20; |
| 466 |
this.visSteps = true; |
| 467 |
this.visFastSlow = true; |
| 468 |
this.visPauseRun = true; |
| 469 |
this.visCircle = true; |
| 470 |
this.triGrab = true; |
| 471 |
// This stuff is very much private. |
| 472 |
// These are stored when the figure is drawn so that mouse events can find |
| 473 |
// what was clicked more easily. It's simpler than recalculating with |
| 474 |
// every event. |
| 475 |
this.pCircle = null; |
| 476 |
this.pUpStep = null; |
| 477 |
this.pDownStep = null; |
| 478 |
this.pFaster = null; |
| 479 |
this.pSlower = null; |
| 480 |
this.pPauseRun = null; |
| 481 |
// The states of various parts of the widget; e.g., whether the pause |
| 482 |
// or run icon is present, whether a part is "half-way clicked,"" etc. |
| 483 |
// I'm using 'a' for 'active' and 's' for 'selected.' |
| 484 |
// |
| 485 |
// BUG: If I want to get *really* fancy, then I need another set of |
| 486 |
// flags to indicate that the mouse *was* clicked on something, so it |
| 487 |
// is "selected," but the user moved the mouse away from the item without |
| 488 |
// a mouse-up, so that selected item should be drawn in normal color, not |
| 489 |
// the highlighted color (sColor). If the mouse is moved back over the |
| 490 |
// selected item, then the color can go back to being the selection color. |
| 491 |
this.aRunning = true; |
| 492 |
this.sCircle = false; |
| 493 |
this.sPauseRun = false; |
| 494 |
this.sFaster = false; |
| 495 |
this.sSlower = false; |
| 496 |
this.sUpStep = false; |
| 497 |
this.sDownStep = false; |
| 498 |
} |
| 499 |
static register(ctx, x, y, scale, visWidget, steps, start, timeStep, visSteps, visFastSlow, visPauseRun, visCircle, triGrab, name) { |
| 500 |
// BUG: Add an argument for the size of the steps so that |
| 501 |
// curStep can be incremented by more than 1? |
| 502 |
// This is used something like a constructor. It either creates a new |
| 503 |
// LoopWidget and returns it, or returns one that was created earlier. |
| 504 |
// Many of these arguments are the same as for Widget.constructor(). |
| 505 |
// In addition, we have |
| 506 |
// * ctx is assumed to have a t-matrix prepared to properly draw |
| 507 |
// the widget. |
| 508 |
// * steps is the number of steps required to form a loop -- when it |
| 509 |
// "rolls over" or the "steps per revolution." |
| 510 |
// * start is the starting step, which will usually be zero. |
| 511 |
// * timeStep, in milliseconds, is the time from frame to frame. |
| 512 |
// It seems like anything less than about 10ms is pointless. |
| 513 |
// My guess (?) is that the rate of event generation is throttled |
| 514 |
// somehow. It could be that my various layers of management are |
| 515 |
// slowing things down, but I don't think so. 10ms is an eternity |
| 516 |
// on modern hardware. The eye can only follow about 20 frames per |
| 517 |
// second, at most, or 50ms per frame, so this is no big deal. |
| 518 |
// * visWidget is whether the widget is visible at all -- same as the |
| 519 |
// vis argument to Widget.construtor(). |
| 520 |
// * visSteps is whether the time step controls (at the top) are visible. |
| 521 |
// * visFastSlow is whether the faster/slower controls are visible (at |
| 522 |
// the bottom) |
| 523 |
// * visPauseRun is whether the pause/run controls are visible |
| 524 |
// * visCircle is for the circle (with triangular indicator). |
| 525 |
// * triGrab is whether the user is allowed to grab the indicator |
| 526 |
// triangle and control the animation by dragging it. The indicator |
| 527 |
// triangle is always there, but it might not be grabable. |
| 528 |
// Note that if visCircle == false, then triGrab is implicitly false |
| 529 |
// since the triangle isn't visible either. |
| 530 |
// * name is as in Widget.constructor() |
| 531 |
// |
| 532 |
// There appears to be a tacit assumption that time is measured in |
| 533 |
// integer steps, but fractional values are fine. So they aren't really |
| 534 |
// time "steps," but more like time increments. |
| 535 |
// |
| 536 |
// BUG: Maybe I should have different classes for some of these choices. |
| 537 |
// There are just too many. These could all use (internally) the |
| 538 |
// same class, just not with such a crazy number of options. |
| 539 |
// Instead of messing with LoopWidget.name or something, be explicit. |
| 540 |
let type = "LoopWidget"; |
| 541 |
// Something like this line must appear with every regester() method |
| 542 |
// for each widget. |
| 543 |
let caller = getCaller(); |
| 544 |
// I am forcing the type here, but if the wrong type is returned, |
| 545 |
// then there are bigger problems. |
| 546 |
let w = WidgetManager.knownWidget(caller, type, name); |
| 547 |
if (w != null) { |
| 548 |
// Widget is known, but it needs to be drawn too. |
| 549 |
w.draw(ctx); |
| 550 |
return w; |
| 551 |
} |
| 552 |
// Got here, so this widget is not already known, and must be created. |
| 553 |
// This class has NO construtor, by design, so this falls through to the |
| 554 |
// super-class Widget constructor. |
| 555 |
// Careful: Internally, I use an "is hidden" flag, but the user passes in |
| 556 |
// an "is visible" flag. |
| 557 |
// BUG: Change the names to be consistent. |
| 558 |
w = new LoopAnimWidget(caller, type, x, y, scale, !visWidget, name); |
| 559 |
// Now the additional stuff. This is what would be in a constructor |
| 560 |
// if this class had one. |
| 561 |
w.steps = steps; |
| 562 |
w.start = start; |
| 563 |
w.timeStep = timeStep; |
| 564 |
w.visSteps = visSteps; |
| 565 |
w.visFastSlow = visFastSlow; |
| 566 |
w.visPauseRun = visPauseRun; |
| 567 |
w.visCircle = visCircle; |
| 568 |
w.triGrab = triGrab; |
| 569 |
w.curStep = w.start; |
| 570 |
// Note the existence of this widget for the future. |
| 571 |
WidgetManager.register(w); |
| 572 |
// This is a special case because it's an animiation. The animation |
| 573 |
// needs to be scheduled. It may make sense not to call setInterval() |
| 574 |
// immediately; rather, it may be better to call setTimeout() so that |
| 575 |
// setInterval() is called after a brief pause. It depends on how these |
| 576 |
// two work. Try an immediate call; it *should* be fine. |
| 577 |
w.animID = setInterval(doAnimation, w.timeStep, w); |
| 578 |
// Before returning the widget, it must be drawn. |
| 579 |
w.draw(ctx); |
| 580 |
return w; |
| 581 |
} |
| 582 |
advanceFrame() { |
| 583 |
// This kind of animation repeats. |
| 584 |
this.curStep += this.stepsPerFrame; |
| 585 |
if (this.curStep >= this.steps) |
| 586 |
this.curStep -= this.steps; |
| 587 |
} |
| 588 |
draw(ctx) { |
| 589 |
// This is drawn with (0,0) at the center of the circle. The ctx must be |
| 590 |
// shifted and scaled based on where the user wants the widget relative |
| 591 |
// to the larger drawing area. |
| 592 |
if (this.hide === true) |
| 593 |
return; |
| 594 |
// Don't attempt to draw to a tikz file. |
| 595 |
if (ctx instanceof CTX) |
| 596 |
return; |
| 597 |
let saveT = ctx.getTransform(); |
| 598 |
ctx.translate(this.widgetX, this.widgetY); |
| 599 |
ctx.scale(this.scale, this.scale); |
| 600 |
var p = new Path2D(); |
| 601 |
// These values determine where the entire drawing is. (cx,cy) is the |
| 602 |
// center of the circular thing, with radius r. |
| 603 |
// In unscaled terms, it's clear that cx should equal r to make the |
| 604 |
// widget but up against x = 0. It's messier for the y-coordinate and you |
| 605 |
// need to work backwards from what the values defined below. The total |
| 606 |
// height is 2r (the circle), plus circWidth (circle line thickness), |
| 607 |
// plus 2 * arrowHeight (the go fast/go slow things). Then we need to |
| 608 |
// add the stuff on the top: upperGap, plus stepHeight, plus stepThick. |
| 609 |
// This is silly and probably confusing to the user. Just place the |
| 610 |
// widget relative to the center of the circle. In other words, set |
| 611 |
// (cx,cy) = (0,0). The user just needs adjust accordingly, and |
| 612 |
// exactly what he wants to do will be influenced by whether the upper |
| 613 |
// and lower sub-controls are present. |
| 614 |
var r = 40; |
| 615 |
var cx = 0; |
| 616 |
var cy = 0; |
| 617 |
var circWidth = 3; |
| 618 |
if (this.visCircle == true) { |
| 619 |
// Draw the circle. (cx,cy) is center r and r are the two axes of the |
| 620 |
// elipse. 0 is that the ellipse isn't rotated, and the last two are |
| 621 |
// the start and end angle. |
| 622 |
p.ellipse(cx, cy, r, r, 0, 0, 2 * Math.PI); |
| 623 |
// Note this circle for reference by mouse events. |
| 624 |
this.pCircle = new Path2D(p); |
| 625 |
ctx.lineWidth = circWidth; |
| 626 |
if (this.sCircle == true) |
| 627 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 628 |
else |
| 629 |
ctx.strokeStyle = "black"; |
| 630 |
ctx.stroke(p); |
| 631 |
ctx.lineWidth = 1; |
| 632 |
// A little triangle to point to the location within the animation loop. |
| 633 |
// This value is in radians, in [0,2pi). |
| 634 |
// Location of indicator triangle around the perimeter of the circle. |
| 635 |
// Minus so that it travels clock-wise, which seems to be our natural expectation. |
| 636 |
var loc = -2 * Math.PI * this.curStep / this.steps; |
| 637 |
var triHeight = 10; |
| 638 |
// This is half the full angle at the outer point. |
| 639 |
var triAngle = Math.PI / 20; |
| 640 |
p = new Path2D(); |
| 641 |
var x = cx + (r - circWidth / 2) * Math.cos(loc); |
| 642 |
var y = cy + (r - circWidth / 2) * Math.sin(loc); |
| 643 |
p.moveTo(x, y); |
| 644 |
x = cx + (r - triHeight) * Math.cos(loc + triAngle); |
| 645 |
y = cy + (r - triHeight) * Math.sin(loc + triAngle); |
| 646 |
p.lineTo(x, y); |
| 647 |
x = cx + (r - triHeight) * Math.cos(loc - triAngle); |
| 648 |
y = cy + (r - triHeight) * Math.sin(loc - triAngle); |
| 649 |
p.lineTo(x, y); |
| 650 |
p.closePath(); |
| 651 |
ctx.strokeStyle = "red"; |
| 652 |
ctx.stroke(p); |
| 653 |
ctx.strokeStyle = "black"; |
| 654 |
} |
| 655 |
// Next, some controls at the bottom for going faster/slower and |
| 656 |
// pausing/running. |
| 657 |
// First, a pair of '>' for going faster. |
| 658 |
let arrowOffset = 18; |
| 659 |
let lowerGap = 6; |
| 660 |
let arrowHeight = 7; |
| 661 |
let arrowDepth = 4; |
| 662 |
let arrowPairSpace = 3; |
| 663 |
let arrowThick = 1.25; |
| 664 |
if (this.visFastSlow === true) { |
| 665 |
ctx.lineWidth = arrowThick; |
| 666 |
if (this.sFaster == true) |
| 667 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 668 |
else |
| 669 |
ctx.strokeStyle = "black"; |
| 670 |
p = new Path2D(); |
| 671 |
p.moveTo(cx + arrowOffset, cy - r - circWidth / 2 - lowerGap); |
| 672 |
p.lineTo(cx + arrowOffset + arrowDepth, cy - r - circWidth / 2 - lowerGap - arrowHeight); |
| 673 |
p.lineTo(cx + arrowOffset, cy - r - circWidth / 2 - lowerGap - 2 * arrowHeight); |
| 674 |
ctx.stroke(p); |
| 675 |
// You can't just shift a path. Needs to be rebuilt. |
| 676 |
// BUG: I need to add that ability to my FPath class. |
| 677 |
// Maybe I have already? |
| 678 |
p = new Path2D(); |
| 679 |
p.moveTo(cx + arrowOffset + arrowPairSpace, cy - r - circWidth / 2 - lowerGap); |
| 680 |
p.lineTo(cx + arrowOffset + arrowDepth + arrowPairSpace, cy - r - circWidth / 2 - lowerGap - arrowHeight); |
| 681 |
p.lineTo(cx + arrowOffset + arrowPairSpace, cy - r - circWidth / 2 - lowerGap - 2 * arrowHeight); |
| 682 |
ctx.stroke(p); |
| 683 |
// A rectangle for the clickable area. |
| 684 |
this.pFaster = new Path2D(); |
| 685 |
this.pFaster.rect(cx + arrowOffset - arrowThick, cy - r - circWidth / 2 - lowerGap - 2 * arrowHeight, arrowPairSpace + arrowDepth + 2 * arrowThick, 2 * arrowHeight); |
| 686 |
// ctx.strokeStyle = 'green'; |
| 687 |
// ctx.stroke(this.pFaster); |
| 688 |
// Same idea: '<' to go slower. |
| 689 |
if (this.sSlower == true) |
| 690 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 691 |
else |
| 692 |
ctx.strokeStyle = "black"; |
| 693 |
p = new Path2D(); |
| 694 |
p.moveTo(cx - arrowOffset, cy - r - circWidth / 2 - lowerGap); |
| 695 |
p.lineTo(cx - arrowOffset - arrowDepth, cy - r - circWidth / 2 - lowerGap - arrowHeight); |
| 696 |
p.lineTo(cx - arrowOffset, cy - r - circWidth / 2 - lowerGap - 2 * arrowHeight); |
| 697 |
ctx.stroke(p); |
| 698 |
p = new Path2D(); |
| 699 |
p.moveTo(cx - arrowOffset - arrowPairSpace, cy - r - circWidth / 2 - lowerGap); |
| 700 |
p.lineTo(cx - arrowOffset - arrowDepth - arrowPairSpace, cy - r - circWidth / 2 - lowerGap - arrowHeight); |
| 701 |
p.lineTo(cx - arrowOffset - arrowPairSpace, cy - r - circWidth / 2 - lowerGap - 2 * arrowHeight); |
| 702 |
ctx.stroke(p); |
| 703 |
// And the clickable area. |
| 704 |
this.pSlower = new Path2D(); |
| 705 |
this.pSlower.rect(cx - arrowOffset - arrowPairSpace - arrowDepth - arrowThick, cy - r - circWidth / 2 - lowerGap - 2 * arrowHeight, arrowPairSpace + arrowDepth + 2 * arrowThick, 2 * arrowHeight); |
| 706 |
// ctx.strokeStyle = 'green'; |
| 707 |
// ctx.stroke(this.pSlower); |
| 708 |
} |
| 709 |
ctx.lineWidth = 1; |
| 710 |
if (this.visPauseRun === true) { |
| 711 |
// A || or triangle for pause or run. |
| 712 |
let pauseSpace = 3.25; |
| 713 |
let pauseThick = 1.5; |
| 714 |
let pauseHeight = 2 * arrowHeight; |
| 715 |
let runThick = 1.5; |
| 716 |
let runLeftRight = 5; |
| 717 |
let runHeight = 2 * arrowHeight; |
| 718 |
if (this.sPauseRun == true) |
| 719 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 720 |
else |
| 721 |
ctx.strokeStyle = "black"; |
| 722 |
if (this.aRunning === true) { |
| 723 |
// The animation is running, so show || to allow pausing. |
| 724 |
ctx.lineWidth = pauseThick; |
| 725 |
p = new Path2D(); |
| 726 |
p.moveTo(cx + pauseSpace, cy - r - circWidth / 2 - lowerGap); |
| 727 |
p.lineTo(cx + pauseSpace, cy - r - circWidth / 2 - lowerGap - pauseHeight); |
| 728 |
ctx.stroke(p); |
| 729 |
p = new Path2D(); |
| 730 |
p.moveTo(cx - pauseSpace, cy - r - circWidth / 2 - lowerGap); |
| 731 |
p.lineTo(cx - pauseSpace, cy - r - circWidth / 2 - lowerGap - pauseHeight); |
| 732 |
ctx.stroke(p); |
| 733 |
ctx.lineWidth = 1; |
| 734 |
} |
| 735 |
else { |
| 736 |
// Animation is paused, so show triangle to run it again. |
| 737 |
ctx.lineWidth = runThick; |
| 738 |
p = new Path2D(); |
| 739 |
p.moveTo(cx - runLeftRight, cy - r - circWidth / 2 - lowerGap); |
| 740 |
p.lineTo(cx - runLeftRight, cy - r - circWidth / 2 - lowerGap - runHeight); |
| 741 |
p.lineTo(cx + runLeftRight, cy - r - circWidth / 2 - lowerGap - runHeight / 2); |
| 742 |
p.closePath(); |
| 743 |
ctx.stroke(p); |
| 744 |
} |
| 745 |
// Either way (paused or running), we need the clickable area. |
| 746 |
// This area is too generous for the "run" triangle, because |
| 747 |
// I use the same rectangle for "pause" and "run," but no big deal. |
| 748 |
this.pPauseRun = new Path2D(); |
| 749 |
this.pPauseRun.rect(cx - runLeftRight - runThick, cy - r - circWidth / 2 - lowerGap - runHeight - runThick, 2 * runLeftRight + 2 * runThick, runHeight + 2 * runThick); |
| 750 |
// ctx.strokeStyle = "blue"; |
| 751 |
// ctx.lineWidth = 0.5; |
| 752 |
// ctx.stroke(this.pPauseRun); |
| 753 |
// ctx.strokeStyle = "black"; |
| 754 |
} |
| 755 |
ctx.lineWidth = 1; |
| 756 |
// Now some symbols above the circle for adjusting the step size. |
| 757 |
if (this.visSteps === true) { |
| 758 |
// Up and down arrows. |
| 759 |
let stepSpace = 20; |
| 760 |
let stepThick = 2.0; |
| 761 |
let upperGap = 8; |
| 762 |
let stepHeight = 15; |
| 763 |
let stepArrowHeight = 8; |
| 764 |
let stepArrowWidth = 5; |
| 765 |
ctx.lineWidth = stepThick; |
| 766 |
if (this.sDownStep == true) { |
| 767 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 768 |
ctx.fillStyle = LoopAnimWidget.sColor; |
| 769 |
} |
| 770 |
else { |
| 771 |
ctx.strokeStyle = "black"; |
| 772 |
ctx.fillStyle = "black"; |
| 773 |
} |
| 774 |
// Vertical line |
| 775 |
p = new Path2D(); |
| 776 |
p.moveTo(cx - stepSpace, cy + r + circWidth / 2 + upperGap); |
| 777 |
p.lineTo(cx - stepSpace, cy + r + circWidth / 2 + upperGap + stepHeight); |
| 778 |
ctx.stroke(p); |
| 779 |
// Arrow head |
| 780 |
p = new Path2D(); |
| 781 |
p.moveTo(cx - stepSpace, cy + r + circWidth / 2 + upperGap - stepThick); |
| 782 |
p.lineTo(cx - stepSpace + stepArrowWidth, cy + r + circWidth / 2 + upperGap + stepArrowHeight - stepThick); |
| 783 |
p.lineTo(cx - stepSpace - stepArrowWidth, cy + r + circWidth / 2 + upperGap + stepArrowHeight - stepThick); |
| 784 |
p.closePath(); |
| 785 |
ctx.fill(p); |
| 786 |
// Clickable area for down arrow. |
| 787 |
this.pDownStep = new Path2D(); |
| 788 |
this.pDownStep.rect(cx - stepSpace - stepArrowWidth, cy + r + circWidth / 2 + upperGap - stepThick, 2 * stepArrowWidth, stepHeight + stepThick); |
| 789 |
// ctx.strokeStyle = "blue"; |
| 790 |
// ctx.lineWidth = 0.5; |
| 791 |
// ctx.stroke(this.pDownStep); |
| 792 |
// ctx.strokeStyle = "black"; |
| 793 |
// ctx.lineWidth = stepThick; |
| 794 |
// Again, to the right, arrow head up. |
| 795 |
if (this.sUpStep == true) { |
| 796 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 797 |
ctx.fillStyle = LoopAnimWidget.sColor; |
| 798 |
} |
| 799 |
else { |
| 800 |
ctx.strokeStyle = "black"; |
| 801 |
ctx.fillStyle = "black"; |
| 802 |
} |
| 803 |
p = new Path2D(); |
| 804 |
p.moveTo(cx + stepSpace, cy + r + circWidth / 2 + upperGap); |
| 805 |
p.lineTo(cx + stepSpace, cy + r + circWidth / 2 + upperGap + stepHeight); |
| 806 |
ctx.stroke(p); |
| 807 |
p = new Path2D(); |
| 808 |
p.moveTo(cx + stepSpace, cy + r + circWidth / 2 + upperGap + stepHeight + stepThick); |
| 809 |
p.lineTo(cx + stepSpace + stepArrowWidth, cy + r + circWidth / 2 + upperGap + stepHeight - stepArrowHeight + stepThick); |
| 810 |
p.lineTo(cx + stepSpace - stepArrowWidth, cy + r + circWidth / 2 + upperGap + stepHeight - stepArrowHeight + stepThick); |
| 811 |
p.closePath(); |
| 812 |
ctx.fill(p); |
| 813 |
// Clickable area for up arrow. |
| 814 |
this.pUpStep = new Path2D(); |
| 815 |
this.pUpStep.rect(cx + stepSpace - stepArrowWidth, cy + r + circWidth / 2 + upperGap, 2 * stepArrowWidth, stepHeight + stepThick); |
| 816 |
// ctx.strokeStyle = "blue"; |
| 817 |
// ctx.lineWidth = 0.5; |
| 818 |
// ctx.stroke(this.pUpStep); |
| 819 |
// ctx.strokeStyle = "black"; |
| 820 |
ctx.lineWidth = 1; |
| 821 |
// A little step icon. |
| 822 |
ctx.strokeStyle = "black"; |
| 823 |
ctx.fillStyle = "black"; |
| 824 |
stepThick = 1.5; |
| 825 |
let stepSize = 6; |
| 826 |
ctx.lineWidth = stepThick; |
| 827 |
// Steps made as one path, starting at upper-left |
| 828 |
p = new Path2D(); |
| 829 |
p.moveTo(cx - stepSize, cy + r + circWidth / 2 + upperGap + 2 * stepSize); |
| 830 |
p.lineTo(cx, cy + r + circWidth / 2 + upperGap + 2 * stepSize); |
| 831 |
p.lineTo(cx, cy + r + circWidth / 2 + upperGap + stepSize); |
| 832 |
p.lineTo(cx + stepSize, cy + r + circWidth / 2 + upperGap + stepSize); |
| 833 |
p.lineTo(cx + stepSize, cy + r + circWidth / 2 + upperGap); |
| 834 |
ctx.stroke(p); |
| 835 |
} |
| 836 |
ctx.lineWidth = 1; |
| 837 |
// Used this to verify the constants. The rectangle barely encloses |
| 838 |
// the widget. |
| 839 |
//ctx.strokeRect( |
| 840 |
// -LoopWidget.Radius,-LoopWidget.Radius - LoopWidget.TopHeight, |
| 841 |
// 2*LoopWidget.Radius, |
| 842 |
// 2*LoopWidget.Radius + LoopWidget.TopHeight + LoopWidget.BottomHeight); |
| 843 |
ctx.setTransform(saveT); |
| 844 |
} |
| 845 |
mouseDown(x, y) { |
| 846 |
// (x,y) is given in coordinates relative to the owning figure. |
| 847 |
// Return true iff these coordinates apply to this widget. |
| 848 |
if (this.hide === true) |
| 849 |
return false; |
| 850 |
// Adjust coordinates relative to what the draw() methods uses. |
| 851 |
// This way we can compare (x,y) to what is on the screen. |
| 852 |
x -= this.widgetX; |
| 853 |
y -= this.widgetY; |
| 854 |
x /= this.scale; |
| 855 |
y /= this.scale; |
| 856 |
// The widget also has a scale, which must be taken into account. |
| 857 |
WidgetManager.bogusCtx.resetTransform(); |
| 858 |
// There is isPointInPath() and isPointInStroke(). |
| 859 |
// It seems that isPointInPath() works on an abstract geometric basis; |
| 860 |
// the lineWidth of the ctx doesn't matter. OTOH, isPointInStroke() |
| 861 |
// is affected by the lineWidth -- as it must be to work in any |
| 862 |
// reasonable way. |
| 863 |
// |
| 864 |
// Note also that isPointInPath() defaults to the non-zero winding rule. |
| 865 |
// Pass "evenodd" as the final argument for that winding rule. |
| 866 |
// Check the pause/run area first since it should be "on top of" |
| 867 |
// the circle area. |
| 868 |
if (this.pPauseRun !== null) { |
| 869 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pPauseRun, x, y); |
| 870 |
if (isin === true) { |
| 871 |
this.sPauseRun = true; |
| 872 |
WidgetManager.mouseOwner = this; |
| 873 |
renderFrame(this); |
| 874 |
return true; |
| 875 |
} |
| 876 |
} |
| 877 |
// And the run faster area. |
| 878 |
if (this.pFaster !== null) { |
| 879 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pFaster, x, y); |
| 880 |
if (isin === true) { |
| 881 |
this.sFaster = true; |
| 882 |
WidgetManager.mouseOwner = this; |
| 883 |
renderFrame(this); |
| 884 |
return true; |
| 885 |
} |
| 886 |
} |
| 887 |
// The run slower area. |
| 888 |
if (this.pSlower !== null) { |
| 889 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pSlower, x, y); |
| 890 |
if (isin === true) { |
| 891 |
this.sSlower = true; |
| 892 |
WidgetManager.mouseOwner = this; |
| 893 |
renderFrame(this); |
| 894 |
return true; |
| 895 |
} |
| 896 |
} |
| 897 |
// The longer step area. |
| 898 |
if (this.pUpStep !== null) { |
| 899 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pUpStep, x, y); |
| 900 |
if (isin === true) { |
| 901 |
this.sUpStep = true; |
| 902 |
WidgetManager.mouseOwner = this; |
| 903 |
renderFrame(this); |
| 904 |
return true; |
| 905 |
} |
| 906 |
} |
| 907 |
// The shorter step area. |
| 908 |
if (this.pDownStep !== null) { |
| 909 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pDownStep, x, y); |
| 910 |
if (isin === true) { |
| 911 |
this.sDownStep = true; |
| 912 |
WidgetManager.mouseOwner = this; |
| 913 |
renderFrame(this); |
| 914 |
return true; |
| 915 |
} |
| 916 |
} |
| 917 |
// Last thing to check since it should be "underneath" everything. |
| 918 |
if (this.pCircle !== null) { |
| 919 |
// If the user clicked near the circle, then set the indicator and |
| 920 |
// current step to that position. Be generous with the clickable area. |
| 921 |
WidgetManager.bogusCtx.lineWidth = 15; |
| 922 |
let isin = WidgetManager.bogusCtx.isPointInStroke(this.pCircle, x, y); |
| 923 |
if (isin === true) { |
| 924 |
// Act on this click and take ownership for future draggging. |
| 925 |
// Want an angle in [0,2pi]. |
| 926 |
this.sCircle = true; |
| 927 |
// Again, minus since clockwise. |
| 928 |
let alpha = -Math.atan2(y, x); |
| 929 |
if (alpha < 0) |
| 930 |
alpha += 2 * Math.PI; |
| 931 |
this.curStep = Math.floor(this.steps * alpha / (2 * Math.PI)); |
| 932 |
WidgetManager.mouseOwner = this; |
| 933 |
// The appearance of the widget has changed. |
| 934 |
renderFrame(this); |
| 935 |
return true; |
| 936 |
} |
| 937 |
} |
| 938 |
return false; |
| 939 |
} |
| 940 |
mouseMove(x, y) { |
| 941 |
// As above. |
| 942 |
if (this.hide === true) |
| 943 |
return; |
| 944 |
x -= this.widgetX; |
| 945 |
y -= this.widgetY; |
| 946 |
x /= this.scale; |
| 947 |
y /= this.scale; |
| 948 |
WidgetManager.bogusCtx.resetTransform(); |
| 949 |
if (this.sCircle === true) { |
| 950 |
// What I will do it check that the mouse is "close enough" to the |
| 951 |
// circle, but it can be a *long* ways away. |
| 952 |
WidgetManager.bogusCtx.lineWidth = 40; |
| 953 |
let isin = WidgetManager.bogusCtx.isPointInStroke(this.pCircle, x, y); |
| 954 |
if (isin == false) |
| 955 |
return; |
| 956 |
// Minus to make clockwise. |
| 957 |
let alpha = -Math.atan2(y, x); |
| 958 |
if (alpha < 0) |
| 959 |
alpha += 2 * Math.PI; |
| 960 |
this.curStep = Math.floor(this.steps * alpha / (2 * Math.PI)); |
| 961 |
// The appearance of the widget may have changed. |
| 962 |
renderFrame(this); |
| 963 |
} |
| 964 |
// BUG: I might (?) want colors to change based on what the mouse |
| 965 |
// is over. See the BUG comment that goes with aRunning, sCircle, etc., |
| 966 |
// at the top of the class. |
| 967 |
} |
| 968 |
mouseUp(x, y) { |
| 969 |
// As above. |
| 970 |
if (this.hide === true) |
| 971 |
return; |
| 972 |
x -= this.widgetX; |
| 973 |
y -= this.widgetY; |
| 974 |
x /= this.scale; |
| 975 |
y /= this.scale; |
| 976 |
WidgetManager.bogusCtx.resetTransform(); |
| 977 |
// The mouse is up, so nothing can remain selected. In most cases, |
| 978 |
// releasing the mouse over the selected item means that something |
| 979 |
// must be done since the "button" was properly pressed. |
| 980 |
this.sCircle = false; |
| 981 |
if (this.sPauseRun) { |
| 982 |
// Did they *release* the mouse over the pause/run area? |
| 983 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pPauseRun, x, y); |
| 984 |
if (isin === true) { |
| 985 |
// Start/stop the animation. |
| 986 |
if (this.aRunning === true) |
| 987 |
// Currently running. Pause it. |
| 988 |
clearInterval(this.animID); |
| 989 |
else |
| 990 |
// Currently paused. Restart it. |
| 991 |
this.animID = setInterval(doAnimation, this.timeStep, this); |
| 992 |
// Change the pause/run icon too. |
| 993 |
if (this.aRunning === true) |
| 994 |
this.aRunning = false; |
| 995 |
else |
| 996 |
this.aRunning = true; |
| 997 |
} |
| 998 |
this.sPauseRun = false; |
| 999 |
} |
| 1000 |
if (this.sFaster === true) { |
| 1001 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pFaster, x, y); |
| 1002 |
if (isin === true) { |
| 1003 |
// Make the animation run a bit faster by reducing the frame-to- |
| 1004 |
// frame time step -- the animation speed. This could be done in |
| 1005 |
// a lot of different ways, by using a factor of 1.4 seems about |
| 1006 |
// right. |
| 1007 |
this.timeStep /= 1.4; |
| 1008 |
if (this.timeStep < 1) |
| 1009 |
this.timeStep = 1; |
| 1010 |
// Stop the animation and restart it at the new speed, |
| 1011 |
// but only if it is currently running. It restarts always, but |
| 1012 |
// don't try to halt it if it's not running. |
| 1013 |
if (this.aRunning === true) |
| 1014 |
clearInterval(this.animID); |
| 1015 |
this.animID = setInterval(doAnimation, this.timeStep, this); |
| 1016 |
this.aRunning = true; |
| 1017 |
} |
| 1018 |
this.sFaster = false; |
| 1019 |
} |
| 1020 |
if (this.sSlower === true) { |
| 1021 |
// Just as above, but make it go slower. |
| 1022 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pSlower, x, y); |
| 1023 |
if (isin === true) { |
| 1024 |
this.timeStep *= 1.4; |
| 1025 |
// More than a second per frame is silly. |
| 1026 |
if (this.timeStep > 1000) |
| 1027 |
this.timeStep = 1000; |
| 1028 |
if (this.aRunning === true) |
| 1029 |
clearInterval(this.animID); |
| 1030 |
this.animID = setInterval(doAnimation, this.timeStep, this); |
| 1031 |
this.aRunning = true; |
| 1032 |
} |
| 1033 |
this.sSlower = false; |
| 1034 |
} |
| 1035 |
if (this.sUpStep === true) { |
| 1036 |
// Make the number of time increments per frame larger. |
| 1037 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pUpStep, x, y); |
| 1038 |
if (isin === true) { |
| 1039 |
// Use a smaller ratio here. Conceptually, it seems like |
| 1040 |
// this should be an integer, but it really doesn't have to be. |
| 1041 |
this.stepsPerFrame *= 1.25; |
| 1042 |
// Fewer than 3 frames per cycle seems silly. For most animations, |
| 1043 |
// you'd proably want at least 10 or 20, at a minimum. |
| 1044 |
if (this.stepsPerFrame > this.steps / 3) |
| 1045 |
this.stepsPerFrame = this.steps / 3; |
| 1046 |
} |
| 1047 |
this.sUpStep = false; |
| 1048 |
} |
| 1049 |
if (this.sDownStep === true) { |
| 1050 |
// As above. |
| 1051 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pDownStep, x, y); |
| 1052 |
if (isin === true) { |
| 1053 |
this.stepsPerFrame /= 1.25; |
| 1054 |
// I am tempted to put a lower bound on this, but it's 'not |
| 1055 |
// absolutely necessary. |
| 1056 |
} |
| 1057 |
this.sDownStep = false; |
| 1058 |
} |
| 1059 |
// The appearance of the widget may have changed. |
| 1060 |
renderFrame(this); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
// These are useful to the user to help properly place things. |
| 1064 |
// These values are given with scale equal to 1, and are worked |
| 1065 |
// out from the actual drawing code. |
| 1066 |
// The TopHeight is the amount above the circle that is used for the |
| 1067 |
// time-step controls, and BottomHeight is the amount used for the |
| 1068 |
// faster/slower, pause/run contols. If you don't want the circle at |
| 1069 |
// all, then it's a little awkward to work out placement, but it works. |
| 1070 |
// There is some imprecision here due to line thickneses, but very close. |
| 1071 |
LoopAnimWidget.Radius = 41.5; |
| 1072 |
LoopAnimWidget.TopHeight = 24.5; |
| 1073 |
LoopAnimWidget.BottomHeight = 21.0; |
| 1074 |
// When things are "selected," draw them in this color. |
| 1075 |
LoopAnimWidget.sColor = "blue"; |
| 1076 |
// An OpenAnimWidget is for an open-ended animation that doesn't loop back |
| 1077 |
// on itself and repeat. In many respects, it's similar to a LoopWidget. |
| 1078 |
// It looks different because it's a long bar, sort of like a scroll bar. |
| 1079 |
// It's so similar that there are minimal (for me) comments. See |
| 1080 |
// LoopWidgets for certain details. |
| 1081 |
class OpenAnimWidget extends AnimationWidget { |
| 1082 |
constructor() { |
| 1083 |
super(...arguments); |
| 1084 |
// These things are important to the animation drawing code, and are |
| 1085 |
// meant to be public(ish). |
| 1086 |
// These are as passed to register(). |
| 1087 |
this.barLength = 100; |
| 1088 |
this.timeStep = 25; |
| 1089 |
this.decay = 1.0001; |
| 1090 |
this.visSteps = true; |
| 1091 |
this.visFastSlow = true; |
| 1092 |
this.visPauseRun = true; |
| 1093 |
this.visBar = true; |
| 1094 |
this.barGrab = true; |
| 1095 |
// As for LoopWidth, width modest changes: |
| 1096 |
// Clickable areas: |
| 1097 |
this.pBar = null; |
| 1098 |
this.pDot = null; |
| 1099 |
this.pFaster = null; |
| 1100 |
this.pSlower = null; |
| 1101 |
this.pPauseRun = null; |
| 1102 |
this.pUpStep = null; |
| 1103 |
this.pDownStep = null; |
| 1104 |
// States of parts: |
| 1105 |
// |
| 1106 |
// BUG: If I want to get *really* fancy, then I need another set of |
| 1107 |
// flags to indicate that the mouse *was* clicked on something, so it |
| 1108 |
// is "selected," but the user moved the mouse away from the item without |
| 1109 |
// a mouse-up, so that selected item should be drawn in normal color, not |
| 1110 |
// the highlighted color (sColor). If the mouse is moved back over the |
| 1111 |
// selected item, then the color can go back to being the selection color. |
| 1112 |
this.aRunning = true; |
| 1113 |
this.sDot = false; |
| 1114 |
this.sFaster = false; |
| 1115 |
this.sSlower = false; |
| 1116 |
this.sPauseRun = false; |
| 1117 |
this.sUpStep = false; |
| 1118 |
this.sDownStep = false; |
| 1119 |
} |
| 1120 |
static register(ctx, x, y, scale, width, visWidget, timeStep, decay, visSteps, visFastSlow, visPauseRun, visBar, barGrab, name) { |
| 1121 |
// This is used something like a constructor. It either creates a new |
| 1122 |
// LoopWidget and returns it, or returns one that was created earlier. |
| 1123 |
// Many of these arguments are the same as for Widget.constructor(). |
| 1124 |
// In addition, we have |
| 1125 |
// * ctx is assumed to have a t-matrix prepared to properly draw |
| 1126 |
// the widget. |
| 1127 |
// * width is the length of the indicator bar. If you want both the |
| 1128 |
// controls (time steps and fast/slow), then this should be at least |
| 1129 |
// 200 so that the controls don't stick out. Of course, this is the |
| 1130 |
// unscaled size, and you can make the entire thing smaller with the |
| 1131 |
// scale argument. |
| 1132 |
// * timeStep, in milliseconds, is the time from frame to frame. |
| 1133 |
// * decay is complicated. The value of this.curStep must be mapped to |
| 1134 |
// the linear bar, which is not infinite. |
| 1135 |
// We need a map from [0,infty) to [0,width). Define |
| 1136 |
// f(s) = 1 - 1/a^s |
| 1137 |
// This maps [0,infty) to [0,1), provided that a > 1. Then |
| 1138 |
// g(s) = w f(s) |
| 1139 |
// is the function we want. But what about a? The closer a is to 1, |
| 1140 |
// the faster g(s) will approach w. Typically, you'll want a to be |
| 1141 |
// something like 1.001 or 1.0001, depending on the number of steps |
| 1142 |
// in your animation. The decay argment determines a: |
| 1143 |
// a = 1 + 1/decay, so you'll usually want decay to be somewhere in the |
| 1144 |
// range from 100 to (maybe) 100,000. It depends how big the steps |
| 1145 |
// are and how long you want the animiation to run. For comparison, |
| 1146 |
// decay = 1,000 puts f(2000) = 0.86 and f(5000) = 0.99, while |
| 1147 |
// decay = 10,000 puts f(2000) = 0.18, f(5000) = 0.39, |
| 1148 |
// f(20,000) = 0.86. You can also work backwards. If you want |
| 1149 |
// f(n) = x, where x is in [0,1), like x = 85%, then you want |
| 1150 |
// 1 / (1 + 1/a)^n = x, or |
| 1151 |
// a = 1 / [ x^(1/n) - 1 ] |
| 1152 |
// That's not so informative, but you can write it as |
| 1153 |
// a = 1 / [ exp(-ln(x)/n) - 1 ] |
| 1154 |
// If we take x \approx 0.86 so that ln(x) = -0.15 (exactly), then |
| 1155 |
// a = 1 / [ exp(-0.15/n) - 1 ] |
| 1156 |
// Plug in the value for n at which you want to have reached the |
| 1157 |
// 86% level, and you get a. |
| 1158 |
// For brevity, in the code, I use this.decay as the value, a, |
| 1159 |
// discussed above. |
| 1160 |
// BUG: I feel like I made an algebra mistake, but that's the idea. |
| 1161 |
// * visWidget is whether the widget is visible at all -- same as the |
| 1162 |
// vis argument to Widget.construtor(). |
| 1163 |
// * visSteps is whether the time step controls (at the right) are visible. |
| 1164 |
// * visFastSlow is whether the faster/slower controls are visible (at |
| 1165 |
// the left) |
| 1166 |
// * visPauseRun is whether the pause/run controls are visible. |
| 1167 |
// * visBar is for the progress bar (with dot indicator). |
| 1168 |
// * barGrab is whether the user is allowed to grab the dot indicator |
| 1169 |
// triangle and control the animation by dragging it. |
| 1170 |
// * name is as in Widget.constructor() |
| 1171 |
// |
| 1172 |
// BUG: Maybe I should have different classes for some of these choices. |
| 1173 |
// There are just too many. These could all use (internally) the |
| 1174 |
// same class, just not with such a crazy number of options. |
| 1175 |
// As for LoopWidget. |
| 1176 |
let type = "OpenAnimWidget"; |
| 1177 |
let caller = getCaller(); |
| 1178 |
let w = WidgetManager.knownWidget(caller, type, name); |
| 1179 |
if (w != null) { |
| 1180 |
w.draw(ctx); |
| 1181 |
return w; |
| 1182 |
} |
| 1183 |
// Got here, so this widget is not already known, and must be created. |
| 1184 |
w = new OpenAnimWidget(caller, type, x, y, scale, !visWidget, name); |
| 1185 |
// Adjust the length for the scale so that if the user asks for |
| 1186 |
// a bar that is X px long, he gets it. So, the scale adjusts the size |
| 1187 |
// of the "bits", not the total size. |
| 1188 |
w.barLength = width / scale; |
| 1189 |
w.timeStep = timeStep; |
| 1190 |
w.visSteps = visSteps; |
| 1191 |
w.visFastSlow = visFastSlow; |
| 1192 |
w.visPauseRun = visPauseRun; |
| 1193 |
w.visBar = visBar; |
| 1194 |
w.barGrab = barGrab; |
| 1195 |
// For internal use, we convert the given decay to the value we use |
| 1196 |
// for exponentiation. |
| 1197 |
w.decay = 1 + 1 / decay; |
| 1198 |
// Note the existence of this widget for the future. |
| 1199 |
WidgetManager.register(w); |
| 1200 |
// This is a special case because it's an animiation, as with LoopWidget. |
| 1201 |
w.animID = setInterval(doAnimation, w.timeStep, w); |
| 1202 |
// Before returning the widget, it must be drawn. |
| 1203 |
w.draw(ctx); |
| 1204 |
return w; |
| 1205 |
} |
| 1206 |
advanceFrame() { |
| 1207 |
// This animation is open-ended; curStep grows without limit (up to |
| 1208 |
// E500 or whatever it is). |
| 1209 |
this.curStep += this.stepsPerFrame; |
| 1210 |
} |
| 1211 |
draw(ctx) { |
| 1212 |
// This is drawn with (0,0) at the lower-right corner. |
| 1213 |
if (this.hide === true) |
| 1214 |
return; |
| 1215 |
// Don't draw the widget to a tikz file. |
| 1216 |
if (ctx instanceof CTX) |
| 1217 |
return; |
| 1218 |
let saveT = ctx.getTransform(); |
| 1219 |
ctx.translate(this.widgetX, this.widgetY); |
| 1220 |
ctx.scale(this.scale, this.scale); |
| 1221 |
var p = new Path2D(); |
| 1222 |
if (this.visBar == true) { |
| 1223 |
// These *could* be made accessible to the user, but we already |
| 1224 |
// have a heap of arguments to create this thing. |
| 1225 |
let indDotRadius = 5.0; |
| 1226 |
let barThick = 3.0; |
| 1227 |
let indDotThick = 2.0; |
| 1228 |
// Draw the indicator bar and dot. |
| 1229 |
// Bar first. |
| 1230 |
p.moveTo(0, indDotRadius + OpenAnimWidget.ControlsHeight); |
| 1231 |
p.lineTo(this.barLength, indDotRadius + OpenAnimWidget.ControlsHeight); |
| 1232 |
// Note this circle for reference by mouse events. |
| 1233 |
if (this.barGrab === true) |
| 1234 |
this.pBar = new Path2D(p); |
| 1235 |
ctx.lineWidth = barThick; |
| 1236 |
ctx.stroke(p); |
| 1237 |
// Now the dot. |
| 1238 |
p = new Path2D(); |
| 1239 |
let dx = Math.pow(this.decay, this.curStep); |
| 1240 |
dx = this.barLength * (1 - 1 / dx); |
| 1241 |
p.ellipse(dx, indDotRadius + OpenAnimWidget.ControlsHeight, indDotRadius, indDotRadius, 0, 0, 2 * Math.PI); |
| 1242 |
if (this.barGrab === true) |
| 1243 |
this.pDot = new Path2D(p); |
| 1244 |
if (this.sDot == true) |
| 1245 |
ctx.fillStyle = OpenAnimWidget.sColor; |
| 1246 |
else |
| 1247 |
ctx.fillStyle = "red"; |
| 1248 |
ctx.fill(p); |
| 1249 |
ctx.lineWidth = indDotThick; |
| 1250 |
ctx.strokeStyle = "black"; |
| 1251 |
ctx.stroke(p); |
| 1252 |
} |
| 1253 |
// There may be controls under the bar for faster/slower, pause/run |
| 1254 |
// and larger/smaller steps. Whatever of these is present, they should be |
| 1255 |
// centered, which is a pain. The total width of the pause/run controls |
| 1256 |
// is 40.5, obtained by checking the size of the box necessary to |
| 1257 |
// exactly enclose the controls. Height of that box is 14. I've set |
| 1258 |
// things up so that the height of the "step size" controls is also 14, |
| 1259 |
// and the width of that part is 32. These *could* be expressed in |
| 1260 |
// terms of the various constants defined below, but hard-coding is |
| 1261 |
// easier. |
| 1262 |
// These controls are drawn relative to their individual centers, |
| 1263 |
// so the shifting is done relative to those centers and their widths. |
| 1264 |
let pauseRunWidth = 40; |
| 1265 |
let stepsWidth = 32; |
| 1266 |
let intraGap = 8; |
| 1267 |
// Here (compared to LoopWidget), I use cx and cy to shift the parts |
| 1268 |
// of the control down and right. The right-shift is used for centering |
| 1269 |
// Changing the t-matrix of ctx would work too. |
| 1270 |
let cy = OpenAnimWidget.BarHeight - 4; |
| 1271 |
let cx = this.barLength / 2; |
| 1272 |
// We position the right bit, based on whether the left bit is present. |
| 1273 |
if (this.visSteps === true) |
| 1274 |
cx += intraGap + (pauseRunWidth / 2); |
| 1275 |
// Used for both fast/slow "chevrons" and for up/down arrows. |
| 1276 |
let arrowHeight = 7; |
| 1277 |
if (this.visFastSlow === true) { |
| 1278 |
// I made this a little tighter than for LoopWidget. |
| 1279 |
let arrowOffset = 12; //18; |
| 1280 |
let arrowDepth = 4; |
| 1281 |
let arrowPairSpace = 3; |
| 1282 |
let arrowThick = 1.25; |
| 1283 |
// First, a pair of '>' for going faster. |
| 1284 |
ctx.lineWidth = arrowThick; |
| 1285 |
if (this.sFaster == true) |
| 1286 |
ctx.strokeStyle = OpenAnimWidget.sColor; |
| 1287 |
else |
| 1288 |
ctx.strokeStyle = "black"; |
| 1289 |
p = new Path2D(); |
| 1290 |
p.moveTo(cx + arrowOffset, cy); |
| 1291 |
p.lineTo(cx + arrowOffset + arrowDepth, cy + arrowHeight); |
| 1292 |
p.lineTo(cx + arrowOffset, cy + 2 * arrowHeight); |
| 1293 |
ctx.stroke(p); |
| 1294 |
// You can't just shift a path. Needs to be rebuilt. |
| 1295 |
p = new Path2D(); |
| 1296 |
p.moveTo(cx + arrowOffset + arrowPairSpace, cy); |
| 1297 |
p.lineTo(cx + arrowOffset + arrowDepth + arrowPairSpace, cy + arrowHeight); |
| 1298 |
p.lineTo(cx + arrowOffset + arrowPairSpace, cy + 2 * arrowHeight); |
| 1299 |
ctx.stroke(p); |
| 1300 |
// A rectangle for the clickable area. |
| 1301 |
this.pFaster = new Path2D(); |
| 1302 |
this.pFaster.rect(cx + arrowOffset - arrowThick, cy, arrowPairSpace + arrowDepth + 2 * arrowThick, 2 * arrowHeight); |
| 1303 |
/* |
| 1304 |
// BUG: testing |
| 1305 |
ctx.strokeStyle = "green"; |
| 1306 |
ctx.lineWidth = 0.5; |
| 1307 |
ctx.stroke(this.pFaster); |
| 1308 |
ctx.strokeStyle = "black"; |
| 1309 |
ctx.lineWidth = arrowThick; |
| 1310 |
*/ |
| 1311 |
// Same idea: '<' to go slower. |
| 1312 |
if (this.sSlower == true) |
| 1313 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 1314 |
else |
| 1315 |
ctx.strokeStyle = "black"; |
| 1316 |
p = new Path2D(); |
| 1317 |
p.moveTo(cx - arrowOffset, cy); |
| 1318 |
p.lineTo(cx - arrowOffset - arrowDepth, cy + arrowHeight); |
| 1319 |
p.lineTo(cx - arrowOffset, cy + 2 * arrowHeight); |
| 1320 |
ctx.stroke(p); |
| 1321 |
p = new Path2D(); |
| 1322 |
p.moveTo(cx - arrowOffset - arrowPairSpace, cy); |
| 1323 |
p.lineTo(cx - arrowOffset - arrowDepth - arrowPairSpace, cy + arrowHeight); |
| 1324 |
p.lineTo(cx - arrowOffset - arrowPairSpace, cy + 2 * arrowHeight); |
| 1325 |
ctx.stroke(p); |
| 1326 |
// And the clickable area. |
| 1327 |
this.pSlower = new Path2D(); |
| 1328 |
this.pSlower.rect(cx - arrowOffset - arrowPairSpace - arrowDepth - arrowThick, cy, arrowPairSpace + arrowDepth + 2 * arrowThick, 2 * arrowHeight); |
| 1329 |
/* |
| 1330 |
// BUG: testing |
| 1331 |
ctx.strokeStyle = "yellow"; |
| 1332 |
ctx.lineWidth = 0.5; |
| 1333 |
ctx.stroke(this.pSlower); |
| 1334 |
ctx.strokeStyle = "black"; |
| 1335 |
*/ |
| 1336 |
/* |
| 1337 |
// BUG: test box around entire thing. |
| 1338 |
p = new Path2D(); |
| 1339 |
p.rect(cx - arrowOffset - arrowPairSpace - arrowDepth - arrowThick,cy, |
| 1340 |
2*(arrowOffset + arrowPairSpace + arrowDepth + arrowThick), |
| 1341 |
2*arrowHeight); |
| 1342 |
|
| 1343 |
//let temp = 2*(arrowOffset + arrowPairSpace + arrowDepth + arrowThick); |
| 1344 |
//console.log("val: " +temp); |
| 1345 |
|
| 1346 |
ctx.strokeStyle = "red"; |
| 1347 |
ctx.stroke(p); |
| 1348 |
*/ |
| 1349 |
ctx.strokeStyle = "black"; |
| 1350 |
} |
| 1351 |
ctx.lineWidth = 1; |
| 1352 |
if (this.visPauseRun === true) { |
| 1353 |
// A || or triangle for pause or run. |
| 1354 |
let pauseSpace = 3.25; |
| 1355 |
let pauseThick = 1.5; |
| 1356 |
let pauseHeight = 2 * arrowHeight; |
| 1357 |
let runThick = 1.5; |
| 1358 |
let runLeftRight = 5; |
| 1359 |
let runHeight = 2 * arrowHeight; |
| 1360 |
if (this.sPauseRun == true) |
| 1361 |
ctx.strokeStyle = LoopAnimWidget.sColor; |
| 1362 |
else |
| 1363 |
ctx.strokeStyle = "black"; |
| 1364 |
if (this.aRunning === true) { |
| 1365 |
// The animation is running, so show || to allow pausing. |
| 1366 |
ctx.lineWidth = pauseThick; |
| 1367 |
p = new Path2D(); |
| 1368 |
p.moveTo(cx + pauseSpace, cy); |
| 1369 |
p.lineTo(cx + pauseSpace, cy + pauseHeight); |
| 1370 |
ctx.stroke(p); |
| 1371 |
p = new Path2D(); |
| 1372 |
p.moveTo(cx - pauseSpace, cy); |
| 1373 |
p.lineTo(cx - pauseSpace, cy + pauseHeight); |
| 1374 |
ctx.stroke(p); |
| 1375 |
ctx.lineWidth = 1; |
| 1376 |
} |
| 1377 |
else { |
| 1378 |
// Animation is paused, so show triangle to run it again. |
| 1379 |
ctx.lineWidth = runThick; |
| 1380 |
p = new Path2D(); |
| 1381 |
p.moveTo(cx - runLeftRight, cy); |
| 1382 |
p.lineTo(cx - runLeftRight, cy + runHeight); |
| 1383 |
p.lineTo(cx + runLeftRight, cy + runHeight / 2); |
| 1384 |
p.closePath(); |
| 1385 |
ctx.stroke(p); |
| 1386 |
} |
| 1387 |
// Either way (paused or running), we need the clickable area. |
| 1388 |
// This area is too generous for the "run" triangle, because |
| 1389 |
// I use the same rectangle for "pause" and "run," but no big deal. |
| 1390 |
this.pPauseRun = new Path2D(); |
| 1391 |
this.pPauseRun.rect(cx - runLeftRight - runThick, cy - runThick, 2 * runLeftRight + 2 * runThick, runHeight + 2 * runThick); |
| 1392 |
/* |
| 1393 |
// BUG: testing |
| 1394 |
ctx.strokeStyle = "blue"; |
| 1395 |
ctx.lineWidth = 0.5; |
| 1396 |
ctx.stroke(this.pPauseRun); |
| 1397 |
ctx.strokeStyle = "black"; |
| 1398 |
*/ |
| 1399 |
} |
| 1400 |
// And position the left controls, based on whether the ones on |
| 1401 |
// the right are present. |
| 1402 |
cy += 12.5; |
| 1403 |
cx = this.barLength / 2; |
| 1404 |
if ((this.visPauseRun === true) || (this.visFastSlow === true)) |
| 1405 |
cx -= intraGap + stepsWidth / 2; |
| 1406 |
ctx.lineWidth = 1; |
| 1407 |
// Symbols for adjusting the step size. |
| 1408 |
if (this.visSteps === true) { |
| 1409 |
// Up and down arrows. This is a little smaller than for LoopWidget, |
| 1410 |
// so that the height matches the paure/run controls. I also |
| 1411 |
// tightened up the spacing a bit. |
| 1412 |
// Note that I am also using stepThick as a proxy for adjustment |
| 1413 |
// of the fact that the tip of the arrow head is a little tall. |
| 1414 |
let stepSpace = 12; |
| 1415 |
let stepThick = 2.0; |
| 1416 |
let stepHeight = 12.5; |
| 1417 |
let stepArrowHeight = 5.0; |
| 1418 |
let stepArrowWidth = 4.0; |
| 1419 |
ctx.lineWidth = stepThick; |
| 1420 |
if (this.sDownStep === true) { |
| 1421 |
ctx.strokeStyle = OpenAnimWidget.sColor; |
| 1422 |
ctx.fillStyle = OpenAnimWidget.sColor; |
| 1423 |
} |
| 1424 |
else { |
| 1425 |
ctx.strokeStyle = "black"; |
| 1426 |
ctx.fillStyle = "black"; |
| 1427 |
} |
| 1428 |
// Vertical line |
| 1429 |
p = new Path2D(); |
| 1430 |
p.moveTo(cx - stepSpace, cy); |
| 1431 |
p.lineTo(cx - stepSpace, cy - stepHeight); |
| 1432 |
ctx.stroke(p); |
| 1433 |
// Arrow head |
| 1434 |
p = new Path2D(); |
| 1435 |
p.moveTo(cx - stepSpace, cy + stepThick); |
| 1436 |
p.lineTo(cx - stepSpace + stepArrowWidth, cy - stepArrowHeight + stepThick); |
| 1437 |
p.lineTo(cx - stepSpace - stepArrowWidth, cy - stepArrowHeight + stepThick); |
| 1438 |
p.closePath(); |
| 1439 |
ctx.fill(p); |
| 1440 |
// Clickable area for down arrow. |
| 1441 |
this.pDownStep = new Path2D(); |
| 1442 |
this.pDownStep.rect(cx - stepSpace - stepArrowWidth, cy - stepHeight, 2 * stepArrowWidth, stepHeight + stepThick); |
| 1443 |
/* |
| 1444 |
// BUG: testing |
| 1445 |
ctx.strokeStyle = "blue"; |
| 1446 |
ctx.lineWidth = 0.5; |
| 1447 |
ctx.stroke(this.pDownStep); |
| 1448 |
ctx.strokeStyle = "black"; |
| 1449 |
ctx.lineWidth = stepThick; |
| 1450 |
*/ |
| 1451 |
// Again, to the right, arrow head up. |
| 1452 |
if (this.sUpStep === true) { |
| 1453 |
ctx.strokeStyle = OpenAnimWidget.sColor; |
| 1454 |
ctx.fillStyle = OpenAnimWidget.sColor; |
| 1455 |
} |
| 1456 |
else { |
| 1457 |
ctx.strokeStyle = "black"; |
| 1458 |
ctx.fillStyle = "black"; |
| 1459 |
} |
| 1460 |
p = new Path2D(); |
| 1461 |
p.moveTo(cx + stepSpace, cy + stepThick); |
| 1462 |
p.lineTo(cx + stepSpace, cy + stepThick - stepHeight); |
| 1463 |
ctx.stroke(p); |
| 1464 |
p = new Path2D(); |
| 1465 |
p.moveTo(cx + stepSpace, cy - stepHeight); |
| 1466 |
p.lineTo(cx + stepSpace + stepArrowWidth, cy - stepHeight + stepArrowHeight); |
| 1467 |
p.lineTo(cx + stepSpace - stepArrowWidth, cy - stepHeight + stepArrowHeight); |
| 1468 |
p.closePath(); |
| 1469 |
ctx.fill(p); |
| 1470 |
// Clickable area for up arrow. |
| 1471 |
this.pUpStep = new Path2D(); |
| 1472 |
this.pUpStep.rect(cx + stepSpace - stepArrowWidth, cy - stepHeight, 2 * stepArrowWidth, stepHeight + stepThick); |
| 1473 |
/* |
| 1474 |
// BUG: testing |
| 1475 |
ctx.strokeStyle = "blue"; |
| 1476 |
ctx.lineWidth = 0.5; |
| 1477 |
ctx.stroke(this.pUpStep); |
| 1478 |
ctx.strokeStyle = "black"; |
| 1479 |
*/ |
| 1480 |
ctx.lineWidth = 1; |
| 1481 |
// A little step icon. |
| 1482 |
ctx.strokeStyle = "black"; |
| 1483 |
ctx.fillStyle = "black"; |
| 1484 |
stepThick = 1.5; |
| 1485 |
let stepSize = 5; |
| 1486 |
ctx.lineWidth = stepThick; |
| 1487 |
// Steps made as one path, starting at upper-left |
| 1488 |
p = new Path2D(); |
| 1489 |
p.moveTo(cx - stepSize, cy - 2 * stepSize); |
| 1490 |
p.lineTo(cx, cy - 2 * stepSize); |
| 1491 |
p.lineTo(cx, cy - stepSize); |
| 1492 |
p.lineTo(cx + stepSize, cy - stepSize); |
| 1493 |
p.lineTo(cx + stepSize, cy); |
| 1494 |
ctx.stroke(p); |
| 1495 |
/* |
| 1496 |
// BUG: Testing box around it all. |
| 1497 |
p = new Path2D(); |
| 1498 |
p.rect(cx - stepSpace - stepArrowWidth,cy - stepHeight, |
| 1499 |
2*(stepSpace + stepArrowWidth), |
| 1500 |
stepHeight + stepThick); |
| 1501 |
|
| 1502 |
ctx.strokeStyle = "red"; |
| 1503 |
ctx.lineWidth = 0.5; |
| 1504 |
ctx.stroke(p); |
| 1505 |
|
| 1506 |
//let temp = 2*(stepSpace + stepArrowWidth); |
| 1507 |
//let temp = stepHeight + stepThick; |
| 1508 |
//console.log("val: " + temp); |
| 1509 |
*/ |
| 1510 |
ctx.strokeStyle = "black"; |
| 1511 |
} |
| 1512 |
ctx.lineWidth = 1; |
| 1513 |
ctx.setTransform(saveT); |
| 1514 |
} |
| 1515 |
mouseDown(x, y) { |
| 1516 |
// (x,y) is given in coordinates relative to the owning figure. |
| 1517 |
// Return true iff these coordinates apply to this widget. |
| 1518 |
// BUG: This is almost identical to LoopWidget. DRY? |
| 1519 |
if (this.hide === true) |
| 1520 |
return false; |
| 1521 |
// Adjust coordinates relative to what the draw() methods uses. |
| 1522 |
x -= this.widgetX; |
| 1523 |
y -= this.widgetY; |
| 1524 |
x /= this.scale; |
| 1525 |
y /= this.scale; |
| 1526 |
// The widget also has a scale, which must be taken into account. |
| 1527 |
WidgetManager.bogusCtx.resetTransform(); |
| 1528 |
// The run faster area. |
| 1529 |
if (this.pFaster !== null) { |
| 1530 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pFaster, x, y); |
| 1531 |
if (isin === true) { |
| 1532 |
this.sFaster = true; |
| 1533 |
WidgetManager.mouseOwner = this; |
| 1534 |
renderFrame(this); |
| 1535 |
return true; |
| 1536 |
} |
| 1537 |
} |
| 1538 |
// The run slower area. |
| 1539 |
if (this.pSlower !== null) { |
| 1540 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pSlower, x, y); |
| 1541 |
if (isin === true) { |
| 1542 |
this.sSlower = true; |
| 1543 |
WidgetManager.mouseOwner = this; |
| 1544 |
renderFrame(this); |
| 1545 |
return true; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
// The pause/run area. |
| 1549 |
if (this.pPauseRun !== null) { |
| 1550 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pPauseRun, x, y); |
| 1551 |
if (isin === true) { |
| 1552 |
this.sPauseRun = true; |
| 1553 |
WidgetManager.mouseOwner = this; |
| 1554 |
renderFrame(this); |
| 1555 |
return true; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
// The longer step area. |
| 1559 |
if (this.pUpStep !== null) { |
| 1560 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pUpStep, x, y); |
| 1561 |
if (isin === true) { |
| 1562 |
this.sUpStep = true; |
| 1563 |
WidgetManager.mouseOwner = this; |
| 1564 |
renderFrame(this); |
| 1565 |
return true; |
| 1566 |
} |
| 1567 |
} |
| 1568 |
// The shorter step area. |
| 1569 |
if (this.pDownStep !== null) { |
| 1570 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pDownStep, x, y); |
| 1571 |
if (isin === true) { |
| 1572 |
this.sDownStep = true; |
| 1573 |
WidgetManager.mouseOwner = this; |
| 1574 |
renderFrame(this); |
| 1575 |
return true; |
| 1576 |
} |
| 1577 |
} |
| 1578 |
// Last thing to check since it should be "underneath" everything, |
| 1579 |
// although I don't think there's the same kind of overlap that |
| 1580 |
// there was for LoopWidget. |
| 1581 |
if (this.pDot !== null) { |
| 1582 |
// See whether the user clicked on or near the dot. He must click |
| 1583 |
// on the dot, not at some random point along the bar. |
| 1584 |
WidgetManager.bogusCtx.lineWidth = 2; |
| 1585 |
let isin = WidgetManager.bogusCtx.isPointInStroke(this.pDot, x, y); |
| 1586 |
if (isin === false) |
| 1587 |
isin = WidgetManager.bogusCtx.isPointInPath(this.pDot, x, y); |
| 1588 |
if (isin === true) { |
| 1589 |
this.sDot = true; |
| 1590 |
// Move the dot (slightly) so that it is centered at (x,y). |
| 1591 |
// We don't actually "move the dot;" instead we adjust |
| 1592 |
// this.curStep to put the dot where we want it. That is, we |
| 1593 |
// invert g(s) = w (1-1/a^s). See the discussion, in register(), |
| 1594 |
// of this function. We have |
| 1595 |
// x = w (1 - 1/a^s) |
| 1596 |
// a^s = w / (w - x) |
| 1597 |
// s = log_a [ w / (w - x) ] |
| 1598 |
// And recall that log_a (z) = ln(z) / ln(a). |
| 1599 |
let ratio = this.barLength / (this.barLength - x); |
| 1600 |
let s = Math.log(ratio) / Math.log(this.decay); |
| 1601 |
if (s < 0) |
| 1602 |
s = 0; |
| 1603 |
this.curStep = s; |
| 1604 |
WidgetManager.mouseOwner = this; |
| 1605 |
renderFrame(this); |
| 1606 |
return true; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
return false; |
| 1610 |
} |
| 1611 |
mouseMove(x, y) { |
| 1612 |
// As above. |
| 1613 |
if (this.hide === true) |
| 1614 |
return; |
| 1615 |
x -= this.widgetX; |
| 1616 |
y -= this.widgetY; |
| 1617 |
x /= this.scale; |
| 1618 |
y /= this.scale; |
| 1619 |
WidgetManager.bogusCtx.resetTransform(); |
| 1620 |
if (this.sDot === true) { |
| 1621 |
// What I will do it check that the mouse is "close enough" to the |
| 1622 |
// bar, but it can be a long ways away. |
| 1623 |
WidgetManager.bogusCtx.lineWidth = 20; |
| 1624 |
let isin = WidgetManager.bogusCtx.isPointInStroke(this.pBar, x, y); |
| 1625 |
if (isin == false) |
| 1626 |
return; |
| 1627 |
let ratio = this.barLength / (this.barLength - x); |
| 1628 |
let s = Math.log(ratio) / Math.log(this.decay); |
| 1629 |
if (s < 0) |
| 1630 |
s = 0; |
| 1631 |
this.curStep = s; |
| 1632 |
// The appearance of the widget may have changed. |
| 1633 |
renderFrame(this); |
| 1634 |
} |
| 1635 |
// BUG: I might (?) want colors to change based on what the mouse |
| 1636 |
// is over. See the BUG comment that goes with aRunning, sCircle, etc., |
| 1637 |
// at the top of the class. |
| 1638 |
} |
| 1639 |
mouseUp(x, y) { |
| 1640 |
// As above. |
| 1641 |
if (this.hide === true) |
| 1642 |
return; |
| 1643 |
x -= this.widgetX; |
| 1644 |
y -= this.widgetY; |
| 1645 |
x /= this.scale; |
| 1646 |
y /= this.scale; |
| 1647 |
WidgetManager.bogusCtx.resetTransform(); |
| 1648 |
// The mouse is up, so nothing can remain selected. In most cases, |
| 1649 |
// releasing the mouse over the selected item means that something |
| 1650 |
// must be done since the "button" was properly pressed. |
| 1651 |
this.sDot = false; |
| 1652 |
if (this.sFaster === true) { |
| 1653 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pFaster, x, y); |
| 1654 |
if (isin === true) { |
| 1655 |
this.timeStep /= 1.4; |
| 1656 |
if (this.timeStep < 1) |
| 1657 |
this.timeStep = 1; |
| 1658 |
// Stop the animation and restart it at the new speed |
| 1659 |
if (this.aRunning === true) |
| 1660 |
clearInterval(this.animID); |
| 1661 |
this.animID = setInterval(doAnimation, this.timeStep, this); |
| 1662 |
this.aRunning = true; |
| 1663 |
} |
| 1664 |
this.sFaster = false; |
| 1665 |
} |
| 1666 |
if (this.sSlower === true) { |
| 1667 |
// Just as above, but make it go slower. |
| 1668 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pSlower, x, y); |
| 1669 |
if (isin === true) { |
| 1670 |
this.timeStep *= 1.4; |
| 1671 |
// More than a second per frame is silly. |
| 1672 |
if (this.timeStep > 1000) |
| 1673 |
this.timeStep = 1000; |
| 1674 |
if (this.aRunning === true) |
| 1675 |
clearInterval(this.animID); |
| 1676 |
this.animID = setInterval(doAnimation, this.timeStep, this); |
| 1677 |
this.aRunning = true; |
| 1678 |
} |
| 1679 |
this.sSlower = false; |
| 1680 |
} |
| 1681 |
if (this.sPauseRun) { |
| 1682 |
// Did they *release* the mouse over the pause/run area? |
| 1683 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pPauseRun, x, y); |
| 1684 |
if (isin === true) { |
| 1685 |
// Start/stop the animation. |
| 1686 |
if (this.aRunning === true) |
| 1687 |
// Currently running. Pause it. |
| 1688 |
clearInterval(this.animID); |
| 1689 |
else |
| 1690 |
// Currently paused. Restart it. |
| 1691 |
this.animID = setInterval(doAnimation, this.timeStep, this); |
| 1692 |
// Change the pause/run icon too. |
| 1693 |
if (this.aRunning === true) |
| 1694 |
this.aRunning = false; |
| 1695 |
else |
| 1696 |
this.aRunning = true; |
| 1697 |
} |
| 1698 |
this.sPauseRun = false; |
| 1699 |
} |
| 1700 |
if (this.sUpStep === true) { |
| 1701 |
// Make the number of time increments per frame larger. |
| 1702 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pUpStep, x, y); |
| 1703 |
if (isin === true) { |
| 1704 |
// Use a smaller ratio here. Conceptually, it seems like |
| 1705 |
// this should be an integer, but it really doesn't have to be. |
| 1706 |
// Unlike LoopWidget, there's no upper limit on the number |
| 1707 |
// of steps per frame. |
| 1708 |
this.stepsPerFrame *= 1.25; |
| 1709 |
} |
| 1710 |
this.sUpStep = false; |
| 1711 |
} |
| 1712 |
if (this.sDownStep === true) { |
| 1713 |
// As above. |
| 1714 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pDownStep, x, y); |
| 1715 |
if (isin === true) |
| 1716 |
this.stepsPerFrame /= 1.25; |
| 1717 |
this.sDownStep = false; |
| 1718 |
} |
| 1719 |
// The appearance of the widget may have changed. |
| 1720 |
renderFrame(this); |
| 1721 |
} |
| 1722 |
} |
| 1723 |
// These are useful to the user to help properly place things. |
| 1724 |
// The widget is placed based on the lower-left corner, and the |
| 1725 |
// user can specify the width. The BarHeight is the height of the |
| 1726 |
// bar portion -- it's essentially the radius of the indicator dot -- |
| 1727 |
// and ControlsHeight is for the controls. |
| 1728 |
OpenAnimWidget.BarHeight = 6.0; |
| 1729 |
OpenAnimWidget.ControlsHeight = 20.0; |
| 1730 |
// This is useful to help the user place stuff above the control. |
| 1731 |
OpenAnimWidget.TotalHeight = 32.0; |
| 1732 |
// When things are "selected," draw them in this color. |
| 1733 |
OpenAnimWidget.sColor = "blue"; |
| 1734 |
// This is to allow dragging points around. After the much more complicated |
| 1735 |
// animation widgets, this is a lot easier. One difference is that dots |
| 1736 |
// are not drawn automatically; the user must call the widget's draw() |
| 1737 |
// method. This is because the order of drawing may matter -- what should |
| 1738 |
// be on top? |
| 1739 |
// |
| 1740 |
// At one point, this was more flexible, but -- see DraggableDrawWidget below |
| 1741 |
// -- it seems better to keep this widget simple (just dots). |
| 1742 |
class DraggableDotWidget extends Widget { |
| 1743 |
constructor() { |
| 1744 |
super(...arguments); |
| 1745 |
// The clickable area for the dot, and whether it is selected. |
| 1746 |
// pDot : Path2D | null = null; |
| 1747 |
this.pDot = null; |
| 1748 |
this.selected = false; |
| 1749 |
// The default radius of a dot. |
| 1750 |
this.dotRadius = 3.0; |
| 1751 |
} |
| 1752 |
static register(ctx, x, y, name) { |
| 1753 |
// As with other widgets. Note that there is no scale since it doesn't |
| 1754 |
// make sense here. |
| 1755 |
let type = "DraggableDotWidget"; |
| 1756 |
let caller = getCaller(); |
| 1757 |
let w = WidgetManager.knownWidget(caller, type, name); |
| 1758 |
if (w != null) { |
| 1759 |
return w; |
| 1760 |
} |
| 1761 |
// Got here, so this widget is not already known, and must be created. |
| 1762 |
// The 'false' here means that we always assume the dot is visible. Leting |
| 1763 |
// this be invisible would be pointless, although it is hidable by |
| 1764 |
// directly changing the Widget.hide field. |
| 1765 |
w = new DraggableDotWidget(caller, type, x, y, 1.0, false, name); |
| 1766 |
// Note the existence of this widget for the future. |
| 1767 |
WidgetManager.register(w); |
| 1768 |
// Note that we do *not* draw this widget here. |
| 1769 |
return w; |
| 1770 |
} |
| 1771 |
draw(ctx) { |
| 1772 |
// Because these widgets might be drawn for tikz output, this uses |
| 1773 |
// FPath instead of Path2D. |
| 1774 |
// |
| 1775 |
// BUG: As a rule, I don't think people will want most widgets (like |
| 1776 |
// LoopWidget) to be drawn to the paper version, but I suppose it should |
| 1777 |
// be possible. *I* would like it for documentation purposes. |
| 1778 |
if (this.hide === true) |
| 1779 |
return; |
| 1780 |
// Adjusting the coordinates this way feels a little weird, but it's |
| 1781 |
// how the other widgets work, and it's actually easier. |
| 1782 |
let saveT = ctx.getTransform(); |
| 1783 |
ctx.translate(this.widgetX, this.widgetY); |
| 1784 |
ctx.scale(this.scale, this.scale); |
| 1785 |
// BUG: Somehow, sometimes using FPath puts hair on these dots?? |
| 1786 |
// It looks like it happens where the segments meet. There's probably |
| 1787 |
// some algebra mistake. Either that or the JS implementation of |
| 1788 |
// bezier curves sucks. For now, I do not use bezier curves, and |
| 1789 |
// leave it as an ellipse internally. |
| 1790 |
let p = new FPath(); |
| 1791 |
//let p = new Path2D(); |
| 1792 |
let r = this.dotRadius; |
| 1793 |
p.ellipse(0, 0, r, r, 0, 0, 2 * Math.PI, true); |
| 1794 |
//this.pDot = new Path2D(p); |
| 1795 |
// this.pDot = new FPath(p); |
| 1796 |
this.pDot = p; |
| 1797 |
if (this.selected === true) |
| 1798 |
ctx.fillStyle = DraggableDotWidget.sColor; |
| 1799 |
else |
| 1800 |
ctx.fillStyle = "red"; |
| 1801 |
ctx.fill(p); |
| 1802 |
ctx.setTransform(saveT); |
| 1803 |
} |
| 1804 |
mouseDown(x, y) { |
| 1805 |
if (this.hide === true) |
| 1806 |
return false; |
| 1807 |
// Adjust coordinates relative to what the draw() methods uses. |
| 1808 |
x -= this.widgetX; |
| 1809 |
y -= this.widgetY; |
| 1810 |
x /= this.scale; |
| 1811 |
y /= this.scale; |
| 1812 |
// The widget also has a scale, which must be taken into account. |
| 1813 |
WidgetManager.bogusCtx.resetTransform(); |
| 1814 |
if (this.pDot !== null) { |
| 1815 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pDot, x, y); |
| 1816 |
if (isin === true) { |
| 1817 |
this.selected = true; |
| 1818 |
WidgetManager.mouseOwner = this; |
| 1819 |
renderFrame(this); |
| 1820 |
return true; |
| 1821 |
} |
| 1822 |
} |
| 1823 |
return false; |
| 1824 |
} |
| 1825 |
mouseMove(x, y) { |
| 1826 |
// As above. |
| 1827 |
if (this.hide === true) |
| 1828 |
return; |
| 1829 |
// Restrict to be within the figure area, at the least. |
| 1830 |
// By default (the code here, not this.drawSelFcn, whatever that might |
| 1831 |
// do), the dot doesn't get "lost," but it can be drawn partially outside |
| 1832 |
// the figure proper, leaving "crumbs" that aren't erased until you |
| 1833 |
// scroll the page. |
| 1834 |
let wh = getFigureRect(this); |
| 1835 |
// Rrestrict to the entire figure rectangle, tightened up a bit. |
| 1836 |
wh.w -= 2 * this.dotRadius; |
| 1837 |
wh.ha -= 2 * this.dotRadius; |
| 1838 |
wh.hb -= 2 * this.dotRadius; |
| 1839 |
// Note the minus with hb. The "height below" is a postive value, but |
| 1840 |
// we are comparing to a potentially negative y. |
| 1841 |
if (x <= this.dotRadius) |
| 1842 |
return; |
| 1843 |
if (x >= wh.w) |
| 1844 |
return; |
| 1845 |
if (y <= -wh.hb) |
| 1846 |
return; |
| 1847 |
if (y >= wh.ha) |
| 1848 |
return; |
| 1849 |
if (this.selected === true) { |
| 1850 |
this.widgetX = x; |
| 1851 |
this.widgetY = y; |
| 1852 |
renderFrame(this); |
| 1853 |
} |
| 1854 |
} |
| 1855 |
mouseUp(x, y) { |
| 1856 |
if (this.hide === true) |
| 1857 |
return; |
| 1858 |
// This is more round-about because of the possibility that the |
| 1859 |
// mouse-up occured outside the figure area. We need to reach |
| 1860 |
// the renderFrame() line whatever happens. |
| 1861 |
if (this.selected === true) { |
| 1862 |
this.selected = false; |
| 1863 |
let wh = getFigureRect(this); |
| 1864 |
wh.w -= 2 * this.dotRadius; |
| 1865 |
wh.ha -= 2 * this.dotRadius; |
| 1866 |
wh.hb -= 2 * this.dotRadius; |
| 1867 |
// Note the minus with hb. The "height below" is a postive value, but |
| 1868 |
// we are comparing to a potentially negative y. |
| 1869 |
if ((x > this.dotRadius) && (x < wh.w) && |
| 1870 |
// (y > this.dotRadius) && (y < wh.h)) |
| 1871 |
(y > -wh.hb) && (y < wh.ha)) { |
| 1872 |
this.widgetX = x; |
| 1873 |
this.widgetY = y; |
| 1874 |
} |
| 1875 |
renderFrame(this); |
| 1876 |
} |
| 1877 |
} |
| 1878 |
} |
| 1879 |
// When things are "selected," draw them in this color. |
| 1880 |
DraggableDotWidget.sColor = "blue"; |
| 1881 |
class DraggableDrawWidget extends Widget { |
| 1882 |
constructor() { |
| 1883 |
super(...arguments); |
| 1884 |
// The clickable area for the "dot" (which could have any shape) |
| 1885 |
// and whether it is selected. |
| 1886 |
this.pDot = null; |
| 1887 |
this.selected = false; |
| 1888 |
// The user must provide these functions. |
| 1889 |
// Note the litle cheat to make the ts compiler shut its yapper. |
| 1890 |
this.drawFcn = null; |
| 1891 |
this.drawSelFcn = null; |
| 1892 |
this.testPosFcn = null; |
| 1893 |
} |
| 1894 |
static register(ctx, x, y, drawFcn, drawSelFcn, testPosFcn, name) { |
| 1895 |
// The drawFcn should be defined to draw whatevever it wants. |
| 1896 |
// It should take the ctx as the sole argument, and return a path |
| 1897 |
// such that a click in the path (using ctx.isPointInPath()). |
| 1898 |
// |
| 1899 |
// The drawSelFcn is similar, but is used for drawing when the |
| 1900 |
// item is selected -- so that the user can change the color or |
| 1901 |
// whatever. |
| 1902 |
// |
| 1903 |
// The testPosFcn receives (x,y) as an argument, along with the (w,h) |
| 1904 |
// of the figure area (in pdf points) and should return |
| 1905 |
// true (point is acceptable) or false (not acceptable). |
| 1906 |
let type = "DraggableDrawWidget"; |
| 1907 |
let caller = getCaller(); |
| 1908 |
let w = WidgetManager.knownWidget(caller, type, name); |
| 1909 |
if (w != null) { |
| 1910 |
return w; |
| 1911 |
} |
| 1912 |
// Got here, so this widget is not already known, and must be created. |
| 1913 |
// The 'false' here means that we always assume the dot is visible. Leting |
| 1914 |
// this be invisible would be pointless, although it is hidable by |
| 1915 |
// directly changing the Widget.hide field. |
| 1916 |
w = new DraggableDrawWidget(caller, type, x, y, 1.0, false, name); |
| 1917 |
w.drawFcn = drawFcn; |
| 1918 |
w.drawSelFcn = drawSelFcn; |
| 1919 |
w.testPosFcn = testPosFcn; |
| 1920 |
// Note the existence of this widget for the future. |
| 1921 |
WidgetManager.register(w); |
| 1922 |
return w; |
| 1923 |
} |
| 1924 |
draw(ctx) { |
| 1925 |
// Because these widgets might be drawn for tikz output, this uses |
| 1926 |
// FPath instead of Path2D. |
| 1927 |
// |
| 1928 |
// BUG: As a rule, I don't think people will want most widgets (like |
| 1929 |
// LoopWidget) to be drawn to the paper version, but I suppose it should |
| 1930 |
// be possible. *I* would like it for documentation purposes. |
| 1931 |
if (this.hide === true) |
| 1932 |
return; |
| 1933 |
// Adjusting the coordinates this way feels a little weird, but it's |
| 1934 |
// how the other widgets work, and it's actually easier. |
| 1935 |
let saveT = ctx.getTransform(); |
| 1936 |
ctx.translate(this.widgetX, this.widgetY); |
| 1937 |
ctx.scale(this.scale, this.scale); |
| 1938 |
if (this.selected === true) |
| 1939 |
this.pDot = this.drawSelFcn(ctx); |
| 1940 |
else |
| 1941 |
this.pDot = this.drawFcn(ctx); |
| 1942 |
ctx.setTransform(saveT); |
| 1943 |
} |
| 1944 |
mouseDown(x, y) { |
| 1945 |
//console.log("dot down"); |
| 1946 |
if (this.hide === true) |
| 1947 |
return false; |
| 1948 |
// Adjust coordinates relative to what the draw() methods uses. |
| 1949 |
x -= this.widgetX; |
| 1950 |
y -= this.widgetY; |
| 1951 |
x /= this.scale; |
| 1952 |
y /= this.scale; |
| 1953 |
WidgetManager.bogusCtx.resetTransform(); |
| 1954 |
if (this.pDot !== null) { |
| 1955 |
let isin = WidgetManager.bogusCtx.isPointInPath(this.pDot, x, y); |
| 1956 |
if (isin === true) { |
| 1957 |
this.selected = true; |
| 1958 |
WidgetManager.mouseOwner = this; |
| 1959 |
renderFrame(this); |
| 1960 |
return true; |
| 1961 |
} |
| 1962 |
} |
| 1963 |
return false; |
| 1964 |
} |
| 1965 |
mouseMove(x, y) { |
| 1966 |
// As above. |
| 1967 |
if (this.hide === true) |
| 1968 |
return; |
| 1969 |
// Restrict to be within the figure area, at the least. |
| 1970 |
// By default (the code here, not this.drawSelFcn, whatever that might |
| 1971 |
// do), the dot doesn't get "lost," but it can be drawn partially outside |
| 1972 |
// the figure proper, leaving "crumbs" that aren't erased until you |
| 1973 |
// scroll the page. |
| 1974 |
let wh = getFigureRect(this); |
| 1975 |
if (this.testPosFcn(x, y, wh.w, wh.ha, wh.hb) === false) |
| 1976 |
return; |
| 1977 |
if (this.selected === true) { |
| 1978 |
this.widgetX = x; |
| 1979 |
this.widgetY = y; |
| 1980 |
renderFrame(this); |
| 1981 |
} |
| 1982 |
} |
| 1983 |
mouseUp(x, y) { |
| 1984 |
if (this.hide === true) |
| 1985 |
return; |
| 1986 |
// This is more round-about because of the possibility that the |
| 1987 |
// mouse-up occured outside the figure area. We need to reach |
| 1988 |
// the renderFrame() line whatever happens. |
| 1989 |
if (this.selected === true) { |
| 1990 |
this.selected = false; |
| 1991 |
let wh = getFigureRect(this); |
| 1992 |
if (this.testPosFcn(x, y, wh.w, wh.ha, wh.hb) === true) { |
| 1993 |
this.widgetX = x; |
| 1994 |
this.widgetY = y; |
| 1995 |
} |
| 1996 |
renderFrame(this); |
| 1997 |
} |
| 1998 |
} |
| 1999 |
} |
| 2000 |
// When things are "selected," draw them in this color. |
| 2001 |
DraggableDrawWidget.sColor = "blue"; |
| 2002 |
// A numerical value widget. This uses an HTML <input type = "number"> |
| 2003 |
// thing by putting it on top of the canvas. Using HTML this way is not the |
| 2004 |
// direction I want to go, but I do want to see if it's feasible and what's |
| 2005 |
// involved. Certain widgets that already exist in HTML are probably not |
| 2006 |
// worth building from scratch as "pure canvas" widgets. Also, this |
| 2007 |
// allows me to delay dealing with keyboard events. |
| 2008 |
// |
| 2009 |
// The idea is to put the HTML widget on top of the canvas, with absolute |
| 2010 |
// placement. |
| 2011 |
// |
| 2012 |
// NOTE: Firefox generates a warning about "ansynchronous panning." Somehow |
| 2013 |
// it sees that I'm doing something tricky and warns about it. As far as I |
| 2014 |
// can tell, I'm not doing anything likely to be deprecated or problematic |
| 2015 |
// in the future. Edge doesn't complain. |
| 2016 |
class NumberInputWidget extends Widget { |
| 2017 |
constructor() { |
| 2018 |
super(...arguments); |
| 2019 |
// An HTML <input type = "number"> thing. This is the HTML DOM element, |
| 2020 |
// like from document.getElementById() or createElement("input"). |
| 2021 |
this.theWidget = null; |
| 2022 |
} |
| 2023 |
static register(ctx, x, y, v, name) { |
| 2024 |
// Even fewer arguments than usual. |
| 2025 |
// v = initial value; |
| 2026 |
let type = "NumberInputWidget"; |
| 2027 |
let caller = getCaller(); |
| 2028 |
let w = WidgetManager.knownWidget(caller, type, name); |
| 2029 |
if (w != null) { |
| 2030 |
w.draw(ctx); |
| 2031 |
return w; |
| 2032 |
} |
| 2033 |
// Got here, so this widget is not already known, and must be created. |
| 2034 |
w = new NumberInputWidget(caller, type, x, y, 1.0, false, name); |
| 2035 |
// Note the existence of this widget for the future. |
| 2036 |
WidgetManager.register(w); |
| 2037 |
// The usual syntax for this within HTML is |
| 2038 |
// <input type="number" id="something" name="whatever" min="10" max="100"> |
| 2039 |
// There are some other possible settings too. See |
| 2040 |
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number |
| 2041 |
// |
| 2042 |
// BUG: There's some funny business here that I don't like at all. |
| 2043 |
// These DOM elements are placed relative to the body as a whole, |
| 2044 |
// not the canvas. So, I need to know where the figure containing |
| 2045 |
// this widget falls on the entire document. The body height is adjusted |
| 2046 |
// when I tweak the scroll bars (mainbody.style.height and width), |
| 2047 |
// and the widget needs to be placed relative to that. |
| 2048 |
// |
| 2049 |
// As a result, every time the user zooms (or resizes the window), |
| 2050 |
// every widget placed in the DOM this way needs to be repositioned. |
| 2051 |
// One reasonable way to deal with that is to recalculate the position |
| 2052 |
// every time draw() is called. |
| 2053 |
// |
| 2054 |
// So this isn't really a BUG; it works as intended. But using |
| 2055 |
// HTML this way is an entirely different (and unpleasant) approach. |
| 2056 |
w.theWidget = document.createElement("input"); |
| 2057 |
w.theWidget.setAttribute("type", "number"); |
| 2058 |
w.theWidget.value = v.toString(); |
| 2059 |
w.theWidget.style.position = "absolute"; |
| 2060 |
w.theWidget.style.display = "block"; |
| 2061 |
w.theWidget.style.left = 400 + "px"; |
| 2062 |
w.theWidget.style.top = 900 + "px"; |
| 2063 |
w.theWidget.style.width = 50 + "px"; |
| 2064 |
w.theWidget.style.height = 10 + "px"; |
| 2065 |
w.theWidget.style.zIndex = "99"; |
| 2066 |
document.body.appendChild(w.theWidget); |
| 2067 |
// The owner will want to redraw it's figure when this changes. |
| 2068 |
w.theWidget.onchange = function () { |
| 2069 |
// console.log("change"); |
| 2070 |
let myFunc = getAugmentedFunction(w.owner); |
| 2071 |
let fpc = myFunc.figurePanelClass; |
| 2072 |
fpc.render(); |
| 2073 |
}; |
| 2074 |
// Before returning the widget, it must be drawn. |
| 2075 |
w.draw(ctx); |
| 2076 |
return w; |
| 2077 |
} |
| 2078 |
getValue() { |
| 2079 |
// Return the numerical value. |
| 2080 |
// |
| 2081 |
// BUG: This seems to return a string -- whatever is in the field. |
| 2082 |
// The caller will typically need to do parseInt() or parseFloat() |
| 2083 |
// on the result. If this widget were better, then it would know |
| 2084 |
// what it's supposed to return and limit the possible things it can hold. |
| 2085 |
// BUG: I should probably have getter functions for everything in |
| 2086 |
// all widgets instead of having the user access fields directly. |
| 2087 |
// So, define LoopWidget.getCurStep(), etc. |
| 2088 |
// It doesn't *really* matter, but it makes it clearer to the user |
| 2089 |
// what he's supposed to have access to. |
| 2090 |
return this.theWidget.value; |
| 2091 |
} |
| 2092 |
draw(ctx) { |
| 2093 |
// This widget is really an HTML DOM element, so this function doesn't |
| 2094 |
// actually draw the widget. It repositions the element in the DOM. |
| 2095 |
// See the discussion in register(). |
| 2096 |
// BUG: make accessing this information a function, like I did |
| 2097 |
// for getFigureRect(). |
| 2098 |
let myFunc = getAugmentedFunction(this.owner); |
| 2099 |
let fpc = myFunc.figurePanelClass; |
| 2100 |
let totalV = fpc.totalV; |
| 2101 |
// The vertical position is relatively easy, but the horizontal position |
| 2102 |
// requires a calculation similar to fullRender() since the page |
| 2103 |
// is centered. Also, the vertical position must be given in LH coordinates |
| 2104 |
// relative to the entire document. |
| 2105 |
// BUG: Make this calculation a function used in main.js too. |
| 2106 |
// Can I combine with what's done for mouse events there too? |
| 2107 |
// In fact, this is more like the mouse calculation than like fullRender(). |
| 2108 |
let vpos = fpc.totalV + fpc.h - this.widgetY; |
| 2109 |
// let vpos = totalV + this.widgetY; |
| 2110 |
// BUG: I don't like reaching into the DOM this way to get the canvas, |
| 2111 |
// but what is the alternative? |
| 2112 |
let hpos = this.widgetX; |
| 2113 |
let canvas = document.getElementById("pdf_renderer"); |
| 2114 |
let visWidth = document.documentElement.clientWidth; |
| 2115 |
let totWidth = FullPanel.getFullWidth(); |
| 2116 |
if (visWidth > totWidth) { |
| 2117 |
// No horizontal scroll bar. Center it. |
| 2118 |
let canvasCenter = canvas.width / 2; |
| 2119 |
let docCenter = FullPanel.getFullWidth() / 2; |
| 2120 |
canvasCenter = canvasCenter / PDFDocument.getZoom(); |
| 2121 |
hpos = hpos + (canvasCenter - docCenter); |
| 2122 |
} |
| 2123 |
else { |
| 2124 |
// Shift according to the horizontal scroll bar. |
| 2125 |
hpos = hpos - window.scrollX; |
| 2126 |
} |
| 2127 |
hpos += fpc.margin; |
| 2128 |
//console.log("wid at " +hpos+ " " +vpos); |
| 2129 |
// Don't forget the stupid "px"! |
| 2130 |
this.theWidget.style.top = vpos + "px"; |
| 2131 |
this.theWidget.style.left = hpos + "px"; |
| 2132 |
} |
| 2133 |
} |
| 2134 |
// Another DOM-based widget. A simple button. |
| 2135 |
class ButtonWidget extends Widget { |
| 2136 |
constructor() { |
| 2137 |
super(...arguments); |
| 2138 |
// An HTML <input type = "button"> thing. |
| 2139 |
this.theWidget = null; |
| 2140 |
// Sometimes you want to treat the button as a boolean |
| 2141 |
// Each time the button is clicked, this toggles. |
| 2142 |
this.clickState = false; |
| 2143 |
// And this is set to true whenever the button is clicked. |
| 2144 |
this.resetState = false; |
| 2145 |
} |
| 2146 |
static register(ctx, x, y, text, name) { |
| 2147 |
// Even fewer arguments than usual. |
| 2148 |
let type = "ButtonWidget"; |
| 2149 |
let caller = getCaller(); |
| 2150 |
let w = WidgetManager.knownWidget(caller, type, name); |
| 2151 |
if (w != null) { |
| 2152 |
w.draw(ctx); |
| 2153 |
return w; |
| 2154 |
} |
| 2155 |
// Got here, so this widget is not already known, and must be created. |
| 2156 |
w = new ButtonWidget(caller, type, x, y, 1.0, false, name); |
| 2157 |
// Note the existence of this widget for the future. |
| 2158 |
WidgetManager.register(w); |
| 2159 |
// The usual syntax for this within HTML is |
| 2160 |
// <button type="button" id="something" name="whatever"> |
| 2161 |
// |
| 2162 |
// BUG: There's some funny business here that I don't like at all. |
| 2163 |
// These DOM elements are placed relative to the body as a whole, |
| 2164 |
// not the canvas. So, I need to know where the figure containing |
| 2165 |
// this widget falls on the entire document. The body height is adjusted |
| 2166 |
// when I tweak the scroll bars (mainbody.style.height and width), |
| 2167 |
// and the widget needs to be placed relative to that. |
| 2168 |
// |
| 2169 |
// As a result, every time the user zooms (or resizes the window), |
| 2170 |
// every widget placed in the DOM this way needs to be repositioned. |
| 2171 |
// One reasonable way to deal with that is to recalculate the position |
| 2172 |
// every time draw() is called. |
| 2173 |
// |
| 2174 |
// So this isn't really a BUG; it works as intended. But using |
| 2175 |
// HTML this way is an entirely different (and unpleasant) approach. |
| 2176 |
w.theWidget = document.createElement("button"); |
| 2177 |
w.theWidget.setAttribute("type", "button"); |
| 2178 |
w.theWidget.style.fontSize = "10px"; |
| 2179 |
w.theWidget.textContent = text; |
| 2180 |
w.theWidget.style.position = "absolute"; |
| 2181 |
w.theWidget.style.display = "block"; |
| 2182 |
w.theWidget.style.left = 400 + "px"; |
| 2183 |
w.theWidget.style.top = 900 + "px"; |
| 2184 |
//w.theWidget.style.width = 100+"px"; // Width automatic, based on text. |
| 2185 |
w.theWidget.style.height = 18 + "px"; |
| 2186 |
w.theWidget.style.zIndex = "99"; |
| 2187 |
document.body.appendChild(w.theWidget); |
| 2188 |
// Need to hear about clicks.... |
| 2189 |
// BUG: I would have thought that this would work, but I don't think that |
| 2190 |
// the doClick() method is seeing the correct class instance or something. |
| 2191 |
//w.theWidget.addEventListener('click',w.doClick,false); |
| 2192 |
// Instead, define the action here. |
| 2193 |
w.theWidget.addEventListener('click', () => { |
| 2194 |
if (w.clickState === false) |
| 2195 |
w.clickState = true; |
| 2196 |
else { |
| 2197 |
w.clickState = false; |
| 2198 |
w.resetState = true; |
| 2199 |
} |
| 2200 |
// If the figure is part of an animation, then it will be drawn taking |
| 2201 |
// into account this click in the next frame. If the figure is *not* |
| 2202 |
// an animation, then we need to refresh. |
| 2203 |
let myFunc = getAugmentedFunction(w.owner); |
| 2204 |
let fpc = myFunc.figurePanelClass; |
| 2205 |
fpc.render(); |
| 2206 |
}, false); |
| 2207 |
// Before returning the widget, it must be drawn. |
| 2208 |
w.draw(ctx); |
| 2209 |
return w; |
| 2210 |
} |
| 2211 |
doClick() { |
| 2212 |
// BUG: This doesn't work. See above. |
| 2213 |
if (this.clickState === false) |
| 2214 |
this.clickState = true; |
| 2215 |
else |
| 2216 |
this.clickState = false; |
| 2217 |
//console.log("swapped click"); |
| 2218 |
} |
| 2219 |
draw(ctx) { |
| 2220 |
// This widget is really an HTML DOM element, so this function doesn't |
| 2221 |
// actually draw the widget. It repositions the element in the DOM. |
| 2222 |
// See the discussion in register(). |
| 2223 |
let myFunc = getAugmentedFunction(this.owner); |
| 2224 |
let fpc = myFunc.figurePanelClass; |
| 2225 |
// The horizontal position requires a calculation similar to fullRender() |
| 2226 |
// since the page is centered and the vertical position must be given |
| 2227 |
// in LH coordinates relative to the entire document. |
| 2228 |
// BUG: Make this calculation a function used in main.js too. |
| 2229 |
// Can I combine with what's done for mouse events there too? |
| 2230 |
// In fact, this is more like the mouse calculation than like fullRender(). |
| 2231 |
let vpos = fpc.totalV + fpc.h - this.widgetY; |
| 2232 |
let hpos = this.widgetX; |
| 2233 |
// BUG: Again, with the DOM access. |
| 2234 |
let canvas = document.getElementById("pdf_renderer"); |
| 2235 |
let visWidth = document.documentElement.clientWidth; |
| 2236 |
let totWidth = FullPanel.getFullWidth(); |
| 2237 |
if (visWidth > totWidth) { |
| 2238 |
// No horizontal scroll bar. Center it. |
| 2239 |
let canvasCenter = canvas.width / 2; |
| 2240 |
let docCenter = FullPanel.getFullWidth() / 2; |
| 2241 |
canvasCenter = canvasCenter / PDFDocument.getZoom(); |
| 2242 |
hpos = hpos + (canvasCenter - docCenter); |
| 2243 |
} |
| 2244 |
else { |
| 2245 |
// Shift according to the horizontal scroll bar. |
| 2246 |
hpos = hpos - window.scrollX; |
| 2247 |
} |
| 2248 |
hpos += fpc.margin; |
| 2249 |
// It seems that these must be placed using style.top, not style.bottom. |
| 2250 |
// The clientHeight seems to be the height of the text in the button, |
| 2251 |
// which style.height is the actually button. Don't forget the stupid "px"! |
| 2252 |
let w = this.theWidget; |
| 2253 |
w.style.top = (vpos - parseInt(w.style.height, 10)) + "px"; |
| 2254 |
w.style.left = hpos + "px"; |
| 2255 |
} |
| 2256 |
} |