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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "fsCCalendarComponent.h"

//#include <chrono>
//std::chrono::system_clock::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
#include <ctime>

#include <fsCore/fsRect.h>
#include <fsCore/src/fsCVariableTable.h>
#include <fsCore/src/fsCResourceName.h>

#include <fsRenderer/src/fsCFontManager.h>
#include <fsRenderer/src/components/fsITextSpriteComponent.h>

#include "../../src/fsCEntity.h"
#include "../../src/fsCEntityFactory.h"


const fsCUniqueId fsCCalendarComponent::mTypeId("fsCCalendarComponent");

fsBool fsCCalendarComponent::leapYearGet(fsS32 pYear) const<--- The member function 'fsCCalendarComponent::leapYearGet' can be static.
{
	return (pYear % 4 == 0 && pYear % 100 != 0) || (pYear % 400 == 0);
}

fsS32 fsCCalendarComponent::daysInMonthGet(fsS32 pMonth, fsS32 pYear) const
{
	const fsS32 daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (pMonth != 1 || !leapYearGet(pYear))
	{
		return daysInMonth[pMonth];
	}
	return 29;
}

fsS32 fsCCalendarComponent::firstDayOfMonthGet(fsS32 pMonth, fsS32 pYear) const<--- The member function 'fsCCalendarComponent::firstDayOfMonthGet' can be static.
{
	struct tm when;
	when.tm_hour = 0;
	when.tm_min = 0;
	when.tm_sec = 0;
	when.tm_mday = 1;
	when.tm_year = pYear - 1900;
	when.tm_mon = pMonth;

	time_t firstDay = mktime(&when);
	when = *localtime(&firstDay);

	// tm_day days from Su, we use Mo as the start of the week
	if (when.tm_wday == 0)
	{
		return 6;
	}
	return when.tm_wday - 1;
}

fsStr fsCCalendarComponent::currentDateTime()
{
	// https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
	const time_t now = time(nullptr);
	const tm tstruct = *localtime(&now);
	fsChar buf[80];
	// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime for more information about date/time format
	strftime(buf, sizeof(buf), "%Y-%m-%d-%w.%X", &tstruct);
	return buf;
}

void fsCCalendarComponent::textEntryCreate(const fsStr& pStr, const fsPoint& pPos) const
{
	fsCEntity* const txtEnt = parentGet()->entityFactoryGet()->emptyEntityCreate(pStr + "Ent");
	fsITextSpriteComponent* const txtSprComp = txtEnt->textComponentAdd();
	txtEnt->posSet(pPos);
	txtSprComp->initialise(pStr, mFont);
	txtSprComp->formattingRectSizeSet(fsRect(fsPoint(0, 0), txtSprComp->stringWidthInPixelsGet() + 16, 32));
	txtSprComp->alignmentSet(fsITextSpriteComponent::eHALIGN_RIGHT, fsITextSpriteComponent::eVALIGN_MIDDLE);
	txtSprComp->colourSet(mFontColour);
	parentGet()->childAdd(txtEnt);
}

fsPoint fsCCalendarComponent::dateOfMonthPosGet(fsS32 pDay) const
{
	return fsPoint((pDay % (7)) * mStride.x,
	               mStride.y + ((pDay) / 7) * mStride.y);
}

void fsCCalendarComponent::datesOfMonthPrint(const fsPoint& pPos, fsS32 pFirstDayOfMonth, fsS32 pDaysInMonth) const
{
	for (fsS32 day = 0; day < pDaysInMonth; ++day)
	{
		const fsPoint pos = pPos + dateOfMonthPosGet(day + pFirstDayOfMonth);
		textEntryCreate(day + 1, pos);
	}
}

void fsCCalendarComponent::monthHeadingPrint(const fsPoint& pPos, fsS32 pMonth) const
{
	const fsStr montshOfYear[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
	//	textEntryCreate(fsCLocalisedStrings::localisedStringGet(montshOfYear[pMonth]), fsPoint(pPos.x + 3 * mStride.x, pPos.y - mStride.y));
	textEntryCreate(montshOfYear[pMonth], fsPoint(pPos.x + 3 * mStride.x, pPos.y - mStride.y));
}

void fsCCalendarComponent::daysOfWeekHeadingsPrint(const fsPoint& pPos) const
{
	const fsStr dayOfWeekInit[7] = {"mo", "tu", "we", "th", "fr", "sa", "su"};
	std::vector<fsStr> dayOfWeek;
	dayOfWeek.assign(&dayOfWeekInit[0], &dayOfWeekInit[0] + 7);
	fsS32 i = 0;
	for (const fsStr day : dayOfWeek)
	{
		//		const fsStr localisedDay = fsCLocalisedStrings::localisedStringGet(day);
		textEntryCreate(day, fsPoint(i++ * mStride.x, 0));
	}
}

void fsCCalendarComponent::initialiseFromVariableTableDo(const fsCVariableTable& pVars)
{
	fsIComponent::initialiseFromVariableTableDo(pVars);

	const fsStr font = pVars.strGet("font");
	const auto fontSize = fsITextSpriteComponent::fontSizeGet(pVars);
	mStride.x = pVars.s32Get("strideX");
	mStride.y = pVars.s32Get("strideY");

	mFont = fsCFontManager::instanceGet()->fontGet(
		fsSFontParams(
			fsCResourceName(font, fsCResourceName::eRES_LOCATION_ASSETS),
			fontSize.x,
			fontSize.y
			));
	mFontColour = fsSColour4B(pVars.strGetWithDefault("fontColourRGBA", "000000ff"));

	thisMonthPrint();
}

void fsCCalendarComponent::thisMonthPrint() const
{
	fsStr todayStr = currentDateTime();
	fsStr yearStr = todayStr;
	fsStr monthStr = todayStr;
	monthStr.erase(0, monthStr.find('-') + 1);
	fsStr dayStr = monthStr;
	dayStr.erase(0, dayStr.find('-') + 1);

	yearStr.erase(yearStr.find('-'));
	monthStr.erase(monthStr.find('-'));
	dayStr.erase(dayStr.find('-'));

	if (monthStr[0] == '0')
	{
		monthStr.erase(0, 1);
	}

	if (dayStr[0] == '0')
	{
		dayStr.erase(0, 1);
	}

	fsS32 year = yearStr.toS32();
	fsS32 month = monthStr.toS32() - 1;
	// fsS32 day = dayStr.toS32();

	monthHeadingPrint(mPos, month);
	daysOfWeekHeadingsPrint(mPos);

	datesOfMonthPrint(mPos, firstDayOfMonthGet(month, year), daysInMonthGet(month, year));
}

fsIComponent* fsCCalendarComponent::create(fsCEntity* pParent)
{
	return new fsCCalendarComponent(pParent);
}

fsCCalendarComponent::fsCCalendarComponent(fsCEntity* const pParent) :
	fsIComponent(pParent),
	mFont(nullptr)
{
}

fsCCalendarComponent::~fsCCalendarComponent()
{
}