Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:51:39+00:00 2026-05-27T03:51:39+00:00

So I’m implementing a svg-editor-like GUI on one application I’m working here. These are

  • 0

So I’m implementing a svg-editor-like GUI on one application I’m working here. These are some examples of the logic that would be needed on it:

  • If the user clicks with the right button on the canvas, a new node should be created, and subsequent nodes should be “linked” with a line, forming a polygon
  • If the user clicks with the left button on a node, I should move the entire set of polygons accordingly to the mouse position
  • The user is able to remove nodes
  • Selected nodes should be colored differently
  • The user is able to select multiple nodes by pressing SHIFT and clicking on nodes

And so on.

I’ve already implemented all of these items, but I didn’t liked the end result, mainly because I had to use a lot of flags to manipulate states (mouse clicked && left button && not moving? do this), and surely this code could be more elegant. So I’ve researched a little and came to these options:

  • Pipeline pattern: I would create classes that would handle each logical event separately, and use a priority order to provide what to do/what would be handed first, and how the event would propagate to the subsequent Pipeline items.

  • MVC: this is the most common response but how I could use it to make the code more clean is very blurry to me at the moment.

  • State Machine: That would be nice but managing the granularity of the state machine would be complicated

So I’m asking the S.O. gurus on tips on how to build a better and happier code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:51:39+00:00Added an answer on May 27, 2026 at 3:51 am

    I suggest separating the logic for the mapping of UI inputs to a specific operation into dedicated objects. Lets’s call them Sensor object. Not knowing your implementation language, I’ll be generic with this, but you should get the idea.

    OperationSensor
    + OnKeyDown
    + OnKeyPress
    + OnKeyUp
    + OnLeftMouseDown
    + OnLeftMouseUp
    + OnNodeSelect
    + OnNodeDeselect
    + OnDragStart
    + OnDragStop
    

    Let’s say you have a central class that aggregates all the various UI inputs, UiInputManager. It uses the language specific mechanisms to listen for keyboard and mouse input. It also detects basic operations such as detecting that if the mouse is depressed, and then moved, that is a logic “drag”.

    UiInputManager
    // event listeners
    + keyboard_keydownHandler
    + keyboard_keyupHandler
    + mouse_leftdownHandler
    + mouse_rightdownHandler
    // active sensor list, can be added to or removed from
    + Sensors
    

    The UiInputManager is NOT responsible for knowing what operations those inputs are causing. It simply notifies its Sensors in a language specific way.

    foreach sensor in Sensors
        sensor.OnDragStarted
    

    or, if the sensors listen for logical events issued by the UiInputManager

    RaiseEvent DragStarted
    

    What you have now is the plumbing to route input to the OperationSensor subclasses. Each OperationSensor has the logic just pertaining to a single operation. If it detects the operation’s criteria has been met then it creates the appropriate Command object and passes it back up.

    // Ctrl + zooms in, Ctrl - zooms out
    ZoomSensor : OperationSensor
    
       override OnKeyDown
       {
          if keyDown.Char = '+' && keyDown.IsCtrlDepressed
             base.IssueCommand(new ZoomCommand(changeZoomBy:=10)
          elseif keyDown.Char = '-' && keyDown.IsCtrlDepressed
             base.IssueCommand(new ZoomCommand(changeZoomBy:=-10)                                 
       }
    

    I would recommend that the command objects pass from the Sensors to the UiInputManager. The manager can then pass them into your command processing subsystem. This gives the manager an opportunity to perhaps notify the Sensors that an operation completed, allowing them to reset their inner state if needed.

    Multi-step operations can be handled in two different ways. You can either implement inner state machines inside a SensorOperation, or you can have a “step 1” sensor create a “step 2” sensor and add it to the active sensor list, possibly even removing itself from the list. When “step 2” completes, it can re-add the “step 1” sensor and remove itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.