AnalogClock in c# with godot custom drawing

systemerrorsystemerror Posts: 9Member
edited July 24 in Tutorials

Hi community,
on base of an android-sourcecode (URL below) i made a godot-c#-clock for learning c#:

heres the code:

using Godot;
using System;

public class Clock : Node2D
{
    private int height, width = 0;
    private float radius = 0.0f;
    private int offset=10;
    private int handTruncation, hourHandTruncation = 0;
    private int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12};


    public override void _Ready()
    {
        height=200;
        width=200;
        int min = Math.Min(height, width);
        radius=min/2;
        handTruncation = min / 20;
        hourHandTruncation = min / 7;
    }


    public override void _Process(float delta)
    {
        Update();
    }


    public override void _Draw()
    {
         DrawClockCircles();
         DrawHands();
         DrawNumerals();
    }


    public void DrawClockCircles()
    {
        var center = new Vector2(width / 2 + offset, height / 2 + offset);
        var color = new Color(0, 0, 1, 0.5f);
        DrawCircle(center, radius, color);
        DrawCircle(center, 12, color);
    }


    public void DrawHand(double loc, bool isHour, Color color, float w) {
        double angle = Math.PI * loc / 30 - Math.PI / 2;
        int handRadius = isHour ? (int)(radius - handTruncation - hourHandTruncation) : (int)(radius - handTruncation);
        var from = new Vector2(width / 2 + offset, height / 2 + offset);
        var to = new Vector2( (float) (width / 2 + Math.Cos(angle) * handRadius + offset), (float) (height / 2 + Math.Sin(angle) * handRadius + offset));
        DrawLine(from, to, color, w, true);
    }


    public void DrawHands()
    {
        DateTime now = DateTime.Now;
        float hour = now.Hour;
        hour = hour > 12 ? hour - 12 : hour;
        var color = new Color(1, 1, 1);
        DrawHand((hour + now.Minute / 60) * 5f, true, color, 3.0f);
        DrawHand(now.Minute, false, color, 3.0f);
        color = new Color(1, 1, 0);
        DrawHand(now.Second, false, color, 1.0f);
    }


    private void DrawNumerals() {
        for (int number = 1; number < numbers.Length+1; number++) {
            String tmp = number.ToString();
            double angle = Math.PI / 6 * (number - 3);
            int x = (int) (width / 2 + Math.Cos(angle) * radius - tmp.Length / 2);
            int y = (int) (height / 2 + Math.Sin(angle) * radius + tmp.Length / 2);
            var center = new Vector2(x + offset-5, y + offset+5);
            var color = new Color(1, 1, 1);
            var label = new Label();
            var font = label.GetFont("");
            DrawString(font, center, tmp, color);
        }
    }

}

how to start: start a new project 2d, add a Node2D, to the node add a c#-script with the code above, change screensize in preferences to 220x220, backgroundcolor to black and transparency to 0.5

Original code:
https://ssaurel.com/blog/learn-to-draw-an-analog-clock-on-android-with-the-canvas-2d-api/


Tags :

Comments

Sign In or Register to comment.