Module: CDK::Movement

Included in:
CDKOBJS
Defined in:
lib/cdk/mixins/movement.rb

Instance Method Summary collapse

Instance Method Details

#move(xplace, yplace, relative, refresh_flag) ⇒ Object



3
4
5
6
# File 'lib/cdk/mixins/movement.rb', line 3

def move(xplace, yplace, relative, refresh_flag)
  self.move_specific(xplace, yplace, relative, refresh_flag,
      [@win, @shadow_win], [])
end

#move_specific(xplace, yplace, relative, refresh_flag, windows, subwidgets) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cdk/mixins/movement.rb', line 8

def move_specific(xplace, yplace, relative, refresh_flag,
    windows, subwidgets)
  current_x = @win.getbegx
  current_y = @win.getbegy
  xpos = xplace
  ypos = yplace

  # If this is a relative move, then we will adjust where we want
  # to move to.
  if relative
    xpos = @win.getbegx + xplace
    ypos = @win.getbegy + yplace
  end

  # Adjust the window if we need to
  xtmp = [xpos]
  ytmp = [ypos]
  alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Get the difference
  xdiff = current_x - xpos
  ydiff = current_y - ypos

  # Move the window to the new location.
  windows.each do |window|
    CDK.moveCursesWindow(window, -xdiff, -ydiff)
  end

  subwidgets.each do |subwidget|
    subwidget.move(xplace, yplace, relative, false)
  end

  # Touch the windows so they 'move'
  CDK::SCREEN.refreshCDKWindow(@screen.window)

  # Redraw the window, if they asked for it
  if refresh_flag
    self.draw(@box)
  end
end

#position(win) ⇒ Object

This allows the user to use the cursor keys to adjust the postion of the widget.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cdk/mixins/movement.rb', line 53

def position(win)
  parent = @screen.window
  orig_x = win.getbegx
  orig_y = win.getbegy
  beg_x = parent.getbegx
  beg_y = parent.getbegy
  end_x = beg_x + @screen.window.getmaxx
  end_y = beg_y + @screen.window.getmaxy

  # Let them move the widget around until they hit return.
  while !([CDK::KEY_RETURN, Ncurses::KEY_ENTER].include?(
      key = self.getch([])))
    case key
    when Ncurses::KEY_UP, '8'.ord
      if win.getbegy > beg_y
        self.move(0, -1, true, true)
      else
        CDK.Beep
      end
    when Ncurses::KEY_DOWN, '2'.ord
      if (win.getbegy + win.getmaxy) < end_y
        self.move(0, 1, true, true)
      else
        CDK.Beep
      end
    when Ncurses::KEY_LEFT, '4'.ord
      if win.getbegx > beg_x
        self.move(-1, 0, true, true)
      else
        CDK.Beep
      end
    when Ncurses::KEY_RIGHT, '6'.ord
      if (win.getbegx + win.getmaxx) < end_x
        self.move(1, 0, true, true)
      else
        CDK.Beep
      end
    when '7'.ord
      if win.getbegy > beg_y && win.getbegx > beg_x
        self.move(-1, -1, true, true)
      else
        CDK.Beep
      end
    when '9'.ord
      if (win.getbegx + win.getmaxx) < end_x && win.getbegy > beg_y
        self.move(1, -1, true, true)
      else
        CDK.Beep
      end
    when '1'.ord
      if win.getbegx > beg_x && (win.getbegy + win.getmaxy) < end_y
        self.move(-1, 1, true, true)
      else
        CDK.Beep
      end
    when '3'.ord
      if (win.getbegx + win.getmaxx) < end_x &&
          (win.getbegy + win.getmaxy) < end_y
        self.move(1, 1, true, true)
      else
        CDK.Beep
      end
    when '5'.ord
      self.move(CDK::CENTER, CDK::CENTER, false, true)
    when 't'.ord
      self.move(win.getbegx, CDK::TOP, false, true)
    when 'b'.ord
      self.move(win.getbegx, CDK::BOTTOM, false, true)
    when 'l'.ord
      self.move(CDK::LEFT, win.getbegy, false, true)
    when 'r'.ord
      self.move(CDK::RIGHT, win.getbegy, false, true)
    when 'c'.ord
      self.move(CDK::CENTER, win.getbegy, false, true)
    when 'C'.ord
      self.move(win.getbegx, CDK::CENTER, false, true)
    when CDK::REFRESH
      @screen.erase
      @screen.refresh
    when CDK::KEY_ESC
      self.move(orig_x, orig_y, false, true)
    else
      CDK.Beep
    end
  end
end