style.monkey2 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. If _skin bounds+=_skin.Bounds
  127. bounds+=_border
  128. bounds+=_margin
  129. Return bounds
  130. End
  131. #rem monkeydoc Measure text.
  132. #end
  133. Method MeasureText:Vec2i( text:String )
  134. If Not text Return New Vec2i( 0,0 )
  135. If text.Contains( "~n" )
  136. Local lines:=text.Split( "~n" ),w:=0
  137. For Local line:=Eachin lines
  138. w=Max( w,Int( _font.TextWidth( line ) ) )
  139. Next
  140. Return New Vec2i( w,_font.Height * lines.Length )
  141. Else
  142. Return New Vec2i( _font.TextWidth( text ),_font.Height )
  143. Endif
  144. End
  145. Method DrawText( canvas:Canvas,text:String,x:Int,y:Int,handlex:Float=0,handley:Float=0 )
  146. Local font:=canvas.Font
  147. Local color:=canvas.Color
  148. canvas.Font=_font
  149. canvas.Color=_textColor
  150. canvas.DrawText( text,x,y,handlex,handley )
  151. canvas.Font=font
  152. canvas.Color=color
  153. End
  154. Method DrawText( canvas:Canvas,text:String,rect:Recti,gravity:Vec2f )
  155. If Not text Return
  156. Local size:=MeasureText( text )
  157. Local x:=rect.Left + (rect.Width-size.x) * gravity.x
  158. Local y:=rect.Top + (rect.Height-size.y) * gravity.y
  159. Local font:=canvas.Font
  160. Local color:=canvas.Color
  161. canvas.Font=_font
  162. canvas.Color=_textColor
  163. If text.Contains( "~n" )
  164. Local lines:=text.Split( "~n" )
  165. For Local line:=Eachin lines
  166. If line canvas.DrawText( line,x + (size.x-_font.TextWidth( line )) * gravity.x,y )
  167. y+=_font.Height
  168. Next
  169. Else If text<>"~n"
  170. canvas.DrawText( text,x,y )
  171. Endif
  172. canvas.Font=font
  173. canvas.Color=color
  174. End
  175. Method DrawIcon( canvas:Canvas,icon:Image,x:Int,y:Int )
  176. Local color:=canvas.Color
  177. canvas.Color=_iconColor
  178. canvas.DrawImage( icon,x,y )
  179. canvas.Color=color
  180. End
  181. #rem monkeydoc @hidden
  182. #end
  183. Method Render( canvas:Canvas,bounds:Recti )
  184. bounds-=_margin
  185. Local border:=Border
  186. Local bdcolor:=BorderColor
  187. If (border.Width Or border.Height) And bdcolor.a
  188. canvas.Color=bdcolor
  189. Local x:=bounds.X,y:=bounds.Y
  190. Local w:=bounds.Width,h:=bounds.Height
  191. Local l:=-border.min.x,r:=border.max.x
  192. Local t:=-border.min.y,b:=border.max.y
  193. canvas.DrawRect( x,y,l,h-b )
  194. canvas.DrawRect( x+l,y,w-l,t )
  195. canvas.DrawRect( x+w-r,y+t,r,h-t )
  196. canvas.DrawRect( x,y+h-b,w-r,b )
  197. Endif
  198. bounds-=border
  199. Local bgcolor:=BackgroundColor
  200. If bgcolor.a
  201. canvas.Color=bgcolor
  202. canvas.DrawRect( bounds )
  203. Endif
  204. Local skin:=Skin
  205. Local skcolor:=SkinColor
  206. If skin And skcolor.a
  207. canvas.Color=skcolor
  208. skin.Draw( canvas,bounds )
  209. Endif
  210. canvas.Font=_font
  211. canvas.Color=Color.White
  212. End
  213. '***** INTERNAL *****
  214. #rem monkeydoc @hidden
  215. #end
  216. Property States:StringMap<Style>()
  217. If Not _states _states=New StringMap<Style>
  218. Return _states
  219. End
  220. Private
  221. Field _name:String
  222. Field _bgcolor:Color=Color.None
  223. Field _padding:Recti
  224. Field _skin:Skin
  225. Field _skcolor:Color=Color.White
  226. Field _border:Recti
  227. Field _bdcolor:Color=Color.None
  228. Field _margin:Recti
  229. Field _textColor:Color=Color.Black
  230. Field _iconColor:Color=Color.White
  231. Field _icons:Image[]
  232. Field _font:Font
  233. Field _states:=New StringMap<Style>
  234. Method Init( style:Style )
  235. If Not style Return
  236. _bgcolor=style._bgcolor
  237. _padding=style._padding
  238. _skin=style._skin
  239. _skcolor=style._skcolor
  240. _border=style._border
  241. _bdcolor=style._bdcolor
  242. _margin=style._margin
  243. _textColor=style._textColor
  244. _iconColor=style._iconColor
  245. _icons=style._icons.Slice( 0 )
  246. _font=style._font
  247. _states.Clear()
  248. If style._states
  249. For Local it:=Eachin style._states
  250. _states[it.Key]=New Style( it.Value )
  251. Next
  252. Endif
  253. End
  254. End