omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Problem positioning ShapeNode

    Pythonista
    ui.path scene
    3
    4
    970
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nerdtronn
      nerdtronn last edited by ccc

      I’m trying to draw a box in the center of the screen using the below. The box is drawn centered about the origin (lower left corner of the screen). It doesn’t seem to respect the x and y values I’m giving it. I’m sure I’m missing something obvious :). Can anyone point me in the right direction?

      import scene as sc
      import ui
      
      
      class MyScene (sc.Scene):
        def setup(s):
          s.background_color = 'blue'
          s.bdr = sc.ShapeNode()
          s.bdr.fill_color = (0,0,0,0)
          s.bdr.stroke_color = 'yellow'
          cx = s.size.width
          cy = s.size.height
          s.bdr.path = ui.Path.rect(cx / 2, cy / 2, 100, 100)
          s.bdr.line_width = 3
          s.add_child(s.bdr)
      
      
      sc.run(MyScene())
      
      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @nerdtronn last edited by

        @nerdtronn try

        		s.bdr = sc.ShapeNode()
        		s.bdr.position = s.size/2
        
        1 Reply Last reply Reply Quote 1
        • JonB
          JonB last edited by JonB

          Just to expand a bit, shapenode can be a bit confusing and frustrating to work with.

          When you draw a path, that path has some coordinate system -- eg your line_to's, rects, etc.

          But that natural coordinate system basically gets thrown away -- the path is converted to an image, then the bounding box of that image becomes the new coordinate system (you can define the anchor_point to position the Sprite based on the corner, or center).

          What that means is that if you had Path drawing code that worked inside draw(), and want to convert individual paths to ShapeNodes, the relative positions get all screwed up, because there is not a good way to know for complex paths where the bottom left corner of the bounds will be. As a simple example, line_to(0,0,50,50) will have the anchor point at bottom left as you might expect. But line_to(0,0,50,-50) will not even have the anchor point on the line, unless you manually adjust anchor_point =[0,1]

          1 Reply Last reply Reply Quote 1
          • nerdtronn
            nerdtronn last edited by

            Thanks for the replies! After reading them a few times and experimenting I think I'm understanding it now.

            To create and position a rect using paths, I'm creating it with ui.Path.rect() and just giving 0,0 for the position with the desired size. Then I'm setting the position property to locate the center where I want it.

            Seems to be working, thanks for the help!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors