Proof of concept: base classes for options, events, interactions #93
Proof of concept: base classes for options, events, interactions #93mggower wants to merge 4 commits intotechdebt/importsfrom
Conversation
There was a problem hiding this comment.
I am anticipating some push back against the use of interfaces over types here. When it comes to inheritance/sharing properties interfaces are by far the easier/cleaner approach. By leaning into inheritance we will be less likely to have inconsistencies in our type model
There was a problem hiding this comment.
This is probably just my preference for functional programming over oop, but my assumption was that anything you can do w/ types you can do with interfaces. e.g.
interface X extends Y { foo: string, bar: string }
// same as
type X = Y & { foo: string, bar: string }With the notable difference that types can express both intersections & and unions |. Typescript briefly notes the differences here, and honestly I like it that you can't mutate a type while you can mutate an interface (mutating an interface feels like a bug in waiting).
It's worth noting that the creator of Typescript also created C#. C#, like Java, is heavily oriented towards oop, but has also incorporated a lot of functional concepts on top. What if they both (and Typescript...) had just started with a simple functional pattern, avoided oop, and cut the language's surface area in half...?
| import { Container, DisplayObject } from 'pixi.js' | ||
| import { RenderObjectLifecycle } from '../types' | ||
|
|
||
| export default abstract class RenderObject<T extends DisplayObject = DisplayObject> implements RenderObjectLifecycle { |
There was a problem hiding this comment.
RenderObject
- abstract class serves as a base for any display object in an app. i.e. Strokes, Icon, etc.
- creates a required structure for all display objects making them easy to manage in a generic way
- simple base methods can be overridden or enhanced in practice
| import { Options, DefaultOptions, Viewport, Dimensions } from '../types/api' | ||
| import { DEFAULT_OPTIONS, isNumber } from '../utils' | ||
|
|
||
| export default class RendererOptions implements DefaultOptions { |
There was a problem hiding this comment.
RendererOptions
- straight forward class that manages and updates renderer options
- automatically applies defaults
- single instance that can be a shared as a dependency without losing reference to the object after updates
| import { EventHandlers } from '../../types/api' | ||
| import { EventSystem } from 'pixi.js' | ||
|
|
||
| export default class ApplicationEvents { |
There was a problem hiding this comment.
ApplicationEvents
- similar to
RendererOptions, this class will manage and update application events - can also manage other properties like the
EventSystemfor easy access in relevant classes
| import Drag from './plugins/Drag' | ||
| import Zoom from './plugins/Zoom' | ||
|
|
||
| export default class InteractionManager { |
There was a problem hiding this comment.
InteractionManager
- primarily for organization and usability
this:
this.renderer.dragInteraction.dragging
this.renderer.zoomInteraction.zooming
this.renderer.interactionObjectManager.mount(this.hitArea)
this.renderer.interactionbecomes this when you share the reference instead of the entire renderer:
this.interactions.isDragging
this.interactions.isZooming
this.interactions.manager.mount(this.hitArea)
this.interactions.container| import Interaction from '../Interaction' | ||
| import HitArea from '../hitArea/HitArea' | ||
|
|
||
| export default abstract class EventsManager extends Interaction { |
There was a problem hiding this comment.
EventsManager
- will be the base class for defining things like node events, viewport events, etc.
- simplifies the relationship between hit area + events
| import { AllFederatedEventMap, Container, IHitArea } from 'pixi.js' | ||
| import RenderObject from '../../RenderObject' | ||
|
|
||
| export default class HitArea<T extends IHitArea = IHitArea> extends RenderObject<Container> { |
There was a problem hiding this comment.
HitArea
- base class can be used on its own or extended to define a particular shape (circle, line, rect, etc.)
nothing in here is "plugged" in to the renderer yet, but this stands as a proof of concept for some structured base classes