Class: Reight::Index

Inherits:
Object
  • Object
show all
Includes:
Activatable, HasHelp, Hookable
Defined in:
lib/reight/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasHelp

#help, #name, #set_help

Methods included from Hookable

#hook

Methods included from Activatable

#activated, #activated!, #active=, #active?

Constructor Details

#initialize(index = 0, min: 0, max: nil, &changed) ⇒ Index

Returns a new instance of Index.



10
11
12
13
14
15
16
17
18
# File 'lib/reight/index.rb', line 10

def initialize(index = 0, min: 0, max: nil, &changed)
  hook :changed

  super()
  @min, @max = min, max

  self.changed(&changed) if changed
  self.index = index
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



20
21
22
# File 'lib/reight/index.rb', line 20

def index
  @index
end

Instance Method Details

#clickedObject



72
73
74
75
# File 'lib/reight/index.rb', line 72

def clicked()
  self.index += 1 if next?
  self.index -= 1 if prev?
end

#drawObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/reight/index.rb', line 29

def draw()
  no_stroke

  sp   = sprite
  w, h = sp.w, sp.h
  dec  = pressing? && prev?
  inc  = pressing? && next?
  decy = dec ? 1 : 0
  incy = inc ? 1 : 0

  fill 220
  rect 0,     decy, h, h, 2 if dec
  rect w - h, incy, h, h, 2 if inc

  text_align CENTER, CENTER
  fill 220
  text '<',   0,     decy + 1, h, h
  text '>',   w - h, incy + 1, h, h
  text index, 0,     1,        w, h
  fill 50
  text '<',   0,     decy,     h, h
  text '>',   w - h, incy,     h, h
  text index, 0,     0,        w, h
end

#hover(x, y) ⇒ Object



68
69
70
# File 'lib/reight/index.rb', line 68

def hover(x, y)
  r8.flash x < (sprite.w / 2) ? 'Prev' : 'Next'
end

#next?Boolean

Returns:

  • (Boolean)


56
# File 'lib/reight/index.rb', line 56

def next? = !prev?

#pressed(x, y) ⇒ Object



58
59
60
# File 'lib/reight/index.rb', line 58

def pressed(x, y)
  @pressing = true
end

#pressing?Boolean

Returns:

  • (Boolean)


66
# File 'lib/reight/index.rb', line 66

def pressing? = @pressing

#prev?Boolean

Returns:

  • (Boolean)


54
# File 'lib/reight/index.rb', line 54

def prev? = sprite.mouse_x < sprite.w / 2

#released(x, y) ⇒ Object



62
63
64
# File 'lib/reight/index.rb', line 62

def released(x, y)
  @pressing = false
end

#spriteObject



77
78
79
80
81
82
83
84
85
# File 'lib/reight/index.rb', line 77

def sprite()
  @sprite ||= RubySketch::Sprite.new(physics: false).tap do |sp|
    sp.draw           {draw}
    sp.mouse_pressed  {pressed  sp.mouse_x, sp.mouse_y}
    sp.mouse_released {released sp.mouse_x, sp.mouse_y}
    sp.mouse_moved    {hover    sp.mouse_x, sp.mouse_y}
    sp.mouse_clicked  {clicked}
  end
end