Post by filax on Apr 22, 2020 18:45:02 GMT
Hi
I just begin with monkey, but i worked before with Blitzbasic and Blitmax, i just want to share
a GUI squeleton, i wanted the most light possible, without graphics.
For the moment, there is :
- Windows
- Buttons
That all But from the class button it's easy to add some new gadget (like checkbox, radio etc...)
Free for you to increase the gui!, if i see that the forum people are enjoy with, i should release some others gadgets...
I think that the code is not perfect at all But it's a start base to create some controls!
But it's terribly difficult to find some documentation for monkey2...
enjoy
Exemple.monkey2
opal_Kernel.monkey2
opal_Button.monkey2
opal_Window.monkey2
opal_Mouse.monkey2
I just begin with monkey, but i worked before with Blitzbasic and Blitmax, i just want to share
a GUI squeleton, i wanted the most light possible, without graphics.
For the moment, there is :
- Windows
- Buttons
That all But from the class button it's easy to add some new gadget (like checkbox, radio etc...)
Free for you to increase the gui!, if i see that the forum people are enjoy with, i should release some others gadgets...
I think that the code is not perfect at all But it's a start base to create some controls!
But it's terribly difficult to find some documentation for monkey2...
enjoy
Exemple.monkey2
Namespace OpalApp
#Import "<std>"
#Import "<mojo>"
#Import "opal_Kernel.monkey2"
Using std..
Using mojo..
Global but1:opalButton
Global but2:opalButton
Global test1:opalWindow
Global test2:opalWindow
Class MyWindow Extends Window
Method New( )
'Super.New("test",opalScreenWidth,opalScreenHeight,WindowFlags.Resizable)
Super.New("test",opalScreenWidth,opalScreenHeight,WindowFlags.Fullscreen)
test1=New opalWindow(50,50,250,250,"My Window 1dqsdqsdqdqsdqsdqsdqsdqsdqsdqsdqsd",3)
test1.locked=True
test2=New opalWindow(300,300,300,200,"My Window 2",2)
but1=New opalButton(test1,10,10,100,25,"Button Asdsdfsdfsdfsdfsdfsdfsdf")
but2=New opalButton(test2,0,0,130,25,"Quit ↺◉◎⌨▽")
End
Method OnRender( canvas:Canvas ) Override
App.RequestRender()
ClearColor=Color.Black
If but1.Click()=True Then
Local test3:opalWindow=New opalWindow(400+Rnd(300),400+Rnd(200),200,200,"My Window 2",4)
Local but3:opalButton=New opalButton(test3,10,10,100,25,"Button A")
'Local filename:String = RequestFile("Load the file")
Endif
If but2.Click()=True Then
App.Terminate()
Endif
opalKernel.opalRefresh(canvas)
End
End
Function Main()
New AppInstance
New MyWindow
App.Run()
End
opal_Kernel.monkey2
Namespace OpalApp
#Import "opal_Mouse.monkey2"
#Import "opal_Button.monkey2"
#Import "opal_Window.monkey2"
' unicode graphisme : https://www.fileformat.info/info/unicode/block/index.htm
Global opalScreenWidth:int=800
Global opalScreenHeight:Int=600
' ---------------------------------------------------------------------------------------------------------------------------------------------
Class opalKernel
Function opalRefresh(canvas:Canvas)
opalMouse.Refresh() ' On refresh dabord le gestionnaire de souris
opalWindow.Refresh(canvas)
End
Function draw3DBox(canvas:Canvas,x:Int,y:Int,w:Int,h:Int,style:Int,backCol:Color,lightCol:Color,darkCol:Color,caption:String="")
' Effacement du fond et setup
canvas.LineWidth = 1
canvas.Color = Color.Black
canvas.Alpha= 1.0
canvas.DrawRect(x-1,y-1,w+2,h+3)
Select style
Case 1
canvas.Color = backCol
canvas.Alpha= 1.0
canvas.DrawRect(x,y,w,h)
canvas.Color = darkCol
canvas.Alpha= 1.0 ; canvas.DrawLine(x,y+h, x+w, y+h)
canvas.Alpha= 0.5 ; canvas.DrawLine(x,y+h-1, x+w, y+h-1)
canvas.Alpha= 1.0 ;canvas.DrawLine(x+w-1,y, x+w-1, y+h)
canvas.Alpha= 0.5 ; canvas.DrawLine(x+w-2,y+2, x+w-2, y+h-1)
canvas.Color = lightCol
canvas.Alpha= 1.0 ; canvas.DrawLine(x,y, x+w, y)
canvas.Alpha= 0.5 ; canvas.DrawLine(x+1,y+1, x+w-1, y+1)
canvas.Alpha= 1.0 ; canvas.DrawLine(x,y, x, y+h)
canvas.Alpha= 0.5 ; canvas.DrawLine(x+1,y+1, x+1, y+h-1)
Case 2
canvas.Color = backCol
canvas.Alpha= 1.0
canvas.DrawRect(x,y,w,h)
canvas.Color = lightCol
canvas.Alpha= 1.0 ; canvas.DrawLine(x,y+h, x+w, y+h)
canvas.Alpha= 0.5 ; canvas.DrawLine(x,y+h-1, x+w, y+h-1)
canvas.Alpha= 1.0 ;canvas.DrawLine(x+w-1,y, x+w-1, y+h)
canvas.Alpha= 0.5 ; canvas.DrawLine(x+w-2,y+2, x+w-2, y+h-1)
canvas.Color = darkCol
canvas.Alpha= 1.0 ; canvas.DrawLine(x,y, x+w, y)
canvas.Alpha= 0.5 ; canvas.DrawLine(x+1,y+1, x+w-1, y+1)
canvas.Alpha= 0.7 ; canvas.DrawLine(x,y, x, y+h)
canvas.Alpha= 0.5 ; canvas.DrawLine(x+1,y+1, x+1, y+h-1)
End Select
' Affichage du viewport
If caption<>""
' Definition du viewport
canvas.Alpha= 1.0 ; canvas.Scissor=New Recti(x+2,y+2,x+w-2,y+h)
opalKernel.drawCaption(canvas,x+(w/2),y+(h/2),2,Color.White,caption,True)
Endif
' reinitialisation du viewport
canvas.Scissor=New Recti(0,0,opalScreenWidth,opalScreenHeight)
End
Function drawCaption(canvas:Canvas,x:Int,y:Int,style:Int,col:Color,caption:String="",shadow:Byte=False)
If caption<>""
Local width:Int=canvas.Font.TextWidth(caption)
Local height:Int=canvas.Font.Height
Local nx:Int=0
Local ny:Int=y-(height/2)
Select style
Case 1 ' Left
nx = x
Case 2 ' Center
nx = x-(width/2)
Case 3 ' Right
nx = x-width
End Select
If shadow=True Then
canvas.Color = Color.Black
canvas.DrawText(caption,nx+1,ny+1,0,0)
Endif
canvas.Color = col
canvas.DrawText(caption,nx,ny,0,0)
EndIf
End
End
opal_Button.monkey2
Namespace OpalApp
' Button lists
Global opalButtonList:List<opalButton> = New List<opalButton>
' Button vars
Global opalPrevButtonID:Object
' ---------------------------------------------------------------------------------------------------------------------------------------------
Class opalButton
Field parentId:opalWindow
Field id:opalButton
Field isSystem:Bool
Field backCol:Color
Field lightCol:Color
Field darkCol:Color
Field x:Int
Field y:Int
Field w:Int
Field h:Int
Field drawX:Int
Field drawY:Int
Field caption:String = ""
Field state:Int
Method New(id:opalWindow,x:Int,y:Int,w:Int,h:Int,caption:String,system:Bool=False)
Self.parentId = id
Self.id=Self
Self.isSystem=system
Self.x=x
Self.y=y
Self.w=w
Self.h=h
Self.backCol = New Color(0.6,0.6,0.6)
Self.lightCol = New Color(1.0,1.0,1.0)
Self.darkCol = New Color(0.3,0.3,0.3)
Self.caption = caption
opalButtonList.AddLast(Self)
End
Method Click:Bool()
If Self.state=3 Then
Self.state=0
Return True
EndIf
Return False
End Method
Method Redraw(canvas:Canvas)
Select Self.state
Case 0
opalKernel.draw3DBox(canvas,Self.drawX,Self.drawY,Self.w,Self.h,1,Self.backCol,Self.lightCol,Self.darkCol,Self.caption)
Case 1
opalKernel.draw3DBox(canvas,Self.drawX,Self.drawY,Self.w,Self.h,1,Self.backCol,Self.lightCol,Self.darkCol,Self.caption)
Case 2
opalKernel.draw3DBox(canvas,Self.drawX,Self.drawY,Self.w,Self.h,2,Self.backCol,Self.lightCol,Self.darkCol,Self.caption)
End Select
End Method
' Delete Button
Function Kill(id:opalButton)
opalButtonList.Remove(id)
id=Null
End Function
' Refresh buttons
Function Refresh(id:opalWindow,canvas:Canvas)
For Local i:=Eachin opalButtonList
' Si le parent est bien celui de la fenetre en cours
If i.parentId=id Then
' Si le parent n'est pas minimized OU sis c'est un bouton system
If i.parentId.minimized=False Or i.isSystem=True Then
' modification de l'offset des boutons X/Y (en fonction de la position de la fenetre parent)
If i.isSystem=False Then
i.drawX = i.x+i.parentId.x+3 ; i.drawY = i.y+i.parentId.y+i.parentId.titleHeight +6
Else
i.drawX = i.x+i.parentId.x ; i.drawY = i.parentId.y
Endif
' Reset the state
i.state=0
' Si la fenetre parent du bouton est en avant
If i.parentId.front=True Then
' Check button highlight
If opalMouse.checkMouseZone(i.x,i.y,i.w,i.h) Then
i.state=1
EndIf
' Check button pressed
If opalMousePressLeft=True And opalMouse.checkMouseZone(i.drawX,i.drawY,i.w,i.h,True) Then
opalWindowCanMove=False
opalPrevButtonID=i.id
i.state=2
EndIf
' Check button release
If opalMouseReleaseLeft=True And opalPrevButtonID=i.id And opalMouse.checkMouseZone(i.drawX,i.drawY,i.w,i.h) Then
i.state=3
EndIf
Endif
' Affichage des boutons
i.Redraw(canvas)
Endif
Endif
Next
End
End
opal_Window.monkey2
Namespace OpalApp
' Widget lists
Global opalWindowList:List<opalWindow> = New List<opalWindow>
' Windows vars
Global opalPrevWindowID:Object
Global opalWindowCanMove:Int=True
Global opalWindowCanSwap:Int=True
' ---------------------------------------------------------------------------------------------------------------------------------------------
Class opalWindow
Field id:opalWindow
Field backCol:Color
Field lightCol:Color
Field darkCol:Color
Field selectCol:Color
Field unselectCol:Color
Field x:Int
Field y:Int
Field w:Int
Field h:Int
Field dragX:Int
Field dragY:Int
Field style:Int
Field closeButton:opalButton
Field minimizeButton:opalButton
Field body:Bool
Field move:Bool
Field front:Bool
Field locked:Bool
Field minimized:Bool
Field caption:String=""
Field titleHeight:Int=25
Method New(x:Int,y:Int,w:Int,h:Int,caption:String,style:Int=1)
Self.id=Self
Self.x=x
Self.y=y
Self.w=w
Self.h=h
Self.style=style
Self.backCol = New Color(0.58,0.58,0.58)
Self.lightCol = New Color(1.0,1.0,1.0)
Self.darkCol = New Color(0.3,0.3,0.3)
Self.selectCol = New Color(0.4,0.41,0.6)
Self.unselectCol = New Color(0.66,0.67,0.69)
Self.caption = caption
Select style
Case 2
Self.closeButton = New opalButton(Self,w-Self.titleHeight,y,Self.titleHeight,Self.titleHeight,"▣",True)
Case 3
Self.minimizeButton = New opalButton(Self,w-((Self.titleHeight*2)+1),y,Self.titleHeight,Self.titleHeight,"◰",True)
Self.closeButton = New opalButton(Self,w-Self.titleHeight,y,Self.titleHeight,Self.titleHeight,"▣",True)
Case 4
Self.minimizeButton = New opalButton(Self,w-Self.titleHeight,y,Self.titleHeight,Self.titleHeight,"◰",True)
End Select
' Put the last window infront and add it to the windows list
Self.BringToFront()
End
' Redraw Window
Method Redraw(canvas:Canvas)
Local curColor:Color
' Window title color selection
If Self.front=True Then
curColor=Self.selectCol
Else
curColor=Self.unselectCol
Endif
' Window background clear
canvas.Color = Color.Black ; canvas.Alpha= 1.0
If Self.minimized=False Then
canvas.DrawRect(x,y,w,h)
Else
canvas.DrawRect(x,y,w,titleHeight)
Endif
' Redraw Window
Select Self.style
Case 1
opalKernel.draw3DBox(canvas,x,y,w,titleHeight,1,curColor,lightCol,darkCol,caption)
If Self.minimized=False Then opalKernel.draw3DBox(canvas,x,y+titleHeight+3,w,h-(titleHeight+5),1,backCol,lightCol,darkCol)
Case 2
opalKernel.draw3DBox(canvas,x,y,w-(titleHeight+1),titleHeight,1,curColor,lightCol,darkCol,caption)
If Self.minimized=False Then opalKernel.draw3DBox(canvas,x,y+titleHeight+3,w,h-(titleHeight+5),1,backCol,lightCol,darkCol)
Case 3
opalKernel.draw3DBox(canvas,x,y,w-((titleHeight*2)+2),titleHeight,1,curColor,lightCol,darkCol,caption)
If Self.minimized=False Then opalKernel.draw3DBox(canvas,x,y+titleHeight+3,w,h-(titleHeight+5),1,backCol,lightCol,darkCol)
Case 4
opalKernel.draw3DBox(canvas,x,y,w-(titleHeight+1),titleHeight,1,curColor,lightCol,darkCol,caption)
If Self.minimized=False Then opalKernel.draw3DBox(canvas,x,y+titleHeight+3,w,h-(titleHeight+5),1,backCol,lightCol,darkCol)
End Select
End Method
' Bring window to front
Method BringToFront()
For Local i:opalWindow = Eachin opalWindowList
i.front=False
Next
' If Self<>GUI_VirtualWindow Then
opalWindowList.Remove(Self) ; opalWindowList.AddLast(Self)
' EndIf
opalPrevWindowID=Self.id ; Self.front=True
End Method
' Delete Window
Function Kill(id:opalWindow)
opalButton.Kill(id.closeButton)
opalWindowList.Remove(id)
' Purge des enfants
For Local i:=Eachin opalButtonList
If i.parentId=id Then opalButton.Kill(i)
Next
'Print "Window number = "+opalWindowList.Count()
'Print "Button number = "+opalButtonList.Count()
id=Null
End Function
' Refresh Windows
Function Refresh(canvas:Canvas)
For Local i:=Eachin opalWindowList
' On redessine les fenetres
i.Redraw(canvas)
' canvas.Scissor=New Recti( i.x,i.y,i.w,i.h)
' On refresh les boutons
opalButton.Refresh(i,canvas)
' canvas.Scissor=New Recti(0,0,800,600)
' Test du bouton de fermeture
If i.closeButton<>Null Then
If i.closeButton.Click()=True Then Kill(i)
Endif
' Test du bouton de reduction
If i.minimizeButton<>Null Then
If i.minimizeButton.Click()=True Then i.minimized=Not(i.minimized)
Endif
' Drag zone the window
If i.move=True Then
i.x=opalMouseX-i.dragX ; i.y=opalMouseY-i.dragY
Else
i.dragX=opalMouseX-i.x ; i.dragY=opalMouseY-i.y
EndIf
' If the user activate the window
If opalPrevWindowID=i.id And i.front=False And opalWindowCanSwap=True Then
i.BringToFront()
EndIf
' test the mouse zone if minimized
Local winHeight:Int=i.h
If i.minimized=True Then winHeight=i.titleHeight
If opalMouse.checkMouseZone(i.x,i.y,i.w,winHeight)=True And opalMouseClickLeft=True Then
opalPrevWindowID=i.id
If opalMouseY>i.y+i.titleHeight Then
i.body=True
Else
i.body=False
EndIf
EndIf
If opalMouse.checkMouseZone(i.x,i.y,i.w,winHeight)=True
opalMouseOnWindow=True
Else
opalMouseOnWindow=False
EndIf
' If the user move the window
If opalMouse.checkMouseZone(i.x,i.y,i.w,i.titleHeight)=True And opalMousePressLeft=True And i.front=True And i.body=False And i.locked=False And opalWindowCanMove=True Then
i.move=True
Else
i.move=False
EndIf
Next
End
End
opal_Mouse.monkey2
Namespace OpalApp
' Mouse vars
Global opalMouseX:Int
Global opalMouseY:Int
Global opalMouseZ:Int
Global opalMousePressLeft:Int
Global opalMouseClickLeft:Int
Global opalMouseReleaseLeft:Int
Global opalMouseOldLeft:Int
Global opalMouseClickLeftX:Int
Global opalMouseClickLeftY:Int
Global opalMouseReleaseLeftX:Int
Global opalMouseReleaseLeftY:Int
Global opalMousePressRight:Int
Global opalMouseClickRight:Int
Global opalMouseReleaseRight:Int
Global opalMouseOldRight:Int
Global opalMouseClickRightX:Int
Global opalMouseClickRightY:Int
Global opalMouseOnWindow:Int
Global opalMouseOldZ:Int
Global opalMouseWheel:Int
Class opalMouse
Function Refresh()
' Pefresh mouse pos
opalMouseX=Mouse.X
opalMouseY=Mouse.Y
opalMouseZ=Mouse.Wheel.Y ' Not sure about command Mouse.Wheel.Y ???
' Test mouse wheel
If opalMouseOldZ=opalMouseZ Then
opalMouseWheel=0
EndIf
If opalMouseOldZ<opalMouseZ Then
opalMouseWheel=-1
opalMouseOldZ=opalMouseZ
EndIf
If opalMouseOldZ>opalMouseZ Then
opalMouseWheel=1
opalMouseOldZ=opalMouseZ
EndIf
' Test Left Button
If Mouse.ButtonPressed(MouseButton.Left) Then
opalMouseClickLeft = True
opalMouseClickLeftX = opalMouseX
opalMouseClickLeftY= opalMouseY
opalMouseReleaseLeftX=-1
opalMouseReleaseLeftY=-1
' Important : Reset the previous gadget ID's
opalPrevButtonID=Null
' opalPrevCheckboxID=Null
Else
opalMouseClickLeft = False
EndIf
opalMouseOldLeft = opalMousePressLeft
opalMousePressLeft = Mouse.ButtonDown(MouseButton.Left)
' Emulate mouse release
If opalMouseOldLeft And opalMousePressLeft=False Then
opalMouseReleaseLeft = True
opalMouseClickLeftX = -1
opalMouseClickLeftY = -1
opalMouseReleaseLeftX=opalMouseX
opalMouseReleaseLeftY=opalMouseY
opalWindowCanMove=True' Used For the GUI Windows
Else
opalMouseReleaseLeft = False
EndIf
' Test Right Button
If Mouse.ButtonPressed(MouseButton.Right) Then
opalMouseClickRight = True
opalMouseClickRightX = opalMouseX
opalMouseClickRightY= opalMouseY
Else
opalMouseClickRight = False
EndIf
opalMouseOldRight = opalMousePressRight
opalMousePressRight = Mouse.ButtonDown(MouseButton.Right)
' Emulate mouse release
If opalMouseOldRight And opalMousePressRight=False Then
opalMouseReleaseRight = True
opalMouseClickRightX = -1
opalMouseClickRightY = -1
opalWindowCanMove=True
Else
opalMouseReleaseRight = False
EndIf
End Function
' Test if the mouse is under a zone
Function checkMouseZone:Bool(x:Int,y:Int,w:Int,h:Int,click:Byte=False)
If opalMouseX>=x And opalMouseX<=x+w And opalMouseY>=y And opalMouseY<=y+h Then
If Not click Then
Return True
Else
If opalMouseClickLeftX>=x And opalMouseClickLeftX<=(x+w)-1 And opalMouseClickLeftY>=y And opalMouseClickLeftY<=(y+h)-1 Then
Return True
Else
Return False
EndIf
EndIf
Else
Return False
EndIf
Return False
End Function
End