window.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/math32"
  7. "github.com/g3n/engine/window"
  8. "github.com/g3n/engine/gui/assets/icon"
  9. )
  10. /*********************************************
  11. Window panel
  12. +-------------------------------------+---+
  13. | Title panel | X |
  14. +-------------------------------------+---+
  15. | Content panel |
  16. | +-----------------------------------+ |
  17. | | | |
  18. | | | |
  19. | | | |
  20. | | | |
  21. | | | |
  22. | | | |
  23. | +-----------------------------------+ |
  24. | |
  25. +-----------------------------------------+
  26. *********************************************/
  27. // Window represents a window GUI element
  28. type Window struct {
  29. Panel // Embedded Panel
  30. styles *WindowStyles
  31. title *WindowTitle // internal optional title panel
  32. client Panel // internal client panel
  33. resizable bool // Specifies whether the window is resizable
  34. drag bool // Whether the mouse buttons is pressed (i.e. when dragging)
  35. dragPadding float32 // Extra width used to resize (in addition to border sizes)
  36. // To keep track of which window borders the cursor is over
  37. overTop bool
  38. overRight bool
  39. overBottom bool
  40. overLeft bool
  41. // Minimum and maximum sizes
  42. minSize math32.Vector2
  43. maxSize math32.Vector2
  44. }
  45. // TODO: Fix window builder examples
  46. // WindowStyle contains the styling of a Window
  47. type WindowStyle struct {
  48. PanelStyle
  49. TitleStyle WindowTitleStyle
  50. }
  51. // WindowStyles contains a WindowStyle for each valid GUI state
  52. type WindowStyles struct {
  53. Normal WindowStyle
  54. Over WindowStyle
  55. Focus WindowStyle
  56. Disabled WindowStyle
  57. }
  58. // NewWindow creates and returns a pointer to a new window with the
  59. // specified dimensions
  60. func NewWindow(width, height float32) *Window {
  61. w := new(Window)
  62. w.styles = &StyleDefault().Window
  63. w.Panel.Initialize(width, height)
  64. w.Panel.Subscribe(OnMouseDown, w.onMouse)
  65. w.Panel.Subscribe(OnMouseUp, w.onMouse)
  66. w.Panel.Subscribe(OnCursor, w.onCursor)
  67. w.Panel.Subscribe(OnCursorEnter, w.onCursor)
  68. w.Panel.Subscribe(OnCursorLeave, w.onCursor)
  69. w.Panel.Subscribe(OnResize, func(evname string, ev interface{}) { w.recalc() })
  70. w.client.Initialize(0, 0)
  71. w.Panel.Add(&w.client)
  72. w.dragPadding = 5
  73. w.recalc()
  74. w.update()
  75. return w
  76. }
  77. // SetResizable sets whether the window is resizable.
  78. func (w *Window) SetResizable(state bool) {
  79. w.resizable = state
  80. }
  81. // SetCloseButton sets whether the window has a close button on the top right.
  82. func (w *Window) SetCloseButton(state bool) {
  83. w.title.setCloseButton(state)
  84. }
  85. // SetTitle sets the title of the window.
  86. func (w *Window) SetTitle(text string) {
  87. if w.title == nil {
  88. w.title = newWindowTitle(w, text)
  89. w.Panel.Add(w.title)
  90. } else {
  91. w.title.label.SetText(text)
  92. }
  93. w.update()
  94. w.recalc()
  95. }
  96. // Add adds a child panel to the client area of this window
  97. func (w *Window) Add(ichild IPanel) *Window {
  98. w.client.Add(ichild)
  99. return w
  100. }
  101. // SetLayout sets the layout of the client panel.
  102. func (w *Window) SetLayout(layout ILayout) {
  103. w.client.SetLayout(layout)
  104. }
  105. // onMouse process subscribed mouse events over the window
  106. func (w *Window) onMouse(evname string, ev interface{}) {
  107. switch evname {
  108. case OnMouseDown:
  109. // Move the window above everything contained in its parent
  110. parent := w.Parent().(IPanel).GetPanel()
  111. parent.SetTopChild(w)
  112. // If the click happened inside the draggable area, then set drag to true
  113. if w.overTop || w.overRight || w.overBottom || w.overLeft {
  114. w.drag = true
  115. w.root.SetMouseFocus(w)
  116. }
  117. case OnMouseUp:
  118. w.drag = false
  119. w.root.SetMouseFocus(nil)
  120. default:
  121. return
  122. }
  123. w.root.StopPropagation(StopAll)
  124. }
  125. // onCursor process subscribed cursor events over the window
  126. func (w *Window) onCursor(evname string, ev interface{}) {
  127. // If the window is not resizable we are not interested in cursor movements
  128. if !w.resizable {
  129. return
  130. }
  131. if evname == OnCursor {
  132. cev := ev.(*window.CursorEvent)
  133. // If already dragging - update window size and position depending
  134. // on the cursor position and the borders being dragged
  135. if w.drag {
  136. if w.overTop {
  137. delta := cev.Ypos - w.pospix.Y
  138. newHeight := w.Height() - delta
  139. minHeight := w.title.height
  140. if newHeight >= minHeight {
  141. w.SetPositionY(w.Position().Y + delta)
  142. w.SetHeight(math32.Max(newHeight, minHeight))
  143. } else {
  144. w.SetPositionY(w.Position().Y + w.Height() - minHeight)
  145. w.SetHeight(w.title.height)
  146. }
  147. }
  148. if w.overRight {
  149. delta := cev.Xpos - (w.pospix.X + w.width)
  150. newWidth := w.Width() + delta
  151. w.SetWidth(math32.Max(newWidth, w.title.label.Width() + w.title.closeButton.Width()))
  152. }
  153. if w.overBottom {
  154. delta := cev.Ypos - (w.pospix.Y + w.height)
  155. newHeight := w.Height() + delta
  156. w.SetHeight(math32.Max(newHeight, w.title.height))
  157. }
  158. if w.overLeft {
  159. delta := cev.Xpos - w.pospix.X
  160. newWidth := w.Width() - delta
  161. minWidth := w.title.label.Width() + w.title.closeButton.Width()
  162. if newWidth >= minWidth {
  163. w.SetPositionX(w.Position().X + delta)
  164. w.SetWidth(math32.Max(newWidth, minWidth))
  165. } else {
  166. w.SetPositionX(w.Position().X + w.Width() - minWidth)
  167. w.SetWidth(minWidth)
  168. }
  169. }
  170. } else {
  171. // Obtain cursor position relative to window
  172. cx := cev.Xpos - w.pospix.X
  173. cy := cev.Ypos - w.pospix.Y
  174. // Check if cursor is on the top of the window (border + drag margin)
  175. if cy <= w.borderSizes.Top {
  176. w.overTop = true
  177. w.root.SetCursorVResize()
  178. } else {
  179. w.overTop = false
  180. }
  181. // Check if cursor is on the bottom of the window (border + drag margin)
  182. if cy >= w.height-w.borderSizes.Bottom - w.dragPadding {
  183. w.overBottom = true
  184. } else {
  185. w.overBottom = false
  186. }
  187. // Check if cursor is on the left of the window (border + drag margin)
  188. if cx <= w.borderSizes.Left + w.dragPadding {
  189. w.overLeft = true
  190. w.root.SetCursorHResize()
  191. } else {
  192. w.overLeft = false
  193. }
  194. // Check if cursor is on the right of the window (border + drag margin)
  195. if cx >= w.width-w.borderSizes.Right - w.dragPadding {
  196. w.overRight = true
  197. w.root.SetCursorHResize()
  198. } else {
  199. w.overRight = false
  200. }
  201. // Update cursor image based on cursor position
  202. if (w.overTop || w.overBottom) && !w.overRight && !w.overLeft {
  203. w.root.SetCursorVResize()
  204. } else if (w.overRight || w.overLeft) && !w.overTop && !w.overBottom {
  205. w.root.SetCursorHResize()
  206. } else if (w.overRight && w.overTop) || (w.overBottom && w.overLeft) {
  207. w.root.SetCursorDiagResize1()
  208. } else if (w.overRight && w.overBottom) || (w.overTop && w.overLeft) {
  209. w.root.SetCursorDiagResize2()
  210. }
  211. // If cursor is not near the border of the window then reset the cursor
  212. if !w.overTop && !w.overRight && !w.overBottom && !w.overLeft {
  213. w.root.SetCursorNormal()
  214. }
  215. }
  216. } else if evname == OnCursorLeave {
  217. w.root.SetCursorNormal()
  218. w.drag = false
  219. }
  220. w.root.StopPropagation(StopAll)
  221. }
  222. // update updates the window's visual state.
  223. func (w *Window) update() {
  224. if !w.Enabled() {
  225. w.applyStyle(&w.styles.Disabled)
  226. return
  227. }
  228. w.applyStyle(&w.styles.Normal)
  229. }
  230. // applyStyle applies a window style to the window.
  231. func (w *Window) applyStyle(s *WindowStyle) {
  232. w.SetBordersColor4(&s.BorderColor)
  233. w.SetBordersFrom(&s.Border)
  234. w.SetPaddingsFrom(&s.Padding)
  235. w.client.SetMarginsFrom(&s.Margin)
  236. w.client.SetColor4(&s.BgColor)
  237. if w.title != nil {
  238. w.title.applyStyle(&s.TitleStyle)
  239. }
  240. }
  241. // recalc recalculates the sizes and positions of the internal panels
  242. // from the outside to the inside.
  243. func (w *Window) recalc() {
  244. // Window title
  245. height := w.content.Height
  246. width := w.content.Width
  247. cx := float32(0)
  248. cy := float32(0)
  249. if w.title != nil {
  250. w.title.SetWidth(w.content.Width)
  251. w.title.recalc()
  252. height -= w.title.height
  253. cy = w.title.height
  254. }
  255. // Content area
  256. w.client.SetPosition(cx, cy)
  257. w.client.SetSize(width, height)
  258. }
  259. // WindowTitle represents the title bar of a Window
  260. type WindowTitle struct {
  261. Panel // Embedded panel
  262. win *Window // Window to which this title belongs
  263. label Label // Label for the title
  264. pressed bool // Whether the left mouse button is pressed
  265. closeButton *Button // The close button on the top right corner
  266. closeButtonVisible bool // Whether the close button is present
  267. // Last mouse coordinates
  268. mouseX float32
  269. mouseY float32
  270. }
  271. // WindowTitleStyle contains the styling for a window title.
  272. type WindowTitleStyle struct {
  273. PanelStyle
  274. FgColor math32.Color4
  275. }
  276. // newWindowTitle creates and returns a pointer to a window title panel.
  277. func newWindowTitle(win *Window, text string) *WindowTitle {
  278. wt := new(WindowTitle)
  279. wt.win = win
  280. wt.Panel.Initialize(0, 0)
  281. wt.label.initialize(text, StyleDefault().Font)
  282. wt.Panel.Add(&wt.label)
  283. wt.closeButton = NewButton("")
  284. wt.closeButton.SetIcon(icon.Close)
  285. wt.closeButton.Subscribe(OnCursorEnter, func(s string, i interface{}) {
  286. wt.win.root.SetCursorNormal()
  287. })
  288. wt.closeButton.Subscribe(OnClick, func(s string, i interface{}) {
  289. wt.win.Parent().GetNode().Remove(wt.win)
  290. wt.win.Dispose()
  291. wt.win.Dispatch("gui.OnWindowClose", nil)
  292. })
  293. wt.Panel.Add(wt.closeButton)
  294. wt.closeButtonVisible = true
  295. wt.Subscribe(OnMouseDown, wt.onMouse)
  296. wt.Subscribe(OnMouseUp, wt.onMouse)
  297. wt.Subscribe(OnCursor, wt.onCursor)
  298. wt.Subscribe(OnCursorEnter, wt.onCursor)
  299. wt.Subscribe(OnCursorLeave, wt.onCursor)
  300. wt.recalc()
  301. return wt
  302. }
  303. // setCloseButton sets whether the close button is present on the top right corner.
  304. func (wt *WindowTitle) setCloseButton(state bool) {
  305. if state {
  306. wt.closeButtonVisible = true
  307. wt.Panel.Add(wt.closeButton)
  308. } else {
  309. wt.closeButtonVisible = false
  310. wt.Panel.Remove(wt.closeButton)
  311. }
  312. }
  313. // onMouse process subscribed mouse button events over the window title.
  314. func (wt *WindowTitle) onMouse(evname string, ev interface{}) {
  315. mev := ev.(*window.MouseEvent)
  316. switch evname {
  317. case OnMouseDown:
  318. wt.pressed = true
  319. wt.mouseX = mev.Xpos
  320. wt.mouseY = mev.Ypos
  321. wt.win.root.SetMouseFocus(wt)
  322. case OnMouseUp:
  323. wt.pressed = false
  324. wt.win.root.SetMouseFocus(nil)
  325. default:
  326. return
  327. }
  328. wt.win.root.StopPropagation(Stop3D)
  329. }
  330. // onCursor process subscribed cursor events over the window title.
  331. func (wt *WindowTitle) onCursor(evname string, ev interface{}) {
  332. if evname == OnCursorLeave {
  333. wt.win.root.SetCursorNormal()
  334. wt.pressed = false
  335. } else if evname == OnCursor {
  336. if !wt.pressed {
  337. wt.win.root.StopPropagation(Stop3D)
  338. return
  339. }
  340. cev := ev.(*window.CursorEvent)
  341. dy := wt.mouseY - cev.Ypos
  342. dx := wt.mouseX - cev.Xpos
  343. wt.mouseX = cev.Xpos
  344. wt.mouseY = cev.Ypos
  345. posX := wt.win.Position().X - dx
  346. posY := wt.win.Position().Y - dy
  347. wt.win.SetPosition(posX, posY)
  348. }
  349. wt.win.root.StopPropagation(Stop3D)
  350. }
  351. // applyStyle applies the specified WindowTitleStyle.
  352. func (wt *WindowTitle) applyStyle(s *WindowTitleStyle) {
  353. wt.Panel.ApplyStyle(&s.PanelStyle)
  354. wt.label.SetColor4(&s.FgColor)
  355. }
  356. // recalc recalculates the height and position of the label in the title bar.
  357. func (wt *WindowTitle) recalc() {
  358. xpos := (wt.width - wt.label.width) / 2
  359. wt.label.SetPositionX(xpos)
  360. wt.SetContentHeight(wt.closeButton.Height())
  361. if wt.closeButtonVisible {
  362. wt.closeButton.SetPositionX(wt.width - wt.closeButton.width)
  363. }
  364. }