Advertisement
RedKnight91

nine_slice

Feb 8th, 2019
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /// @function nine_slice(sprite, x, y, width, height, stretched)
  2. /// @arg sprite
  3. /// @arg x
  4. /// @arg y
  5. /// @arg width
  6. /// @arg height
  7. /// @arg stretched
  8. // Initialize variables
  9.  
  10. var _sprite, _x, _y, _x_pos, _y_pos, _center, _width, _height, _stretched, _sprite_width, _sprite_height, _tile_width, _tile_height;
  11.  
  12. // Define arguments
  13. _sprite = argument0; // Choose the panel sprite
  14. _x = argument1; // x coordinate
  15. _y = argument2; // y coordinate
  16. _width = argument3; // Width of the panel
  17. _height = argument4; // Height of the panel
  18. _stretched = argument5; // Stretch the panel (true) or tile (false)
  19.  
  20. // Calculate sprite width and height
  21. _sprite_width = sprite_get_width(_sprite);
  22. _sprite_height = sprite_get_height(_sprite);
  23.  
  24.  
  25. //DRAW PANEL CORNERS
  26.  
  27. // Top Left
  28. draw_sprite(_sprite, 0, _x, _y);
  29.  
  30. // Top Right
  31. draw_sprite(_sprite, 2, _x + _width - _sprite_width, _y);
  32.  
  33. // Bottom Left
  34. draw_sprite(_sprite, 6, _x, _y + _height - _sprite_height);
  35.  
  36. // Bottom Right
  37. draw_sprite(_sprite, 8, _x + _width - _sprite_width, _y + _height - _sprite_height);
  38.  
  39.  
  40. //DRAW PANEL SIDES & CENTER
  41.  
  42. if (_stretched)
  43. {
  44. //Top center
  45. draw_sprite_stretched(_sprite, 1, _x + _sprite_width, _y, _width - _sprite_width * 2, _sprite_height);
  46.  
  47. // Middle Left
  48. draw_sprite_stretched(_sprite, 3, _x, _y + _sprite_height, _sprite_width, _height - _sprite_height*2);
  49.  
  50. // Middle Center
  51. draw_sprite_stretched(_sprite, 4, _x + _sprite_width, _y + _sprite_height, _width - _sprite_width*2, _height - _sprite_height*2);
  52.  
  53. // Middle Right
  54. draw_sprite_stretched(_sprite, 5, _x + _width - _sprite_width, _y + _sprite_height, _sprite_width, _height - _sprite_height*2);
  55.  
  56. //Bottom center
  57. draw_sprite_stretched(_sprite, 7, _x + _sprite_width, _y + _height - _sprite_height, _width - _sprite_width*2, _sprite_height);
  58. }
  59. else
  60. {
  61. // If not stretched, calculate number of sprite repetitions
  62. _width += _width mod _sprite_width;
  63. _height += _height mod _sprite_height;
  64.  
  65. _tile_width = ((_width - _sprite_width * 2) / _sprite_width);
  66. _tile_height = ((_height - _sprite_height * 2) / _sprite_height);
  67.  
  68. for (_x_pos = 1; _x_pos <= _tile_width; _x_pos++)
  69. {
  70. //Top Center
  71. draw_sprite(_sprite, 1, _x + (_x_pos * _sprite_width), _y);
  72.  
  73. //Middle Center
  74. for (_y_pos = 1; _y_pos <= _tile_height; _y_pos++)
  75. draw_sprite(_sprite, 4, _x + (_x_pos * _sprite_width), _y + (_y_pos * _sprite_height));
  76.  
  77. //Bottom Center
  78. draw_sprite(_sprite, 7, _x + (_x_pos * _sprite_width), _y + _height - _sprite_height);
  79. }
  80.  
  81. for (_y_pos = 1; _y_pos <= _tile_height; _y_pos++)
  82. {
  83. // Middle Left
  84. draw_sprite(_sprite, 3, _x, _y + (_y_pos * _sprite_height));
  85.  
  86. // Middle Right
  87. draw_sprite(_sprite, 5, _x + _width - _sprite_width, _y + (_y_pos * _sprite_height));
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement