﻿/*
    1.0     Release
    1.01    Changed the output coordinates to the center of the active artboard.
    2.0     Rewrote the script in jsx for the newer versions of Illustrator.
*/

var sversion = '2.0',
    scriptName = 'dViz: Analog Clock' + ' ' + sversion

var doc;

var h = new Date().getHours(),
    m = new Date().getMinutes(),
    s = new Date().getSeconds(),
    r = 20,
    useSec = false,
    drwHrs = true;
    
function drawClock() {
    var centerPoint = doc.activeView.centerPoint;
    
    clockFace = doc.activeLayer.pathItems.ellipse(centerPoint[1] + r, centerPoint[0] - r, r * 2, r * 2);
    clockFace.strokeWidth = r / 20;

    // Draw the hours arm
    hArmPath = doc.activeLayer.pathItems.add();
    hArmPath.setEntirePath([ 
        centerPoint,
        [centerPoint[0], centerPoint[1]+r * 0.5]
    ]);
    hArmPath.strokeCap = StrokeCap.ROUNDENDCAP;
    hArmPath.strokeWidth = r / 8;
    hArmPath.rotate(-(30 * (h % 12 + m / 60)), true, false, false, false, Transformation.BOTTOMLEFT);

    // Draw minutes arm
    mArmPath = doc.activeLayer.pathItems.add();
    mArmPath.setEntirePath([ 
        centerPoint,
        [centerPoint[0], centerPoint[1]+r * 0.8]
    ]);
    mArmPath.strokeCap = StrokeCap.ROUNDENDCAP;
    mArmPath.strokeWidth = r / 8;
    mArmPath.rotate(-(6 * m), true, false, false, false, Transformation.BOTTOMLEFT);

    if (useSec) {
        // Draw the seconds arm
        sArmPath = doc.activeLayer.pathItems.add();
        sArmPath.setEntirePath([ 
            centerPoint,
            [centerPoint[0], centerPoint[1]+r * 0.8]
        ]);
        sArmPath.strokeCap = StrokeCap.BUTTENDCAP;
        sArmPath.strokeWidth = r / 18;
        sArmPath.rotate(-(6 * s), true, false, false, false, Transformation.BOTTOMLEFT);
    }

    if (drwHrs) {
        // Draw the hour marks
        for(var counter = 0; counter < 12; counter++) {
            
            //var rimPoint = centerPoint + new Point({ length: r, angle: 90 + counter * 30}),
                //hubPoint = centerPoint + new Point({ length: (counter % 3 == 0 ? 0.87 : 0.92) * r, angle: 90 + counter * 30}),
                //hourMark = new Path.Line(rimPoint, hubPoint)
            //var mark = doc.activeLayer.pathItems.add();
            //mark.setEntirePath([ 
                //centerPoint,
                //[centerPoint[0], centerPoint[1]+counter % 3 == 0 ? 0.87 : 0.92]
                [centerPoint[0], centerPoint[1]],
                [centerPoint[0], centerPoint[1]+ 10]
            //]);
            //mark.strokeWidth = counter % 3 == 0 ? r / 18 : r / 25
            //mark.strokeCap = StrokeCap.BUTTENDCAP
            //mark.rotate(-(counter * 30), true, false, false, false, Transformation.CENTER);

        }
    }
}

if (version.split(".")[0] >= 15) {
    if (app.documents.length > 0) {
        if (app.activeDocument != null) {
            doc = app.activeDocument;
                    
            var dlg = new Window('dialog', scriptName); 
            
            dlg.inputGroup = dlg.add('group', undefined, 'Input Fields:');
            (dlg.inputGroup.hInput = dlg.inputGroup.add('edittext', [100,15,160,35], h)).helpTip = "Hours";
            (dlg.inputGroup.hInputTxt = dlg.inputGroup.add('statictext', undefined, 'Hours'));
            (dlg.inputGroup.mInput = dlg.inputGroup.add('edittext', [100,15,160,35], m)).helpTip = "Minutes";
            (dlg.inputGroup.mInputTxt = dlg.inputGroup.add('statictext', undefined, 'Minutes'));
            (dlg.inputGroup.sInput = dlg.inputGroup.add('edittext', [100,15,160,35], s)).helpTip = "Seconds";
            (dlg.inputGroup.sInputTxt = dlg.inputGroup.add('statictext', undefined, 'Seconds'));
            (dlg.inputGroup.buttonGetTime = dlg.inputGroup.add('button', undefined, 'Get Current Time'));
            
            dlg.inputGroup.buttonGetTime.onClick - function() {
                    h = new Date().getHours();
                    m = new Date().getMinutes();
                    s = new Date().getSeconds();
                    
                    dlg.inputGroup.hInput.text = h;
                    dlg.inputGroup.mInput.text = m;
                    dlg.inputGroup.sInput.text = s;
                }
            
            dlg.inputGroup.hInput.onChange = function() {
                    h = parseInt(dlg.inputGroup.hInput.text.replace(/[^\d.-]/g, ''));
                    dlg.inputGroup.hInput.text = h;
                }
            dlg.inputGroup.mInput.onChange = function() {
                    m = parseInt(dlg.inputGroup.mInput.text.replace(/[^\d.-]/g, ''));
                    dlg.inputGroup.mInput.text = m;
                }
            dlg.inputGroup.sInput.onChange = function() {
                    s = parseInt(dlg.inputGroup.sInput.text.replace(/[^\d.-]/g, ''));
                    dlg.inputGroup.sInput.text = s;
                }
            
            dlg.inputGroup.orientation = 'row';
            
            (dlg.stylePanel = dlg.add('group', undefined, 'Style:')).helpTip = "Style";             
            (dlg.stylePanel.rInput = dlg.stylePanel.add('edittext', [100,15,160,35], r)).helpTip = "Radius";
            (dlg.stylePanel.rInputTxt = dlg.stylePanel.add('statictext', undefined, 'Radius'));
            (dlg.stylePanel.useSec = dlg.stylePanel.add('checkbox', undefined, 'Use Seconds')).helpTip = "Use Seconds";
            dlg.stylePanel.useSec.value = useSec;
            (dlg.stylePanel.drwHrs = dlg.stylePanel.add('checkbox', undefined, 'Draw Hour Marks')).helpTip = "Draw Hour Marks";
            dlg.stylePanel.drwHrs.value = drwHrs;
            dlg.stylePanel.orientation = 'row';
            
            dlg.stylePanel.rInput.onChange = function() {
                    r = dlg.stylePanel.rInput.text.replace(/[^\d.-]/g, '');
                    dlg.stylePanel.rInput.text = r;
                }
            
            dlg.buttonGroup = dlg.add('group', undefined, 'Button Group');
            dlg.buttonGroup.cancelBtn = dlg.buttonGroup.add('button', undefined, 'Cancel', {name:'cancel'});
            dlg.buttonGroup.cancelBtn.onClick = function() {dlg.close();};
            dlg.buttonGroup.okBtn = dlg.buttonGroup.add('button', undefined, 'OK', {name:'ok'});
            dlg.buttonGroup.okBtn.onClick = function() {

                h = parseInt(dlg.inputGroup.hInput.text.replace(/[^\d.-]/g, ''));
                m = parseInt(dlg.inputGroup.mInput.text.replace(/[^\d.-]/g, ''));
                s = parseInt(dlg.inputGroup.sInput.text.replace(/[^\d.-]/g, ''));
                
                r = parseInt(dlg.stylePanel.rInput.text.replace(/[^\d.-]/g, ''));
                
                useSec = dlg.stylePanel.useSec.value;
                drwHrs = dlg.stylePanel.drwHrs.value;
                
                drawClock();
                dlg.close();
            }
            dlg.buttonGroup.orientation = 'row';
            
            dlg.show();
        } else { Window.alert("You must have an active document to run this script."); }
    } else { Window.alert("You must open at least one document to run this script."); }
} else { Window.alert("Your version of Adobe illustrator is too old to run this script."); }
