Guest User

BlockOS Dynamic App Placement

a guest
Jun 19th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.41 KB | None | 0 0
  1. // ------------------------------------------------------------
  2. // 2 App Positioning Handler
  3. // ------------------------------------------------------------
  4.  
  5. // Needs to: onMouseClick->GetPositionOfMouse->FindWhatIcon->onMouseDrag->MoveIcon->SaveIconPos
  6. package BlockOSPackage
  7. {
  8.     // Detects when the left mouse button is pushed down
  9.     function GuiMouseEventCtrl::onMouseDown(%this, %mod, %pos, %click)
  10.     {
  11.         // Check if it's the BlockOS control
  12.         if(%this.getName() $= "BlockOS_MouseCapture")
  13.         {
  14.             // Get the icon the mouse clicked on
  15.             %obj = getIconFromPos(%pos);
  16.             if(isObject(%obj))
  17.             {
  18.                 // Store the icon object
  19.                 BlockOS_MouseCapture.selectedIcon = %obj;
  20.                 BlockOS_MouseCapture.selectedIcon.originalPos = %obj.position;
  21.             }
  22.         }
  23.         // Make sure we don't break everything else by parenting the function
  24.         parent::onMouseDown(%this, %mod, %pos, %click);
  25.     }
  26.    
  27.     // This function is called when a user drags the mouse
  28.     function GuiMouseEventCtrl::onMouseDragged(%this, %bool, %pos, %x)
  29.     {
  30.         // Check if it's the BlockOS control
  31.         if(%this.getName() $= "BlockOS_MouseCapture")
  32.         {
  33.             // Find what icon the user clicked on in the previous function
  34.             %obj = BlockOS_MouseCapture.selectedIcon;
  35.            
  36.             // Some maths that put the cursor to the center of the icon for movement
  37.             %x = getWord(%pos, 0) - (getWord(BlockOS_MouseCapture.selectedIcon.extent, 0) / 2);
  38.             %y = getWord(%pos, 1) - (getWord(BlockOS_MouseCapture.selectedIcon.extent, 1) / 2);
  39.            
  40.             // Moves the icon as the mouse moves
  41.             %obj.position = %x SPC %y;
  42.         }
  43.         // Make sure we don't break everything else by parenting the function
  44.         parent::onMouseDragged(%this, %bool, %pos, %x);
  45.     }
  46.  
  47.     // This function is called when the user let's go of the left mouse button
  48.     function GuiMouseEventCtrl::onMouseUp(%this, %mod, %pos, %click)
  49.     {
  50.         // Again, to check if it's the BlockOS control
  51.         if(%this.getName() $= "BlockOS_MouseCapture")
  52.         {
  53.             // Now, if the user has not dragged the icon
  54.             if(BlockOS_MouseCapture.selectedIcon.position $= BlockOS_MouseCapture.selectedIcon.originalPos)
  55.             {
  56.                 // We run the icon's command
  57.                 eval(BlockOS_MouseCapture.selectedIcon.command);
  58.             }
  59.             else
  60.             {
  61.                 // If the user has dragged the icon, we run a check to see if it's in a valid place
  62.                 schedule(100, 0, BlockOSIconValidation, BlockOS_MouseCapture.selectedIcon);
  63.                 // We then save these to the desktop.dat file
  64.                 schedule(500, 0, BlockOS_SaveIconPositions);
  65.             }
  66.             // Clears the selected icon from the control
  67.             BlockOS_MouseCapture.selectedIcon = "";
  68.         }
  69.         // Make sure we don't break everything else by parenting the function
  70.         parent::onMouseUp(%this, %mod, %pos, %click);
  71.     }
  72.  
  73.     // This function is called when the right mouse button is pressed
  74.     function GuiMouseEventCtrl::onRightMouseDown(%this, %mod, %pos, %click)
  75.     {
  76.         // Again, to check if it's the BlockOS control
  77.         if(%this.getName() $= "BlockOS_MouseCapture")
  78.         {
  79.             // Get the icon the mouse clicked on
  80.             %object = getIconFromPos(%pos);
  81.             if(isObject(%object))
  82.             {
  83.                 // If this icon does have a right click menu,
  84.                 if(!%object.disableRightClick)
  85.                 {
  86.                     // this generates the menu
  87.                     generateRightClickMenu(%object);
  88.                 }
  89.             }
  90.         }
  91.         // Make sure we don't break everything else by parenting the function
  92.         parent::onRightMouseDown(%this, %mod, %pos, %click);
  93.     }
  94.  
  95.     // This function is called when the right mouse button is released, however we currently don't have a use for it
  96.     // but it's here anyway :D
  97.     function GuiMouseEventCtrl::onRightMouseUp(%this, %mod, %pos, %click)
  98.     {
  99.         if(%this.getName() $= "BlockOS_MouseCapture")
  100.         {
  101.            
  102.         }
  103.         parent::onRightMouseUp(%this, %mod, %pos, %click);
  104.     }
  105. };
  106. // Start the mouse functions
  107. activatePackage(BlockOSPackage);
  108.  
  109. // This is the function we use to check if the icon is in a valid position
  110. function BlockOSIconValidation(%iconObject)
  111. {
  112.     // Get the x and y positions of the icon
  113.     %x = getWord(%iconObject.position, 0);
  114.     %y = getWord(%iconObject.position, 1);
  115.    
  116.     // Check if the icon is on top of the bin
  117.     if(getIconFromPos(%iconObject.position).getName() $= "bin")
  118.     {
  119.         // Just so the bin can't delete itself
  120.         if(%iconObject.getName() !$= "bin")
  121.         {  
  122.             // Check if the icon is default
  123.             if(!%iconObject.noDelete)
  124.             {
  125.                 // If not delete the icon from the canvas
  126.                 %iconObject.delete();
  127.                 %isSorted = true;
  128.             }
  129.         }
  130.     }
  131.    
  132.     // If the icon is on top of the sidebar  
  133.     if(%x < 100)
  134.     {
  135.         // Snap it into the sidebar
  136.         addToSideBar(%iconObject);
  137.         %isSorted = true;    
  138.     }
  139.    
  140.     // If it isn't in on the sidebar
  141.     if(%x > 100 && %iconObject.isSidebar)
  142.     {
  143.         // Remove it if it's on it.
  144.         removeFromSideBar(%iconObject);
  145.     }
  146.    
  147.     // If the icon is overlapping the top bar
  148.     if(%y < 30 && !%iconObject.isSidebar)
  149.     {
  150.         // snap the icon back to it's original position
  151.         %iconObject.position = %iconObject.originalPos;
  152.         %isSorted = true;
  153.     }
  154.    
  155.     // This simply snaps the icons to the 66x66px grid
  156.     %x = mRound(%x / 66) * 66;
  157.     %y = mRound(%y / 66) * 66;
  158.    
  159.     // if the icon is not sorted by any of the previous code
  160.     if(!%isSorted)
  161.     {
  162.         // Put it where the user requested
  163.         %iconObject.position = %x SPC %y;
  164.     }
  165.  
  166. }
  167.  
  168. function getIconFromPos(%pos)
  169. {
  170.     // Get the X and Y positions of the cursor
  171.     %mouseX = getWord(%pos, 0);
  172.     %mouseY = getWord(%pos, 1);
  173.    
  174.     // Loop through all of the icons
  175.     for(%i = 0; %i < BOS_IconGroup.getCount(); %i++)
  176.     {
  177.         // For each one, get the X and Y positions and dimensions
  178.         %obj = BOS_IconGroup.getObject(%i);
  179.         %iconXpos = getWord(%obj.position, 0);
  180.         %iconYpos = getWord(%obj.position, 1);
  181.        
  182.         %iconXDimension = getWord(%obj.extent, 0);
  183.         %iconYDimension = getWord(%obj.extent, 1);
  184.        
  185.         // Check if the cursor is within the area of the icon
  186.         if(%mouseX >= %iconXpos && %mouseY >= %iconYpos && %mouseX < %iconXpos + %iconXDimension && %mouseY < %iconYpos + %iconYDimension)
  187.         {
  188.             // If it is, return the icon object
  189.             return %obj;
  190.         }
  191.     }
  192.     // If we haven't found one there, return false
  193.     return false;
  194. }
  195.  
  196. function mRound(%a)
  197. {
  198.     // Either round up or down
  199.     if(%a - mFloor(%a) > 0.5)
  200.         return mCeil(%a);
  201.     else
  202.         return mFloor(%a);
  203. }
  204.  
  205. // Gui Component
  206. new GuiMouseEventCtrl(BlockOS_MouseCapture) {
  207.    profile = "GuiDefaultProfile";
  208.    horizSizing = "width";
  209.    vertSizing = "height";
  210.    position = "0 0";
  211.    extent = "640 480";
  212.    minExtent = "8 2";
  213.    visible = "1";
  214.    lockMouse = "0";
  215. };
Advertisement
Add Comment
Please, Sign In to add comment