style.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Style is eveything relzted to the look and feel of a widget,
  2. # That is the foreground, background, border colors as well as
  3. # the font
  4. module Zori
  5. class Style
  6. # Style colors is a class that groups the colors of the style
  7. class Colors
  8. attr_accessor :foreground
  9. attr_accessor :background
  10. attr_accessor :border
  11. def initialize(fg = nil, bg = nil, br = nil, hi = nil, sh = nil)
  12. @foreground = fg || [255,255,255]
  13. @background = bg || [0,0,128]
  14. # PaleGreen # GoldenWhisp # PaleGoldenRod
  15. @border = br || [191,191,191]
  16. end
  17. # Returns a deep copy of self
  18. def deep_copy()
  19. return self.class.new(@text, @background, @border, @highlight, @shadow)
  20. end
  21. end
  22. # A spacer is used to leave space in the given direction between widgets
  23. # or between a widget and it's content
  24. class Spacers
  25. attr_accessor :top
  26. attr_accessor :left
  27. attr_accessor :bottom
  28. attr_accessor :right
  29. def initialize(t = 0, l = nil, b = nil, r = nil)
  30. @top = t || 0
  31. @bottom = b || @top
  32. @left = l || @top
  33. @right = r || @left
  34. end
  35. # Returns a deep copy of self
  36. def deep_copy()
  37. return self.class.new(@top, @left, @bottom, @right)
  38. end
  39. end
  40. attr_reader :font
  41. attr_reader :colors
  42. attr_reader :margin
  43. attr_reader :padding
  44. attr_accessor :alignment
  45. #
  46. DEFAULT_FONTNAME = 'dejavuserif'
  47. DEFAULT_MARGIN = 4
  48. DEFAULT_PADDING = 4
  49. DEFAULT_ALIGNMENT = :left
  50. DEFAULT_BACKGROUND = 'green.png'
  51. def self.font_dir
  52. return '/font'
  53. end
  54. def initialize(font = nil, colors_in = nil, background = DEFAULT_BACKGROUND)
  55. @font = font
  56. @colors = colors_in || Zori::Style::Colors.new
  57. @margin = DEFAULT_MARGIN
  58. @padding = DEFAULT_PADDING
  59. @alignment = DEFAULT_ALIGNMENT
  60. load_background(background)
  61. yield self if block_given?
  62. end
  63. def load_background(back)
  64. if back.is_a? String
  65. @bgimage = Zori::Hanao.load_image('ui', 'background', back) rescue nil;
  66. else
  67. @bgimage = back
  68. end
  69. end
  70. # Returns a deep copy of self
  71. def deep_copy()
  72. new_colors = self.colors.deep_copy();
  73. return self.class.new(@font, new_colors, @bgimage)
  74. end
  75. def colors_foreground()
  76. return self.colors.foreground
  77. end
  78. def colors_highlight()
  79. return self.colors.highlight
  80. end
  81. def colors_border()
  82. return self.colors.border
  83. end
  84. # Sets the graph node object to the given Zori style.
  85. def to_graph(node)
  86. node.background_color = @colors.background
  87. node.border_color = @colors.border
  88. node.color = @colors.foreground
  89. node.margin = @margin
  90. node.border_thickness = @thickness
  91. Eruta::Graph.border_thickness_(@id, t)
  92. end
  93. def angle=(a)
  94. Eruta::Graph.angle_(@id, r, g, b, a)
  95. end
  96. def size=(size)
  97. w, h = *size
  98. Eruta::Graph.size_(@id, w, h)
  99. end
  100. def font_id=(f)
  101. Eruta::Graph.font_(@id, f)
  102. end
  103. def self.default
  104. @default ||= self.new()
  105. return @default
  106. end
  107. end # class Style
  108. end # module Zori