Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 134 lines (102 sloc) 5.045 kb
ee8c778 second tutorial snapshot save
megaannum authored
1 # Modifying an Existing Glyph
2
3 In creating the example "Pallet Designer" there was the need for
4 a Glyph that could display both the RGB hex color, the Xterm 256
5 closest color equivalent and the color itself in the background.
6
7 In the Glyphs defined in the forms.vim file, such a Glyph does not
8 exist.
9
10 The approach taken was to create an instance of the Area Glyph,
11 tailor that instance by providing it with additional attributes
12 and methods and, then, use clones of that tailored instance
13 as the new display objects.
14
15 So, first create an instance of the Area Glyph:
16
17 let colorblock = forms#newArea({'width': 6, 'height': 2})
18
19 The colorblock instance has a height of 2 and a width of 6, the
20 right size to hold both a 6 character hex String and a 1 to 3
21 character Xterm 256 Number.
22
23 Next, add attributes to this instance:
24
25 let colorblock.__rgbtxt = "000000"
26 let colorblock.__numbertxt = "0"
27 let colorblock.__hiname = "ERROR_HIGHLIGHT_NOT_SET"
28
29 It has a new attributes to hold both the RGB hex String and the
30 Xterm 256 color number. Also, it has an attribute which will be
31 the name of the highlight that defines its background color.
32
33 Because there are new attributes which will be used to generate a highlight,
34 an 'init()' method is defined. Within the colorblock 'init()' method,
35 there is a call to its Prototype's 'init()' methods (a must) and then
36 the attributes are used to generate the highlight.
37
38 function! colorblock.init(attrs) dict
39 call call(g:forms#Area.init, [a:attrs], self)
40
41 let ctermfg = "0"
42 let guifg="#000000"
43 execute ":hi ".self.__hiname." ctermfg=".ctermfg." ctermbg=".self.__numbertxt." guifg=".guifg." guibg=#".self.__rgbtxt
44 return self
45 endfunction
46
47 While the RGB text takes up the full width of the colorblock Glyph,
48 the Xterm 256 Number does not, so a method is defined that will
49 center the number in the colorblock when it is drawn:
50
51 function! colorblock.setNumberText(numbertxt) dict
52 let l = len(a:numbertxt)
53 if l == 3
54 let self.__numbertxt = " " . a:numbertxt
55 elseif l == 2
56 let self.__numbertxt = " " . a:numbertxt
57 else
58 let self.__numbertxt = " " . a:numbertxt
59 endif
60 endfunction
61
62 Because code that might call the "Pallet Designer" may want to get
63 the RGB and Xterm Number as a result, the colorblock needs to define
64 the 'addResults()' method adding the attribute values to the results
65 Dictionary:
66
67 function! colorblock.addResults(results) dict
68 " only add to results if its activitly being displayed, i.e.,
69 " self.__matchId exisits
70 if exists("self.__matchId")
71 let nt = self.__numbertxt[1:]
72 if nt[0] == ' '
73 let nt = nt[1:]
74 endif
75 if nt[0] == ' '
76 let nt = nt[1:]
77 endif
78 let a:results[self.__hiname] = [self.__rgbtxt, nt]
79 endif
80 endfunction
81
82 A clone of the colorblock Object may not always be displayed. When a Glyph
83 transitions from being displayed to not being displayed, the Viewer will
84 call its 'hide()' method. Generally, Glyphs that simply display characters
85 and do *not* define their own additional highlight colors, do not have
86 to define their own 'hide()' method. But, if a Glyph does highlighting
87 while being drawn, then when the Glyph is not to be drawn, its 'hide()'
88 method must delete its highlight.
89
90 function! colorblock.hide() dict
91 call GlyphDeleteHi(self)
92 endfunction
93
94 As an aside, if one is using a Deck and a Card in the Deck highlights
95 as it draws, then it is import that that Card have a 'hide()'
96 method so that when the Card is no longer being drawn (no longer the
97 top Card), that its highlight can be removed.
98
99 Finally, the colorblock must have a 'draw()' method:
100
101 function! colorblock.draw(allocation) dict
102 call call(g:forms#Area.draw, [a:allocation], self)
103
104 let a = a:allocation
105 call forms#SetStringAt(self.__rgbtxt, a.line, a.column)
106 call forms#SetStringAt(self.__numbertxt, a.line+1, a.column)
107 call GlyphHilight(self, self.__hiname, a)
108 endfunction
109
110 The 'draw()' method calls its Prototype's draw method which
111 saves the 'allocation' parameter and then draws the RGB hex String,
112 the Xterm Number and sets the color using the highlight for the given
113 allocation.
114
115
116 Lastly, this colorblock Object is used as the Prototype for a number of
117 specific 'blocks' each with their own highlight name:
118
119 let oneblock = colorblock.clone().init({ 'hiname' : 'MOneHi' })
120
121
122 The colorblock is cloned and the new Object is initialized.
123
124 The colorblock block was defined and used in private code; it was not
125 created for general usage. As a result, somethings that a more
126 reusable Glyph might have are missing with the colorblock Object.
127 For instance, there is no 'reinit()' method defined since in the
128 "Pallet Designer" the blocks would never need it. Also, the 'init()'
129 code does no attribute value validation.
130
131 Such short cuts are quite alright as long as there is no expectation
132 that the developer or others might, at a later time, attempt reuse.
133
Something went wrong with that request. Please try again.