#include #include #include "abstractWatchGUI.h" #include "analogueWatchGUI.h" analogueWatch::analogueWatch (): watchObject () { penFace = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); penHands[0] = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); //Second hand penHands[1] = CreatePen(PS_SOLID, 1, RGB(0, 255, 0)); //Minute hand penHands[2] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //Hour hand penClear = CreatePen(PS_SOLID, 1, RGB(255, 255, 255)); brushFace = CreateSolidBrush(RGB(8, 86, 45)); brushSpotSmall = CreateSolidBrush(RGB(0, 0, 255)); brushSpotLarge = CreateSolidBrush(RGB(255, 0, 0)); brushDisplay = CreateSolidBrush(RGB(255, 255, 255)); brushActionButton = CreateSolidBrush(RGB(255, 0, 0)); brushResetButton = CreateSolidBrush(RGB(0, 0, 0)); brushOtherButtons = CreateSolidBrush(RGB(8, 86, 45)); } analogueWatch::~analogueWatch () { DeleteObject(penFace); DeleteObject(penHands[0]); DeleteObject(penHands[1]); DeleteObject(penHands[2]); DeleteObject(penClear); DeleteObject(brushFace); DeleteObject(brushSpotSmall); DeleteObject(brushSpotLarge); DeleteObject(brushDisplay); DeleteObject(brushActionButton); DeleteObject(brushResetButton); DeleteObject(brushOtherButtons); } //Calculates the angle and end point for each hand POINT analogueWatch::calculateTip (int value, int length) { double angle = (3.14159265 * (((value * 360) / 60) - 90)) / 180; POINT answer; answer.x = watchCentreX + (int)(length * cos(angle)); answer.y = watchCentreY + (int)(length * sin(angle)); return answer; } int analogueWatch::checkMouseClick (int x, int y) { for (int i = 0; i < 4; i++) { if ((x >= buttonsStartX[i] && x <= buttonsEndX[i]) && (y >= watchStartY && y <= faceStartY)) { return i; } } return -1; } void analogueWatch::repaintGUI (HDC hdc, int size) { ratio = size; di = ratio * 6; buttonSize = (ratio * 2); displayPadding = (ratio / 2); spotLarge = (ratio / 5); spotSmall = (ratio / 7); faceStartX = (watchStartX + buttonSize); faceStartY = (watchStartY + buttonSize); displayStartX = (faceStartX + buttonSize); displayStartY = (faceStartY + buttonSize); displayEndX = (displayStartX + (2 * di) + (2 * displayPadding)); displayEndY = (displayStartY + (2 * di) + (2 * displayPadding)); faceEndX = (displayEndX + buttonSize); faceEndY = (displayEndY + buttonSize); watchCentreX = (displayStartX + displayPadding + di); watchCentreY = (displayStartY + displayPadding + di); handLengths[0] = di - (di / 5); handLengths[1] = di - (di / 3); handLengths[2] = di - (di / 2); int quarter = ((faceEndX - faceStartX) / 4); buttonsStartX[ACTION_BUTTON] = ((faceStartX + (2 * quarter)) - buttonSize); buttonsStartX[MODE_BUTTON] = faceStartX; buttonsStartX[GUI_BUTTON] = (faceEndX - buttonSize); buttonsStartX[RESET_BUTTON] = ((faceEndX - quarter) - (buttonSize / 2)); //Buttons are different lengths, thus the reason to store the endX (all buttons are the same height) buttonsEndX[ACTION_BUTTON] = (buttonsStartX[ACTION_BUTTON] + (2 * buttonSize)); buttonsEndX[MODE_BUTTON] = (buttonsStartX[MODE_BUTTON] + buttonSize); buttonsEndX[GUI_BUTTON] = (buttonsStartX[GUI_BUTTON] + buttonSize); buttonsEndX[RESET_BUTTON] = (buttonsStartX[RESET_BUTTON] + (buttonSize / 2)); HPEN penPrevious = (HPEN)SelectObject(hdc, penFace); HBRUSH brushPrevious = (HBRUSH)SelectObject(hdc, brushFace); Rectangle(hdc, faceStartX, faceStartY, faceEndX, faceEndY); SelectObject(hdc, brushActionButton); Rectangle(hdc, buttonsStartX[ACTION_BUTTON], watchStartY, buttonsEndX[ACTION_BUTTON], faceStartY); SelectObject(hdc, brushResetButton); Rectangle(hdc, buttonsStartX[RESET_BUTTON], watchStartY, buttonsEndX[RESET_BUTTON], faceStartY); SelectObject(hdc, brushOtherButtons); Rectangle(hdc, buttonsStartX[MODE_BUTTON], watchStartY, buttonsEndX[MODE_BUTTON], faceStartY); Rectangle(hdc, buttonsStartX[GUI_BUTTON], watchStartY, buttonsEndX[GUI_BUTTON], faceStartY); SelectObject(hdc, brushDisplay); Ellipse(hdc, displayStartX, displayStartY, displayEndX, displayEndY); //Clock face circle POINT currentPoint; SelectObject(hdc, brushSpotLarge); //draw the 5 second place markers for (int i = 0; i < 12; i++) { currentPoint = calculateTip(i * 5, di); Ellipse(hdc, currentPoint.x - spotLarge, currentPoint.y - spotLarge, currentPoint.x + spotLarge, currentPoint.y + spotLarge); } SelectObject(hdc, brushSpotSmall); //draw the other place markers for (int i = 0; i < 60; i++) { if (i == 0 || i % 5 == 0) { continue; } currentPoint = calculateTip(i, di); Ellipse(hdc, currentPoint.x - spotSmall, currentPoint.y - spotSmall, currentPoint.x + spotSmall, currentPoint.y + spotSmall); } SelectObject(hdc, penPrevious); SelectObject(hdc, brushPrevious); resetTime(); } void analogueWatch::repaintTime (HDC hdc, int hour, int min, int sec) { if (hour >= 12) { hour -= 12; //12 hour instead of 24 } //Makes the hour hand move slightly during the hour, 1 place per 12 mins hour = ((hour * 5) + (min / 12)); unsigned short int timeBuffer[3] = {sec, min, hour}; HPEN penPrevious = (HPEN)SelectObject(hdc, penClear); //Only clear the previous time if the GUI has not been repainted if (pointPrevious[0].x != -1) { for (int i = 0; i < 3; i++) { MoveToEx(hdc, watchCentreX, watchCentreY, NULL); LineTo(hdc, pointPrevious[i].x, pointPrevious[i].y); } } //Paint each hand for (int i = 0; i < 3; i++) { SelectObject(hdc, penHands[i]); pointPrevious[i] = calculateTip(timeBuffer[i], handLengths[i]); MoveToEx(hdc, watchCentreX, watchCentreY, NULL); LineTo(hdc, pointPrevious[i].x, pointPrevious[i].y); } SelectObject(hdc, penPrevious); } void analogueWatch::resetTime () { //If repaintTime() tries to remove the previous hands, and the clock size has been reduced, it will make slashes //in the GUI, thus don't want the previous hands to be removed first time pointPrevious[0].x = -1; }