Making a graphical editor with Ruby. How?

J

Joao Barros

I would like to build a simple graphical editor in Ruby. The editor
should allow a subset of the usual graphical editing operations: draw
boxes, circles, connectors, text labels, copy, paste, and some more.
What is the best way to do it in Ruby? Is there some graphic library?
Should I use another language (library) together with Ruby? Which one.
Thanks in advance for any help.

Joao
 
G

gabriele renzi

il 29 Oct 2003 11:01:17 -0800, (e-mail address removed) (Joao Barros) ha
scritto::
I would like to build a simple graphical editor in Ruby. The editor
should allow a subset of the usual graphical editing operations: draw
boxes, circles, connectors, text labels, copy, paste, and some more.
What is the best way to do it in Ruby? Is there some graphic library?
Should I use another language (library) together with Ruby? Which one.
Thanks in advance for any help.


I think you can do it with all the GUI supported from ruby (fltk, FOX,
GTK, Tk [1])
At least I can tell that the FXRuby distribution comes with a scribble
sample application


[1] dunno if wxWindows and Qt are actually usable from ruby
 
J

Jeremy Henty

I would like to build a simple graphical editor in Ruby. ... What
is the best way to do it in Ruby?

I recommend Ruby-Gnome2 and the GnomeCanvas. You get lots of basic
primitives such as lines, arcs, ellipses etc. for free and a nice
interface for binding actions to GUI events. I can't say this is the
best way because I haven't used any other approach in anger, but I do
think it works very well. Kudos to the Ruby-Gnome2 team!

Jeremy Henty
 
N

Nathan Weston

I've actually been working on a project like this for a while now
(specifically for drawing maps, for RPGs and such). The big thing you
will need is a canvas which does collision detection -- so you can
tell when a line on the canvas or whatever is moused over, allowing
the user to click on it and move it, etc. You'll also want a fairly
rich set of canvas items.

Tk: This is what I'm using right now. It has good ruby bindings which
your users won't have to build themselves. It does collision detection
and the canvas API is pretty powerful overall.
On the downside, image support is limited, and text items can't be
rotated (which I would like to do). The look and feel is rather
archaic, and the built-in widgets are sub-par. Non-canvas things are
sometimes a pain.
I'm considering moving to another toolkit, but first I have to find
one with a sufficiently powerful canvas, or implement the needed
canvas features myself, which I lack the time an expertise for at the
moment.

Qt: Has collision detection.
Lines are only single-segment, with width 1, and no curves. Polygons
are solids with no outline. You can draw more complex stuff, but not
as full-fledged canvas items with collision detection.
It is very difficult to implement your own canvas items, as the APIs
relating to this are not very well documented. I tried and ended up
with many segfaults.
Also, the only ruby bindings are for Qt 2.x, and I don't know if they
are maintained anymore (although they work well).

FOX: No collision detection
WxWindows: No collision detection, IIRC
Gnome: Well, I am biased against gnome/GTK since I'm a KDE fan and I
think gnome is ugly (both the look and the pseudo-OO C API). OTOH, it
has ruby bindings which I imagine improve the API, and a decent set of
canvas items. The documentation doesn't seem to be very good -- for
example, I can't tell whether it does collision detection. (Does
anyone know?)

Let us know what you decide on, and feel free to drop me an email if
you want to discuss implementation details or take a peek at my code
(which should be going up on sourceforge anyway, as soon as I can come
up with a name for the project).

Nathan
 
J

Jeremy Henty

Gnome: Well, I am biased against gnome/GTK since I'm a KDE fan

I am biased against both Gnome and KDE because they bring my crusty
old 233MHz 64M system to its knees. Luckily you can use Ruby and the
GnomeCanvas without installing all of Gnome (despite what the name
might suggest).

I have installed:
glib2
pango
atk
gtk2
libart
libglade
libgnomecanvas
ruby-glib2
ruby-pango
ruby-gdkpixbuf
ruby-gtk2
ruby-libart
ruby-gnomecanvas

The canvas is just a background for rendering canvas objects, so the
alleged ugliness of Gnome is not an issue. (You still get the Gtk
look and feel of course.) It does collision detection: a canvas item
can bind actions to only those events that occur on top of itself.
You can also bind actions to events that miss all items and hit the
background.

Documentation: well, there is a nice set of code samples and test
programs that exercise all the features. I didn't find it hard to
track down the ones that demonstrated the features I wanted to use and
discover which methods to call. I leave it up to you whether or not
you think that counts as documentation but it got me up and running
pretty quickly.

HTH,

Regards,

Jeremy Henty
 
R

Richard Dale

Nathan said:
Qt: Has collision detection.
Lines are only single-segment, with width 1, and no curves. Polygons
are solids with no outline. You can draw more complex stuff, but not
as full-fledged canvas items with collision detection.
It is very difficult to implement your own canvas items, as the APIs
relating to this are not very well documented. I tried and ended up
with many segfaults.
Also, the only ruby bindings are for Qt 2.x, and I don't know if they
are maintained anymore (although they work well).
The QtRuby bindings in the KDE cvs under 'kdebindings/qtruby' are based on
Qt3, and I think the QCanvas classes have been improved since Qt2. Here's a
couple of methods from the qtruby/rubylib/examples/canvastest/canvastest.rb
example:

def contentsMousePressEvent(e)
super
list = canvas.collisions(e.pos)
return if list.empty?
c = list.first
return if c.rtti != Qt::CanvasRectangle.rtti()
c.hide
@canvas.update
end


def make_rect
rect = Qt::CanvasRectangle.new(rand(@canvas.width()),
rand(@canvas.height()),
@canvas.width / 5, @canvas.width / 5,
@canvas)
z = rand(256)
color = Qt::Color.new(z,z,z)
rect.setBrush(Qt::Brush.new(color))
color = Qt::Color.new(rand(32)*8, rand(32)*8, rand(32)*8)
rect.setPen(Qt::pen.new(color, 6))
rect.setZ(z)
rect.show
@rects << rect
end

It looks to me as though it does variable width lines (via 'setBrush()' or
'brush=' calls), handles collision detection, and does curves
(Qt::CanvasSpline?).

I haven't really announced the bindings yet as they're not quite finished
(the kde extension isn't checked in yet), but they do work well enough to
try out and use as they are.

-- Richard
 
J

Joao Barros

Does this work in Windows XP too?

Joao

Jeremy Henty said:
I am biased against both Gnome and KDE because they bring my crusty
old 233MHz 64M system to its knees. Luckily you can use Ruby and the
GnomeCanvas without installing all of Gnome (despite what the name
might suggest).

I have installed:
glib2
pango
atk
gtk2
libart
libglade
libgnomecanvas
ruby-glib2
ruby-pango
ruby-gdkpixbuf
ruby-gtk2
ruby-libart
ruby-gnomecanvas

The canvas is just a background for rendering canvas objects, so the
alleged ugliness of Gnome is not an issue. (You still get the Gtk
look and feel of course.) It does collision detection: a canvas item
can bind actions to only those events that occur on top of itself.
You can also bind actions to events that miss all items and hit the
background.

Documentation: well, there is a nice set of code samples and test
programs that exercise all the features. I didn't find it hard to
track down the ones that demonstrated the features I wanted to use and
discover which methods to call. I leave it up to you whether or not
you think that counts as documentation but it got me up and running
pretty quickly.

HTH,

Regards,

Jeremy Henty
 
N

Nathan Weston

Where can I find the sample code?
All I've found are the API docs, which aren't helpful to me without
prior knowledge of gnome programming.

But if the canvas does collision detection, I might be considering
gnome for a future version of my project.

Nathan
 
N

Nathan Weston

Richard Dale said:
Nathan Weston wrote:

The QtRuby bindings in the KDE cvs under 'kdebindings/qtruby' are based on
Qt3,

Excellent! I've always liked Qt and I'm glad to that someone is
working on ruby bindings again.
It looks to me as though it does variable width lines (via 'setBrush()' or
'brush=' calls), handles collision detection, and does curves
(Qt::CanvasSpline?).

The low-level drawing functions do support variable width,
multi-segment lines and curves. However, to get collision detection
you need to use the QCanvas* classes, and QCanvasLine is only a single
segment.
It does seem that the width limitation has been removed, though, which
means it might be possible to create a polyline class that just wraps
a bunch of QCanvasLines.

Nathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top