GameView.qml 7.1 KB

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