KyrietS / CodeMap

CodeMap is a graphical editor for creating source code maps. It is ideal for creating quick projections of complex modules with many dependencies.
MIT License
1 stars 1 forks source link

Try event-based canvas approach #70

Closed KyrietS closed 4 months ago

KyrietS commented 5 months ago

Instead of rendering canvas in an immediate mode and executing scripts in immediate mode I might want to try event-based approach. It means that change on the canvas will be triggered by an incoming event and not Update() function.

I would also limit the use of Input:: functions and scrap the Entity Component System which slows me down too much. Instead, I propose the old-fashioned classes with composition and inheritance.

Implementation progress (features)

KyrietS commented 4 months ago

On branch new-canvas I re-implemented the whole Canvas replacing Entity Component System with good-old but simple classes, interfaces and polymorphism. Representing everything as an entity with components attached to it is highly scalable. Unfortunately attaching Native Script to every entity in order to add some behaviour to them is very difficult to maintain.

Implementing everything in an immediate mode fashion is easy at first but when it comes to handling some interactions between elements it starts to be a nightmare. That's why I needed an event-based approach to implement logic for this app. Now I have full control over the order of which events are propagated. I also have the ability to "Handle" the event and automatically stop propagating it further. It's extremely useful, for example:

CommonKeyboardShortcutsController has the priority of handling keyboard events, like KeyPressed/KeyReleased. If it detects pressing "Z" while "Ctrl" is being held then the event is marked as Handled and new event (Events::Canvas::Undo) is emited. This way I don't have to worry that KeyPressed for "Z" key will be handled somewhere else in the application. It was handled and consumed in CommonKeyboardShortcutsController and that's it. Implementing something like this in OnUpdate() function with classic NativeScript approach would require much more effort.

+ Another good change was scrapping Transform component and just using world positions inside elements. All position calculation is done in canvas (world) coordination space.

+ Added generic ControlPoint that can be used in multiple canvas elements.

+ Fixed many bugs

Overall I can conclude this experiment as a big success.

KyrietS commented 4 months ago

Now it's official ed27d2ae605bfbb0827be100d340faa2ee4b265f 🥳