Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Overlay/Overlay with controls/Overlay with controls
Function:
Required plugins for this example: MBS Images Plugin, MBS Overlay Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Overlay/Overlay with controls/Overlay with controls
This example is the version from Sun, 10th Dec 2016.
Project "Overlay with controls.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() create End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
EventHandler Function KeyDown(Key As String) As Boolean Select case asc(key) case 28 'left move -10,0 Return true case 29 'right move 10,0 Return true case 30 'top move 0,-10 Return true case 31 'down move 0,10 Return true end Select End EventHandler
Sub Create() o=new MyOverlay(500,100,300,440) dim b as new oRectangle b.x=0 b.y=0 b.width=o.Width b.height=o.Height b.fillColor=&c777777 o.root.AddChild b dim f as FolderItem = FindFile("cubes.png") dim p as new oPicture(f) p.x = 0 p.y = 200 b.AddChild p dim r as new oRectangle r.x=30 r.y=30 r.width=100 r.height=50 r.fillColor=&cFF0000 b.AddChild r dim c as new oOval c.x=60 c.y=60 c.width=100 c.height=100 b.AddChild c dim u as new oButton u.x=180 u.y=150 u.width=100 u.height=30 u.NormalPicture=xNormal u.DownPicture=xDown b.AddChild u dim s as new oScrollCanvas s.x=10 s.y=140 s.width=100 s.height=50 b.AddChild s dim t as new otext t.text="Click to scroll" t.x=10 t.y=190 t.width=100 t.height=10 t.textsize=9 t.textFont="Courier" b.AddChild t o.redraw o.UpdateShow End Sub
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend End Function
Private Sub move(x as integer, y as integer) o.Move o.Left+x,o.top+y End Sub
Property Private counter As Integer
Property Private m As picture
Property Private o As MyOverlay
Property Private p As picture
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyOverlay Inherits OverlayMBS
EventHandler Function MouseDown(x as integer, y as integer, modifiers as integer) As boolean dim b as Boolean = root.MouseDown(x,y) if root.NeedRedraw then redraw Update end if Return b End EventHandler
EventHandler Function MouseUp(x as integer, y as integer, modifiers as integer) As boolean dim b as Boolean = root.MouseUp(x,y) if root.NeedRedraw then redraw Update end if Return b End EventHandler
EventHandler Sub WindowBoundsChanged() log "windowboundschanged" redraw End EventHandler
EventHandler Sub WindowClosed() log "windowclosed" End EventHandler
EventHandler Sub WindowHidden() log "windowhidden" End EventHandler
EventHandler Sub WindowShown() log "windowShown" End EventHandler
Sub Constructor(left As integer, top As integer, width As integer, height As integer) Super.Constructor(left, top, width, height) dim p,m as Picture p = new Picture(width,height,32) m = new Picture(width,height,32) me.Pict=p me.Mask=m dim r as new oControl r.x=0 r.y=0 r.width=width r.height=height me.root=r End Sub
Private Function Window1open() As Boolean dim i,c as integer c=WindowCount-1 for i=0 to c if window(i) isa window1 then Return true end if next End Function
Private Sub log(s as string) System.DebugLog s End Sub
Sub redraw() dim g as Graphics g=me.pict.Graphics g.ForeColor=&cFFFFFF g.FillRect 0,0,g.Width,g.Height root.draw g,0,0 g=me.Mask.Graphics g.ForeColor=&cFFFFFF g.FillRect 0,0,g.Width,g.Height root.drawmask g,0,0 End Sub
Property root As oControl
End Class
Class oControl
Sub AddChild(o as oControl) Children.Append o End Sub
Function MouseDown(mx as integer, my as integer) As Boolean for each c as oControl in Children dim xx as integer = mx-c.x dim yy as integer = my-c.y if c.inside(xx,yy) then dim b as Boolean = c.MouseDown(xx,yy) if c.NeedRedraw then NeedRedraw=true end if if b then Return true end if end if next Return false End Function
Function MouseUp(mx as integer, my as integer) As Boolean for each c as oControl in Children dim xx as integer = mx-c.x dim yy as integer = my-c.y if c.inside(xx,yy) then dim b as Boolean = c.MouseUp(xx,yy) if c.NeedRedraw then NeedRedraw=true end if if b then Return true end if end if next Return false End Function
Sub draw(g as Graphics, px as integer, py as integer) for each c as oControl in Children c.draw g,px+x,py+y next End Sub
Sub drawmask(g as Graphics, px as integer, py as integer) for each c as oControl in Children c.drawmask g,px+x,py+y next End Sub
Function inside(mx as integer, my as integer) As Boolean if mx<0 then Return false if my<0 then Return false if mx>=width then Return false if my>=height then Return false Return true End Function
Property Children() As oControl
Property NeedRedraw As Boolean
Property height As Integer
Property root As oControl
Property width As Integer
Property x As Integer
Property y As Integer
End Class
Class oRectangle Inherits oControl
Sub draw(g as Graphics, px as integer, py as integer) g.ForeColor=fillColor g.FillRect x+px,y+py,width,height super.draw(g,px,py) End Sub
Sub drawMask(g as Graphics, px as integer, py as integer) g.ForeColor=&c000000 g.FillRect x+px,y+py,width,height super.drawMask(g,px,py) End Sub
Property fillColor As color
End Class
Class oOval Inherits oControl
Sub draw(g as Graphics, px as integer, py as integer) g.ForeColor=fillColor g.FillOval x+px,y+py,width,height super.draw(g,px,py) End Sub
Sub drawMask(g as Graphics, px as integer, py as integer) g.ForeColor=&c000000 g.FillOval x+px,y+py,width,height super.drawmask(g,px,py) End Sub
Property fillColor As color
End Class
Class oButton Inherits oControl
Function MouseDown(mx as integer, my as integer) As Boolean down=True NeedRedraw=True Return super.MouseDown(mx,my) End Function
Function MouseUp(mx as integer, my as integer) As Boolean down=false NeedRedraw=True Return super.MouseDown(mx,my) End Function
Sub draw(g as Graphics, px as integer, py as integer) if down then g.DrawPicture DownPicture, px+x, py+y else g.DrawPicture NormalPicture, px+x, py+y end if super.draw(g,px,py) End Sub
Sub drawMask(g as Graphics, px as integer, py as integer) g.ForeColor=&c000000 g.FillRect x+px,y+py,width,height super.drawMask(g,px,py) End Sub
Property Down As Boolean
Property DownPicture As Picture
Property NormalPicture As Picture
End Class
ExternalFile xDown
End ExternalFile
ExternalFile xNormal
End ExternalFile
Class oScrollCanvas Inherits oControl
Sub Constructor() pic = new Picture(300,50,32) dim g as Graphics = pic.Graphics g.ForeColor=&cFFFFFF g.FillRect 0, 0, g.Width, g.Height dim f as color = &cFF0000 dim y as Double = 25.0 g.ForeColor=&c777777 g.DrawLine 0, 25, 299, 25 dim ly as Double for x=0 to 299 if x mod 10=0 then g.ForeColor=&c777777 g.DrawLine x,20,x,30 end if y=y+rnd*4.0-2.0 if y<=0 then y=0 elseif y>=50 then y=49 end if g.ForeColor=&cFF0000 g.DrawLine x-1, ly, x, y ly=Y next End Sub
Function MouseDown(mx as integer, my as integer) As Boolean NeedRedraw=True Return true End Function
Function MouseUp(mx as integer, my as integer) As Boolean if mx*2<width then if offset>0 then offset=offset-5 end if else if offset<pic.Width-width then offset=offset+5 end if end if NeedRedraw=True Return true End Function
Sub draw(g as Graphics, px as integer, py as integer) g.DrawPicture pic, x+px, y+py, width, height, offset, 0, width, height super.draw(g,px,py) End Sub
Sub drawMask(g as Graphics, px as integer, py as integer) g.ForeColor=&c000000 g.FillRect x+px,y+py,width,height super.drawMask(g,px,py) End Sub
Property offset As Integer
Property pic As Picture
End Class
Class oText Inherits oControl
Sub draw(g as Graphics, px as integer, py as integer) g.ForeColor=textColor g.TextFont=textFont g.TextSize=textsize dim textwidth as integer = g.StringWidth(text) dim textheight as integer = g.StringHeight(text,width) g.DrawString text, px+x+(me.width-textwidth)/2, py+y+(me.height-textheight)/2+textsize End Sub
Property text As string
Property textColor As color
Property textFont As String
Property textsize As Integer
End Class
Class oPicture Inherits oControl
Sub Constructor() End Sub
Sub Constructor(f as FolderItem) Constructor f.OpenAsPNGMBS(0) End Sub
Sub Constructor(p as PNGPictureMBS) pic = p.Pict mask = p.Mask End Sub
Sub Constructor(pic as Picture, mask as Picture) self.pic = pic self.mask = mask End Sub
Sub draw(g as Graphics, px as integer, py as integer) g.DrawPicture pic, px+x, py+y super.draw(g,px,py) End Sub
Sub drawMask(g as Graphics, px as integer, py as integer) g.DrawPicture mask, px+x, py+y super.drawMask(g,px,py) End Sub
Property mask As Picture
Property pic As Picture
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Overlay Plugin.





Links
MBS Xojo Plugins