skin.monkey2 3.0 KB

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