GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 6:58 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Controlling Shapes
PostPosted: Sun Oct 28, 2012 10:05 am 
Offline

Joined: Sun Oct 28, 2012 9:36 am
Posts: 4
Location: kampala
hi everyone,
I'm a newbie to pygtk programming, I know how to draw shapes(lines, rectangles etc) on the drawingarea widget at runtime, but cannot control each shapes after it has been drawn. eg. draw a rectangle on the screen then afterward, select it with the mouse and adjust its size or move it to another location within the exposed area. (like how one can draw shapes and edit them in word processors)

thanks in advance.

_________________
There are 10 types of people in the world,
Those who understand And and Not


Top
 Profile  
 
 Post subject: Re: Controlling Shapes
PostPosted: Sun Oct 28, 2012 11:03 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
You have two approaches to solve this problem.
1. Build on what you've done by adding a toggle box that switches between the various modes of drawing and transformations (dilation, translation and potentially rotation). Then with the function that picks up mouse click/release events on the drawing area (I assume you're doing runtime drawing by connecting a click signal) switch according to this mode. Then draw/translate/etc. according to the change in mouse position between the two states.
2. Make a custom widget. Do all of the above (but also you can embed the mode selector in it as well as the data for object locations etc.).


Top
 Profile  
 
 Post subject: Re: Controlling Shapes
PostPosted: Sun Oct 28, 2012 12:01 pm 
Offline

Joined: Sun Oct 28, 2012 9:36 am
Posts: 4
Location: kampala
Thanks Paul for your first response, but now, I can't select the individual shapes drawn. How do I select a particular shape on the drawingArea?

_________________
There are 10 types of people in the world,
Those who understand And and Not


Top
 Profile  
 
 Post subject: Re: Controlling Shapes
PostPosted: Sun Oct 28, 2012 10:30 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
It will depend on how you do it. btw I don't know python so I am trying to keep my answers generic as anything specific will be in c.
Each shape should have an associated data structure. E.g. for polygonal shapes: number of vertices then an ordered array of x,y pairs describing them (you can then build on this by adding things like line/fill colour etc.). Your drawing area would then have an associated array of these structures (if you do go the custom widget path then you would add this array as a member of your widget - either publicly or privately through accessor functions).
In order to select a shape you need to determine which shape the mouse is nearest when clicked. This can also be done a variety of ways, but I would lean towards defining a centre of mass (http://en.wikipedia.org/wiki/Centroid#Of_a_finite_set_of_points) for each polygon and doing a distance^2 check on all shapes in your array to pick the closest one. You might also want to highlight it in some way when selected (e.g. halving the alpha, bitwise negation of the colour, etc.) to indicate to the user which item they have selected. Having a drag animation could also help.
If you look around you might find some projects that can help as examples to guide you (e.g. goocanvas), though they're more likely written in c/c++.


Top
 Profile  
 
 Post subject: Re: Controlling Shapes
PostPosted: Mon Oct 29, 2012 6:01 am 
Offline

Joined: Sun Oct 28, 2012 9:36 am
Posts: 4
Location: kampala
this is very clear Paul and managed to get pygoocanvas which i am looking at right now. i know some basic C++ so some reference through it would also do me good, as i will try to convert the codes.
thanks

_________________
There are 10 types of people in the world,
Those who understand And and Not


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group