Code archives/Graphics/NinePatch Image
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Short description sort of sums it up. Basically, load an image (animated or not), create a NNinePatch, and call the InitWithImage method and pass the image to it. You can specify the top, left, bottom, and right border sizes as well as scaling for those (e.g., if you have a high-resolution image and you want the 50x50 pixel corners to be 10x10 pixel corners, you'd pass a scale of 0.2). As an example, here's an image you can load and some test code: SuperStrict Import "Ninepatch.bmx" Graphics 800, 600, 0 SetBlend(ALPHABLEND) Local nine:NNinePatch = New NNinePatch.InitWithImage(LoadAnimImage("../test.png", 128, 128, 0, 4), 12, 12, 12, 12, .8) SetClsColor(192, 192, 192) While Not AppTerminate() Cls Local mx%, my# mx = MouseX() my = MouseY() Local frame% = 0 If mx > 100 And mx < 356 And my > 100 And my < 356 Then frame = 1+MouseDown(1) EndIf nine.DrawRect(100, 100, 256, 256, frame) Flip Wend | |||||
SuperStrict Type NNinePatch Field _img:TImage Field _left_border#, _right_border#, _top_border#, _bottom_border# Field _width%, _height% Field _border_scale#=1.0 Method DrawRect(x#, y#, width#, height#, frame%=0) Const NINEPATCH_MINIMUM#=0.5# Local lb%, rb%, tb%, bb% lb = (NINEPATCH_MINIMUM <= _left_border) rb = (NINEPATCH_MINIMUM <= _right_border) tb = (NINEPATCH_MINIMUM <= _top_border) bb = (NINEPATCH_MINIMUM <= _bottom_border) Local lw# = lb*_left_border Local rw# = rb*_right_border Local th# = tb*_top_border Local bh# = bb*_bottom_border Local dw# = width-(lw+rw)*_border_scale Local dh# = height-(th+bh)*_border_scale Local sw# = _width-lw-rw Local sh# = _height-th-bh Local handlex#, handley# GetHandle(handlex,handley) If tb Then If lb Then DrawSubImageRect(_img, x, y, lw*_border_scale, th*_border_scale, 0, 0, lw, th, 0, 0, frame) EndIf DrawSubImageRect(_img, x+lw*_border_scale, y, dw, th*_border_scale, lw, 0, sw, th, 0, 0, frame ) If rb Then DrawSubImageRect(_img, x+dw+lw*_border_scale, y, rw*_border_scale, th*_border_scale, sw+lw, 0, rw, th, 0, 0, frame) EndIf EndIf If lb Then DrawSubImageRect(_img, x, y+th*_border_scale, lw*_border_scale, dh, 0, th, lw, sh, 0, 0, frame) EndIf DrawSubImageRect(_img, x+lw*_border_scale, y+th*_border_scale, dw, dh, lw, th, sw, sh, 0, 0, frame ) If rb Then DrawSubImageRect(_img, x+dw+lw*_border_scale, y+th*_border_scale, rw*_border_scale, dh, sw+lw, th, rw, sh, 0, 0, frame) EndIf If tb Then If lb Then DrawSubImageRect(_img, x, y+dh+th*_border_scale, lw*_border_scale, bh*_border_scale, 0, sh+th, lw, bh, 0, 0, frame) EndIf DrawSubImageRect(_img, x+lw*_border_scale, y+dh+th*_border_scale, dw, bh*_border_scale, lw, sh+th, sw, bh, 0, 0, frame ) If rb Then DrawSubImageRect(_img, x+dw+lw*_border_scale, y+dh+th*_border_scale, rw*_border_scale, bh*_border_scale, sw+lw, sh+th, rw, bh, 0, 0, frame) EndIf EndIf End Method Method InitWithImage:NNinePatch(img:TImage, left_border#=8, right_border#=8, top_border#=8, bottom_border#=8, border_scale#=1.0) _img = img _width = ImageWidth(img) _height = ImageHeight(img) _left_border = left_border _right_border = right_border _top_border = top_border _bottom_border = bottom_border _border_scale = border_scale Return Self End Method End Type |
Comments
None.
Code Archives Forum