Thread Rating:
  • 337 Vote(s) - 2.71 Average
  • 1
  • 2
  • 3
  • 4
  • 5
画面内でのオブジェクトの位置
11-29-2011, 03:23 PM,
#1
画面内でのオブジェクトの位置
Curl のオブジェクトが、画面内のどこにあるのかを取得するサンプルを作ってみました。
GRect、transform-from-display-coordinates を利用すれば取得できるんですね。

Code:
|| SimpleEditableCanvas is a drop target
{define-class public SimpleEditableCanvas {inherits Canvas}
  {constructor {default ...}
    {construct-super ...}
  }
  
  {method public {on-drop e:Drop}:void
    {e.accept-drop
        {proc {a:any,
               x:Distance,
               y:Distance,
               effect:#DragEffect}:DropResult
            || Only support move
            {if {effect.has-effect? "move"} then
                {return {DropResultMove action =
                    {proc {}:void
                        || get the dragee option from
                        || the object being dropped
                        let dragee:Dragee = a.dragee
                        || compute the new position in the drop
                        || container
                        let (real-x:Distance, real-y:Distance) =
                            {dragee.get-drop-position x, y, self}
                        {self.add a, x = real-x, y = real-y}
                    }}}
             else
                {return {DropResultNone}}
            }
        }}
    {e.consume}
    {super.on-drop e}
  }
  
  {method public {on-drag-over e:DragOver}:void
    {e.will-accept-drop?
        {proc {t:Type,
               x:Distance,
               y:Distance,
               effect:#DragEffect}:DragEffect
            || only supports move
            {if {effect.has-effect? "move"} then
                {return drag-effect-move}
             else
                {return drag-effect-none}
            }
        }}
    {e.consume}
    {super.on-drag-over e}
  }
}

{value let simple-canvas =
    {SimpleEditableCanvas
        width = 3in,
        height = 2in,
        background = "beige"}
    let simple-canvas-obj =
        {Frame
            width = 0.5in,
            height = 0.25in,
            background = "blue",
            dragee = {ImageDragee},
            {on pp:PointerPress at fr:Frame do
                {if pp.click-count == 2 then
                    
                    
                    def grect:GRect = {fr.get-bounds}
                    
                    let (goir-x:Distance, goir-y:Distance, goir-root:Graphic) =
                        {fr.get-origin-in-root}
                    
                    def  (ttdc-new-x:Distance, ttdc-new-y:Distance) =
                        {fr.transform-to-display-coordinates
                            grect.lextent, grect.ascent
                        }
                    
                    def v:View =
                        {View
                            {VBox
                                margin = 3pt,
                                "画面左上から (" & goir-x / 1pt & "pt, " & goir-y / 1pt & "pt)",
                                "ブラウザの左上から (" & ttdc-new-x / 1pt & "pt ," & ttdc-new-y / 1pt & "pt)",  
                                "width = " & grect.width / 1pt & "pt",
                                "height = " & grect.height / 1pt & "pt"
                                
                            }}
                    {v.show}
                    
                }
            }
        }
    {simple-canvas.add simple-canvas-obj, x = 1.5in, y = 1in}
    {value simple-canvas}
}
11-30-2011, 09:43 AM,
#2
RE: 画面内でのオブジェクトの位置
青い四角のダブルクリックでダイアログが出るんですね。

「画面左上から」と「ブラウザの左上から」の文言は
逆でしょうか。
ブラウザのウィンドウを移動して試すと
「ブラウザの左上」の値だけが変わります。
11-30-2011, 01:54 PM, (This post was last modified: 11-30-2011, 01:54 PM by umemura.)
#3
RE: 画面内でのオブジェクトの位置
(11-30-2011, 09:43 AM)ashimo Wrote: 「画面左上から」と「ブラウザの左上から」の文言は
逆でしょうか。


スイマセン。逆でした。

ちなみに、複数のディスプレイをつなげている場合は、プライマリのディスプレイを基点にしているようですね。

あと、get-bounds で取得できる GRect は、width などをelastic で指定して、
実際の値をDistanceでうまく取得できないときなどにも使えて便利な気がします。



Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('354')