Module: RNDK::Draw

Defined in:
lib/rndk/core/draw.rb

Class Method Summary collapse

Class Method Details

.attrbox(win, tlc, trc, blc, brc, horz, vert, attr) ⇒ Object

This draws a box with attributes and lets the user define each element of the box



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rndk/core/draw.rb', line 31

def Draw.attrbox(win, tlc, trc, blc, brc, horz, vert, attr)
  x1 = 0
  y1 = 0
  y2 = Ncurses.getmaxy(win) - 1
  x2 = Ncurses.getmaxx(win) - 1
  count = 0

  # Draw horizontal lines
  if horz != 0
    Ncurses.mvwhline(win, y1, 0, horz | attr, Ncurses.getmaxx(win))
    Ncurses.mvwhline(win, y2, 0, horz | attr, Ncurses.getmaxx(win))
    count += 1
  end

  # Draw vertical lines
  if vert != 0
    Ncurses.mvwvline(win, 0, x1, vert | attr, Ncurses.getmaxy(win))
    Ncurses.mvwvline(win, 0, x2, vert | attr, Ncurses.getmaxy(win))
    count += 1
  end

  # Draw in the corners.
  if tlc != 0
    Ncurses.mvwaddch(win, y1, x1, tlc | attr)
    count += 1
  end
  if trc != 0
    Ncurses.mvwaddch(win, y1, x2, trc | attr)
    count += 1
  end
  if blc != 0
    Ncurses.mvwaddch(win, y2, x1, blc | attr)
    count += 1
  end
  if brc != 0
    Ncurses.mvwaddch(win, y2, x2, brc | attr)
    count += 1
  end
  if count != 0
    Ncurses.wrefresh win
  end
end

.boxWindow(window, attr) ⇒ Object

This prints out a box around a window with attributes



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rndk/core/draw.rb', line 7

def Draw.boxWindow(window, attr)
  tlx = 0
  tly = 0
  brx = Ncurses.getmaxx(window) - 1
  bry = Ncurses.getmaxy(window) - 1

  # Draw horizontal lines.
  Ncurses.mvwhline(window, tly, 0, Ncurses::ACS_HLINE | attr, Ncurses.getmaxx(window))
  Ncurses.mvwhline(window, bry, 0, Ncurses::ACS_HLINE | attr, Ncurses.getmaxx(window))

  # Draw horizontal lines.
  Ncurses.mvwvline(window, 0, tlx, Ncurses::ACS_VLINE | attr, Ncurses.getmaxy(window))
  Ncurses.mvwvline(window, 0, brx, Ncurses::ACS_VLINE | attr, Ncurses.getmaxy(window))

  # Draw in the corners.
  Ncurses.mvwaddch(window, tly, tlx, Ncurses::ACS_ULCORNER | attr)
  Ncurses.mvwaddch(window, tly, brx, Ncurses::ACS_URCORNER | attr)
  Ncurses.mvwaddch(window, bry, tlx, Ncurses::ACS_LLCORNER | attr)
  Ncurses.mvwaddch(window, bry, brx, Ncurses::ACS_LRCORNER | attr)
  Ncurses.wrefresh(window)
end

.drawLine(window, startx, starty, endx, endy, line) ⇒ Object

This draws a line on the given window. (odd angle lines not working yet)



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
# File 'lib/rndk/core/draw.rb', line 83

def Draw.drawLine(window, startx, starty, endx, endy, line)
  xdiff = endx - startx
  ydiff = endy - starty
  x = 0
  y = 0

  # Determine if we're drawing a horizontal or vertical line.
  if ydiff == 0
    if xdiff > 0
      Ncurses.mvwhline(window, starty, startx, line, xdiff)
    end
  elsif xdiff == 0
    if ydiff > 0
      Ncurses.mvwvline(window, starty, startx, line, ydiff)
    end
  else
    # We need to determine the angle of the line.
    height = xdiff
    width = ydiff
    xratio = if height > width then 1 else width / height end
    yration = if width > height then width / height else 1 end
    xadj = 0
    yadj = 0

    # Set the vars
    x = startx
    y = starty
    while x!= endx && y != endy
      # Add the char to the window
      Ncurses.mvwaddch(window, y, x, line)

      # Make the x and y adjustments.
      if xadj != xratio
        x = if xdiff < 0 then x - 1 else x + 1 end
        xadj += 1
      else
        xadj = 0
      end
      if yadj != yratio
        y = if ydiff < 0 then y - 1 else y + 1 end
        yadj += 1
      else
        yadj = 0
      end
    end
  end
end

.drawObjBox(win, widget) ⇒ Object

Draw a box around the given window using the widget's defined line-drawing characters



76
77
78
79
80
# File 'lib/rndk/core/draw.rb', line 76

def Draw.drawObjBox(win, widget)
  Draw.attrbox(win,
      widget.ULChar, widget.URChar, widget.LLChar, widget.LRChar,
      widget.HZChar, widget.VTChar, widget.BXAttr)
end

.drawShadow(shadow_win) ⇒ Object

This draws a shadow around a window.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rndk/core/draw.rb', line 132

def Draw.drawShadow(shadow_win)
  unless shadow_win.nil?
    x_hi = Ncurses.getmaxx(shadow_win) - 1
    y_hi = Ncurses.getmaxy(shadow_win) - 1

    # Draw the line on the bottom.
    Ncurses.mvwhline(shadow_win, y_hi, 1, Ncurses::ACS_HLINE | RNDK::Color[:dim], x_hi)

    # Draw the line on teh right.
    Ncurses.mvwvline(shadow_win, 0, x_hi, Ncurses::ACS_VLINE | RNDK::Color[:dim], y_hi)

    Ncurses.mvwaddch(shadow_win, 0, x_hi, Ncurses::ACS_URCORNER | RNDK::Color[:dim])
    Ncurses.mvwaddch(shadow_win, y_hi, 0, Ncurses::ACS_LLCORNER | RNDK::Color[:dim])
    Ncurses.mvwaddch(shadow_win, y_hi, x_hi, Ncurses::ACS_LRCORNER | RNDK::Color[:dim])
    Ncurses.wrefresh shadow_win
  end
end

.writeBlanks(window, xpos, ypos, align, start, endn) ⇒ Object

Write a string of blanks using writeChar()



151
152
153
154
155
156
157
158
159
# File 'lib/rndk/core/draw.rb', line 151

def Draw.writeBlanks(window, xpos, ypos, align, start, endn)
  if start < endn
    want = (endn - start) + 1000
    blanks = ''

    RNDK.cleanChar(blanks, want - 1, ' ')
    Draw.writeChar(window, xpos, ypos, blanks, align, start, endn)
  end
end

.writeChar(window, xpos, ypos, string, align, start, endn) ⇒ Object

This writes out a char string with no attributes



162
163
164
165
# File 'lib/rndk/core/draw.rb', line 162

def Draw.writeChar(window, xpos, ypos, string, align, start, endn)
  Draw.writeCharAttrib(window, xpos, ypos, string, RNDK::Color[:normal],
      align, start, endn)
end

.writeCharAttrib(window, xpos, ypos, string, attr, align, start, endn) ⇒ Object

This writes out a char string with attributes



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rndk/core/draw.rb', line 168

def Draw.writeCharAttrib(window, xpos, ypos, string, attr, align,
    start, endn)
  display = endn - start

  if align == RNDK::HORIZONTAL
    # Draw the message on a horizontal axis
    display = [display, Ncurses.getmaxx(window) - 1].min
    (0...display).each do |x|
      Ncurses.mvwaddch(window, ypos, xpos + x, string[x + start].ord | attr)
    end
  else
    # Draw the message on a vertical axis
    display = [display, Ncurses.getmaxy(window) - 1].min
    (0...display).each do |x|
      Ncurses.mvwaddch(window, ypos + x, xpos, string[x + start].ord | attr)
    end
  end
end

.writeChtype(window, xpos, ypos, string, align, start, endn) ⇒ Object

This writes out a chtype string



188
189
190
191
# File 'lib/rndk/core/draw.rb', line 188

def Draw.writeChtype (window, xpos, ypos, string, align, start, endn)
  Draw.writeChtypeAttrib(window, xpos, ypos, string, RNDK::Color[:normal],
      align, start, endn)
end

.writeChtypeAttrib(window, xpos, ypos, string, attr, align, start, endn) ⇒ Object

This writes out a chtype string with the given attributes added.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rndk/core/draw.rb', line 194

def Draw.writeChtypeAttrib(window, xpos, ypos, string, attr,
    align, start, endn)
  diff = endn - start
  display = 0
  x = 0
  if align == RNDK::HORIZONTAL
    # Draw the message on a horizontal axis.
    display = [diff, Ncurses.getmaxx(window) - xpos].min
    (0...display).each do |x|
      Ncurses.mvwaddch(window, ypos, xpos + x, string[x + start].ord | attr)
    end
  else
    # Draw the message on a vertical axis.
    display = [diff, Ncurses.getmaxy(window) - ypos].min
    (0...display).each do |x|
      Ncurses.mvwaddch(window, ypos + x, xpos, string[x + start].ord | attr)
    end
  end
end