jamshark70

Avatar

jamshark70's code

Demo of Model-View-Controller design

1
08 Dec'18 20:49 in guiviewcontrollermvcmodel

This question comes up often: What is the best way to structure GUI code. IMO, Model-View-Controller (when done correctly) gives you the most flexibility while avoiding common bugs. But it's hard to wrap your head around it first. So, here's a quick example using NodeProxies. - Model: This is the thing that is actually doing the work. Here, it's a control proxy representing a frequency value. When something interesting happens in the model (e.g., the frequency changes), it should broadcast a notification (in SC, using the '.changed' method). NodeProxy already does this internally (which is why I'm using it for this example!). - View: The GUI display -- a Slider, in this case. The view knows who its model is, and should respond to user action by updating the model. - Controller: An object that receives notifications from the model and communicates with the view. This SimpleController object is looking for '\source' notifications, and changes the view's displayed value. Benefits: You can make as many views as you want, on the same model, and their separate controllers all receive the notifications and keep everything in sync *without* having to manage exponentially-increasing connections. You can set the model programmatically and all views automatically update. Drawbacks: The model needs to send notifications. Sometimes it's hard to anticipate which notifications will be needed. Bugs can be hard to track down (but when each component is doing its job correctly, it ends up being easier to maintain).

Decode %hex encoded characters

15 Oct'18 03:57 in utilitystringhex

A quick utility function for decoding UTF-8 characters in %hex codes (such as for web URLs). Save the code into a .sc file in your Extensions/ directory, recompile, and then: "%E5%A4%A7%E5%AE%B6%E5%A5%BD".decodeHex; -> 大家好 This probably works only for UTF-8. SC can save the individual bytes of Unicode characters into String objects, and the IDE will print UTF-8 code points correctly, but there is no way to switch the IDE to other Unicode variants.