mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 09:40:21 +00:00
Added a few Monkey2 examples (#3811)
* Added Monkey2 (extension .monkey2) example This compiles with the most up to date Monkey2 release (V1.1.06). * Sorting example in Monkey2 * Add files via upload * Gui example using the MojoX module
This commit is contained in:
committed by
Colin Seymour
parent
db1d4f7893
commit
a4e6fc78c8
83
samples/Monkey/encodeToPng.monkey2
Normal file
83
samples/Monkey/encodeToPng.monkey2
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
|
||||||
|
#Import "<std>"
|
||||||
|
Using std..
|
||||||
|
|
||||||
|
'Set your own path here. Defaults to build folder.
|
||||||
|
Global path:= AppDir() + "encodeToPng.png"
|
||||||
|
|
||||||
|
Function Main()
|
||||||
|
|
||||||
|
'Write from PixMap
|
||||||
|
Local source := New Pixmap( 100, 100 )
|
||||||
|
For Local y := 0 Until source.Width
|
||||||
|
For Local x := 0 Until source.Height
|
||||||
|
'Generates random pixels
|
||||||
|
source.SetPixelARGB( x, y, ARGB( 255, Rnd(0, 255), 0, Rnd(0, 255) ) )
|
||||||
|
Next
|
||||||
|
Next
|
||||||
|
source.Save( path )
|
||||||
|
|
||||||
|
'Read from png to PixMap
|
||||||
|
Local dest := Pixmap.Load( path )
|
||||||
|
Local a := ""
|
||||||
|
Local r := ""
|
||||||
|
Local g := ""
|
||||||
|
Local b := ""
|
||||||
|
For Local y := 0 Until dest.Width
|
||||||
|
For Local x := 0 Until source.Height
|
||||||
|
Local argb := dest.GetPixelARGB(x,y)
|
||||||
|
a += ARGBToAlpha( argb ) + " "
|
||||||
|
r += ARGBToRed( argb ) + " "
|
||||||
|
g += ARGBToGreen( argb ) + " "
|
||||||
|
b += ARGBToBlue( argb ) + " "
|
||||||
|
Next
|
||||||
|
a += "~n"
|
||||||
|
r += "~n"
|
||||||
|
g += "~n"
|
||||||
|
b += "~n"
|
||||||
|
Next
|
||||||
|
|
||||||
|
'Print resulting pixels
|
||||||
|
Print( " ~nAlpha:~n" + a )
|
||||||
|
Print( " ~nRed:~n" + r )
|
||||||
|
Print( " ~nGreen:~n" + g )
|
||||||
|
Print( " ~nBlue:~n" + b )
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
'**************** Color Functions ****************
|
||||||
|
|
||||||
|
|
||||||
|
Function ARGB:UInt( a:Float, r:Float, g:Float, b:Float )
|
||||||
|
Assert ( a<=1.0, "Alpha max value is 1.0" )
|
||||||
|
Assert ( r<=1.0, "Red max value is 1.0" )
|
||||||
|
Assert ( g<=1.0, "Green max value is 1.0" )
|
||||||
|
Assert ( b<=1.0, "Blue max value is 1.0" )
|
||||||
|
Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)
|
||||||
|
End
|
||||||
|
|
||||||
|
Function ARGB:UInt( a:Int, r:Int, g:Int, b:Int )
|
||||||
|
Assert ( a<256, "Alpha can't be higher than 255" )
|
||||||
|
Assert ( r<256, "Red can't be higher than 255" )
|
||||||
|
Assert ( g<256, "Green can't be higher than 255" )
|
||||||
|
Assert ( b<256, "Blue can't be higher than 255" )
|
||||||
|
Return( a Shl 24 | r Shl 16 | g Shl 8 | b )
|
||||||
|
End
|
||||||
|
|
||||||
|
Function ARGBToAlpha:Int( argb:UInt )
|
||||||
|
Return argb Shr 24 & $ff
|
||||||
|
End
|
||||||
|
|
||||||
|
Function ARGBToRed:Int( argb:UInt )
|
||||||
|
Return argb Shr 16 & $ff
|
||||||
|
End
|
||||||
|
|
||||||
|
Function ARGBToGreen:Int( argb:UInt )
|
||||||
|
Return argb Shr 8 & $ff
|
||||||
|
End
|
||||||
|
|
||||||
|
Function ARGBToBlue:Int( argb:UInt )
|
||||||
|
Return argb & $ff
|
||||||
|
End
|
||||||
|
|
||||||
185
samples/Monkey/example.monkey2
Normal file
185
samples/Monkey/example.monkey2
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
|
||||||
|
Namespace example
|
||||||
|
|
||||||
|
#rem
|
||||||
|
multi
|
||||||
|
line
|
||||||
|
comment
|
||||||
|
#end
|
||||||
|
|
||||||
|
#rem
|
||||||
|
nested
|
||||||
|
#rem
|
||||||
|
multi
|
||||||
|
line
|
||||||
|
#end
|
||||||
|
comment
|
||||||
|
#end
|
||||||
|
|
||||||
|
'Importing a module pre-compile in the modules folder
|
||||||
|
#Import "<mojo>"
|
||||||
|
|
||||||
|
'Setting search paths for namespaces
|
||||||
|
Using mojo..
|
||||||
|
Using std..
|
||||||
|
|
||||||
|
Const ONECONST:Int = 1
|
||||||
|
Const TWOCONST := 2
|
||||||
|
Const THREECONST := 3, FOURCONST:Int = 4
|
||||||
|
|
||||||
|
Global someVariable:Int = 4
|
||||||
|
|
||||||
|
Function Main()
|
||||||
|
'creating arrays
|
||||||
|
Local scores:Int[]= New Int[](10,20,30)
|
||||||
|
Local text:String[]= New String[]( "Hello","There","World" )
|
||||||
|
|
||||||
|
' string type
|
||||||
|
Local string1:String = "Hello world"
|
||||||
|
Local string2:= "Hello world"
|
||||||
|
|
||||||
|
' escape characers in strings
|
||||||
|
Local string4 := "~qHello World~q"
|
||||||
|
Local string5 := "~tIndented~n"
|
||||||
|
Local string6 := "tilde is wavey... ~~"
|
||||||
|
Print string4
|
||||||
|
Print string5
|
||||||
|
Print string6
|
||||||
|
|
||||||
|
' string pseudofunctions
|
||||||
|
Print " Hello World ~n".Trim() ' prints "Hello World" whithout whitespace
|
||||||
|
Print "Hello World".ToUpper() ' prints "HELLO WORLD"
|
||||||
|
|
||||||
|
' preprocessor keywords
|
||||||
|
#If __TARGET__ = "android"
|
||||||
|
'DoStuff()
|
||||||
|
#ElseIf __TARGET__ = "ios"
|
||||||
|
'DoOtherStuff()
|
||||||
|
#End
|
||||||
|
|
||||||
|
' operators
|
||||||
|
Local a := 32
|
||||||
|
Local b := 32 ~ 0
|
||||||
|
b ~= 16
|
||||||
|
b |= 16
|
||||||
|
b &= 16
|
||||||
|
Local c := a | b
|
||||||
|
Print c
|
||||||
|
|
||||||
|
'Creates a new Window class and starts the main App loop, using the Mojo module
|
||||||
|
New AppInstance
|
||||||
|
New GameWindow
|
||||||
|
App.Run()
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
'------------------------------------------ Class Examples ------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
'You can extend the Window class to customize its behavior
|
||||||
|
Class GameWindow Extends Window
|
||||||
|
|
||||||
|
Private
|
||||||
|
Field _spiral :Float[]
|
||||||
|
Field _circle :Float[]
|
||||||
|
|
||||||
|
Public
|
||||||
|
Method New()
|
||||||
|
Super.New( "Test", 800, 600, WindowFlags.Resizable )
|
||||||
|
End
|
||||||
|
|
||||||
|
'Properties can be used to create "read-only" values
|
||||||
|
Property Spiral:Float[]()
|
||||||
|
Return _spiral
|
||||||
|
End
|
||||||
|
|
||||||
|
'Or to control what happens to a value when assigned
|
||||||
|
Property Circle:Float[]()
|
||||||
|
Return _circle
|
||||||
|
Setter( values:Float[] )
|
||||||
|
If( values.Length > 2 ) And ( values.Length Mod 2 = 0 )
|
||||||
|
_circle = values
|
||||||
|
Else
|
||||||
|
Print( "Circle values need to be an even number larger than 1" )
|
||||||
|
End
|
||||||
|
End
|
||||||
|
|
||||||
|
'Methods require a class instance. The keyword Self is optional when accessing fields and properties
|
||||||
|
'The method Window.OnRender is virtual, and can be overriden
|
||||||
|
'Width and Height are Propreties inherited from the Window class
|
||||||
|
Method OnRender( canvas:Canvas ) Override
|
||||||
|
RequestRender()
|
||||||
|
canvas.Clear( Color.DarkGrey )
|
||||||
|
canvas.Translate( Width/2.0, Height/2.0 )
|
||||||
|
canvas.Rotate( -Millisecs()/200.0 )
|
||||||
|
canvas.Color = New Color( 1, 0.8, 0.2 )
|
||||||
|
DrawLines( canvas, Spiral )
|
||||||
|
DrawLines( canvas, Circle, True )
|
||||||
|
End
|
||||||
|
|
||||||
|
'This method is called whenever the window layout changes, like when resizing
|
||||||
|
Method OnLayout() Override
|
||||||
|
_spiral = CreateSpiral( 0, 0, Height/1.5, Height/1.5, 100 )
|
||||||
|
Circle = CreateCircle( 0, 0, Height/1.5, Height/1.5, 100 )
|
||||||
|
End
|
||||||
|
|
||||||
|
'Functions can be called without a GameWindow instance, like "Static Functions" in other languages.
|
||||||
|
Function DrawLines( canvas:Canvas, lines:Float[], closedShape:Bool = False )
|
||||||
|
For Local n := 0 Until lines.Length Step 2
|
||||||
|
Local l := lines.Length - 3
|
||||||
|
Local x0 := lines[n]
|
||||||
|
Local y0 := lines[n+1]
|
||||||
|
Local x1 := n<l? lines[n+2] Else (closedShape? lines[0] Else x0 ) 'Conditional assignment, uses the "?" symbol to test a condition
|
||||||
|
Local y1 := n<l? lines[n+3] Else (closedShape? lines[1] Else y0 )
|
||||||
|
canvas.DrawLine( x0, y0, x1, y1 )
|
||||||
|
Next
|
||||||
|
End
|
||||||
|
|
||||||
|
Function CreateSpiral:Float[]( x:Double, y:Double, width:Double, height:Double, sides:Int = 32, turns:Float = 3.0 )
|
||||||
|
Local stack := New Stack<Float>
|
||||||
|
Local radStep := (Pi*2.0)/Float(sides)
|
||||||
|
Local xMult := 0.0
|
||||||
|
Local yMult := 0.0
|
||||||
|
Local radiusX:Float = width/2.0
|
||||||
|
Local radiusY:Float = height/2.0
|
||||||
|
Local stepX:Float = radiusX / sides
|
||||||
|
Local stepY:Float = radiusY / sides
|
||||||
|
For Local a := 0.0 To Pi*2 Step radStep
|
||||||
|
stack.Push( ( ( Sin( a*turns ) * radiusX )* xMult ) + x )
|
||||||
|
stack.Push( ( ( Cos( a*turns ) * radiusY )* yMult ) + y )
|
||||||
|
xMult += stepX/radiusX
|
||||||
|
yMult += stepY/radiusY
|
||||||
|
Next
|
||||||
|
Return stack.ToArray()
|
||||||
|
End
|
||||||
|
|
||||||
|
Function CreateCircle:Float[]( x:Double, y:Double, width:Double, height:Double, sides:Int = 32 )
|
||||||
|
Local stack := New Stack<Float>
|
||||||
|
Local radStep := (Pi*2.0)/Float(sides)
|
||||||
|
Local radiusX:Float = width/2.0
|
||||||
|
Local radiusY:Float = height/2.0
|
||||||
|
For Local a := 0.0 To Pi*2 Step radStep
|
||||||
|
stack.Push( ( Sin( a ) * radiusX ) + x )
|
||||||
|
stack.Push( ( Cos( a ) * radiusY ) + y )
|
||||||
|
Next
|
||||||
|
Return stack.ToArray()
|
||||||
|
End
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
'--------- extending with generics -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Class MyList Extends List<Double>
|
||||||
|
End
|
||||||
|
|
||||||
|
'--------- interfaces ------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Interface Computer
|
||||||
|
Method Boot ()
|
||||||
|
Method Process ()
|
||||||
|
Method Display ()
|
||||||
|
End
|
||||||
|
'
|
||||||
|
Class PC Implements Computer
|
||||||
|
End
|
||||||
|
|
||||||
115
samples/Monkey/gui.monkey2
Normal file
115
samples/Monkey/gui.monkey2
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
#Import "<mojo>"
|
||||||
|
#Import "<mojox>"
|
||||||
|
|
||||||
|
Using std..
|
||||||
|
Using mojo..
|
||||||
|
Using mojox..
|
||||||
|
|
||||||
|
Function Main()
|
||||||
|
New AppInstance
|
||||||
|
New TestGui
|
||||||
|
App.Run()
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
Class TestGui Extends Window
|
||||||
|
Field mainDock:DockingView
|
||||||
|
Field rgtDock:ScrollView
|
||||||
|
Field graphView:GraphView
|
||||||
|
|
||||||
|
Const smallFont:Font = Font.Load( "font::DejaVuSans.ttf", 10 )
|
||||||
|
|
||||||
|
Method New()
|
||||||
|
Super.New( "Test", 1024, 640, WindowFlags.Resizable )
|
||||||
|
mainDock = New MainDock()
|
||||||
|
rgtDock = New RightDock()
|
||||||
|
mainDock.AddView( rgtDock, "right", "400", True )
|
||||||
|
ContentView = mainDock
|
||||||
|
End
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
Class MainDock Extends DockingView
|
||||||
|
Method New()
|
||||||
|
Layout="fill"
|
||||||
|
Local newStyle := Style.Copy()
|
||||||
|
newStyle.BackgroundColor = Color.DarkGrey
|
||||||
|
newStyle.BorderColor = Color.Black
|
||||||
|
newStyle.Font = TestGui.smallFont
|
||||||
|
Style = newStyle
|
||||||
|
End
|
||||||
|
|
||||||
|
Method OnRender( canvas:Canvas ) Override
|
||||||
|
Super.OnRender( canvas )
|
||||||
|
canvas.Color = New Color( Rnd(), Rnd(), Rnd() )
|
||||||
|
canvas.DrawCircle( Frame.Width/4, Frame.Height/2, Frame.Height/4 )
|
||||||
|
canvas.Color = Color.Aluminum
|
||||||
|
canvas.DrawText( "gameview:" + App.FPS + " fps", 5, 5 )
|
||||||
|
End
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Class RightDock Extends ScrollView
|
||||||
|
Private
|
||||||
|
Field _panSpeed := 10.0
|
||||||
|
|
||||||
|
Public
|
||||||
|
Method New()
|
||||||
|
Layout="fill"
|
||||||
|
ScrollBarsVisible = True
|
||||||
|
|
||||||
|
Local newStyle := Style.Copy()
|
||||||
|
newStyle.BackgroundColor = Color.Grey
|
||||||
|
newStyle.BorderColor = Color.Black
|
||||||
|
newStyle.Font = TestGui.smallFont
|
||||||
|
Style = newStyle
|
||||||
|
|
||||||
|
Local graph:=New GraphView
|
||||||
|
ContentView = graph
|
||||||
|
|
||||||
|
Scroll = New Vec2i( graph.Frame.Width/2, graph.Frame.Height/2 ) 'Doesn't work!
|
||||||
|
End
|
||||||
|
|
||||||
|
Method OnRender( canvas:Canvas ) Override
|
||||||
|
Super.OnRender( canvas )
|
||||||
|
canvas.Color = Color.Aluminum
|
||||||
|
canvas.DrawText( "size:" + Frame + " ,scroll:" + Scroll , 5, 5 )
|
||||||
|
End
|
||||||
|
|
||||||
|
Method OnMouseEvent( event:MouseEvent ) Override
|
||||||
|
Select event.Type
|
||||||
|
Case EventType.MouseWheel
|
||||||
|
Scroll = New Vec2i( Scroll.X+(event.Wheel.X*_panSpeed), Scroll.Y-(event.Wheel.Y*_panSpeed) )
|
||||||
|
App.RequestRender()
|
||||||
|
End
|
||||||
|
End
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
Class GraphView Extends View
|
||||||
|
Private
|
||||||
|
Field _panSpeed := 5.0
|
||||||
|
Field _size := New Vec2i( 1024, 1024 )
|
||||||
|
|
||||||
|
Public
|
||||||
|
Method New()
|
||||||
|
MinSize=New Vec2i( _size.X, _size.Y )
|
||||||
|
End
|
||||||
|
|
||||||
|
Method OnRender( canvas:Canvas ) Override
|
||||||
|
Local r:= 20.0
|
||||||
|
For Local x := 1 Until 10
|
||||||
|
For Local y := 1 Until 10
|
||||||
|
Local v := (x/10.0) -0.05
|
||||||
|
canvas.Color = New Color( v, v, v )
|
||||||
|
canvas.DrawCircle( (x*100)+r, (y*100)+r, r )
|
||||||
|
Next
|
||||||
|
Next
|
||||||
|
End
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
29
samples/Monkey/sorting.monkey2
Normal file
29
samples/Monkey/sorting.monkey2
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
'Showcases use of Lambda functions and Generics.
|
||||||
|
|
||||||
|
#Import "<std>"
|
||||||
|
Using std..
|
||||||
|
|
||||||
|
Function Main()
|
||||||
|
|
||||||
|
Local testStack := New Stack< MyObject >
|
||||||
|
|
||||||
|
For Local n := 1 To 20
|
||||||
|
Local newItem := New MyObject
|
||||||
|
newItem.depth = Rnd( 0, 100 )
|
||||||
|
testStack.Push( newItem )
|
||||||
|
Next
|
||||||
|
|
||||||
|
testStack.Sort( Lambda:Int( x:MyObject,y:MyObject )
|
||||||
|
Return x.depth<=>y.depth
|
||||||
|
End )
|
||||||
|
|
||||||
|
For Local n := Eachin testStack
|
||||||
|
Print( n.depth )
|
||||||
|
Next
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
Struct MyObject
|
||||||
|
Field depth := 0
|
||||||
|
End
|
||||||
Reference in New Issue
Block a user