graph.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. class Graph < Eruta::Graph
  2. class Node
  3. attr_accessor :id
  4. def initialize(id)
  5. @id = id
  6. Graph.register(self)
  7. end
  8. def background_color=(col)
  9. r, g, b, a = *col
  10. a ||= 255
  11. Eruta::Graph.background_color_(@id, r, g, b, a)
  12. end
  13. def border_color=(col)
  14. r, g, b, a = *col
  15. a ||= 255
  16. Eruta::Graph.border_color_(@id, r, g, b, a)
  17. end
  18. def color=(col)
  19. r, g, b, a = *col
  20. a ||= 255
  21. Eruta::Graph.color_(@id, r, g, b, a)
  22. end
  23. def border_thickness=(t)
  24. Eruta::Graph.border_thickness_(@id, t)
  25. end
  26. def margin=(m)
  27. Eruta::Graph.margin_(@id, m)
  28. end
  29. def angle=(a)
  30. Eruta::Graph.angle_(@id, r, g, b, a)
  31. end
  32. def image_flags=(f)
  33. Eruta::Graph.image_flags_(@id,f)
  34. end
  35. def text_flags=(f)
  36. Eruta::Graph.text_flags_(@id, f)
  37. end
  38. #~ def text=(t)
  39. #~ Eruta::Graph.text_(@id, t)
  40. #~ end
  41. def size=(size)
  42. w, h = *size
  43. Eruta::Graph.size_(@id, w, h)
  44. end
  45. def visible=(value)
  46. Eruta::Graph.visible_(@id, value ? 1 : 0)
  47. end
  48. def z
  49. Eruta::Graph.z(@id)
  50. end
  51. def z=(newz)
  52. Eruta::Graph.z_(@id, newz)
  53. end
  54. def font=(store_id)
  55. Eruta::Graph.font_(@id, store_id)
  56. end
  57. def image=(store_id)
  58. Eruta::Graph.image_(@id, store_id)
  59. end
  60. def background_image=(store_id)
  61. Eruta::Graph.background_image_(@id, store_id)
  62. end
  63. def speed=(v)
  64. x, y = *v
  65. Eruta::Graph.speed_(@id, x, y)
  66. end
  67. def size=(sz)
  68. x, y = *sz
  69. Eruta::Graph.size_(@id, x, y)
  70. end
  71. def position=(p)
  72. x, y = *p
  73. Eruta::Graph.position_(@id, x, y)
  74. end
  75. def line_stop=(stop)
  76. Eruta::Graph.line_stop_(@id, store_id)
  77. end
  78. # Forwards methods to Eruta::Graph(@id, * args)
  79. def self.forward_graph(name)
  80. clean_name = name.to_s
  81. prefix = clean_name[0, clean_name.size - 1]
  82. if clean_name[-1] == "!"
  83. clean_name = prefix + '_bang'
  84. elsif clean_name[-1] == "?"
  85. clean_name = prefix + '_p'
  86. elsif clean_name[-1] == "="
  87. clean_name = prefix + '_'
  88. end
  89. # p "graph", name, clean_name
  90. graph_name = "#{clean_name}".to_sym
  91. define_method(name) do |*args|
  92. Eruta::Graph.send(graph_name, @id, *args)
  93. end
  94. end
  95. forward_graph :text=
  96. forward_graph :line_start=
  97. forward_graph :line_stop=
  98. forward_graph :delay=
  99. forward_graph :line_start
  100. forward_graph :line_stop
  101. forward_graph :delay
  102. forward_graph :page_lines=
  103. forward_graph :page_lines
  104. forward_graph :paused=
  105. forward_graph :paused
  106. forward_graph :page=
  107. forward_graph :page
  108. forward_graph :last_page
  109. forward_graph :next_page
  110. forward_graph :previous_page
  111. forward_graph :at_end?
  112. def close()
  113. Graph.unregister(self)
  114. @id = nil
  115. end
  116. end
  117. def self.registry
  118. @registry ||= {}
  119. end
  120. def self.register(thing)
  121. @registry ||= {}
  122. @registry[thing.id] = thing
  123. end
  124. def self.unregister(thing)
  125. @registry ||= {}
  126. @registry[thing.id] = nil
  127. Graph.disable(thing.id)
  128. thing = nil
  129. end
  130. def self.get_unused_id
  131. 29000.times do | i |
  132. return i unless self.registry[i]
  133. end
  134. return nil
  135. end
  136. def self.make_box(x, y, w, h, rx = 4, ry = 4, style_id = -1)
  137. id = self.get_unused_id
  138. nid = Eruta::Graph.make_box(id, x, y, w, h, rx, ry, style_id)
  139. return nil if (nid < 0)
  140. return Node.new(nid)
  141. end
  142. def self.make_image_ex(x, y, w, h, store_id, style_id = -1)
  143. id = self.get_unused_id
  144. nid = Eruta::Graph.make_image(id, x, y, w, h, store_id, style_id)
  145. return nil if (nid < 0)
  146. return Node.new(nid)
  147. end
  148. def self.make_image(x, y, store_id, style_id = -1)
  149. id = self.get_unused_id
  150. nid = Eruta::Graph.make_image(id, x, y, -1, -1, store_id, style_id)
  151. return nil if (nid < 0)
  152. return Node.new(nid)
  153. end
  154. def self.make_text(x, y, w, h, text, style_id = -1)
  155. id = self.get_unused_id
  156. nid = Eruta::Graph.make_text(id, x, y, w, h, text, style_id)
  157. return nil if (nid < 0)
  158. return Node.new(nid)
  159. end
  160. def self.make_longtext(x, y, w, h, text, style_id = -1)
  161. id = self.get_unused_id
  162. nid = Eruta::Graph.make_longtext(id, x, y, w, h, text, style_id)
  163. return nil if (nid < 0)
  164. return Node.new(nid)
  165. end
  166. end