Message Boards Message Boards

EventHandler polymorphism with OOP

Posted 1 year ago

In the process of coding GraphEditor, at one point in the process of modifying the code, a need emerged to handle multiple EventHandler parts in an easy-to-understand manner. Therefore, I tried to simplify the handling of multiple EventHandlers by encapsulating them in object-oriented programming (OOP). In other words, by using OOP polymorphism, a good prospect was obtained.

Wolfram Document Sample of EventHandler

The Wolfram Document contains a sample that illustrates how to use EventHandler.

DynamicModule[{col = Green}, 
 EventHandler[
  Dynamic[Graphics[{col, Disk[]}, 
    ImageSize -> 
     Tiny]], {"MouseClicked" :> (col = 
      col /. {Red -> Green, Green -> Red})}]]

Object Oriented Programming for EventHandler First, let's try to express EventHandler in OOP.

diskClass1[name_[_]] := Module[
  {col = Green},
  style[name[test]] := 
   EventHandler[
    Dynamic[Graphics[{col, Disk[]}, 
      ImageSize -> 
       Tiny]], {"MouseClicked" :> (col = 
        col /. {Red -> Green, Green -> Red})}]
  ]

Construct an instance named alpha from class,

diskClass1[alpha[x]]

Ensure that it can be executed.

style[alpha[test]]

EventHandler Polymorphism with Method Preceded Definition

Next, let us try to represent multiple EventHandler by polymorphism, a property of OOP.

diskClass2[name_[_]] := Module[
  {col1 = LightGreen,
   col2 = LightGreen,
   toggler = {LightRed -> LightGreen, LightGreen -> LightRed}},
  style[name[clickable]] := 
   EventHandler[
    Dynamic[Graphics[{col1, Disk[]}, 
      ImageSize -> 
       Tiny]], {"MouseClicked" :> (col1 = col1 /. toggler)}];
  style[name[stilldown]] := 
   EventHandler[
    Dynamic[Graphics[{col2, Disk[]}, 
      ImageSize -> Tiny]], {"MouseDown" :> (col2 = col2 /. toggler),
     "MouseUp" :> (col2 = col2 /. toggler)}]
  ]

Construct an instance from the class, and then use as,

diskClass2[alpha[x]]

The same method, but with different parameters, appears to perform different functions .

{style[alpha[clickable]], style[alpha[stilldown]]}

EventHandler Polymorphism with Instance Preceded Definition

From a code-reading perspective, the order, instance-name [method [style]] might be better than calling it, method [instance-name [style]].

diskClass2[name_[_]] := Module[
  {col1 = LightGreen,
   col2 = LightGreen,
   toggler = {LightRed -> LightGreen, LightGreen -> LightRed}},
  name[style[clickable]] := 
   EventHandler[
    Dynamic[Graphics[{col1, Disk[]}, 
      ImageSize -> 
       Tiny]], {"MouseClicked" :> (col1 = col1 /. toggler)}];
  name[style[stilldown]] := 
   EventHandler[
    Dynamic[Graphics[{col2, Disk[]}, 
      ImageSize -> Tiny]], {"MouseDown" :> (col2 = col2 /. toggler),
     "MouseUp" :> (col2 = col2 /. toggler)}]
  ]

Construct an instance named alpha from class,

diskClass2[alpha[x]]

Let different behavior styles be instances of the same.

{alpha[style[clickable]], alpha[style[stilldown]]}

Since it is OOP, it is possible to create another instance with a different name.

diskClass2[beta[x]]

{beta[style[clickable]], beta[style[stilldown]]}

Enjoy.

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: Moderation Team
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract