piDogDragables
Notes
About
The piDogDragables classes are an example to show one way to implement dragable objects in a canvas.
The base class is DragableObject and the subclasses can implement the Super's methods or allow default handling by the superclass.
In this example project, these objects are stored in an array and checked with calls to MouseMove until an object returns true.
Then MouseDragged MouseUp, and Rotate events are forwarded to the matching object's Methods.
Each object is given a delegate in the Constructor. This delegate allows the objects to call back to the Canvas and request a redraw.
The canvas then calls back to the Object through the paint event which is routed back to the objects' paint methods.
The objects then draw their content into the given Graphics object at the given offset and zoom.
Each object has a variable to track what layer it resides in (Scrolling,Dragging,or Overlay) and a state variable to determine how it should be rendered.
You may use/reuse the piDogDragables module and parts of it as you like! Please share back any improvements with me.
Enumerations
Layers
(Scrolling - Dragging - Overlay - All)
Used to designate the rendering layer for an object
States
(Normal - Hover - Drag)
Used to designate the display state for an object
piDogDragables.DragableBox
Super: piDogDragables.DragableObject
Methods
Sub Constructor(bounds As piDogGeometry.doubleRect, refreshCallback As RefreshDelegate, layer as piDogDragables.layers, fillcolor As color)
creates a new draggableBox
Bounds specifies initial location and size
RefreshCallback will be called when redraw is needed
Layer can be Overlay,Drag, or Scrolling
FillColor is the color
Function Paint(g As graphics,optional m as graphics, area As REALbasic.Rect,zoom as double=1,xOffset As double=0,yOffset As double=0) As Boolean
Method to call when a redraw should be done
g is the main graphics object to draw into
m is the mask graphics for carbon and Win32
area is the invalid rect
zoom is the magnification 1.0=100%
xOffset and yOffset should be subtracted from coords when drawing
Properties
FillColor As Color
piDogDragables.DragableImage
Super: piDogDragables.DragableObject
Methods
Sub Constructor(bounds As piDogGeometry.doubleRect, refreshCallback As RefreshDelegate, layer as piDogDragables.layers, pic as picture)
creates a new draggableImage
Bounds specifies initial location and size
RefreshCallback will be called when redraw is needed
Layer can be Overlay,Drag, or Scrolling
FillColor is the color
pic is the main image. Should have alpha or mask
Function MouseDown(x as integer, y As Integer) As Boolean
returns true if the click is within the bounds of this object and in a nontransparent portion of the image.
Function MouseMove(x As integer,y as Integer) As boolean
returns true if the coords are within the bounds of this object and over a non trasparent portion of the image.
Function Paint(g As graphics,optional m as graphics, area As REALbasic.Rect,zoom as double=1,xOffset As double=0,yOffset As double=0) As Boolean
Method to call when a redraw should be done
g is the main graphics object to draw into
m is the mask graphics for carbon and Win32
area is the invalid rect
zoom is the magnification 1.0=100%
xOffset and yOffset should be subtracted from coords when drawing
piDogDragables.DragableObject
Delegates
Delegate Sub RefreshDelegate(dBounds As piDogGeometry . DoubleRect, layer As piDogDragables.Layers)
This is the delegate prototype to be passed in in the constructor
Events
MouseDown(x As integer,y as Integer) as Boolean
The object was clicked. This event can be used to override the built in handling.
MouseDrag(x As integer,y As integer) as Boolean
The object was dragged. This event can be used to override the built in handling.
MouseUp(x As integer,y as Integer)
The object was released. This event can be used to override the built in handling.
Methods
Sub Close()
prototype only
Sub Constructor(bounds As piDogGeometry.doubleRect, refreshCallback As RefreshDelegate)
creates a new draggableObject
Bounds specifies initial location and size
RefreshCallback will be called when redraw is needed
Function MouseDown(x as integer,y As integer) As Boolean
Returns true if the click is within the bounds of the object
Sub MouseDrag(x As integer,y As integer)
mouse drag causes an update to the bounds and a redraw
Sub MouseExit()
the mouse has exited the bounds of the object
Function MouseMove(x As integer,y as Integer) As boolean
returns true if the mouse is within the bounds of the object
Sub MouseUp(x As integer,y As Integer)
resets the objects state
Function Paint(g As graphics,optional m as graphics, area As REALbasic.Rect,zoom as double=1,xOffset As double=0,yOffset As double=0) As Boolean
prototype only
Sub Rotate(angle as Double)
sets the rotation of the object and redraws if needed
Properties
Bounds As piDogGeometry.DoubleRect
Inited As Boolean
Layer As piDogDragables.Layers
MouseDownX As Double
MouseDownY As Double
Refresh As RefreshDelegate
State As piDogDragables.States
piDogDragables.DragableSpinningBox
Super: piDogDragables.DragableBox
Methods
Sub Close()
clean up
Sub Constructor(bounds As piDogGeometry.doubleRect, refreshCallback As RefreshDelegate, layer as piDogDragables.layers, fillcolor As color)
creates a new draggableSpinningBox
Bounds specifies initial location and size
RefreshCallback will be called when redraw is needed
Layer can be Overlay,Drag, or Scrolling
FillColor is the color
Sub Start()
starts to rotation timer
Sub Stop()
stops the rotation timer
piDogDragables.DragableTextBox
Super: piDogDragables.DragableObject
Methods
Sub Constructor(bounds As piDogGeometry.doubleRect, refreshCallback As RefreshDelegate, layer as piDogDragables.layers, fillcolor As color, text As String)
creates a new draggableTextBox
Bounds specifies initial location and size
RefreshCallback will be called when redraw is needed
Layer can be Overlay,Drag, or Scrolling
FillColor is the color
Text is the initial Text
Function Paint(g As graphics,optional m as graphics, area As REALbasic.Rect,zoom as double=1,xOffset As double=0,yOffset As double=0) As Boolean
Method to call when a redraw should be done
g is the main graphics object to draw into
m is the mask graphics for carbon and Win32
area is the invalid rect
zoom is the magnification 1.0=100%
xOffset and yOffset should be subtracted from coords when drawing
Properties
Text As String
Sets the text to be displayed and calls refresh.
TextColor As Color
sets the textcolor and calls a refresh