questions about MVP and graphics

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

questions about MVP and graphics

John Whittaker-2
I'm trying to do some simple graphics operations in Dolphin 4.01.3, and I'm
having some trouble understanding how the MVP framework works.  Here's a
somewhat random list of questions, but all of them  are ones I've been
stumped by when trying to do the graphics:

1.  What's the correct way to create a simple Container view in the View
Composer?  Before using the View Composer I had created a subclass of
ContainerView called LightningView in the CHB.  I did this because I
believed that I wanted to override onPaintRequired to perform my drawing
operations, and ContainerView seemed about the simplest view class.  I had
also created a Presenter subclass called LightningPresenter.  The
LightningPresenter class currently doesn't have any code or member
variables.  Now, in the ViewComposer I created a new Shell View to get going
(because I didn't know of any other way).  Then I used the Mutate View
command to mutate the view into a ContainerView.  Then I set some of the
aspect variables I was interested in--like the background color (black), and
the preferred extent.  Then I think (IIRC) I mutated this view into a
LightningView (sorry my memory is a little fuzzy here).  Finally, I saved
the view as a LightningPresenter  "Default view".   The process I used to
save this view seems a bit convoluted, so I suspect I may have done things
the hard way.  Is this the right way?

2.  Some of the drawing operations I'm attempting to do from the
onPaintRequired method of the LightningView class aren't working.  I think
the problem is due to my lack of understanding of how the graphics contexts
work in Dolphin.  I want to simply draw a "range ring" around the center
point of the view's window.  To accomplish this I created a class called
RangeRing.  RangeRing is a subclass of another class I created called
GraphicObject.  The protocol established by the GraphicObject base class is
that each GraphicObject has a draw method.  Now, my RangeRing>>draw doesn't
work.  I don't see anything.  The code below shows the onPaintRequired
method for LightningView.  You see some other graphics operations, like the
lineTo, simply because I wanted to see if ANYTHING could be seen, and the
line is.  I think I'm missing something about the graphics device contexts
passed through the "aPaintEvent" event and how they relate to Canvases.
What should I do?   Where should I look to learn about the right way to draw
graphics?

LightingView>>onPaintRequired: aPaintEvent
 | canvas rect  magentapen oldpen newbrush oldbrush rangeRing |

        canvas := aPaintEvent canvas.
        magentapen := Pen color: (RGB red: 255 green: 0 blue: 255).
        newbrush := Brush color: (RGB red: 0 green: 255 blue: 0).
       oldpen :=  canvas pen: magentapen.
       oldbrush := canvas brush: newbrush.
       canvas lineTo: 200@200.
       rect := self clientRectangle.
       canvas setBkMode: TRANSPARENT.
       rangeRing := RangeRing new.
       rangeRing initialize: magentapen rangeText: '25' box: (Rectangle
left: 0 top: 0 right: 100 bottom: 100).
        rangeRing draw: canvas.
       canvas pen: oldpen.



RangeRing>>draw: onCanvas
 | centerPoint absolutebBox xHalfRange yHalfRange|

 centerPoint := Point new.
 centerPoint x: ((onCanvas extent) x // 2).
 centerPoint y: ((onCanvas extent) y // 2).
 "onView canvas pen: penColor."
 xHalfRange := ((boundingBox right) - (boundingBox left)) // 2.
 yHalfRange := ((boundingBox bottom) - (boundingBox top)) // 2.
 absolutebBox := Rectangle new.
 absolutebBox left: (centerPoint x  - xHalfRange).
 absolutebBox right: (centerPoint x + xHalfRange).
 absolutebBox top: (centerPoint y - yHalfRange).
 absolutebBox bottom: (centerPoint y + yHalfRange).

 (GDILibrary default) arc: ( onCanvas handle)
  left: (absolutebBox left) top: (absolutebBox top)
                right: (absolutebBox right) bottom: (absolutebBox bottom)
                startX: (centerPoint x) startY: (centerPoint y - yHalfRange)
endX: (centerPoint x)
                          endY: (centerPoint y - yHalfRange -1) .



3.  How does one change the size of a window (view)?   I set the preferred
extent of the LightningView to 600@600, but when I create a new instance of
the view from a workspace, as in lp := LightningPresenter create, the window
size isn't 600 by 600.  Furthemore,  I don't seem to be able to change its
size.  I thought that the extent: method from View would work, but it seems
to have no effect.  "lp view extent: 600@600"  doesn't seem to change
anything.


Thanks for any help.  I'm sure I'm missing some obvious things here.


John Whittaker


Reply | Threaded
Open this post in threaded view
|

Re: questions about MVP and graphics

Ian Bartholomew-4
John,

[1] and [2] snipped.

Rather than going through your question point by point I'll run through a
way of (what I think) you are trying to do.

NB1 This is very "bare bones" but should give you some ideas.
NB2 This is not the only way and some of these classes are not really needed
in this example.

Create an Object subclass called MyDrawingTool
Add one method -
drawOn: aCanvas within: aRectangle
    aCanvas ellipse: aRectangle

Create a Model subclass called MyModel
Add one method -
drawingTool
    ^MyDrawingTool new

Create a Presenter subclass called MyPresenter
Add one class method -
defaultModel
    ^MyModel new

Create a View subclass called MyView
Add two methods -
initialize
    super initialize.
    self setModel: MyModel new

onPaintRequired: aPaintEvent
    | canvas |
    canvas := aPaintEvent canvas.
    self model drawingTool
        drawOn: canvas
        within: ((Point zero extent: self extent) insetBy: 100)

Finally, open a workspace and evaluate the following. This creates a
resource of MyView (it will now appear in the resources list) and makes it
the default view for MyPresenter. The #initialize above method just ensures
that we open the resource in the ViewComposer even when it does not have an
attached Presenter/Model.

MyPresenter addView: MyView asResource: 'Default view'

You can now go the presenter and edit it's default view, as normal, to set
the background colour etc.

NB. You have now created an MVP triad that appears in the resource list and
which can be dropped onto any composite presenter in just the same way as
any existing resources. You can now add code to MyModel to change the
drawingTool answered (probably by adding more DrawingTool classes to the
image) and also code to MyPresenter to allow input (a pop up menu for
example) to change triads drawing tool on the fly.

[3]

> 3.  How does one change the size of a window (view)?

I think this is just because you are trying to use the view out of context.
Using the above as an example. #show must have a top level shell to work on.
Evaluating -

MyPresenter show

causes Dolphin to create, for itself, a Shell which it then uses as the
wrapper for the MyPresenter MVP triad. To resize this you have to resize the
wrapping Shell and not the Presenter you have created

m := MyPresenter show.
m view topView extent: 200@200

In "normal" use you would create you own top shell and be able to specify
it's extent directly.

Sorry if this is a bit short - things to do - but it might help a bit?

Ian


Reply | Threaded
Open this post in threaded view
|

Re: questions about MVP and graphics

John Whittaker-2
Ian,

Thanks for the tips.  I'll have to digest them for a while.  I may have more
questions to follow...

John Whittaker