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. _font=App.DefaultFont
  7. End
  8. Method New( style:Style )
  9. Init( style )
  10. End
  11. Method New( name:String,style:Style )
  12. _name=name
  13. Init( style )
  14. End
  15. Method Copy:Style()
  16. Return New Style( Self )
  17. End
  18. Method Set( style:Style )
  19. Init( style )
  20. End
  21. Method GetState:Style( name:String )
  22. Local state:=_states[name]
  23. If Not state Return Self
  24. Return state
  25. End
  26. Method AddState:Style( name:String )
  27. Local state:=_states[name]
  28. If state Return state
  29. state=New Style( Self )
  30. _states[name]=state
  31. Return state
  32. End
  33. #rem monkeydoc Name of the style.
  34. #end
  35. Property Name:String()
  36. Return _name
  37. End
  38. #rem monkeydoc Background color.
  39. #end
  40. Property BackgroundColor:Color()
  41. Return _bgcolor
  42. Setter( backgroundColor:Color )
  43. _bgcolor=backgroundColor
  44. End
  45. #rem monkeydoc Padding rect.
  46. #end
  47. Property Padding:Recti()
  48. Return _padding
  49. Setter( padding:Recti )
  50. _padding=padding
  51. End
  52. #rem monkeydoc @hidden
  53. #end
  54. Property Skin:Skin()
  55. Return _skin
  56. Setter( skin:Skin )
  57. _skin=skin
  58. End
  59. #rem monkeydoc @hidden
  60. #end
  61. Property SkinColor:Color()
  62. Return _skcolor
  63. Setter( skinColor:Color )
  64. _skcolor=skinColor
  65. End
  66. #rem monkeydoc Border rect.
  67. #end
  68. Property Border:Recti()
  69. Return _border
  70. Setter( border:Recti )
  71. _border=border
  72. End
  73. #rem monkeydoc Border color.
  74. #End
  75. Property BorderColor:Color()
  76. Return _bdcolor
  77. Setter( borderColor:Color )
  78. _bdcolor=borderColor
  79. End
  80. #rem monkeydoc Margin rect.
  81. #end
  82. Property Margin:Recti()
  83. Return _margin
  84. Setter( margin:Recti )
  85. _margin=margin
  86. End
  87. #rem monkeydoc Color to use when drawing text.
  88. #end
  89. Property TextColor:Color()
  90. Return _textColor
  91. Setter( color:Color )
  92. _textColor=color
  93. End
  94. #rem monkeydoc Color to use when drawing icons.
  95. #end
  96. Property IconColor:Color()
  97. Return _iconColor
  98. Setter( color:Color )
  99. _iconColor=color
  100. End
  101. #rem monkeydoc Font to use when drawing text.
  102. Deprecated! Just use [[Font]] instead...
  103. #end
  104. Property DefaultFont:Font()
  105. Return _font
  106. Setter( font:Font )
  107. _font=font
  108. End
  109. #rem monkeydoc Font to use when drawing text.
  110. #end
  111. Property Font:Font()
  112. Return _font
  113. Setter( font:Font )
  114. _font=font
  115. End
  116. #rem monkeydoc Custom icons.
  117. #end
  118. Property Icons:Image[]()
  119. Return _icons
  120. Setter( icons:Image[] )
  121. _icons=icons
  122. End
  123. #rem monkeydoc Total style bounds.
  124. #end
  125. Property Bounds:Recti()
  126. Local bounds:=_padding
  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