style.monkey2 5.9 KB

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