GameView.qml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. Item {
  4. id: gameView
  5. objectName: "GameView"
  6. property bool isPaused: false
  7. property real gameSpeed: 1.0
  8. signal mapClicked(real x, real y)
  9. signal unitSelected(int unitId)
  10. signal areaSelected(real x1, real y1, real x2, real y2)
  11. function setPaused(paused) {
  12. isPaused = paused
  13. }
  14. function setGameSpeed(speed) {
  15. gameSpeed = speed
  16. }
  17. function issueCommand(command) {
  18. console.log("Command issued:", command)
  19. // Handle unit commands
  20. }
  21. // OpenGL rendering area
  22. Rectangle {
  23. id: renderArea
  24. anchors.fill: parent
  25. color: "transparent"
  26. // This will be replaced by actual OpenGL rendering
  27. Text {
  28. anchors.centerIn: parent
  29. text: "3D Game World\n(OpenGL Render Area)\n\nPress WASD to move camera\nMouse to look around\nScroll to zoom"
  30. color: "white"
  31. font.pointSize: 16
  32. horizontalAlignment: Text.AlignHCenter
  33. }
  34. // Camera controls info
  35. Rectangle {
  36. anchors.top: parent.top
  37. anchors.left: parent.left
  38. anchors.margins: 10
  39. width: 200
  40. height: 120
  41. color: "#34495e"
  42. opacity: 0.8
  43. Column {
  44. anchors.fill: parent
  45. anchors.margins: 8
  46. spacing: 4
  47. Text {
  48. text: "Camera Controls:"
  49. color: "white"
  50. font.bold: true
  51. font.pointSize: 10
  52. }
  53. Text {
  54. text: "WASD - Move"
  55. color: "white"
  56. font.pointSize: 9
  57. }
  58. Text {
  59. text: "Mouse - Look"
  60. color: "white"
  61. font.pointSize: 9
  62. }
  63. Text {
  64. text: "Scroll - Zoom"
  65. color: "white"
  66. font.pointSize: 9
  67. }
  68. Text {
  69. text: "Q/E - Rotate"
  70. color: "white"
  71. font.pointSize: 9
  72. }
  73. Text {
  74. text: "R/F - Up/Down"
  75. color: "white"
  76. font.pointSize: 9
  77. }
  78. }
  79. }
  80. MouseArea {
  81. id: mouseArea
  82. anchors.fill: parent
  83. acceptedButtons: Qt.LeftButton | Qt.RightButton
  84. property bool isSelecting: false
  85. property real startX: 0
  86. property real startY: 0
  87. onPressed: function(mouse) {
  88. if (mouse.button === Qt.LeftButton) {
  89. isSelecting = true
  90. startX = mouse.x
  91. startY = mouse.y
  92. selectionBox.x = startX
  93. selectionBox.y = startY
  94. selectionBox.width = 0
  95. selectionBox.height = 0
  96. selectionBox.visible = true
  97. }
  98. }
  99. onPositionChanged: function(mouse) {
  100. if (isSelecting) {
  101. var endX = mouse.x
  102. var endY = mouse.y
  103. selectionBox.x = Math.min(startX, endX)
  104. selectionBox.y = Math.min(startY, endY)
  105. selectionBox.width = Math.abs(endX - startX)
  106. selectionBox.height = Math.abs(endY - startY)
  107. }
  108. }
  109. onReleased: function(mouse) {
  110. if (mouse.button === Qt.LeftButton && isSelecting) {
  111. isSelecting = false
  112. selectionBox.visible = false
  113. if (selectionBox.width > 5 && selectionBox.height > 5) {
  114. // Area selection
  115. areaSelected(selectionBox.x, selectionBox.y,
  116. selectionBox.x + selectionBox.width,
  117. selectionBox.y + selectionBox.height)
  118. } else {
  119. // Point selection
  120. mapClicked(mouse.x, mouse.y)
  121. if (typeof game !== 'undefined' && game.onMapClicked) {
  122. game.onMapClicked(mouse.x, mouse.y)
  123. }
  124. }
  125. }
  126. }
  127. }
  128. // Selection box
  129. Rectangle {
  130. id: selectionBox
  131. visible: false
  132. border.color: "white"
  133. border.width: 1
  134. color: "transparent"
  135. }
  136. }
  137. // Edge scrolling areas
  138. MouseArea {
  139. id: leftEdge
  140. anchors.left: parent.left
  141. anchors.top: parent.top
  142. anchors.bottom: parent.bottom
  143. width: 10
  144. hoverEnabled: true
  145. onEntered: {
  146. scrollTimer.direction = "left"
  147. scrollTimer.start()
  148. }
  149. onExited: scrollTimer.stop()
  150. }
  151. MouseArea {
  152. id: rightEdge
  153. anchors.right: parent.right
  154. anchors.top: parent.top
  155. anchors.bottom: parent.bottom
  156. width: 10
  157. hoverEnabled: true
  158. onEntered: {
  159. scrollTimer.direction = "right"
  160. scrollTimer.start()
  161. }
  162. onExited: scrollTimer.stop()
  163. }
  164. MouseArea {
  165. id: topEdge
  166. anchors.top: parent.top
  167. anchors.left: parent.left
  168. anchors.right: parent.right
  169. height: 10
  170. hoverEnabled: true
  171. onEntered: {
  172. scrollTimer.direction = "up"
  173. scrollTimer.start()
  174. }
  175. onExited: scrollTimer.stop()
  176. }
  177. MouseArea {
  178. id: bottomEdge
  179. anchors.bottom: parent.bottom
  180. anchors.left: parent.left
  181. anchors.right: parent.right
  182. height: 10
  183. hoverEnabled: true
  184. onEntered: {
  185. scrollTimer.direction = "down"
  186. scrollTimer.start()
  187. }
  188. onExited: scrollTimer.stop()
  189. }
  190. Timer {
  191. id: scrollTimer
  192. interval: 16 // ~60 FPS
  193. repeat: true
  194. property string direction: ""
  195. onTriggered: {
  196. // Handle camera movement based on direction
  197. console.log("Edge scroll:", direction)
  198. }
  199. }
  200. // Keyboard handling
  201. Keys.onPressed: function(event) {
  202. switch (event.key) {
  203. case Qt.Key_W:
  204. console.log("Move camera forward")
  205. break
  206. case Qt.Key_S:
  207. console.log("Move camera backward")
  208. break
  209. case Qt.Key_A:
  210. console.log("Move camera left")
  211. break
  212. case Qt.Key_D:
  213. console.log("Move camera right")
  214. break
  215. case Qt.Key_Q:
  216. console.log("Rotate camera left")
  217. break
  218. case Qt.Key_E:
  219. console.log("Rotate camera right")
  220. break
  221. case Qt.Key_R:
  222. console.log("Move camera up")
  223. break
  224. case Qt.Key_F:
  225. console.log("Move camera down")
  226. break
  227. }
  228. }
  229. focus: true
  230. }