/ld27

To get this branch, use:
bzr branch /bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/debug/instrument.lua

  • Committer: Josh C
  • Date: 2014-07-03 14:41:57 UTC
  • Revision ID: josh@9ix.org-20140703144157-xydxt62xzcdq6bdk
cluke009 zoetrope + my spritebatch changes

Show diffs side-by-side

added added

removed removed

 
1
-- Class: DebugInstrument
 
2
-- This is a container that manages the location and size of debug instruments
 
3
-- onscreen. This also creates a title bar and framed content area.
 
4
--
 
5
-- Event: onResize
 
6
--              Responsible for setting the dimensions of all member sprites. Receives
 
7
--              x, y, width, and height arguments. This *must* be defined in order for
 
8
--              an instrument to be drawn properly. You do not need to resize the built-in
 
9
--              frame, titleBar, and title properties in this handler -- that is taken care
 
10
--              of for you.
 
11
--
 
12
--              An instrument is guaranteed to receive this event at least once before it
 
13
--              appears onscreen, so there is no need to set up dimensions of sprites anywhere
 
14
--              but here.
 
15
--
 
16
-- Extends:
 
17
--              <Group>
 
18
 
 
19
DebugInstrument = Group:extend
 
20
{
 
21
        -- Property: contentHeight
 
22
        -- Set by the instrument to indicate how much vertical space it wants.
 
23
        -- Changing this causes the debugger to recalculate layout for all instruments.
 
24
        -- A '*' indicates that it wants all available space. If the instrument
 
25
        -- doesn't want to be considered for layout, then it should set its visible
 
26
        -- property to false.
 
27
        contentHeight = 0,
 
28
 
 
29
        -- Property: width
 
30
        -- Should be either 'wide' or 'narrow', *not* a pixel width. This must
 
31
        -- be set when the instrument is created, and cannot be changed.
 
32
        width = 'narrow',
 
33
 
 
34
        -- Property: innerBorderColor
 
35
        -- Property: outerBorderColor
 
36
        -- Property: backgroundColor
 
37
        -- Property: titleBarColor
 
38
        -- Property: titleColor
 
39
        -- Color properties to customize appearance. These must be set when the
 
40
        -- instrument is created, and cannot be changed.
 
41
        outerBorderColor = {0, 0, 0},
 
42
        innerBorderColor = {255, 255, 255},
 
43
        backgroundColor = {0, 0, 0, 200},
 
44
        titleBarColor = {255, 255, 255},
 
45
        titleColor = {0, 0, 0},
 
46
 
 
47
        -- Property: spacing
 
48
        -- A recommended number of pixels to inset content or otherwise use
 
49
        -- as spacing, for consistency. You can ignore this if you like.
 
50
        spacing = 5,
 
51
 
 
52
        -- Property: titleBarHeight
 
53
        -- How tall the title bar should be. This must be set when the instrument
 
54
        -- is created, and cannot be changed.
 
55
        titleBarHeight = 20,
 
56
 
 
57
        -- Property: font
 
58
        -- Recommended font, for consistency.
 
59
 
 
60
        -- Property: outerFrame
 
61
        -- The <Fill> used to draw the outer frame of the instrument.
 
62
 
 
63
        -- Property: innerFrame
 
64
        -- The <Fill> used to draw the inner frame and background of the instrument.
 
65
 
 
66
        -- Property: titleBar
 
67
        -- The <Fill> used to draw the background of the title bar.
 
68
 
 
69
        -- Property: title
 
70
        -- The <Text> used to draw the instrument title on the title bar.
 
71
 
 
72
        new = function (self, obj)
 
73
                obj = self:extend(obj or {})
 
74
 
 
75
                if obj.width == 'wide' then
 
76
                        obj.font = 12
 
77
                else
 
78
                        obj.font = 11
 
79
                end
 
80
 
 
81
                obj.outerFrame = obj:add(Fill:new{ width = 0, height = 0, border = obj.outerBorderColor, fill = {0, 0, 0, 0} })
 
82
                obj.innerFrame = obj:add(Fill:new{ width = 0, height = 0, border = obj.innerBorderColor, fill = obj.backgroundColor })
 
83
                obj.titleBar = obj:add(Fill:new{ width = 0, height = obj.titleBarHeight, fill = obj.titlebarColor })
 
84
                obj.title = obj:add(Text:new{ width = 0, height = 0, fill = obj.titlebarColor, font = obj.font,
 
85
                                      tint = {obj.titleColor[1] / 255, obj.titleColor[2] / 255, obj.titleColor[3] / 255}})
 
86
 
 
87
                if obj.onNew then obj:onNew() end
 
88
                return obj
 
89
        end,
 
90
 
 
91
        -- Method: resize
 
92
        -- Tells the instrument to resize itself to match the dimensions passed.
 
93
        --
 
94
        -- Arguments:
 
95
        --              x - x coordinate in pixels
 
96
        --              y - y coordinate in pixels
 
97
        --              width - width in pixels
 
98
        --              height - height in pixels
 
99
        --
 
100
        -- Returns:
 
101
        --              nothing
 
102
 
 
103
        resize = function (self, x, y, width, height)
 
104
                self.outerFrame.x = x - 1
 
105
                self.outerFrame.y = y - 1
 
106
                self.outerFrame.width = width + 2
 
107
                self.outerFrame.height = height + 2
 
108
 
 
109
                self.innerFrame.x = x
 
110
                self.innerFrame.y = y
 
111
                self.innerFrame.width = width
 
112
                self.innerFrame.height = height
 
113
 
 
114
                self.titleBar.x = x
 
115
                self.titleBar.y = y
 
116
                self.titleBar.width = width
 
117
 
 
118
                local titleHeight = self.title._fontObj:getHeight()
 
119
                local titleInset = (self.titleBarHeight - titleHeight) / 2
 
120
                self.title.x = self.titleBar.x + titleInset
 
121
                self.title.y = self.titleBar.y + titleInset
 
122
                self.title.width = width - titleInset * 2
 
123
                self.title.height = titleHeight
 
124
 
 
125
                if self.onResize then
 
126
                        self:onResize(x, y + self.titleBarHeight, width, height - self.titleBarHeight)
 
127
                end
 
128
        end,
 
129
 
 
130
        -- Method: totalHeight
 
131
        -- Returns the total height of the instrument, including the title bar.
 
132
        -- 
 
133
        -- Arguments:
 
134
        --              none
 
135
        --
 
136
        -- Returns:
 
137
        --              pixel height, 0 if no display wanted, or '*' (for maximum height available)
 
138
 
 
139
        totalHeight = function (self)
 
140
                if self.contentHeight == 0 or self.contentHeight == '*' then
 
141
                        return self.contentHeight
 
142
                else
 
143
                        return self.contentHeight + self.titleBarHeight
 
144
                end
 
145
        end,
 
146
 
 
147
        draw = function (self, x, y)
 
148
                local sx, sy, sw, sh = love.graphics.getScissor()
 
149
                love.graphics.setScissor(the.app.inset.x + self.outerFrame.x - 1,
 
150
                                         the.app.inset.y + self.outerFrame.y - 1,
 
151
                                         self.outerFrame.width + 2, self.outerFrame.height + 2)
 
152
                Group.draw(self, x, y)
 
153
 
 
154
                if sx and sy then
 
155
                        love.graphics.setScissor(sx, sy, sw, sh)
 
156
                else
 
157
                        love.graphics.setScissor()
 
158
                end
 
159
        end
 
160
}
 
161
 
 
162
-- Class: DebugInstrumentButton
 
163
-- A convenience class to keep the appearance of buttons in instruments consistent.
 
164
 
 
165
DebugInstrumentButton = Button:extend
 
166
{
 
167
        width = 75,
 
168
        height = 25,
 
169
 
 
170
        new = function (self, obj)
 
171
                obj = self:extend(obj or {})
 
172
 
 
173
                obj.background = Fill:new
 
174
                {
 
175
                        width = self.width, height = self.height,
 
176
                        fill = {0, 0, 0, 64},
 
177
                        border = {255, 255, 255}
 
178
                }
 
179
 
 
180
                obj.label = Text:new
 
181
                {
 
182
                        width = self.width, height = self.height,
 
183
                        align = 'center',
 
184
                        y = 5,
 
185
                        font = DebugInstrument.font,
 
186
                        text = obj.label
 
187
                }
 
188
 
 
189
                Button.new(obj)
 
190
                return obj
 
191
        end,
 
192
 
 
193
        onMouseEnter = function (self)
 
194
                self.background.fill = { 128, 128, 128, 200 }
 
195
        end,
 
196
 
 
197
        onMouseExit = function (self)
 
198
                self.background.fill = {0, 0, 0, 64 }
 
199
        end
 
200
}