Advertisement
Anaristos

Vital gauges control

Mar 3rd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.92 KB | None | 0 0
  1. require("CLRPackage")
  2. --
  3. import("System.Drawing")
  4. import("System.Drawing.Drawing2D")
  5. import("System.Drawing.Text")
  6. import("System.Windows.Forms")
  7. --
  8. gauges = {}
  9. --
  10. function gauges.ctor( loc ) -- application object creator
  11.     --
  12.     -- private variables
  13.     --
  14.     local self = {} -- method and property container.
  15.    
  16.     local pos = loc or Point( 10, 10 ) -- control starting location in parent container.
  17.    
  18.     local this = GroupBox() -- display surface.
  19.  
  20.     local bars = {} -- bar values savearea.
  21.    
  22.     local pointerLocation  = Point( 0, 0 ) -- current location of the mouse pointer.
  23.     local clientPosition   = Point( 0, 0 ) -- current location of control relative to the parent.
  24.     local adjustedLocation = Point( 0, 0 ) -- resultant control location after move.
  25.  
  26.     local current, cIndex = {}, 0 -- this stack is used to maintain the cursor being used.
  27.  
  28.     local moving = false -- if set, the control is being dragged.
  29.  
  30.     local sf = StringFormat() -- text formatter for our percentage values.
  31.  
  32.     sf.Alignment     = StringAlignment.Far  -- set horizontal alignment.
  33.     sf.LineAlignment = StringAlignment.Near -- set vertical alignment.
  34.  
  35.     local textFont = Font( "Miriam Fixed", 12, GraphicsUnit.Pixel ) -- percentage font.
  36.  
  37.     local hpRect = Rectangle( Point( 10, 20 ), Size( 100, 10 ) ) -- hp    bar
  38.     local mnRect = Rectangle( Point( 10, 40 ), Size( 100, 10 ) ) -- mana  bar
  39.     local mvRect = Rectangle( Point( 10, 60 ), Size( 100, 10 ) ) -- moves bar
  40.  
  41.     local barRect = { hp = hpRect, mn = mnRect, mv = mvRect } -- reference table for bar rectangles.
  42.  
  43.     local hptRect = RectangleF( PointF( 115, 20 ), SizeF( 30, 10 ) ) -- hp    pct
  44.     local mntRect = RectangleF( PointF( 115, 40 ), SizeF( 30, 10 ) ) -- mana  pct
  45.     local mvtRect = RectangleF( PointF( 115, 60 ), SizeF( 30, 10 ) ) -- moves pct
  46.  
  47.     local textRect = { hp = hptRect, mn = mntRect, mv = mvtRect } -- reference table for text rectangles.
  48.  
  49.     local colors = {
  50.                         Color.DodgerBlue   -- value is over  100%
  51.                        ,Color.SpringGreen  -- value is over   75%
  52.                        ,Color.Yellow       -- value is over   50%
  53.                        ,Color.Orchid       -- value is over   25%
  54.                        ,Color.Red          -- value is under  26%
  55.                    }
  56.     --
  57.     -- private methods
  58.     --
  59.     local function getIndex( value ) -- determine which color to use to paint the bar.
  60.  
  61.                if type( value ) == "number" then
  62.                    local i = 5
  63.                    if value > 100 then i = 1
  64.                       elseif value > 75 then i = 2
  65.                           elseif value > 50 then i = 3
  66.                               elseif value > 25 then i = 4
  67.                    end
  68.                    return i
  69.                else
  70.                     return nil
  71.                end
  72.  
  73.           end
  74.  
  75.     local function move( p ) -- This function moves the control while it is being dragged.
  76.  
  77.                if self.Movable then
  78.                    local r = this.Parent.ClientRectangle               
  79.                    local x, y = p.X, p.Y               
  80.                    if x > r.Right  - this.Width  then x = r.Right  - this.Width  end               
  81.                    if y > r.Bottom - this.Height then y = r.Bottom - this.Height end               
  82.                    if x < r.Left then x = r.Left end               
  83.                    if y < r.Top  then y = r.Top  end               
  84.                    this.Location = Point ( x, y ) -- relocate control to new position.
  85.                end
  86.  
  87.           end
  88.  
  89.     local function parent_Resize( sender, e )
  90.  
  91.                move( this.Location ) -- make sure the control stays within the parent visible area.
  92.  
  93.           end
  94.  
  95.     local function gauges_MouseDown( sender, e )
  96.  
  97.                if self.Movable then
  98.                    if e.Button == MouseButtons.Left then
  99.                        cIndex = cIndex + 1 -- update stack pointer.
  100.                        current[cIndex] = this.Cursor -- push current cursor into stack.
  101.                        this.Cursor = Cursors.SizeAll -- show move cursor.                  
  102.                        pointerLocation = e.Location -- remember where the mouse pointer is.
  103.                        this.ForeColor = Color.FromArgb( 255, 224, 224, 224 )
  104.                        this.BackColor = Color.FromArgb( 255, 105, 105, 105 )
  105.                        moving = true
  106.                    end
  107.                end
  108.                
  109.           end
  110.  
  111.     local function gauges_MouseEnter( sender, e )
  112.  
  113.                if self.Movable then
  114.                    cIndex = cIndex + 1 -- update stack pointer.            
  115.                    current[cIndex] = this.Cursor -- push current cursor into stack.            
  116.                    this.Cursor = Cursors.Hand -- display hand cursor as draggable hint.
  117.                end
  118.  
  119.           end
  120.          
  121.           local function gauges_MouseLeave( sender, e )
  122.  
  123.                if self.Movable then
  124.                    if cIndex > 0 then
  125.                        this.Cursor = current[cIndex] -- pop stack and restore cursor.
  126.                        cIndex = cIndex - 1 -- update stack pointer.
  127.                    end
  128.                end
  129.  
  130.           end
  131.  
  132.           local function gauges_MouseMove( sender, e )
  133.  
  134.                if self.Movable then
  135.                    if moving then
  136.                        clientPosition   = this.Parent:PointToClient( Cursor.Position )
  137.                        adjustedLocation = Point( clientPosition.X - pointerLocation.X, clientPosition.Y - pointerLocation.Y )
  138.                        move( adjustedLocation ) -- move the control while the mouse pointer is in motion.
  139.                    end
  140.                end
  141.  
  142.           end  
  143.  
  144.     local function gauges_MouseUp( sender, e )
  145.  
  146.                if self.Movable then
  147.                    if cIndex > 0 then
  148.                        this.Cursor = current[cIndex] -- pop stack and restore cursor.
  149.                        cIndex = cIndex - 1 -- update stack pointer.
  150.                    end             
  151.                    if moving then
  152.                        moving = false -- indicate dragging has stopped.
  153.                        this.ForeColor = Color.White
  154.                        this.BackColor = Color.Black
  155.                        this:Invalidate( false ) -- force the repainting of the control.
  156.                    end         
  157.                end
  158.  
  159.           end
  160.  
  161.     local function gauges_Paint( sender, e )
  162.  
  163.               local g = e.Graphics -- fetch the graphics object for this control.
  164.  
  165.               for k, w in pairs( bars )
  166.                     do
  167.                         local i = getIndex( w ) -- obtain color index.
  168.                             if i then                          
  169.                                 if w > 100 then w = 100 end -- take care of possible overflow.
  170.                                 local r1 = Rectangle.FromLTRB( barRect[k].Left,     barRect[k].Top, barRect[k].Left + w, barRect[k].Bottom )
  171.                                 local r2 = Rectangle.FromLTRB( barRect[k].Left + w, barRect[k].Top, barRect[k].Right,    barRect[k].Bottom )
  172.                                 g:FillRectangle( SolidBrush( colors[i] ),  r1 ) -- paint the active bar segment.
  173.                                 g:FillRectangle( SolidBrush( Color.Gray ), r2 ) -- paint the unused bar segement.
  174.                                 g:DrawString( tostring( bars[k] ), textFont, SolidBrush( colors[i] ), textRect[k], sf )                    
  175.                             end
  176.                     end
  177.                    
  178.               g:Dispose() -- destroy graphics object.
  179.              
  180.           end
  181.  
  182.     local function gauges_ParentChanged( sender, e )
  183.  
  184.                pcall( function() this.Parent.Resize:Add( parent_Resize ) end )
  185.                -- piggyback on the parent's resize event if it has one.
  186.           end
  187.     --
  188.     -- public methods
  189.     --
  190.     function self.setBars( data )
  191.         -- data is a table containing one or more k/v pairs representing the value for a bar.
  192.         if type( data ) == "table" then
  193.             for k, v in pairs( data )
  194.                 do
  195.                     bars[k] = v -- stow the bar value
  196.                 end
  197.             this:Invalidate( false ) -- force the repainting of the control.
  198.         end
  199.  
  200.     end
  201.    
  202.     this:SuspendLayout()
  203.     --
  204.     -- GroupBox
  205.     --
  206.     this.AutoSize  = false
  207.     this.BackColor = Color.Black
  208.     this.Dock      = DockStyle.None
  209.     this.Font      = Font( "Lucinda Console", 8.25 )
  210.     this.FlatStyle = FlatStyle.Standard
  211.     this.ForeColor = Color.White
  212.     this.Location  = pos
  213.     this.Name      = "gauges"
  214.     this.Size      = Size( 160, 83 )
  215.     this.TabIndex  = 0
  216.     this.Text      = "Vitals"
  217.  
  218.     this.MouseDown:Add( gauges_MouseDown )
  219.  
  220.     this.MouseEnter:Add( gauges_MouseEnter )
  221.  
  222.     this.MouseLeave:Add( gauges_MouseLeave )
  223.  
  224.     this.MouseMove:Add( gauges_MouseMove )
  225.  
  226.     this.MouseUp:Add( gauges_MouseUp )
  227.  
  228.     this.Paint:Add( gauges_Paint )
  229.  
  230.     this.ParentChanged:Add( gauges_ParentChanged )
  231.     --
  232.     --
  233.     --
  234.     this:ResumeLayout(false)
  235.  
  236.     self.Control = this -- publish a reference to the control
  237.  
  238.     self.Movable = true -- allow control relocation as default.
  239.  
  240.     return self
  241.    
  242. end
  243. --
  244. setmetatable( gauges, { __call = function( _, ... ) return gauges.ctor( ... ) end } )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement