Advertisement
loloof64

client side rust webview

Nov 22nd, 2018
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 6.60 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var rpc = undefined;
  4.  
  5. var crossStr =
  6. '<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"\n'+
  7. 'width="469.785px" height="469.785px"\n'+
  8. 'viewBox="0 0 469.785 469.785" style="enable-background:new 0 0 469.785 469.785;" xml:space="preserve">\n'+
  9. '<g transform="matrix(1.25 0 0 -1.25 0 45)">\n'+
  10. '   <path style="fill:#DD2E44;" d="M228.294-151.753L367.489-12.558c11.116,11.105,11.116,29.116,0,40.22\n'+
  11. '       c-11.105,11.116-29.104,11.116-40.22,0L188.073-111.533L48.866,27.663c-11.093,11.116-29.116,11.116-40.22,0\n'+
  12. '       c-11.105-11.105-11.105-29.116,0-40.22l139.207-139.196L8.338-291.268c-11.116-11.116-11.116-29.116,0-40.22\n'+
  13. '       c5.552-5.564,12.834-8.34,20.116-8.34c7.27,0,14.552,2.776,20.105,8.34l139.514,139.514l139.196-139.196\n'+
  14. '       c5.564-5.552,12.834-8.34,20.116-8.34c7.27,0,14.552,2.788,20.105,8.34c11.116,11.105,11.116,29.104,0,40.22L228.294-151.753z"/>\n'+
  15. '</g>\n'+
  16. '</svg>\n';
  17.  
  18. var circleStr =
  19. '<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"\n'+
  20. '    width="438.533px" height="438.533px" viewBox="0 0 438.533 438.533" style="enable-background:new 0 0 438.533 438.533;"\n'+
  21. '    xml:space="preserve">\n'+
  22. '   <g>\n'+
  23. '       <path d="M409.133,109.203c-19.608-33.592-46.205-60.189-79.798-79.796C295.736,9.801,259.058,0,219.273,0\n'+
  24. '           c-39.781,0-76.47,9.801-110.063,29.407c-33.595,19.604-60.192,46.201-79.8,79.796C9.801,142.8,0,179.489,0,219.267\n'+
  25. '           c0,39.78,9.804,76.463,29.407,110.062c19.607,33.592,46.204,60.189,79.799,79.798c33.597,19.605,70.283,29.407,110.063,29.407\n'+
  26. '           s76.47-9.802,110.065-29.407c33.593-19.602,60.189-46.206,79.795-79.798c19.603-33.596,29.403-70.284,29.403-110.062\n'+
  27. '           C438.533,179.485,428.732,142.795,409.133,109.203z M353.742,297.208c-13.894,23.791-32.736,42.633-56.527,56.534\n'+
  28. '           c-23.791,13.894-49.771,20.834-77.945,20.834c-28.167,0-54.149-6.94-77.943-20.834c-23.791-13.901-42.633-32.743-56.527-56.534\n'+
  29. '           c-13.897-23.791-20.843-49.772-20.843-77.941c0-28.171,6.949-54.152,20.843-77.943c13.891-23.791,32.738-42.637,56.527-56.53\n'+
  30. '           c23.791-13.895,49.772-20.84,77.943-20.84c28.173,0,54.154,6.945,77.945,20.84c23.791,13.894,42.634,32.739,56.527,56.53\n'+
  31. '           c13.895,23.791,20.838,49.772,20.838,77.943C374.58,247.436,367.637,273.417,353.742,297.208z"/>\n'+
  32. '   </g>\n'+
  33. '</svg>\n';
  34.  
  35. function load(){
  36.     var BOARD_SIZE = 300;
  37.     var SQUARE_SIZE = parseInt(BOARD_SIZE / 3);
  38.     var PIECE_SIZE = parseInt(SQUARE_SIZE * 0.8);
  39.     var PIECE_CELL_INSET = (SQUARE_SIZE - PIECE_SIZE) / 2;
  40.  
  41.     function drawBoardLines(canvas) {
  42.         for (var i = 0; i < 2; i++){
  43.             var hLine = new fabric.Rect({
  44.                 left: 10,
  45.                 width: 280,
  46.                 top: SQUARE_SIZE * (i+1) - 5,
  47.                 height: 10,
  48.                 selectable: false,
  49.             });
  50.             canvas.add(hLine);
  51.  
  52.             var vLine = new fabric.Rect({
  53.                 top: 10,
  54.                 height: 280,
  55.                 left: SQUARE_SIZE * (i+1) - 5,
  56.                 width: 10,
  57.                 selectable: false,
  58.             });
  59.             canvas.add(vLine);
  60.         }
  61.  
  62.         canvas.renderAll();
  63.     }
  64.  
  65.     var canvas = new fabric.Canvas('c', {
  66.         backgroundColor: 'rgb(200,200,200)',
  67.         selectionColor: 'black',
  68.         selectionLineWidth: 2
  69.     });
  70.  
  71.     var elements = [];
  72.  
  73.     drawBoardLines(canvas);
  74.  
  75.     var addCross = function(cellX, cellY) {
  76.         fabric.loadSVGFromString(crossStr,function(objects, options) {
  77.             var obj = fabric.util.groupSVGElements(objects, options);
  78.             obj.set({ left: SQUARE_SIZE * (cellX+1) + PIECE_CELL_INSET,
  79.                     top: SQUARE_SIZE * (cellY+1) + PIECE_CELL_INSET,
  80.                 })
  81.                 .setCoords();
  82.             obj.scaleToWidth(PIECE_SIZE);
  83.             obj.scaleToHeight(PIECE_SIZE);
  84.  
  85.             canvas.add(obj).renderAll();
  86.         });
  87.     }
  88.  
  89.     var addCircle = function(cellX, cellY) {
  90.         fabric.loadSVGFromString(circleStr,function(objects, options) {
  91.             var obj = fabric.util.groupSVGElements(objects, options);
  92.             obj.scaleToHeight(PIECE_SIZE)
  93.                 .set({ left: SQUARE_SIZE * (cellX+1) + PIECE_CELL_INSET,
  94.                     top: SQUARE_SIZE * (cellY+1) + PIECE_CELL_INSET,
  95.                 })
  96.                 .setCoords();
  97.             obj.scaleToWidth(PIECE_SIZE);
  98.             obj.scaleToHeight(PIECE_SIZE);
  99.  
  100.             canvas.add(obj).renderAll();
  101.         });
  102.     }
  103.  
  104.     var consume_piece_cb = undefined;
  105.     var consume_turn_cb = undefined;
  106.  
  107.     rpc = {
  108.         invoke : function(arg) { window.external.invoke(JSON.stringify(arg)); },
  109.  
  110.         set_consume_piece_callback: function(cb){
  111.             consume_piece_cb = cb;
  112.         },
  113.  
  114.         set_consume_turn_callback: function(cb){
  115.             consume_turn_cb = cb;
  116.         },
  117.  
  118.         init: function() {
  119.             rpc.invoke({cmd: 'Init'});
  120.         },
  121.  
  122.         play: function(cell_x, cell_y) {
  123.             rpc.invoke({cmd: 'Play', cell_x, cell_y});
  124.         },
  125.  
  126.         reset: function() {
  127.             rpc.invoke({cmd: 'Reset'});
  128.         },
  129.  
  130.         load_piece_at: function(cell_x, cell_y) {
  131.             rpc.invoke({cmd: 'GetPieceAt', cell_x, cell_y})
  132.         },
  133.  
  134.         consume_piece: function(piece, cell_x, cell_y) {
  135.             consume_piece_cb(piece, cell_x, cell_y);
  136.         },
  137.  
  138.         consume_turn: function(turn) {
  139.             consume_turn_cb(turn);
  140.         },
  141.  
  142.         render: function(board) {
  143.             var clear = function(){
  144.                 for (var i = 0; i < elements.length; i++){
  145.                     var currElt = elements[i];
  146.                     canvas.remove(currElt);
  147.                 }
  148.             }
  149.  
  150.             clear();
  151.  
  152.             for (var cellY = 0; cellY < 3; cellY++)
  153.             {
  154.                 for (var cellX = 0; cellX < 3; cellX++)
  155.                 {
  156.                     rpc.load_piece_at(cellX, cellY);
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.     rpc.init();
  163.  
  164.     rpc.set_consume_piece_callback(function(piece, cellX, cellY) {
  165.         if (piece == 'Cross') addCross(cellX, cellY);
  166.         else if (piece == 'Circle') addCircle(cellX, cellY);
  167.     });
  168.  
  169.     canvas.on('mouse:down', function(evt){
  170.         var x = parseInt(evt.pointer.x);
  171.         var y = parseInt(evt.pointer.y);
  172.  
  173.         var cellX = parseInt(x / SQUARE_SIZE);
  174.         var cellY = parseInt(y / SQUARE_SIZE);
  175.  
  176.         if (cellX < 0 || cellX > 2) return;
  177.         if (cellY < 0 || cellY > 2) return;
  178.  
  179.         rpc.play(cellX, cellY);
  180.     });
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement