style.monkey2 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. Namespace mojo.app
  2. #rem monkeydoc The Style class.
  3. #end
  4. Class Style
  5. Method New()
  6. End
  7. Method New( style:Style )
  8. Init( style )
  9. End
  10. Method Copy:Style()
  11. Return New Style( Self )
  12. End
  13. Method GetState:Style( name:String )
  14. Local state:=_states[name]
  15. If Not state Return Self
  16. Return state
  17. End
  18. Method AddState:Style( name:String )
  19. Local state:=_states[name]
  20. If state Return state
  21. state=New Style( Self )
  22. _states[name]=state
  23. Return state
  24. End
  25. #rem monkeydoc Name of the style.
  26. #end
  27. Property Name:String()
  28. Return _name
  29. End
  30. #rem monkeydoc Background color.
  31. #end
  32. Property BackgroundColor:Color()
  33. Return _bgcolor
  34. Setter( backgroundColor:Color )
  35. _bgcolor=backgroundColor
  36. End
  37. #rem monkeydoc Padding rect.
  38. #end
  39. Property Padding:Recti()
  40. Return _padding
  41. Setter( padding:Recti )
  42. _padding=padding
  43. End
  44. #rem monkeydoc @hidden
  45. #end
  46. Property Skin:Skin()
  47. Return _skin
  48. Setter( skin:Skin )
  49. _skin=skin
  50. End
  51. #rem monkeydoc @hidden
  52. #end
  53. Property SkinColor:Color()
  54. Return _skcolor
  55. Setter( skinColor:Color )
  56. _skcolor=skinColor
  57. End
  58. #rem monkeydoc Border rect.
  59. #end
  60. Property Border:Recti()
  61. Return _border
  62. Setter( border:Recti )
  63. _border=border
  64. End
  65. #rem monkeydoc Border color.
  66. #End
  67. Property BorderColor:Color()
  68. Return _bdcolor
  69. Setter( borderColor:Color )
  70. _bdcolor=borderColor
  71. End
  72. #rem monkeydoc Margin rect.
  73. #end
  74. Property Margin:Recti()
  75. Return _margin
  76. Setter( margin:Recti )
  77. _margin=margin
  78. End
  79. #rem monkeydoc Color to use when drawing text.
  80. #end
  81. Property TextColor:Color()
  82. Return _textColor
  83. Setter( color:Color )
  84. _textColor=color
  85. End
  86. #rem monkeydoc Color to use when drawing icons.
  87. #end
  88. Property IconColor:Color()
  89. Return _iconColor
  90. Setter( color:Color )
  91. _iconColor=color
  92. End
  93. #rem monkeydoc Font to use when drawing text.
  94. Deprecated! Just use [[Font]] instead...
  95. #end
  96. Property DefaultFont:Font()
  97. Return _font
  98. Setter( font:Font )
  99. _font=font
  100. End
  101. #rem monkeydoc Font to use when drawing text.
  102. #end
  103. Property Font:Font()
  104. Return _font
  105. Setter( font:Font )
  106. _font=font
  107. End
  108. #rem monkeydoc Custom icons.
  109. #end
  110. Property Icons:Image[]()
  111. Return _icons
  112. Setter( icons:Image[] )
  113. _icons=icons
  114. End
  115. #rem monkeydoc Total style bounds.
  116. #end
  117. Property Bounds:Recti()
  118. Local bounds:=Padding
  119. Local skin:=Skin
  120. If skin bounds+=Skin.Bounds
  121. bounds+=Border
  122. bounds+=Margin
  123. Return bounds
  124. End
  125. #rem monkeydoc Measure text.
  126. #end
  127. Method MeasureText:Vec2i( text:String )
  128. If Not text Return New Vec2i( 0,0 )
  129. If text.Contains( "~n" )
  130. Local lines:=text.Split( "~n" ),w:=0
  131. For Local line:=Eachin lines
  132. w=Max( w,Int( _font.TextWidth( line ) ) )
  133. Next
  134. Return New Vec2i( w,_font.Height * lines.Length )
  135. Else
  136. Return New Vec2i( _font.TextWidth( text ),_font.Height )
  137. Endif
  138. End
  139. Method DrawText( canvas:Canvas,text:String,x:Int,y:Int,handlex:Float=0,handley:Float=0 )
  140. Local font:=canvas.Font
  141. Local color:=canvas.Color
  142. canvas.Font=_font
  143. canvas.Color=_textColor
  144. canvas.DrawText( text,x,y,handlex,handley )
  145. canvas.Font=font
  146. canvas.Color=color
  147. End
  148. Method DrawText( canvas:Canvas,text:String,rect:Recti,gravity:Vec2f )
  149. If Not text Return
  150. Local size:=MeasureText( text )
  151. Local x:=rect.Left + (rect.Width-size.x) * gravity.x
  152. Local y:=rect.Top + (rect.Height-size.y) * gravity.y
  153. Local font:=canvas.Font
  154. Local color:=canvas.Color
  155. canvas.Font=_font
  156. canvas.Color=_textColor
  157. If text.Contains( "~n" )
  158. Local lines:=text.Split( "~n" )
  159. For Local line:=Eachin lines
  160. If line canvas.DrawText( line,x + (size.x-_font.TextWidth( line )) * gravity.x,y )
  161. y+=_font.Height
  162. Next
  163. Else If text<>"~n"
  164. canvas.DrawText( text,x,y )
  165. Endif
  166. canvas.Font=font
  167. canvas.Color=color
  168. End
  169. Method DrawIcon( canvas:Canvas,icon:Image,x:Int,y:Int )
  170. Local color:=canvas.Color
  171. canvas.Color=_iconColor
  172. canvas.DrawImage( icon,x,y )
  173. canvas.Color=color
  174. End
  175. #rem monkeydoc @hidden
  176. #end
  177. Method Render( canvas:Canvas,bounds:Recti )
  178. bounds-=Margin
  179. Local border:=Border
  180. Local bdcolor:=BorderColor
  181. If (border.Width Or border.Height) And bdcolor.a
  182. canvas.Color=bdcolor
  183. Local x:=bounds.X,y:=bounds.Y
  184. Local w:=bounds.Width,h:=bounds.Height
  185. Local l:=-border.min.x,r:=border.max.x
  186. Local t:=-border.min.y,b:=border.max.y
  187. canvas.DrawRect( x,y,l,h-b )
  188. canvas.DrawRect( x+l,y,w-l,t )
  189. canvas.DrawRect( x+w-r,y+t,r,h-t )
  190. canvas.DrawRect( x,y+h-b,w-r,b )
  191. Endif
  192. bounds-=border
  193. Local bgcolor:=BackgroundColor
  194. If bgcolor.a
  195. canvas.Color=bgcolor
  196. canvas.DrawRect( bounds.X,bounds.Y,bounds.Width,bounds.Height )
  197. Endif
  198. Local skin:=Skin
  199. Local skcolor:=SkinColor
  200. If skin And skcolor.a
  201. canvas.Color=skcolor
  202. skin.Draw( canvas,bounds )
  203. Endif
  204. canvas.Font=_font
  205. canvas.Color=Color.White
  206. End
  207. '***** INTERNAL *****
  208. #rem monkeydoc @hidden
  209. #end
  210. Property States:StringMap<Style>()
  211. If Not _states _states=New StringMap<Style>
  212. Return _states
  213. End
  214. Private
  215. Field _name:String
  216. Field _bgcolor:Color=Color.None
  217. Field _padding:Recti
  218. Field _skin:Skin
  219. Field _skcolor:Color=Color.White
  220. Field _border:Recti
  221. Field _bdcolor:Color=Color.None
  222. Field _margin:Recti
  223. Field _textColor:Color=Color.Black
  224. Field _iconColor:Color=Color.White
  225. Field _icons:Image[]
  226. Field _font:Font
  227. Field _states:=New StringMap<Style>
  228. Method Init( style:Style )
  229. If Not style Return
  230. _bgcolor=style._bgcolor
  231. _padding=style._padding
  232. _skin=style._skin
  233. _skcolor=style._skcolor
  234. _border=style._border
  235. _bdcolor=style._bdcolor
  236. _margin=style._margin
  237. _textColor=style._textColor
  238. _iconColor=style._iconColor
  239. _icons=style._icons.Slice( 0 )
  240. _font=style._font
  241. If style._states
  242. For Local it:=Eachin style._states
  243. _states[it.Key]=New Style( it.Value )
  244. Next
  245. Endif
  246. End
  247. End