longtext.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Long text widget.
  2. module Zori
  3. class LongText
  4. include Widget
  5. include Draggable
  6. extend Forwardable
  7. BUTTON_BACKGROUND = [0x33, 0x33, 0xff, 0xaa]
  8. HOVER_BACKGROUND = [0x55, 0x55, 0xff, 0xaa]
  9. def initialize(params={}, &block)
  10. super(params, &block)
  11. text = params[:text]
  12. @margin = params[:margin] || 4
  13. @align = params[:align] || :left
  14. @text = text
  15. @bg = graph_box(@x, @y, @w, @h)
  16. @bg.border_thickness = 0
  17. @bg.border_color = [255, 255, 255, 128]
  18. @bg.background_color = BUTTON_BACKGROUND
  19. text_f = Eruta::ALIGN_LEFT
  20. if @align == :center
  21. text_f = Eruta::ALIGN_CENTER
  22. elsif @align == :right
  23. text_f = Eruta::ALIGN_RIGHT
  24. end
  25. @tg = graph_longtext(@x, @y, @w, @h, text)
  26. @tg.text_flags = text_f
  27. @tg.font = Eruta::Zori.font.id
  28. @tg.background_color = [0,0,0]
  29. @tg.color = [255,255, 64]
  30. @tg.margin = @margin
  31. @tg.page = 0
  32. end
  33. def on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  34. super
  35. # Check for hovering.
  36. if self.inside?(x, y)
  37. @bg.border_thickness = 1
  38. @bg.background_color = HOVER_BACKGROUND
  39. self.hover
  40. else
  41. @bg.border_thickness = 0
  42. @bg.background_color = BUTTON_BACKGROUND
  43. self.unhover
  44. end
  45. return false # don't consume the event
  46. end
  47. def on_mouse_button_down(t, x, y, z, w, b)
  48. super
  49. return false unless self.inside?(x, y)
  50. self.delay = self.delay / 10.0
  51. p self.line_start, self.line_stop, b
  52. if @action
  53. @action.call(self)
  54. end
  55. end
  56. def on_mouse_button_up(t, x, y, z, w, b)
  57. self.delay = self.delay * 10.0
  58. if @tg.paused && (b == 1)
  59. @tg.next_page
  60. end
  61. if (b==2)
  62. @tg.page = 0
  63. end
  64. return super
  65. end
  66. def can_drag?
  67. return true
  68. end
  69. def text=(t)
  70. @tg.text = t
  71. end
  72. def_delegator :@tg, :line_start=
  73. def_delegator :@tg, :line_stop=
  74. def_delegator :@tg, :delay=
  75. def_delegator :@tg, :line_start
  76. def_delegator :@tg, :line_stop
  77. def_delegator :@tg, :delay
  78. def_delegator :@tg, :page_lines=
  79. def_delegator :@tg, :page_lines
  80. def_delegator :@tg, :paused=
  81. def_delegator :@tg, :paused
  82. def_delegator :@tg, :page=
  83. def_delegator :@tg, :page
  84. def_delegator :@tg, :last_page
  85. def_delegator :@tg, :next_page
  86. def_delegator :@tg, :previous_page
  87. def_delegator :@tg, :"at_end?"
  88. end
  89. end