Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("CLRPackage")
- --
- import("System.Drawing")
- import("System.Drawing.Drawing2D")
- import("System.Drawing.Text")
- import("System.Windows.Forms")
- --
- gauges = {}
- --
- function gauges.ctor( loc ) -- application object creator
- --
- -- private variables
- --
- local self = {} -- method and property container.
- local pos = loc or Point( 10, 10 ) -- control starting location in parent container.
- local this = GroupBox() -- display surface.
- local bars = {} -- bar values savearea.
- local pointerLocation = Point( 0, 0 ) -- current location of the mouse pointer.
- local clientPosition = Point( 0, 0 ) -- current location of control relative to the parent.
- local adjustedLocation = Point( 0, 0 ) -- resultant control location after move.
- local current, cIndex = {}, 0 -- this stack is used to maintain the cursor being used.
- local moving = false -- if set, the control is being dragged.
- local sf = StringFormat() -- text formatter for our percentage values.
- sf.Alignment = StringAlignment.Far -- set horizontal alignment.
- sf.LineAlignment = StringAlignment.Near -- set vertical alignment.
- local textFont = Font( "Miriam Fixed", 12, GraphicsUnit.Pixel ) -- percentage font.
- local hpRect = Rectangle( Point( 10, 20 ), Size( 100, 10 ) ) -- hp bar
- local mnRect = Rectangle( Point( 10, 40 ), Size( 100, 10 ) ) -- mana bar
- local mvRect = Rectangle( Point( 10, 60 ), Size( 100, 10 ) ) -- moves bar
- local barRect = { hp = hpRect, mn = mnRect, mv = mvRect } -- reference table for bar rectangles.
- local hptRect = RectangleF( PointF( 115, 20 ), SizeF( 30, 10 ) ) -- hp pct
- local mntRect = RectangleF( PointF( 115, 40 ), SizeF( 30, 10 ) ) -- mana pct
- local mvtRect = RectangleF( PointF( 115, 60 ), SizeF( 30, 10 ) ) -- moves pct
- local textRect = { hp = hptRect, mn = mntRect, mv = mvtRect } -- reference table for text rectangles.
- local colors = {
- Color.DodgerBlue -- value is over 100%
- ,Color.SpringGreen -- value is over 75%
- ,Color.Yellow -- value is over 50%
- ,Color.Orchid -- value is over 25%
- ,Color.Red -- value is under 26%
- }
- --
- -- private methods
- --
- local function getIndex( value ) -- determine which color to use to paint the bar.
- if type( value ) == "number" then
- local i = 5
- if value > 100 then i = 1
- elseif value > 75 then i = 2
- elseif value > 50 then i = 3
- elseif value > 25 then i = 4
- end
- return i
- else
- return nil
- end
- end
- local function move( p ) -- This function moves the control while it is being dragged.
- if self.Movable then
- local r = this.Parent.ClientRectangle
- local x, y = p.X, p.Y
- if x > r.Right - this.Width then x = r.Right - this.Width end
- if y > r.Bottom - this.Height then y = r.Bottom - this.Height end
- if x < r.Left then x = r.Left end
- if y < r.Top then y = r.Top end
- this.Location = Point ( x, y ) -- relocate control to new position.
- end
- end
- local function parent_Resize( sender, e )
- move( this.Location ) -- make sure the control stays within the parent visible area.
- end
- local function gauges_MouseDown( sender, e )
- if self.Movable then
- if e.Button == MouseButtons.Left then
- cIndex = cIndex + 1 -- update stack pointer.
- current[cIndex] = this.Cursor -- push current cursor into stack.
- this.Cursor = Cursors.SizeAll -- show move cursor.
- pointerLocation = e.Location -- remember where the mouse pointer is.
- this.ForeColor = Color.FromArgb( 255, 224, 224, 224 )
- this.BackColor = Color.FromArgb( 255, 105, 105, 105 )
- moving = true
- end
- end
- end
- local function gauges_MouseEnter( sender, e )
- if self.Movable then
- cIndex = cIndex + 1 -- update stack pointer.
- current[cIndex] = this.Cursor -- push current cursor into stack.
- this.Cursor = Cursors.Hand -- display hand cursor as draggable hint.
- end
- end
- local function gauges_MouseLeave( sender, e )
- if self.Movable then
- if cIndex > 0 then
- this.Cursor = current[cIndex] -- pop stack and restore cursor.
- cIndex = cIndex - 1 -- update stack pointer.
- end
- end
- end
- local function gauges_MouseMove( sender, e )
- if self.Movable then
- if moving then
- clientPosition = this.Parent:PointToClient( Cursor.Position )
- adjustedLocation = Point( clientPosition.X - pointerLocation.X, clientPosition.Y - pointerLocation.Y )
- move( adjustedLocation ) -- move the control while the mouse pointer is in motion.
- end
- end
- end
- local function gauges_MouseUp( sender, e )
- if self.Movable then
- if cIndex > 0 then
- this.Cursor = current[cIndex] -- pop stack and restore cursor.
- cIndex = cIndex - 1 -- update stack pointer.
- end
- if moving then
- moving = false -- indicate dragging has stopped.
- this.ForeColor = Color.White
- this.BackColor = Color.Black
- this:Invalidate( false ) -- force the repainting of the control.
- end
- end
- end
- local function gauges_Paint( sender, e )
- local g = e.Graphics -- fetch the graphics object for this control.
- for k, w in pairs( bars )
- do
- local i = getIndex( w ) -- obtain color index.
- if i then
- if w > 100 then w = 100 end -- take care of possible overflow.
- local r1 = Rectangle.FromLTRB( barRect[k].Left, barRect[k].Top, barRect[k].Left + w, barRect[k].Bottom )
- local r2 = Rectangle.FromLTRB( barRect[k].Left + w, barRect[k].Top, barRect[k].Right, barRect[k].Bottom )
- g:FillRectangle( SolidBrush( colors[i] ), r1 ) -- paint the active bar segment.
- g:FillRectangle( SolidBrush( Color.Gray ), r2 ) -- paint the unused bar segement.
- g:DrawString( tostring( bars[k] ), textFont, SolidBrush( colors[i] ), textRect[k], sf )
- end
- end
- g:Dispose() -- destroy graphics object.
- end
- local function gauges_ParentChanged( sender, e )
- pcall( function() this.Parent.Resize:Add( parent_Resize ) end )
- -- piggyback on the parent's resize event if it has one.
- end
- --
- -- public methods
- --
- function self.setBars( data )
- -- data is a table containing one or more k/v pairs representing the value for a bar.
- if type( data ) == "table" then
- for k, v in pairs( data )
- do
- bars[k] = v -- stow the bar value
- end
- this:Invalidate( false ) -- force the repainting of the control.
- end
- end
- this:SuspendLayout()
- --
- -- GroupBox
- --
- this.AutoSize = false
- this.BackColor = Color.Black
- this.Dock = DockStyle.None
- this.Font = Font( "Lucinda Console", 8.25 )
- this.FlatStyle = FlatStyle.Standard
- this.ForeColor = Color.White
- this.Location = pos
- this.Name = "gauges"
- this.Size = Size( 160, 83 )
- this.TabIndex = 0
- this.Text = "Vitals"
- this.MouseDown:Add( gauges_MouseDown )
- this.MouseEnter:Add( gauges_MouseEnter )
- this.MouseLeave:Add( gauges_MouseLeave )
- this.MouseMove:Add( gauges_MouseMove )
- this.MouseUp:Add( gauges_MouseUp )
- this.Paint:Add( gauges_Paint )
- this.ParentChanged:Add( gauges_ParentChanged )
- --
- --
- --
- this:ResumeLayout(false)
- self.Control = this -- publish a reference to the control
- self.Movable = true -- allow control relocation as default.
- return self
- end
- --
- setmetatable( gauges, { __call = function( _, ... ) return gauges.ctor( ... ) end } )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement