Mouse clock cursor made with drawLine [code snippet]

  • #1, by stroncisFriday, 05. May 2017, 22:51 3 years ago
    Generally it's useless, but maybe will help someone learning Lua in Visionaire or spark some creative ideas. Without color code it's a bit hard to read, so copy paste to Visionaire for better visual acuity.

    I'm total beginner myself, so any advice and comments are welcome!

    Sample animation: https://media.giphy.com/media/3o7bu0wrgycxjtVAas/giphy.gif

    -- line lengths
    local rClock = 30 -- clock disc radius
    local rHour = 16
    local rMinute = 20
    local rSecond = 25

    graphics.addDrawFunc("drawClock()", 0)

    function drawClock()

    -- setting user friendly variables
      local mouseX = getCursorPos().x
      local mouseY = getCursorPos().y

    -- Clock circle
    -- !!! this is high CPU load solution! Just think about global warming. Or not.
    -- But it's anyways ugly looking and just for demonstration purposes. Use Sprites!
      for i = 1, 360 do
        local angle = i * math.pi / 180
        graphics.drawLine(mouseX, mouseY, mouseX+rClock*math.cos(angle), mouseY+rClock*math.sin(angle), colorRGB(40,80,170), 0.85)
      end

    -- getting computer time
      local timePC = os.date("*t")

    -- converting to 12hour format
      local hourNow = timePC.hour
      if hourNow >; 12 then hourNow = hourNow - 12 end

    -- drawing clock hands
      local angleHour = (hourNow * 30 - 90) * math.pi / 180
      graphics.drawLine(mouseX, mouseY, mouseX+rHour*math.cos(angleHour), mouseY+rHour*math.sin(angleHour), 000, 1.0)

      local angleMinute = (timePC.min*3 -45) * math.pi / 90
      graphics.drawLine(mouseX, mouseY, mouseX+rMinute*math.cos(angleMinute), mouseY+rMinute*math.sin(angleMinute), colorRGB(160,160,160), 1.0)

      local angleSecond = (timePC.sec*3 -45) * math.pi / 90
      graphics.drawLine(mouseX, mouseY, mouseX+rSecond*math.cos(angleSecond), mouseY+rSecond*math.sin(angleSecond), colorRGB(255,255,255), 1.0)

    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40


    Newbie

    42 Posts


  • #2, by sebastianFriday, 05. May 2017, 23:17 3 years ago
    Thanks for sharing this script. It gives an idea on what to do now for drawing a line in a whole concept and not just a piece of line where its not clear where to put it =)

    For the circle it may would be easeier to use a drawSprite function and using a circle image to display wink (or even use an animation which gets moved to the cursor position)

    For me nothing happens here when i use it. If a call the drawClock() function it gives me an error for the colorRGB part... what could be wrong?

    Thread Captain

    2337 Posts

  • #3, by stroncisSaturday, 06. May 2017, 00:25 3 years ago
    For the circle it may would be easeier to use a drawSprite function and using a circle image to display wink (or even use an animation which gets moved to the cursor position)

    For me nothing happens here when i use it. If a call the drawClock() function it gives me an error for the colorRGB part... what could be wrong?
    I know, it was just proof of concept, as i stated in comment - "just for demonstration purposes". Ok adding to comment "Use sprites!", because some may think it's a good idea to make a circle this way (:

    Ah, colorRGB is just a helper function i made, because there you need to pass integer for color, and it's a bit complicated to use directly. You can mash number keys randomly and it will work, or use my helper function:
    function colorRGB(r,g,b)
      return=r+g*256+b*65536
    end
    1
    2
    3

    Looking further, you can easily replace everything with sprites and make a nice, actual clock in your game. Also, with small changes in stepping, you can make hours and minutes slide, not tick, which is more intuitive.

    Helper function colorRGB will work with any VS t_int used to set color.

    Attached image with sprite and drawLine arrows. Now this is usable and no perceivable impact on performance.

    Newbie

    42 Posts

Write post