skin.monkey2 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. Namespace mojo.app
  2. Using std.resource
  3. #rem monkeydoc @hidden
  4. #end
  5. Class Skin Extends Resource
  6. Property Image:Image()
  7. Return _image
  8. End
  9. Property Bounds:Recti()
  10. Return _bounds
  11. End
  12. Method Draw( canvas:Canvas,rect:Recti )
  13. Local x0:=rect.Left
  14. Local x1:=rect.Left+_x1
  15. Local x2:=rect.Right-(_x3-_x2)
  16. Local x3:=rect.Right
  17. Local y0:=rect.Top
  18. Local y1:=rect.Top+_y1
  19. Local y2:=rect.Bottom-(_y3-_y2)
  20. Local y3:=rect.Bottom
  21. canvas.DrawRect( x0,y0,x1-x0,y1-y0,_image,_x0,_y0,_x1-_x0,_y1-_y0 )
  22. canvas.DrawRect( x1,y0,x2-x1,y1-y0,_image,_x1,_y0,_x2-_x1,_y1-_y0 )
  23. canvas.DrawRect( x2,y0,x3-x2,y1-y0,_image,_x2,_y0,_x3-_x2,_y1-_y0 )
  24. canvas.DrawRect( x0,y1,x1-x0,y2-y1,_image,_x0,_y1,_x1-_x0,_y2-_y1 )
  25. canvas.DrawRect( x1,y1,x2-x1,y2-y1,_image,_x1,_y1,_x2-_x1,_y2-_y1 )
  26. canvas.DrawRect( x2,y1,x3-x2,y2-y1,_image,_x2,_y1,_x3-_x2,_y2-_y1 )
  27. canvas.DrawRect( x0,y2,x1-x0,y3-y2,_image,_x0,_y2,_x1-_x0,_y3-_y2 )
  28. canvas.DrawRect( x1,y2,x2-x1,y3-y2,_image,_x1,_y2,_x2-_x1,_y3-_y2 )
  29. canvas.DrawRect( x2,y2,x3-x2,y3-y2,_image,_x2,_y2,_x3-_x2,_y3-_y2 )
  30. End
  31. Function Load:Skin( path:String )
  32. Local pixmap:=Pixmap.Load( path,,True )
  33. If Not pixmap Return Null
  34. Return New Skin( pixmap )
  35. End
  36. Private
  37. Field _image:Image
  38. Field _bounds:Recti
  39. Field _x0:Int,_x1:Int,_x2:Int,_x3:Int
  40. Field _y0:Int,_y1:Int,_y2:Int,_y3:Int
  41. Method New( pixmap:Pixmap )
  42. Local stretch:Recti
  43. Local padding:Recti
  44. For Local x:=1 Until pixmap.Width-1
  45. Local p:=pixmap.GetPixelARGB( x,0 )
  46. If p=UInt( $ff000000 )
  47. If Not stretch.min.x stretch.min.x=x
  48. stretch.max.x=x+1
  49. Endif
  50. p=pixmap.GetPixelARGB( x,pixmap.Height-1 )
  51. If p=UInt( $ff000000 )
  52. If Not padding.min.x padding.min.x=x
  53. padding.max.x=x+1
  54. Endif
  55. Next
  56. For Local y:=1 Until pixmap.Height-1
  57. Local p:=pixmap.GetPixelARGB( 0,y )
  58. If p=UInt( $ff000000 )
  59. If Not stretch.min.y stretch.min.y=y
  60. stretch.max.y=y+1
  61. Endif
  62. p=pixmap.GetPixelARGB( pixmap.Width-1,y )
  63. If p=UInt( $ff000000 )
  64. If Not padding.min.y padding.min.y=y
  65. padding.max.y=y+1
  66. Endif
  67. Next
  68. If stretch.min.x And stretch.min.y
  69. pixmap=pixmap.Window( 1,1,pixmap.Width-2,pixmap.Height-2 ).Copy()
  70. If Not padding.min.x Or Not padding.min.y padding=stretch
  71. stretch-=New Vec2i( 1,1 )
  72. padding-=New Vec2i( 1,1 )
  73. Else
  74. stretch=New Recti( pixmap.Width/2,pixmap.Height/2,pixmap.Width*2+1,pixmap.Height*2+1 )
  75. padding=stretch
  76. Endif
  77. _x0=0
  78. _x1=stretch.min.x
  79. _x2=stretch.max.x
  80. _x3=pixmap.Width
  81. _y0=0
  82. _y1=stretch.min.y
  83. _y2=stretch.max.y
  84. _y3=pixmap.Height
  85. _image=New Image( pixmap )
  86. _bounds=New Recti( -padding.min.x,-padding.min.y,_x3-padding.max.x,_y3-padding.max.y )
  87. AddDependancy( _image )
  88. End
  89. End
  90. Class ResourceManager Extension
  91. Method OpenSkin:Skin( path:String )
  92. Local slug:="Skin:name="+StripDir( StripExt( path ) )
  93. Local skin:=Cast<Skin>( OpenResource( slug ) )
  94. If skin Return skin
  95. Local pixmap:=OpenPixmap( path,,True )
  96. If pixmap skin=New Skin( pixmap )
  97. AddResource( slug,skin )
  98. Return skin
  99. End
  100. End