diff --git a/Adding-new-objects.md b/Adding-new-objects.md index 9be46a3..55e345d 100644 --- a/Adding-new-objects.md +++ b/Adding-new-objects.md @@ -35,7 +35,8 @@ To create an object, create a new file javascript files in `LogarithmPlotter/qml You can also import other libraries like this: - `.import "../utils.js" as Utils` for string manipulation. - `.import "../objects.js" as Objects` to interact with other objects. -- `.import "../historylib.js" as HistoryLib` to create and manipulate history entries. +- `.import "../objects.js" as Objects` to interact with other objects. +- `.import "../math/latex.js" as Latex` to use LaTeX helper functions. There exists two kinds of objects: - Drawable (extending the `Common.DrawableObject` class, the most primitive kind of object) @@ -46,6 +47,7 @@ There exists two kinds of objects: Executable objects can be targeted by XCursors to calculate their value at a given x coordinate. So to create a new object, choose one of the two to extend, and then create a class in objects.js extending it's class. + ### Methods required for your class: - `static type() -> string` - Returns the type of the object (should be constant, non translatable). @@ -70,7 +72,7 @@ So to create a new object, choose one of the two to extend, and then create a cl - Lists: `new P.List(, format = /^.+$/, label = '', forbidAdding = false)` - Dictionaries: `new P.Dictionary(valueType: , keytType: , format = /^.+$/, preKeyLabel = '', postKeyLabel = ': ', forbidAdding = false)` - Other objects: `new P.ObjectType()` - - In order to allow the properties to be properly translated, you shoudl apply the `QT_TRANSLATE_NOOP` macro to both property names (in the `prop` namespace) and comments (in the `comment` namespace). For example: + - In order to allow the properties to be properly translated, you should apply the `QT_TRANSLATE_NOOP` macro to both property names (in the `prop` namespace) and comments (in the `comment` namespace). For example: ```js static properties() {return { [QT_TRANSLATE_NOOP('prop','expression')]: 'Expression', @@ -92,7 +94,7 @@ So to create a new object, choose one of the two to extend, and then create a cl }} ```` - - For enums that need to be translated, an alias should be created in `parameters.js`, and then use the static class value in your parameter. For example: +- For enums that need to be translated, an alias should be created in `parameters.js`, and then use the static class value in your parameter. For example: ```js Enum.XCursorValuePosition = new Enum( QT_TR_NOOP('Next to target'), @@ -118,7 +120,7 @@ Enum.XCursorValuePosition = new Enum( om_0.name = getNewName('ω') om_0.color = this.color om_0.labelContent = 'name' - om_0.labelPosition = this.phase.execute() >= 0 ? 'bottom' : 'top' + om_0.labelPthis.drawLabel(canvas, ctx, this.labelPosition, canvas.x2px(this.labelX), canvas.y2px(this.execute(this.labelX)))osition = this.phase.execute() >= 0 ? 'bottom' : 'top' history.addToHistory(new HistoryLib.CreateNewObject(om_0.name, 'Point', om_0.export())) labelPosition = 'below' } @@ -131,7 +133,11 @@ Enum.XCursorValuePosition = new Enum( - `color` argument should be exported using the `toString()`, Expressions should be exported using `toEditableString` - `getReadableString() -> string` - Returns the string that should be displayed as help in the objects list and as the label when using the 'name + value' labelContent. - - You can make expressions easily displayable using it's `toString()` method. + - You can make expressions displayable using its `toString()` method. +- `getLatexString() -> string` + - Returns the LaTeX rendered that should be displayed as label on the canvas when using the 'name + value' labelContent. + - You can turn expressions and sets into LaTeX by using their `latexMarkup` properties. + - Similarly, variables should always be parsed with the helper function `Latex.variable` in order to render special characters (greek letters, sub and supscript numbers...) - `execute(x = 1) -> number?` - Only required for ExecutableObject. - Returns the executed value of the object for a given x. Return null when current x cannot return a value. @@ -143,41 +149,12 @@ Enum.XCursorValuePosition = new Enum( - Returns the simplified expression for a given x. - `draw(canvas, ctx)` - Main method for drawing the objects using the [Canvas Context2D](https://doc.qt.io/qt-5/qml-qtquick-context2d.html) methods and the additional methods described below. - - ExecutableObjects can draw their label if they have a labelX and labelPosition using the following code used in numerous other objects: + - ExecutableObjects can draw their label if they have a labelX and labelPosition using the the `drawLabel` object function: ```js - var text = this.getLabel() - ctx.font = `${canvas.textsize}px sans-serif` - var textSize = canvas.measureText(ctx, text) - var posX = canvas.x2px(this.labelX) - var posY = canvas.y2px(this.execute(this.labelX)) - switch(this.labelPosition) { - case 'above': - canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height) - break; - case 'below': - canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height) - break; - case 'left': - canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2) - break; - case 'right': - canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2) - break; - case 'above-left': - canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height) - break; - case 'above-right': - canvas.drawVisibleText(ctx, text, posX, posY-textSize.height) - break; - case 'below-left': - canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height) - break; - case 'below-right': - canvas.drawVisibleText(ctx, text, posX, posY+textSize.height) - break; - } + this.drawLabel(canvas, ctx, this.labelPosition, canvas.x2px(this.labelX), canvas.y2px(this.execute(this.labelX))) ``` - You can also use `Function.drawFunction(canvas, ctx, expr, definitionDomain, destinationDomain, drawPoints = true, drawDash = true)` to rapidly draw a function created on the canvas. + ### Other optional methods: - `update()` - Called every time a property of the object is changed.