#include #include #include "abstractWatchGUI.h" #include "digitalWatchGUI.h" digitalWatch::digitalWatch (): watchObject () { } digitalWatch::~digitalWatch () { } int digitalWatch::checkMouseClick (int x, int y) { for (int i = 0; i < 4; i++) { if ((x >= buttonsStartX[i] && x <= (buttonsStartX[i] + buttonSize)) && (y >= buttonsStartY[i] && y <= (buttonsStartY[i] + buttonSize))) { return i; } } return -1; } void digitalWatch::drawNum (HDC hdc, int fromRight, int fromTop, int num) { if (num == 1) //All the numbers start from the NW apart from "1" { MoveToEx(hdc, fromRight, fromTop, NULL); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); return; } MoveToEx(hdc, (fromRight - ratio), fromTop, NULL); //NW if (num == 0) { LineTo(hdc, fromRight, fromTop); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); LineTo(hdc, (fromRight - ratio), (fromTop + (2 * ratio))); LineTo(hdc, (fromRight - ratio), fromTop); } else if (num == 2) { LineTo(hdc, fromRight, fromTop); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); LineTo(hdc, (fromRight - ratio), (fromTop + (2 * ratio))); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); } else if (num == 3) { LineTo(hdc, fromRight, fromTop); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); LineTo(hdc, (fromRight - ratio), (fromTop + (2 * ratio))); } else if (num == 4) { LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, fromRight, fromTop); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); } else if (num == 5) { LineTo(hdc, fromRight, fromTop); LineTo(hdc, (fromRight - ratio), fromTop); LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); LineTo(hdc, (fromRight - ratio), (fromTop + (2 * ratio))); } else if (num == 6) { LineTo(hdc, (fromRight - ratio), (fromTop + (2 * ratio))); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); } else if (num == 7) { LineTo(hdc, fromRight, fromTop); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); } else if (num == 8) { LineTo(hdc, (fromRight - ratio), (fromTop + (2 * ratio))); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, fromRight, fromTop); LineTo(hdc, (fromRight - ratio), fromTop); } else { LineTo(hdc, (fromRight - ratio), (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + ratio)); LineTo(hdc, fromRight, (fromTop + (2 * ratio))); LineTo(hdc, fromRight, fromTop); LineTo(hdc, (fromRight - ratio), fromTop); } } //Double colon void digitalWatch::drawSep (HDC hdc, int fromRight, int fromTop) { int spotPosition = fromTop + ratio + digitThickness; Ellipse(hdc, (fromRight - digitThickness), (spotPosition - (2 * digitThickness)), fromRight, (spotPosition + digitThickness - (2 * digitThickness))); Ellipse(hdc, (fromRight - digitThickness), (spotPosition + (2 * digitThickness)), fromRight, (spotPosition + digitThickness + (2 * digitThickness))); } void digitalWatch::repaintGUI (HDC hdc, int size) { ratio = size; buttonSize = (ratio * 2); displayPadding = (ratio / 2); digitThickness = (ratio / 5); faceStartX = (watchStartX + buttonSize); faceStartY = (watchStartY + buttonSize); displayStartX = (faceStartX + buttonSize); displayStartY = (faceStartY + (2 * buttonSize)); displayEndX = displayStartX + (2 * displayPadding) + (13 * ratio); displayEndY = displayStartY + (2 * displayPadding) + (2 * ratio); faceEndX = displayEndX + buttonSize; faceEndY = displayEndY + (2 * buttonSize); buttonsStartX[ACTION_BUTTON] = watchStartX; buttonsStartX[MODE_BUTTON] = watchStartX; buttonsStartX[GUI_BUTTON] = faceEndX; buttonsStartX[RESET_BUTTON] = faceEndX; buttonsStartY[ACTION_BUTTON] = (faceStartY + buttonSize); buttonsStartY[MODE_BUTTON] = (faceEndY - (2 * buttonSize)); buttonsStartY[GUI_BUTTON] = (faceStartY + buttonSize); buttonsStartY[RESET_BUTTON] = (faceEndY - (2 * buttonSize)); //An array of points for the polygon to use POINT pointsFace[9] = { (faceStartX + buttonSize), faceStartY, //top, indented from left (faceEndX - buttonSize), faceStartY, //top, indented from right faceEndX, (faceStartY + buttonSize), //indented from top, right faceEndX, (faceEndY - buttonSize), //indented from bottom, right (faceEndX - buttonSize), faceEndY, //bottom, indented from right (faceStartX + buttonSize), faceEndY, //bottom, indented from left faceStartX, (faceEndY - buttonSize), //indented from bottom, left faceStartX, (faceStartX + buttonSize), //indented from top, left (faceStartX + buttonSize), faceStartY}; //top, indented from left HPEN penFace = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); HBRUSH brushFace = CreateSolidBrush(RGB(0, 0, 0)); HPEN penDisplay = CreatePen(PS_SOLID, digitThickness, RGB(8, 30, 85)); HBRUSH brushButtons = CreateSolidBrush(RGB(208, 167, 24)); HPEN penPrevious = (HPEN)SelectObject(hdc, penFace); HBRUSH brushPrevious = (HBRUSH)SelectObject(hdc, brushFace); Polygon(hdc, pointsFace, 9); //Draws the watch outline SelectObject(hdc, brushButtons); for (int i = 0; i < 4; i++) { Rectangle(hdc, buttonsStartX[i], buttonsStartY[i], (buttonsStartX[i] + buttonSize), (buttonsStartY[i] + buttonSize)); } SelectObject(hdc, penDisplay); SelectObject(hdc, brushFace); Rectangle(hdc, displayStartX, displayStartY, displayEndX, displayEndY); SelectObject(hdc, penPrevious); SelectObject(hdc, brushPrevious); DeleteObject(penFace); DeleteObject(penDisplay); DeleteObject(brushFace); DeleteObject(brushButtons); resetTime(); } void digitalWatch::repaintTime (HDC hdc, int hour, int min, int sec) { int fromRight = displayEndX - displayPadding; int fromTop = displayStartY + displayPadding; char thisTime[7]; //Combines hour, min, sec into a string, each item is padded with a 0 if required sprintf(thisTime, "%02u%02u%02u", hour, min, sec); HPEN penDisplay = CreatePen(PS_SOLID, digitThickness, RGB(0, 0, 0)); HBRUSH brushDisplay = CreateSolidBrush(RGB(0, 0, 0)); HPEN penDigits = CreatePen(PS_SOLID, digitThickness, RGB(240, 8, 30)); HBRUSH brushDigits = CreateSolidBrush(RGB(240, 8, 30)); HPEN penPrevious = (HPEN)SelectObject(hdc, penDisplay); HBRUSH brushPrevious = (HBRUSH)SelectObject(hdc, brushDisplay); //Go right to left blanking out digits that have changed for (int i = 5; i >= 0; i--) { //If this digit hasn't changed, then further ones will not have either if (thisTime[i] == lastTime[i]) { break; } Rectangle(hdc, (fromRight - ratio), fromTop, (fromRight + digitThickness), (fromTop + (2 * ratio) + digitThickness)); fromRight -= (2 * ratio); if (i == 2 || i == 4) { fromRight -= ratio; } } fromRight = displayEndX - displayPadding; SelectObject(hdc, penDigits); SelectObject(hdc, brushDigits); //Only paint parts of the time that have changed for (int i = 5; i >= 0; i--) { if (thisTime[i] == lastTime[i]) { break; } lastTime[i] = thisTime[i]; //'0' - '9' in ASCII are 30 - 39, so minus '0' (30) gives you the numeric value //E.g. '7' in ASCII is 37, minus '0' (30) = 7 drawNum(hdc, fromRight, fromTop, (thisTime[i] - '0')); fromRight -= (2 * ratio); if (i == 2 || i == 4) { drawSep(hdc, fromRight, fromTop); fromRight -= ratio; } } SelectObject(hdc, penPrevious); SelectObject(hdc, brushPrevious); DeleteObject(penDisplay); DeleteObject(brushDisplay); DeleteObject(penDigits); DeleteObject(brushDigits); } void digitalWatch::resetTime () { //Reset all the digits to "A", which means they will all be invalid for (int i = 0; i < 6; i++) { lastTime[i] = 'A'; } }