iwmlib/lib/pixi/badge.js

113 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2019-03-21 09:57:27 +01:00
import Theme from './theme.js'
import AbstractPopup from './abstractpopup.js'
import Tooltip from './tooltip.js'
/**
* Class that represents a PixiJS Badge.
2019-07-18 08:59:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @example
* // Create the app
* const app = new PIXIApp({
* view: canvas,
* width: 900,
* height: 250
* }).setup().run()
2019-07-18 08:59:39 +02:00
*
2019-03-21 09:57:27 +01:00
* // Add an DisplayObject to the app
* const circle = new PIXI.Graphics()
* circle.beginFill(0x5251a3)
* circle.drawCircle(50, 50, 40)
* app.scene.addChild(circle)
2019-07-18 08:59:39 +02:00
*
2019-03-21 09:57:27 +01:00
* const badge1 = new Badge({
* object: circle,
* container: app.scene,
* content: 'Das Gesetz ist der Freund des Schwachen.'
* })
*
* @class
* @extends AbstractPopup
* @see {@link https://www.iwm-tuebingen.de/iwmbrowser/lib/pixi/badge.html|DocTest}
*/
export default class Badge extends AbstractPopup {
/**
* Creates an instance of a Badge.
2019-07-18 08:59:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @constructor
* @param {object} [opts] - An options object to specify to style and behaviour of the badge.
* @param {number} [opts.minWidth=0] - The minimum width of the badge.
* @param {number} [opts.minHeight=0] - The minimum height of the badge.
* @param {number} [opts.padding=Theme.padding / 2] - The inner spacing of the badge.
* @param {string|object} [opts.tooltip] - A string for the label of the tooltip or an object to configure the tooltip
* to display.
*/
constructor(opts = {}) {
const theme = Theme.fromString(opts.theme)
2019-07-18 08:59:39 +02:00
opts = Object.assign(
{},
{
minWidth: 0,
minHeight: 0,
padding: theme.padding / 2,
tooltip: null
},
opts
)
2019-03-21 09:57:27 +01:00
super(opts)
this.tooltip = null
// setup
//-----------------
this.setup()
// layout
//-----------------
this.layout()
}
2019-07-18 08:59:39 +02:00
2019-03-21 09:57:27 +01:00
/**
* Creates children and instantiates everything.
*
* @private
* @override
* @return {Badge} A reference to the badge for chaining.
*/
setup() {
super.setup()
// tooltip
//-----------------
if (this.opts.tooltip) {
if (typeof this.opts.tooltip === 'string') {
2019-07-18 08:59:39 +02:00
this.tooltip = new Tooltip({
object: this,
content: this.opts.tooltip
})
2019-03-21 09:57:27 +01:00
} else {
2019-07-30 16:56:29 +02:00
this.opts.tooltip = Object.assign({}, { object: this }, this.opts.tooltip)
2019-03-21 09:57:27 +01:00
this.tooltip = new Tooltip(this.opts.tooltip)
}
}
return this
}
2019-07-18 08:59:39 +02:00
2019-03-21 09:57:27 +01:00
/**
* Should be called to refresh the layout of the badge. Can be used after resizing.
2019-07-18 08:59:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @override
* @return {Badge} A reference to the badge for chaining.
*/
layout() {
super.layout()
2019-07-30 16:56:29 +02:00
this.content.x = this.width / 2 - this.content.width / 2 - this.opts.strokeWidth / 2
this.content.y = this.height / 2 - this.content.height / 2 - this.opts.strokeWidth / 2
2019-03-21 09:57:27 +01:00
return this
}
}