Commit 35ff0072687958e7468306e60f8c2ce3cc28f550

Authored by Arthur Bikmullin
1 parent ff42b50b

Merged with v86a

... ... @@ -32,8 +32,7 @@ Class MyApp Extends App
32 32 '
33 33 soundFmt="wav"
34 34 musicFmt="ogg"
35   - If UserAgent.Contains( "MSIE " ) Or UserAgent.Contains( "Trident/" )
36   - Print "Applying IE fudge..."
  35 + If UserAgent.Contains( "MSIE " ) Or UserAgent.Contains( "Trident/" ) Or UserAgent.Contains( "Edge/" )
37 36 soundFmt="mp3"
38 37 musicFmt="mp3"
39 38 Endif
... ...
  1 +
  2 +Import mojo2
  3 +
  4 +Function Main()
  5 + New Game()
  6 +End
  7 +
  8 +'app
  9 +Class Game Extends App
  10 + Field canvas:Canvas
  11 +
  12 + Field stickLeft:Float[2]
  13 + Field stickRight:Float[2]
  14 +
  15 + Field triggerLeft:float
  16 + Field triggerRight:float
  17 +
  18 + Field buttons:Bool[JOY_MENU + 1]
  19 +
  20 + Method OnCreate()
  21 + SetUpdateRate(60)
  22 + canvas = New Canvas
  23 + End
  24 +
  25 + Method OnUpdate()
  26 +
  27 + 'Slow! Don't do this every update in real code!
  28 + CountJoysticks( True )
  29 +
  30 + 'update all states
  31 + stickLeft[0] = JoyX(0)
  32 + stickLeft[1] = JoyY(0)
  33 +
  34 + stickRight[0] = JoyX(1)
  35 + stickRight[1] = JoyY(1)
  36 +
  37 + 'have to do this (Max) otherwise GLFW seems to combine both triggers incorrectly!
  38 + triggerLeft = Max(0.0, JoyZ(0))
  39 + triggerRight = Max(0.0, JoyZ(1))
  40 +
  41 + For Local index:= JOY_A To JOY_MENU
  42 + buttons[index] = JoyHit(index) Or JoyDown(index)
  43 + Next
  44 + End
  45 +
  46 + Method OnRender()
  47 + canvas.Clear(0, 0, 0)
  48 +
  49 + Local padCenterX:= DeviceWidth() / 2.0
  50 + Local padCenterY:= DeviceHeight() / 2.0
  51 +
  52 + 'left stick
  53 + DrawAnalog(canvas, padCenterX - 125, padCenterY - 50, 40, stickLeft[0], -stickLeft[1], buttons[JOY_LSB])
  54 +
  55 + 'right stick
  56 + DrawAnalog(canvas, padCenterX + 60, padCenterY + 50, 40, stickRight[0], -stickRight[1], buttons[JOY_RSB])
  57 +
  58 + 'dpad
  59 + DrawDPad(canvas, padCenterX - 100, padCenterY + 10, 80, buttons[JOY_UP], buttons[JOY_DOWN], buttons[JOY_LEFT], buttons[JOY_RIGHT])
  60 +
  61 + 'left trigger
  62 + DrawAxis(canvas, padCenterX - 165, padCenterY - 150, 80, 20, triggerLeft)
  63 +
  64 + 'right trigger
  65 + DrawAxis(canvas, padCenterX + 85, padCenterY - 150, 80, 20, triggerRight)
  66 +
  67 + 'left shoulder
  68 + DrawButton(canvas, padCenterX - 165, padCenterY - 120, 80, 20, buttons[JOY_LB])
  69 +
  70 + 'right shoulder
  71 + DrawButton(canvas, padCenterX + 85, padCenterY - 120, 80, 20, buttons[JOY_RB])
  72 +
  73 + 'buttons
  74 + Local buttonSize:= 30.0
  75 + Local buttonCenterX:= padCenterX + 125
  76 + Local buttonCenterY:= padCenterY - 50
  77 +
  78 + 'a
  79 + DrawButton(canvas, buttonCenterX, buttonCenterY + 40 - (buttonSize / 2.0), buttonSize / 2.0, buttons[JOY_A])
  80 +
  81 + 'b
  82 + DrawButton(canvas, buttonCenterX + 40 - (buttonSize / 2.0), buttonCenterY, buttonSize / 2.0, buttons[JOY_B])
  83 +
  84 + 'x
  85 + DrawButton(canvas, buttonCenterX - 40 + (buttonSize / 2.0), buttonCenterY, buttonSize / 2.0, buttons[JOY_X])
  86 +
  87 + 'y
  88 + DrawButton(canvas, buttonCenterX, buttonCenterY - 40 + (buttonSize / 2.0), buttonSize / 2.0, buttons[JOY_Y])
  89 +
  90 + 'start
  91 + DrawButton(canvas, padCenterX + buttonSize, buttonCenterY - (buttonSize / 4.0), buttonSize, buttonSize / 2.0, buttons[JOY_START])
  92 +
  93 + 'back
  94 + DrawButton(canvas, padCenterX - buttonSize - buttonSize, buttonCenterY - (buttonSize / 4.0), buttonSize, buttonSize / 2.0, buttons[JOY_BACK])
  95 +
  96 + 'menu
  97 + DrawButton(canvas, padCenterX, buttonCenterY, buttonSize * 0.75, buttons[JOY_MENU])
  98 +
  99 +
  100 + canvas.Flush()
  101 + End
  102 +End
  103 +
  104 +'functions
  105 +Function DrawAxis:Void(canvas:Canvas, x:Float, y:float, width:Float, height:float, axis:float)
  106 + canvas.SetColor(0.5, 0.5, 0.5)
  107 + canvas.DrawRect(x, y, width, height)
  108 +
  109 + Local size:= Min(16.0, Max(2.0, width * 0.1))
  110 + Local offset:= (width / 2.0) + (axis * ( (width - size) / 2.0)) - (size / 2.0)
  111 +
  112 + If offset >= 0 And offset < width
  113 + canvas.SetColor(0.3, 0.3, 0.3)
  114 + canvas.DrawRect(x + offset, y, size, height)
  115 + EndIf
  116 +End
  117 +
  118 +Function DrawDPad:Void(canvas:Canvas, x:Float, y:Float, size:Float, up:Bool, down:Bool, left:Bool, right:Bool)
  119 + Local buttonSize:= size / 3.0
  120 + Local buttonPadding:= Min(6.0, Max(2.0, size * 0.04))
  121 +
  122 + canvas.SetColor(0.5, 0.5, 0.5)
  123 + canvas.DrawRect(x, y + buttonSize, size, buttonSize)
  124 + canvas.DrawRect(x + buttonSize, y, buttonSize, size)
  125 +
  126 + canvas.SetColor(0.3, 0.3, 0.3)
  127 + canvas.DrawRect(x + buttonSize, y + buttonSize, buttonSize, buttonSize)
  128 +
  129 + 'up
  130 + If up
  131 + canvas.SetColor(0.0, 1.0, 0.0)
  132 + Else
  133 + canvas.SetColor(0.3, 0.3, 0.3)
  134 + EndIf
  135 + canvas.DrawRect(x + buttonSize + buttonPadding, y + buttonPadding, buttonSize - buttonPadding - buttonPadding, buttonSize - buttonPadding)
  136 +
  137 + 'down
  138 + If down
  139 + canvas.SetColor(0.0, 1.0, 0.0)
  140 + Else
  141 + canvas.SetColor(0.3, 0.3, 0.3)
  142 + EndIf
  143 + canvas.DrawRect(x + buttonSize + buttonPadding, y + size - buttonSize, buttonSize - buttonPadding - buttonPadding, buttonSize - buttonPadding)
  144 +
  145 + 'left
  146 + If left
  147 + canvas.SetColor(0.0, 1.0, 0.0)
  148 + Else
  149 + canvas.SetColor(0.3, 0.3, 0.3)
  150 + EndIf
  151 + canvas.DrawRect(x + buttonPadding, y + buttonSize + buttonPadding, buttonSize - buttonPadding, buttonSize - buttonPadding - buttonPadding)
  152 +
  153 + 'right
  154 + If right
  155 + canvas.SetColor(0.0, 1.0, 0.0)
  156 + Else
  157 + canvas.SetColor(0.3, 0.3, 0.3)
  158 + EndIf
  159 + canvas.DrawRect(x + size - buttonSize, y + buttonSize + buttonPadding, buttonSize - buttonPadding, buttonSize - buttonPadding - buttonPadding)
  160 +End
  161 +
  162 +Function DrawAnalog:Void(canvas:Canvas, x:Float, y:Float, radius:Float, axisX:Float, axisY:Float, pressed:Bool)
  163 + Local padding:= Min(8.0, Max(1.0, radius * 0.1))
  164 +
  165 + canvas.SetColor(0.5, 0.5, 0.5)
  166 + canvas.DrawCircle(x, y, radius)
  167 +
  168 + If pressed
  169 + canvas.SetColor(0, 1.0, 0)
  170 + canvas.DrawCircle(x, y, radius - padding)
  171 + EndIf
  172 +
  173 + canvas.SetColor(0.3, 0.3, 0.3)
  174 +
  175 + Local length:= Sqrt( (axisX * axisX) + (axisY * axisY))
  176 + Local size:= Min(19.0, Max(7.0, radius * 0.1))
  177 + Local cursorX:Float
  178 + Local cursorY:Float
  179 +
  180 + If length > 0
  181 + Local ratio:Float
  182 + If Abs(axisX) > Abs(axisY)
  183 + ratio = Abs(axisX / 1.0) / length
  184 + Else
  185 + ratio = Abs(axisY / 1.0) / length
  186 + EndIf
  187 +
  188 + cursorX = axisX * radius * ratio
  189 + cursorY = axisY * radius * ratio
  190 + EndIf
  191 +
  192 + DrawCross(canvas, x + cursorX - (size / 2.0), y + cursorY - (size / 2.0), size, size)
  193 +End
  194 +
  195 +Function DrawCross:Void(canvas:Canvas, x:Float, y:Float, width:Float, height:Float, diagonal:Bool = True)
  196 + ' --- draw a cross ---
  197 + If diagonal
  198 + canvas.DrawLine(x, y, x + width, y + height)
  199 + canvas.DrawLine(x + width, y, x, y + height)
  200 + Else
  201 + Local halfWidth:= width / 2.0
  202 + Local halfHeight:= height / 2.0
  203 + canvas.DrawLine(x + halfWidth, y, x + halfHeight, y + height)
  204 + canvas.DrawLine(x, y + halfHeight, x + width, y + halfHeight)
  205 + EndIf
  206 +End
  207 +
  208 +Function DrawButton:Void(canvas:Canvas, x:Float, y:Float, radius:Float, pressed:Bool)
  209 + Local padding:= Min(8.0, Max(1.0, radius * 0.1))
  210 +
  211 + canvas.SetColor(0.5, 0.5, 0.5)
  212 + canvas.DrawCircle(x, y, radius)
  213 +
  214 + If pressed
  215 + canvas.SetColor(0, 1.0, 0)
  216 + canvas.DrawCircle(x, y, radius - padding)
  217 + EndIf
  218 +End
  219 +
  220 +Function DrawButton:Void(canvas:Canvas, x:Float, y:Float, width:Float, height:Float, pressed:Bool)
  221 + Local padding:= Min(8.0, Max(1.0, width * 0.05))
  222 +
  223 + canvas.SetColor(0.5, 0.5, 0.5)
  224 + canvas.DrawRect(x, y, width, height)
  225 +
  226 + If pressed
  227 + canvas.SetColor(0, 1.0, 0)
  228 + canvas.DrawRect(x + padding, y + padding, width - padding - padding, height - padding - padding)
  229 + EndIf
  230 +End
\ No newline at end of file
... ...
1 1
2   -#If TARGET<>"glfw" And TARGET<>"android" And TARGET<>"ios"
3   -#Error "c++ or java Mojo target required"
4   -#Endif
  2 +#GLFW_WINDOW_WIDTH=900
  3 +#GLFW_WINDOW_HEIGHT=480
5 4
6 5 Import mojo
7   -Import brl.asynctcpstream
  6 +Import brl.httprequest
8 7 Import monkey
9 8
10 9 'main program
... ... @@ -121,7 +120,7 @@ Class HTTPGetter Implements IOnReadComplete,IOnWriteComplete,IOnConnectComplete
121 120 End
122 121
123 122 Field _host:String
124   - Field _port:Int
  123 + Field _port:Int
125 124 Field _listener:HTTPListener
126 125
127 126 Field _stream:AsyncTcpStream
... ... @@ -131,7 +130,7 @@ Class HTTPGetter Implements IOnReadComplete,IOnWriteComplete,IOnConnectComplete
131 130 End
132 131
133 132 'main app
134   -Class IconFun Extends App implements HTTPListener
  133 +Class IconFun Extends App Implements IOnHttpRequestComplete
135 134 Const ICON_SIZE:Int = 32
136 135
137 136 Const MODE_IDLE:Int = 0
... ... @@ -140,8 +139,7 @@ Class IconFun Extends App implements HTTPListener
140 139 Const MODE_LOAD:Int = 3
141 140 Const MODE_SAVE:Int = 4
142 141
143   - Const REMOTE_SERVER:String = "www.skn3.com/junk/iconfun/index.php"
144   - Const REMOTE_PORT:Int = 80
  142 + Const REMOTE_SERVER:String = "www.skn3.com:80/junk/iconfun/index.php"
145 143
146 144 Field paletteR:Int[9]
147 145 Field paletteG:Int[9]
... ... @@ -162,6 +160,7 @@ Class IconFun Extends App implements HTTPListener
162 160 Field editorSubmitInput:ButtonInput
163 161 Field editorSpacing:Int = 5
164 162 Field editorGadgetHeight:Int = 24
  163 + Field editorTransparent:= False
165 164
166 165 Global ms:Int
167 166 Global fontWhite:Image
... ... @@ -170,10 +169,8 @@ Class IconFun Extends App implements HTTPListener
170 169 Field icons:= new List<Icon>
171 170 Field iconsPending:= new List<Icon>
172 171
173   - Field http:= New HTTPGetter
  172 + Field http:HttpRequest
174 173 Field httpMode:Int = MODE_IDLE
175   - Field httpBuffer:String
176   - Field httpDebugBuffer:String
177 174
178 175 Field loadTimestamp:Int
179 176 Field loadInterval:Int = 20000
... ... @@ -303,19 +300,8 @@ Class IconFun Extends App implements HTTPListener
303 300
304 301 'create the image
305 302 icon.image = CreateImage(ICON_SIZE, ICON_SIZE, 1, Image.MidHandle)
306   - 'icon.x = ICON_SIZE' Rnd(ICON_SIZE, DeviceWidth() - ICON_SIZE)
307   - 'icon.y = 100'Rnd(ICON_SIZE, DeviceHeight() - ICON_SIZE)
308   -
309   -#rem
310   - If icon.title="checker"
311   - Print "id="+icon.id
312   - Print "author="+icon.author
313   - Print "title="+icon.title
314   - Print "data="+icon.data
315   - Endif
316   -#End
317 303
318   - 'go through the data
  304 + 'go through the data
319 305 For index = 0 Until icon.data.Length
320 306 Local i:=icon.data[index];
321 307
... ... @@ -401,10 +387,17 @@ Class IconFun Extends App implements HTTPListener
401 387 EndIf
402 388
403 389 'check for mouse interaction with editor pixels
404   - if editorDisabled = False
  390 + If editorDisabled = False
  391 + 'check if in editro box
  392 + If pointerX >= editorRootX - editorSpacing And pointerY >= editorRootY - editorSpacing And pointerX <= editorRootX - editorSpacing + editorSize + editorSpacing + editorSpacing And pointerY <= editorRootY - editorSpacing + editorSize + editorSpacing + editorPaletteSize + editorSpacing + editorSpacing + editorGadgetHeight + editorSpacing + editorGadgetHeight + editorSpacing + editorGadgetHeight + editorSpacing
  393 + editorTransparent = False
  394 + Else
  395 + editorTransparent = True
  396 + EndIf
  397 +
405 398 editorX = pointerX - editorRootX
406 399 editorY = pointerY - editorRootY
407   - if editorX >= 0 And editorY >= 0 And editorX < editorSize And editorY < editorSize
  400 + If editorX >= 0 And editorY >= 0 And editorX < editorSize And editorY < editorSize
408 401 'pointer is within editor
409 402 editorPixelX = (editorX / editorPixelSize)
410 403 editorPixelY = (editorY / editorPixelSize)
... ... @@ -478,7 +471,7 @@ Class IconFun Extends App implements HTTPListener
478 471 Next
479 472
480 473 'render editor
481   - if editorDisabled
  474 + If editorDisabled Or editorTransparent
482 475 SetAlpha(0.2)
483 476 Else
484 477 SetAlpha(1.0)
... ... @@ -571,79 +564,24 @@ Class IconFun Extends App implements HTTPListener
571 564 EndIf
572 565 End
573 566
574   - Method OnHTTPConnected:Void(getter:HTTPGetter)
575   - httpDebugBuffer = "";
576   - End
577   -
578   - Method OnHTTPDataReceived:Void( data:DataBuffer,offset:Int,count:Int,getter:HTTPGetter )
579   - 'add data to http buffer
580   - Local text:String = data.PeekString(offset, count)
581   - httpBuffer += text
582   - httpDebugBuffer += text
583   -
584   - 'process buffer
585   - Local start:Int = 0
586   - Local pos:Int = httpBuffer.Find("~n", start)
587   - Local line:String
588   - Local lines:String[5]
589   - Local valid:Int = false
590   - Local index:Int
591   -
592   - 'do different loads
  567 + Method OnHttpRequestComplete:Void(req:HttpRequest)
  568 + ' --- http request completed ---
593 569 Select httpMode
594 570 Case MODE_SAVE
595   - While pos <> - 1
596   - line = httpBuffer[start .. pos]
597   - start = pos + 1
598   -
599   - if line = "<SAVED>"
600   - 'success
  571 + 'add data to http buffer
  572 + Local httpBuffer:String = req.ResponseText().Replace("~r", "")
  573 +
  574 + 'process buffer
  575 + Local lines:= httpBuffer.Split("~n")
  576 +
  577 + saveSuccess = False
  578 + For Local index:= 0 Until lines.Length()
  579 + If lines[index] = "<SAVED>"
601 580 saveSuccess = True
602   - return
  581 + Exit
603 582 EndIf
604   -
605   - 'find next eol
606   - pos = httpBuffer.Find("~n", start)
607   - Wend
  583 + Next
608 584
609   - Case MODE_LOAD
610   -
611   - While pos <> - 1
612   - line = httpBuffer[start .. pos]
613   - start = pos + 1
614   -
615   - If valid = False
616   - If line = "<ICON>" valid = True
617   - Else
618   - lines[index] = line
619   -
620   - If index = 4
621   - 'add icon
622   - AddIcon(lines[0], lines[1], lines[2], lines[3])
623   - index = 0
624   - valid = False
625   -
626   - 'remember tiemstamp so we dont load any after
627   - loadRemoteTimestamp = lines[4]
628   -
629   - 'clear this part of data from buffer
630   - httpBuffer = httpBuffer[start ..]
631   - Else
632   - 'next readline
633   - index += 1
634   - Endif
635   - Endif
636   -
637   - 'find next eol
638   - pos = httpBuffer.Find("~n", start)
639   - Wend
640   - End
641   - End
642   -
643   - Method OnHTTPPageComplete:Void(getter:HTTPGetter)
644   - ' --- http request completed ---
645   - Select httpMode
646   - Case MODE_SAVE
647 585 if saveSuccess
648 586 'success saving
649 587 ResetEditor()
... ... @@ -655,15 +593,32 @@ Class IconFun Extends App implements HTTPListener
655 593 EndIf
656 594
657 595 Case MODE_LOAD
  596 + 'add data to http buffer
  597 + Local httpBuffer:String = req.ResponseText().Replace("~r", "")
  598 +
  599 + 'process buffer
  600 + Local lines:= httpBuffer.Split("~n")
  601 + Local index:Int
  602 +
  603 + For index = 0 Until lines.Length() Step 6
  604 + If lines[index] = "<ICON>" And index + 5 < lines.Length()
  605 + AddIcon(lines[index + 1], lines[index + 2], lines[index + 3], lines[index + 4])
  606 +
  607 + 'remember tiemstamp so we dont load any after
  608 + If lines[index + 5] > loadRemoteTimestamp
  609 + loadRemoteTimestamp = lines[index + 5]
  610 + EndIf
  611 + EndIf
  612 + Next
  613 +
658 614 loadTimestamp = ms
659 615 End
660 616
661 617 'reset http buffer
662 618 httpMode = MODE_IDLE
663   - httpBuffer = ""
664 619 busyEndTimestamp = ms
665 620 End
666   -
  621 +
667 622 'api
668 623 Method LoadIcons:Void()
669 624 ' --- this will load icons from the server ---
... ... @@ -671,11 +626,12 @@ Class IconFun Extends App implements HTTPListener
671 626 'we can process a load now
672 627 loadTimestamp = ms
673 628 httpMode = MODE_LOAD
674   - if loadRemoteTimestamp.Length
675   - http.GetPage(REMOTE_SERVER + "?mode=load&timestamp=" + loadRemoteTimestamp, REMOTE_PORT, Self)
  629 + If loadRemoteTimestamp.Length
  630 + http = New HttpRequest("GET",REMOTE_SERVER + "?mode=load&timestamp=" + loadRemoteTimestamp, Self)
676 631 Else
677   - http.GetPage(REMOTE_SERVER + "?mode=load", REMOTE_PORT, Self)
  632 + http = New HttpRequest("GET", REMOTE_SERVER + "?mode=load", Self)
678 633 EndIf
  634 + http.Send()
679 635 busyText = "Refreshing Icons..."
680 636 Else
681 637 'we can schedule teh load once the save has finished
... ... @@ -689,7 +645,8 @@ Class IconFun Extends App implements HTTPListener
689 645 If httpMode = MODE_IDLE
690 646 'get data fro meditor
691 647 Local author:String = CleanString(editorAuthorInput.Value())
692   - Local title:String = CleanString(editorTitleInput.Value())
  648 + Local titleRaw:String = editorTitleInput.Value()
  649 + Local title:String = CleanString(titleRaw)
693 650 Local data:String
694 651
695 652 For Local index:Int = 0 until editorPixels.Length
... ... @@ -699,8 +656,9 @@ Class IconFun Extends App implements HTTPListener
699 656 'start http request
700 657 saveSuccess = False
701 658 httpMode = MODE_SAVE
702   - http.GetPage(REMOTE_SERVER + "?mode=save&author=" + author + "&title=" + title + "&data=" + data, REMOTE_PORT, Self)
703   - busyText = "Saving Your Icon '" + title + "' ..."
  659 + http = New HttpRequest("GET", REMOTE_SERVER + "?mode=save&author=" + author + "&title=" + title + "&data=" + data, Self)
  660 + http.Send()
  661 + busyText = "Saving Your Icon '" + titleRaw + "' ..."
704 662 Else
705 663 'save later
706 664 saveNext = True
... ... @@ -725,6 +683,8 @@ Class IconFun Extends App implements HTTPListener
725 683 if icon.id = id Return
726 684 Next
727 685
  686 + Print "adding icon: " + title + " (by: " + author + ")"
  687 +
728 688 'create new icon
729 689 icon = New Icon
730 690 icon.id = id
... ...