diff --git a/2048-qt.pro b/2048-qt.pro index a0ba4d6..a0d2068 100644 --- a/2048-qt.pro +++ b/2048-qt.pro @@ -8,10 +8,12 @@ SOURCES += main.cpp \ lupdate_only { SOURCES += qml/main.qml \ + qml/Tile.qml \ qml/2048.js } -RESOURCES += qml.qrc +RESOURCES += \ + resources.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = @@ -20,11 +22,11 @@ QML_IMPORT_PATH = include(deployment.pri) # Setting the application icon -RC_ICONS = 2048.ico # On Windows -ICON = 2048.ico # On Mac OSX +win32: RC_ICONS = 2048.ico # On Windows +macx: ICON = 2048.ico # On Mac OSX HEADERS += \ myclass.h \ settings.h -TRANSLATIONS = 2048-qt_zh_CN.ts +TRANSLATIONS = ts/2048-qt_zh_CN.ts diff --git a/2048-qt_zh_CN.qm b/2048-qt_zh_CN.qm deleted file mode 100644 index 561fa29..0000000 Binary files a/2048-qt_zh_CN.qm and /dev/null differ diff --git a/2048-qt_zh_CN.ts b/2048-qt_zh_CN.ts deleted file mode 100644 index 60221e8..0000000 --- a/2048-qt_zh_CN.ts +++ /dev/null @@ -1,59 +0,0 @@ - - - - - main - - 2048 Game - 2048游戏 - - - File - 文件 - - - New Game - 新游戏 - - - Exit - 退出 - - - Help - 帮助 - - - About Qt - 关于Qt - - - SCORE - 得分 - - - BEST - 最高分 - - - Join the numbers and get to the <b>2048 tile</b>! - 把相同的数字相加,得到<b>2048</b>! - - - Game Over - 游戏结束 - - - Game Over! - 游戏结束! - - - You Win - 你赢了 - - - You win! Continue playing? - 你赢了!要继续玩吗? - - - diff --git a/main.cpp b/main.cpp index dad4fd9..02e100b 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) QString tsFile = "2048-qt_" + locale; QTranslator translator; - if (translator.load(tsFile, ".")) { + if (translator.load(tsFile, ":/ts")) { qDebug() << "Successfully loaded " + tsFile; } else { qDebug() << "Failed to load " + tsFile; diff --git a/qml/2048.js b/qml/2048.js index a8d7988..0b8dc1c 100644 --- a/qml/2048.js +++ b/qml/2048.js @@ -5,25 +5,41 @@ var gridSize = 4; var cellValues; var tileItems = []; var availableCells; -//var labels = "PRC"; -var labels = "2048"; -var labelFunc; var targetLevel = 11; var checkTargetFlag = true; var tileComponent = Qt.createComponent("Tile.qml"); -switch (labels) { -case "2048": - labelFunc = function(n) { +var label = settings.value("label", "2048"); +var labelOptions = ["2048", "Degree", "Military Rank", "PRC"]; +var labelFunc = { + "2048": + function(n) { return Math.pow(2, n).toString(); - }; - break; -case "PRC": - labelFunc = function(n) { - var dynasties = ["商", "周", "秦", "汉", "唐", "宋", "元", "明", "清", "ROC", "PRC"]; - return dynasties[n-1]; - }; - break; + }, + "PRC": + function(n) { + var arr = ["商", "周", "秦", "汉", "唐", "宋", "元", "明", "清", "民国", "天朝", "天庭"]; + if (n > 0 && n < arr.length) + return arr[n-1]; + else + return ""; + }, + "Military Rank": + function(n) { + var arr = ["少尉", "中尉", "上尉", "少校", "中校", "上校", "大校", "少将", "中将", "上将", "元帅", "大元帅"]; + if (n > 0 && n < arr.length) + return arr[n-1]; + else + return ""; + }, + "Degree": + function(n) { + var arr = ["幼儿园", "小学", "初中", "高中", "学士", "硕士", "博士", "博士后", "勇士", "壮士", "烈士", "圣斗士"]; + if (n > 0 && n < arr.length) + return arr[n-1]; + else + return ""; + } } @@ -303,7 +319,7 @@ function isDead() { return dead; } -function computeTileStyle(n) { +function computeTileStyle(n, tileText) { var fgColors = ["#776E62", "#F9F6F2"]; var bgColors = ["#EEE4DA", "#EDE0C8", "#F2B179", "#F59563", "#F67C5F", "#F65E3B", "#EDCF72", "#EDCC61", "#EDC850", "#EDC53F", "#EDC22E", "#3C3A32"]; var sty = {bgColor: helper.myColors.bggray, @@ -318,26 +334,33 @@ function computeTileStyle(n) { sty.bgColor = bgColors[bgColors.length-1]; } - if (labels === "2048") { - /* Adjust font size according to size of the number - [2, 100): 55 - [100, 1000): 45 - [1000, 2048]: 35 - > 2048: 30 - */ - var pv = Math.pow(2, n); - if (pv >= 100 && pv < 1000) - sty.fontSize = 45; - else if (pv >= 1000 && pv <= 2048) - sty.fontSize = 35; - else if (pv > 2048) - sty.fontSize = 30; - - } + /* Adjust font size according to the length of the text + <= 2: 55 + {3, 4}: 45 + {5, 6}: 35 + > 6: 30 + */ + var tlen = getLengthInBytes(tileText); + if (tlen <= 2) + sty.fontSize = 50; + else if (tlen <= 4) + sty.fontSize = 40; + else if (tlen <= 6) + sty.fontSize = 30; + else + sty.fontSize = 20; return sty; } +function getLengthInBytes(str) { + // getLengthInBytes("一二三") = 6 + // getLengthInBytes("123") = 3 + var b = str.match(/[^\x00-\xff]/g); // Multi-byte characters (Chinese) occupy twice more space + return (str.length + (!b ? 0: b.length)); +} + + function maxTileValue() { var mv = 0; for (var i = 0; i < gridSize; i++) { @@ -353,8 +376,8 @@ function maxTileValue() { function createTileObject(ind, n, isStartup) { var tile; - var sty = computeTileStyle(n); - var tileText = labelFunc(n); + var tileText = labelFunc[label](n); + var sty = computeTileStyle(n, tileText); tile = tileComponent.createObject(tileGrid, {"x": cells.itemAt(ind).x, "y": cells.itemAt(ind).y, "color": sty.bgColor, "tileColor": sty.fgColor, "tileFontSize": sty.fontSize, "tileText": tileText}); if (! isStartup) { @@ -387,8 +410,9 @@ function moveMergeTilesLeftRight(i, v, v2, indices, left) { tileItems[gridSize*i+j].x = cells.itemAt(gridSize*i+indices[j]).x; // tileItems[gridSize*i+j].destroy(); - var sty = computeTileStyle(v2[indices[j]]); - tileItems[gridSize*i+indices[j]].tileText = labelFunc(v2[indices[j]]); + var tileText = labelFunc[label](v2[indices[j]]); + var sty = computeTileStyle(v2[indices[j]], tileText); + tileItems[gridSize*i+indices[j]].tileText = tileText; tileItems[gridSize*i+indices[j]].color = sty.bgColor; tileItems[gridSize*i+indices[j]].tileColor = sty.fgColor; tileItems[gridSize*i+indices[j]].tileFontSize = sty.fontSize; @@ -420,8 +444,9 @@ function moveMergeTilesUpDown(i, v, v2, indices, up) { tileItems[gridSize*j+i].y = cells.itemAt(gridSize*indices[j]+i).y; // tileItems[gridSize*j+i].destroy(); - var sty = computeTileStyle(v2[indices[j]]); - tileItems[gridSize*indices[j]+i].tileText = labelFunc(v2[indices[j]]); + var tileText = labelFunc[label](v2[indices[j]]); + var sty = computeTileStyle(v2[indices[j]], tileText); + tileItems[gridSize*indices[j]+i].tileText = tileText; tileItems[gridSize*indices[j]+i].color = sty.bgColor; tileItems[gridSize*indices[j]+i].tileColor = sty.fgColor; tileItems[gridSize*indices[j]+i].tileFontSize = sty.fontSize; diff --git a/qml/Tile.qml b/qml/Tile.qml index 5efad01..87d45e8 100644 --- a/qml/Tile.qml +++ b/qml/Tile.qml @@ -18,6 +18,7 @@ Rectangle { id: tileLabel text: tileText color: tileColor + font.family: "Sans-serif" font.pixelSize: tileFontSize font.bold: true anchors.centerIn: parent diff --git a/qml/main.qml b/qml/main.qml index 8b44b6b..a9741e8 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -16,6 +16,7 @@ ApplicationWindow { x: (Screen.width - width) / 2 y: (Screen.height - height) / 2 + ExclusiveGroup { id: labelSettingsGroup } menuBar: MenuBar { Menu { title: qsTr("File") @@ -30,9 +31,66 @@ ApplicationWindow { onTriggered: MyScript.cleanUpAndQuit(); } } + + Menu { + title: qsTr("Settings") + MenuItem { + text: qsTr("2048") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[0] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[0]) { + MyScript.label = MyScript.labelOptions[0]; + MyScript.startupFunction(); + } + } + } + MenuItem { + text: qsTr("Degree") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[1] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[1]) { + MyScript.label = MyScript.labelOptions[1]; + MyScript.startupFunction(); + } + } + } + MenuItem { + text: qsTr("Military Rank") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[2] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[2]) { + MyScript.label = MyScript.labelOptions[2]; + MyScript.startupFunction(); + } + } + } + MenuItem { + text: qsTr("PRC") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[3] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[3]) { + MyScript.label = MyScript.labelOptions[3]; + MyScript.startupFunction(); + } + } + } + } + Menu { id: helpMenu title: qsTr("Help") + MenuItem { + text: qsTr("About") + onTriggered: aboutDialog.open(); + } MenuItem { text: qsTr("About Qt") onTriggered: myClass.aboutQt(); @@ -205,6 +263,13 @@ ApplicationWindow { } } + MessageDialog { + id: aboutDialog + title: qsTr("About 2048-Qt") + text: qsTr("

2048-Qt

Version 0.1

2014 Qiaoyong Zhong

") + standardButtons: StandardButton.Ok + } + MessageDialog { id: deadMessage title: qsTr("Game Over") diff --git a/qml.qrc b/resources.qrc similarity index 79% rename from qml.qrc rename to resources.qrc index daf965b..1873523 100644 --- a/qml.qrc +++ b/resources.qrc @@ -1,5 +1,6 @@ + ts/2048-qt_zh_CN.qm qml/main.qml qml/2048.js qml/Tile.qml diff --git a/ts/2048-qt_zh_CN.qm b/ts/2048-qt_zh_CN.qm new file mode 100644 index 0000000..13f2374 Binary files /dev/null and b/ts/2048-qt_zh_CN.qm differ diff --git a/ts/2048-qt_zh_CN.ts b/ts/2048-qt_zh_CN.ts new file mode 100644 index 0000000..55eea72 --- /dev/null +++ b/ts/2048-qt_zh_CN.ts @@ -0,0 +1,113 @@ + + + + + main + + + 2048 Game + 2048游戏 + + + + File + 文件 + + + + + New Game + 新游戏 + + + + Exit + 退出 + + + + Settings + 设置 + + + + 2048 + 2048 + + + + Degree + 学位 + + + + Military Rank + 军衔 + + + + PRC + 天朝 + + + + Help + 帮助 + + + + About + 关于 + + + + About Qt + 关于Qt + + + + SCORE + 得分 + + + + BEST + 最高分 + + + + Join the numbers and get to the <b>2048 tile</b>! + 把相同的数字相加,得到<b>2048</b>! + + + + About 2048-Qt + 关于2048-Qt + + + + <p style='font-weight: bold; font-size: 24px'>2048-Qt</p><p>Version 0.1</p><p>2014 Qiaoyong Zhong</p> + + + + + Game Over + 游戏结束 + + + + Game Over! + 游戏结束! + + + + You Win + 你赢了 + + + + You win! Continue playing? + 你赢了!要继续玩吗? + + +