Tuesday, August 26, 2008

Dragging elements in Silverlight with DLRConsole

DLRConsole Rocks

Dragging elements around in Silverlight is not trivial, so I wrote a Drag class in IronRuby during a demo of DLRConsole to show how cool REPL-programming is. I did a 10-minute screencast of this same demo a while ago, but I've given this demo enough times that it deserves a post on its own.

Meet DLRConsole

Can't show dragging without having stuff to drag, can I? Well, rather than requiring you to download a bunch of stuff, let's just program in the browser. Yes, the browser.

DLRConsole

DLRConsole (download)

You'll need Silverlight 2 Beta 2, but DLRConsole will tell you to install Silverlight if you don't have it.

If you haven't been introduced to DLRConsole yet, there she is. Pretty straight-forward; a Ruby and Python REPL prompt on the left for typing code in, and a Silverlight canvas on the right for pretty stuff to come out. Got it? Good.

Makin' some stuff to drag

So what do you want to drag around? I know, a clock? Of course, since we can't get enough of those freakin' things in Silverlight-land. I've done you a favor and build a clock already, so you don't have to. You're welcome. Click on the "Ruby" text on the bottom left to switch the console to Ruby, and then type the following in the prompt:

require 'lib/clock'
$clock = Clock.show
DLRConsole clock

Awesome, a clock, in all it's majesty. You might have the urge to try to click said clock to move it ... go ahead, it won't bite.

Drag that face all over the place

Didn't move, huh? Sucks. Let's fix that. Go back to the console and type this:

require 'lib/drag'
Drag.new($clock.canvas)

And for the curious, here's the code in lib/drag.rb:

class Drag
  def initialize(obj)
    @click = nil
    @obj = obj
  end
  
  def enable
    @obj.mouse_left_button_down do |s,e| 
      @click = e.get_position @obj
    end
    Application.current.root_visual.mouse_left_button_up do |s,e| 
      @click = nil
    end
    canvas.mouse_move do |s,e|
      unless @click.nil?
        mouse_pos = e.get_position canvas
        @obj.left, @obj.top = mouse_pos.x - @click.x, mouse_pos.y - @click.y
      end
    end
    self
  end
end

Now click, hold, and moooooove! Awesome, we can move shit. Go ahead, get carried away. Try to find that "Silverlight Canvas" TextBlock and make that dragg-able too. Enjoy your new found powers.

7 comments:

Alex said...

Hi Jimmy,

I tried dragging the clock according to your instructions but it didn't work in neither Firefox 3 or Safari 3.1.2, on Mac OS X Tiger with the latest version of Silverlight.

Jimmy Schementi said...

Interesting, doesn't work in tiger? Can you be more specific? What type of mac do you have? Does dlrconsole run, but the code you type in not work? All the screenshots are from leopard.

Jimmy Schementi said...

umm, it's probably a caching issue, try clearing your browser cache. I had issues this morning with one of my macs not downloading the latest version.

Anonymous said...

Jimmy

where do I get the latest zip of console for mac with jscript?

dvorak on tiger only maps char not punctuation!

can I use haxe to create jscript as it creates javascript?

cheers Justin

Air Jordan 4 said...

What a great post! Just one suggestion:If you add some pics, it would be easiler to follow!
By Air Jordan 4

myBlog said...
This comment has been removed by the author.
myBlog said...

Hi Jimmy,
I tried dragging the clock according to your instructions.
It's so cool!
and I tried dragging the clock in WPF/IronPython.
myBlog: IronPython ...《 Drag clock 》
http://softgarden.blog.ocn.ne.jp/blog/2011/07/ironpython_drag.html
thank you.